Neural network training accuracy is too low and prediction is not working in Image Classification with Keras

I am following this guide to learn image classification with neural networks:

https://www.tensorflow.org/tutorials/keras/classification

And I implement this code for my custom dataset. I have 2300 gray scaled 1024x1024 pictures to train model. I hold all my images in 3D numpy array as train_images and test_images. I have 4 class which are 0,1,2,3 and I hold those as list, named "labels".

train_images.shape # returns (2300,1024,1024)
test_images.shape # returns (384,1024,1024)

# normalize values
train_images = train_images / 255.0
test_images = test_images / 255.0

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(1024, 1024)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(4, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_images, labels, epochs=10)

Everything almost same with guide. But my epoch accuracy is around 0.4

Epoch 10/10
...
2176/2300 [===========================>..] - ETA: 0s - loss: 9.5701 - acc: 0.4062
2208/2300 [===========================>..] - ETA: 0s - loss: 9.5628 - acc: 0.4067
2240/2300 [============================>.] - ETA: 0s - loss: 9.5485 - acc: 0.4076
2272/2300 [============================>.] - ETA: 0s - loss: 9.5417 - acc: 0.4080
2300/2300 [==============================] - 12s 5ms/step - loss: 9.5307 - acc: 0.4087

Also in guide some predictions are fractional but when I try to do prediction, My model predictions are only 0 or 1. It says this is %100 (x) but its wrong.

predictions = model.predict(test_images)
print(predictions)
# 0 | 0 | 1 | 0
# 0 | 0 | 1 | 0
# 1 | 0 | 0 | 0

UPDATED

Here is epoch results for 256*256 2 classed 100 images per class:

32/200 [===>..........................] - ETA: 0s - loss: 8.5627 - acc: 0.4688
200/200 [==============================] - 0s 317us/step - loss: 8.0590 - acc: 0.5000
Epoch 10/10

Also I lowered my classes into 2 but my predictions are still return %100 and wrong class.


I dont know where I am doing wrong. If you have any advice/idea I would be grateful. Thank you in advance.

1 answer

  • answered 2020-01-27 19:57 B200011011

    40% accuracy is not good. It needs to train more. You should rescale images to 128 or 256 to save time. Also try increasing epoch count to something like 100 or minimize loss to at least around 1 before testing. Another thing is class imbalance.

    According to this, https://arxiv.org/abs/1708.07747 link Fashion MNIST contains 7000 images per class with 70000 images in total. If your dataset has class imbalance which seems likely then you should look into other metrics and methods.