0

In a CNN model using Keras, I am implementing the following code:

def train_model():
    # Load Dataset
    
    np.random.seed(666555)
    X = np.memmap('Processed/X_train.npy', 'float64', shape = (256, 224, 224, 3), mode = 'c')
    Y = np.memmap('Processed/Y_train.npy', 'float64', shape = (256), mode = 'c')
    X = np.random.shuffle(X)
    Y = np.random.shuffle(Y)
    
    # Training our Model on whole Dataset
    model = define_model()
    model.fit(X, Y, epochs = 30, verbose = 1, steps_per_epoch = 4)
    # Saving our Model
    model.save('One_block_VGG.h5')

    X_test = np.load('Processed/X_test.npy', mmap_mode='r')
    Y_test = np.load('Processed/Y_test.npy', mmap_mode='r')
    _, acc = model.evaluate(X_test, Y_test, verbose = 1)
    print('Accuracy of our Model on Test Set: %.3f' % (acc*100))

train_model()

I am getting the following error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-59-4dc2ba0c028a> in <module>
----> 1 train_model()

<ipython-input-58-994a78e8019e> in train_model()
     11     # Training our Model on whole Dataset
     12     model = define_model()
---> 13     model.fit(X, Y, epochs = 30, verbose = 1, steps_per_epoch = 4)
     14     # Saving our Model
     15     model.save('One_block_VGG.h5')

C:\Anaconda3\lib\site-packages\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, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
   1237                                         steps_per_epoch=steps_per_epoch,
   1238                                         validation_steps=validation_steps,
-> 1239                                         validation_freq=validation_freq)
   1240 
   1241     def evaluate(self,

C:\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
    139     indices_for_conversion_to_dense = []
    140     for i in range(len(feed)):
--> 141         if issparse(fit_inputs[i]) and not K.is_sparse(feed[i]):
    142             indices_for_conversion_to_dense.append(i)
    143 

IndexError: list index out of range

I am loading only 256 images to check my model, but getting the error. How do I solve this error? The original shape of variables are:

X_train - (18873, 224, 224, 3)
X_test - (6127, 224, 224, 3)
Y_train - (18873,)
Y_test - (6127,)

In case you need to check define_model function:

def define_model():
    model = Sequential()
    model.add(Conv2D(32, (3, 3), activation = 'relu', kernel_initializer = 'he_uniform',
                     padding = 'same', input_shape = (224, 224, 3)))
    model.add(MaxPooling2D((2, 2), strides = (2, 2)))
    model.add(Conv2D(64, (3, 3), activation = 'relu', padding = 'same'))
    model.add(MaxPooling2D((2, 2), strides = (2, 2)))
    model.add(Flatten())
    model.add(Dense(128, activation = 'relu', kernel_initializer = 'he_uniform'))
    model.add(Dense(1, activation = 'sigmoid'))
    
    opt = SGD(0.001, 0.9)
    model.compile(optimizer = opt, loss = 'binary_crossentropy', metrics = ['accuracy'])

    return model
Rahul Vishwakarma
  • 1,446
  • 2
  • 7
  • 22

0 Answers0