IndexError:列表索引超出model.fit()的范围

时间:2018-11-05 06:30:12

标签: python python-3.x tensorflow keras-layer index-error

我是使用tensorflow的新手。我正在尝试使用形状(16 * 16)的图像训练我的网络。我将512 * 512的3个灰度图像划分为16 * 16,并将其全部附加。所以我有3072 * 16 * 16。训练时出现错误。我正在使用jupyter笔记本。有人可以帮助我吗?

这是代码

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

2 个答案:

答案 0 :(得分:0)

经过我的研究,变得更加清楚,这是一个已知问题,官方解决方案尚未发布。尽管,仍然有一些建议起作用。

建议更新为每晚更新的构建版本(pip install tf-nightlypip install tf-nightly-gpu

https://github.com/tensorflow/tensorflow/issues/21894#issuecomment-418552609

答案 1 :(得分:0)

我认为此错误是由于传递给模型的x和y的形状引起的。您没有通过标签作为标签!