Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 获取错误“;索引器:列表索引超出范围“;_Tensorflow_Keras_Index Error - Fatal编程技术网

Tensorflow 获取错误“;索引器:列表索引超出范围“;

Tensorflow 获取错误“;索引器:列表索引超出范围“;,tensorflow,keras,index-error,Tensorflow,Keras,Index Error,这是我的密码 model = Sequential() model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(48, 120, 1))) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(Dropout(0.25)) model.add(MaxPooling2D((2, 2))) model.add(Conv2D(128, (3, 3), activation=

这是我的密码

model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(48, 120, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Dropout(0.25))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(Dropout(0.25))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(Dropout(0.25))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(62*4, activation='softmax'))
# print(model.summary())
model.compile(
    loss='categorical_crossentropy',
    optimizer=RMSprop(lr=1e-4),
    metrics=['accuracy']
)
history = model.fit_generator(
    DataGenerator(train_dir, (48, 120, 1)),
    steps_per_epoch=32,
    epochs=30,
    validation_data=DataGenerator(test_dir, (48, 120, 1)),
    validation_steps=32
)
数据发生器

class DataGenerator(Sequence):
    def __init__(self, image_path, shape, to_fit=True, batch_size=32,):
        self.image_path = image_path
        self.shape  = shape
        self.to_fit = to_fit
        self.batch_size = batch_size
        self.all_images = self.get_all_images()
        
    def __len__(self):
        # return the number of batches per epoch
        return len(os.listdir(self.image_path))
        
    def __getitem__(self, index):
        # return a batch of images and (labels) if we are predicting
        if self.to_fit:
            return self._genX_(index), self._genY_(index)
        else:
            return self._genX_(index)
    
    def _genX_(self, index):
        X = self.all_images[index*self.batch_size:(index+1)*self.batch_size]
        X = [cv2.imread(img, 0) for img in X]
        X = [img.reshape(self.shape) for img in X]
        X = [img.astype('float32')/255 for img in X]
        return X
    
    def _genY_(self, index):
        y = self.all_images[index*self.batch_size:(index+1)*self.batch_size]
        return y
    
    def on_epoch_end(self):
        # do something after every epoch
        return
    
    def get_all_images(self):
        return os.listdir(self.image_path)
我得到了什么

Epoch 1/30
Traceback (most recent call last):
  File ".\test_1.py", line 42, in <module>
    validation_steps=32
  File "E:\Environments\Python\Python37\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training.py", line 1732, in fit_generator
    initial_epoch=initial_epoch)
  File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training_generator.py", line 220, in fit_generator
    reset_metrics=False)
  File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training.py", line 1508, in train_on_batch
    class_weight=class_weight)
  File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training.py", line 579, in _standardize_user_data
    exception_prefix='input')
  File "E:\Environments\Python\Python37\lib\site-packages\keras\engine\training_utils.py", line 87, in standardize_input_data
    if isinstance(data[0], list):
IndexError: list index out of range
1/30纪元
回溯(最近一次呼叫最后一次):
文件“\test_1.py”,第42行,在
验证步骤=32
文件“E:\Environments\Python\Python37\lib\site packages\keras\legacy\interfaces.py”,第91行,在包装器中
返回函数(*args,**kwargs)
文件“E:\Environments\Python\Python37\lib\site packages\keras\engine\training.py”,第1732行,在fit\u生成器中
初始_历元=初始_历元)
文件“E:\Environments\Python\Python37\lib\site packages\keras\engine\training\u generator.py”,第220行,在fit\u generator中
重置(度量值=False)
文件“E:\Environments\Python\Python37\lib\site packages\keras\engine\training.py”,第1508行,在批处理的列中
等级重量=等级重量)
文件“E:\Environments\Python\Python37\lib\site packages\keras\engine\training.py”,第579行,在用户数据中
异常(前缀为“输入”)
标准化输入数据中的文件“E:\Environments\Python\Python37\lib\site packages\keras\engine\training\u utils.py”,第87行
如果存在(数据[0],请列出):
索引器:列表索引超出范围
我只想写我自己的生成器来加载很多图片,但看起来不太好


谁能告诉我,我怎么会犯这个错误?如何解决这个问题?

您是否检查
(索引+1)*self.batch\u size
是否低于
len(self.all\u图像)
?已修复,索引*batch\u size太大,找不到图像,我只使用index+batchsize。谢谢