IndexError:在model.fit()中列出超出范围的索引
Posted
技术标签:
【中文标题】IndexError:在model.fit()中列出超出范围的索引【英文标题】:IndexError: list index out of range in model.fit() 【发布时间】:2019-04-08 12:24:36 【问题描述】:我是使用 tensorflow 的新手。我正在尝试用形状(16 * 16)的图像训练我的网络。我已将 3 个 512*512 的灰度图像分成 16*16 并全部附加。所以我有 3072*16*16。训练时出现错误。我正在使用 jupyter notebook。有人可以帮我吗?
这里是代码
import tensorflow as tf
import numpy as np
from numpy import newaxis
import glob
import os
from PIL import Image,ImageOps
import random
from os.path import join
import matplotlib.pyplot as plt
from tensorflow import keras
TRAIN_PATH = 'dataset/2/*.jpg'
LOGS_Path = "dataset/logs/"
CHECKPOINTS_PATH = 'dataset/checkpoints/'
BETA = .75
EXP_NAME = f"beta_BETA"
files_list = glob.glob(join(TRAIN_PATH))
leng=len(files_list)
new_cover = []
for i in range(leng):
img_cover_path = files_list[i]
for j in range (0,512,16):
for k in range (0,512,16):
img_cover = Image.open(img_cover_path)
area=(k,j,k+16,j+16)
img_cover1=img_cover.crop(area)
img_cover1 = np.array(ImageOps.fit(img_cover1(16,16)),dtype=np.float32)
img_cover1 /= 255.
n1.append(img_cover1)
new_cover.append(n1)
new_cover = np.array(new_cover)
new_cover1=np.swapaxes(new_cover, 1,3)
tf.reset_default_graph()
model=keras.Sequential()
#1st
model.add(keras.layers.Conv2D(64, (3, 3), strides=1,padding='SAME', input_shape = (16, 16, 3072))) #number of filters,shape of filter,input image size,activation function
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation(activation='relu'))
#2
model.add(keras.layers.Conv2D(64, (3, 3),strides=1,padding='SAME')) #number of filters,shape of filter,input image size,activation function
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation(activation='relu'))
#3
model.add(keras.layers.Conv2D(64, (3, 3),strides=1,padding='SAME')) #number of filters,shape of filter,input image size,activation function
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation(activation='relu'))
#4
model.add(keras.layers.Conv2D(64, (3, 3),strides=1,padding='SAME')) #number of filters,shape of filter,input image size,activation function
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation(activation='relu'))
#message
#compiling
model.compile(optimizer = tf.train.AdamOptimizer(0.001),loss='mse', metrics = ['accuracy'])
model.summary()
# Store training stats
model.fit(x=new_cover1,y=None, batch_size=32, epochs=1, verbose=1, callbacks=None, validation_split=0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)
它给出了错误:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 16, 16, 64) 1769536
_________________________________________________________________
batch_normalization (BatchNo (None, 16, 16, 64) 256
_________________________________________________________________
activation (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 16, 16, 64) 36928
_________________________________________________________________
batch_normalization_1 (Batch (None, 16, 16, 64) 256
_________________________________________________________________
activation_1 (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 16, 16, 64) 36928
_________________________________________________________________
batch_normalization_2 (Batch (None, 16, 16, 64) 256
_________________________________________________________________
activation_2 (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 16, 16, 64) 36928
_________________________________________________________________
batch_normalization_3 (Batch (None, 16, 16, 64) 256
_________________________________________________________________
activation_3 (Activation) (None, 16, 16, 64) 0
=================================================================
Total params: 1,881,344
Trainable params: 1,880,832
Non-trainable params: 512
_________________________________________________________________
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-20-49da746cee1b> in <module>()
24 model.summary()
25 # Store training stats
---> 26 model.fit(x=new_cover1,y=None, batch_size=32, epochs=1, verbose=1, callbacks=None, validation_split=0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)
27
28 #return model
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
1654 initial_epoch=initial_epoch,
1655 steps_per_epoch=steps_per_epoch,
-> 1656 validation_steps=validation_steps)
1657
1658 def evaluate(self,
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py in fit_loop(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps)
135 indices_for_conversion_to_dense = []
136 for i in range(len(feed)):
--> 137 if issparse is not None and issparse(ins[i]) and not K.is_sparse(feed[i]):
138 indices_for_conversion_to_dense.append(i)
139
IndexError: list index out of range
【问题讨论】:
【参考方案1】:经过我的研究,很明显这是一个已知问题,官方解决方案尚未发布。虽然,仍然有一些建议声称有效。
建议更新到update nightly build版本(pip install tf-nightly
或pip install tf-nightly-gpu
)
https://github.com/tensorflow/tensorflow/issues/21894#issuecomment-418552609
【讨论】:
尝试使用这些更新。仍然没有运气。我还尝试重新启动内核并使用此链接github.com/tflearn/tflearn/issues/408#issuecomment-255617859 中给出的重置图。仍然得到同样的错误。在发送 new_cover 张量进行训练时我做错了什么吗?【参考方案2】:我认为这个错误是因为传递给模型的 x 和 y 的形状。您将 None 作为标签传递!
【讨论】:
以上是关于IndexError:在model.fit()中列出超出范围的索引的主要内容,如果未能解决你的问题,请参考以下文章
同时运行 model.fit() 和 TensorBoard?
model.fit(X,y) 和 model.fit(train_X, train_y) 有啥区别
model.predict() 和 model.fit() 做啥?
keras.models.Model.fit 中的“时代”是啥?