Stack Exchange
Convolutional Neural Network 50% Accuracy Issue
I am building and testing my first convolutional neural network. My dataset consists of 10 training images for each class and 3 test image for each class. My code is below:
from keras.models import Sequential
from keras import backend
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
import numpy as np

classifier = Sequential()

classifier.add(Conv2D(32, (3, 3), input_shape = (432, 288, 3), 
               activation = 'relu'))

classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Flatten())

classifier.add(Dense(units = 128, activation = 'relu'))

classifier.add(Dense(units = 1, activation = 'sigmoid'))

classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy',
                  metrics = ['accuracy'])

train_datagen = ImageDataGenerator(rescale = 1./255,shear_range = 0.2,
                                  zoom_range = 0.2,
                                  horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory(
    '/Users/ab123/Desktop/Project/TrainingData',
    target_size = (432, 288), batch_size = 10, class_mode = 'binary')

test_set = test_datagen.flow_from_directory(
    '/Users/ab123/Desktop/Project/TestData', 
    target_size = (432, 288), batch_size = 10, class_mode = 'binary')

classifier.fit_generator(training_set, steps_per_epoch = 10,
                        epochs = 10, validation_data = test_set,
                        validation_steps = 3)
fname = '/Users/ab123/Desktop/weights-Test-CNN.hdf5'
classifier.save(fname, overwrite = True)

test_image = image.load_img('/Users/ab123/Desktop/e102h.wav.jpg',
                            target_size = (432, 288))

test_image = image.img_to_array(test_image)

test_image = np.expand_dims(test_image, axis = 0)

result = classifier.predict(test_image)

training_set.class_indices

if result[0][0] == 1:
    prediction = 'gravel'
else:
    prediction = 'water'

No errors are thrown, but after 10 epochs, I only have 50% accuracy. I was wondering if someone could help me fix this problem. The output of the program is below.
Found 20 images belonging to 2 classes.
Found 6 images belonging to 2 classes.
Epoch 1/10
10/10 [==============================] - 56s 6s/step - loss: 7.3223 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 2/10
10/10 [==============================] - 56s 6s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 3/10
10/10 [==============================] - 55s 5s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 4/10
10/10 [==============================] - 61s 6s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 5/10
10/10 [==============================] - 62s 6s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 6/10
10/10 [==============================] - 61s 6s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 7/10
10/10 [==============================] - 62s 6s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 8/10
10/10 [==============================] - 59s 6s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 9/10
10/10 [==============================] - 54s 5s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 10/10
10/10 [==============================] - 53s 5s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000

____________________________________UPDATE_____________________________________
I tried running for more epochs but the output was the same.
Epoch 22/1000
10/10 [==============================] - 33s 3s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 23/1000
10/10 [==============================] - 33s 3s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 24/1000
10/10 [==============================] - 34s 3s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 25/1000
10/10 [==============================] - 33s 3s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 26/1000
10/10 [==============================] - 34s 3s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 27/1000
10/10 [==============================] - 36s 4s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 28/1000
10/10 [==============================] - 37s 4s/step - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000


10 epochs may be too low. Have you tried 100 epochs? Or use 1000 epochs and just monitor the changes in accuracy.

ok ill try that; do you have any other suggestions in case that doesn't work?

it fluctuates above 50% inside an epoch and then just changes back to 50 % at the end of the epoch

What format is the training data in, as in the target?

Sorry I don't understand the question.

Each picture is a jpg.

That is too little data, 50% is just random guess since you have binary classification problem. It is just not learning anything usefull.

There are several issues that can cause NNs to not learn. Possible duplicate of: What should I do when my neural network doesn't learn?

@Josef Korbel If I input more data, then, would the neural network perform better?
