My neural network is not working right, and I am trying to find out what is up.

I inserted just three images to a transfer learning (mobilenet) neural network. The three images' classes are: array([[0., 0., 0., 1.], [0., 1., 0., 0.], [0., 1., 0., 0.]])

I did 50 epochs on these pictures and by the 20th epoch or so, the training accuracy stayed at 1.0:

Epoch 50/50 3/3 [==============================] - 6s 2s/step - loss: 1.3671 - acc: 1.0000 - val_loss: 1.3770 - val_acc: 0.0000e+00

Then when I went to predict the outcome of the same three images like so: predictions_test_2 = model_mn.predict(X, batch_size=1, verbose=1)

the predictions were: array([[0.2473848 , 0.25099277, 0.251868 , 0.24975444], [0.24154082, 0.25245225, 0.25358915, 0.25241777], [0.24333884, 0.25127387, 0.25357786, 0.25180945]], dtype=float32)

How could that be if the training accuracy is 1.0?!

This is the code: def mobilenet(img_rows, img_cols, channel=1, num_classes=None):

model = MobileNet( include_top=True,weights='imagenet')


model.outputs = [model.layers[-1].output]

model.layers[-1].outbound_nodes = []

x=Dense(num_classes, activation='softmax')(model.output)


#To set the first 8 layers to non-trainable (weights will not be updated)

for layer in model.layers[:8]:

   layer.trainable = False
model_new = Sequential()
for layer in model.layers[:-1]: # just exclude last layer from copying

model.add(Dense(64, activation='relu'))

# Learning rate is changed to 0.001
sgd = SGD(lr=1e-6,decay=1e-1,momentum=0.95, nesterov=True)
adam=Adam(lr=1e-6, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0000001, amsgrad=True)
model.compile(optimizer=adam, loss='categorical_crossentropy',metrics=['accuracy'])

# checkpoint
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
return model

model_mn = mobilenet(img_rows, img_cols, channel, num_classes)

model_mn.fit(X, Y,batch_size=3,epochs=50,shuffle=True,verbose=1,validation_data=(X_vall, Y_vall))


Chances are, you are overfitting your data, you need to closely monitor the validation accuracy, as if it diverges from the training one too much, then you enter overfitting territory.

Also, using just 3 pictures for training is just too little, your network won't be able to generalize properly with such an small input.

  • $\begingroup$ Hey, thanks for the answer. Of course I am not trying to teach my cnn with just three pictures. I am at a stage of debugging issues I had when trying to run it on a larger group of images. In order to debug, I first entered three pictures of the same class. Of course I am overfitting, but the question is: how is it possible that the training accuracy is 1.0 if the predictions for the same training data are wrong? $\endgroup$ – Keren Jan 9 '19 at 10:25
  • $\begingroup$ Just for the record, are you saying that the three pictures you use to train are the same ones you try to predict? $\endgroup$ – Juan Antonio Gomez Moriano Jan 9 '19 at 23:39
  • $\begingroup$ Yes. Again, this is in no way me trying to actually teach my neural network or see how it's working, it's just me trying to debug and see what the issues are. If the training accuracy is 1.0 but when I try to predict the outcome for the SAME data as my training and the outcome is wrong, then how is my training accuracy 1.0? $\endgroup$ – Keren Jan 12 '19 at 14:19
  • $\begingroup$ Predicting means doing a forward propagation... which you do during training anyway... is there any chance you are saving your model, and then restoring it (from a file I mean) in an incorrect manner? $\endgroup$ – Juan Antonio Gomez Moriano Jan 13 '19 at 23:02
  • $\begingroup$ It could be, how can I check that? $\endgroup$ – Keren Feb 13 '19 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.