卷积神经网络50%精度问题

时间:2018-07-19 20:14:59

标签: python keras conv-neural-network

我正在构建和测试我的第一个卷积神经网络。我的数据集包括每个班级10张训练图像和每个班级3张测试图像。我的代码如下:

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'

没有引发任何错误,但是在10个时期之后,我的准确率只有50%。我想知道是否有人可以帮助我解决此问题。该程序的输出如下。

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 _____________________________________

我尝试运行更多的时期,但输出是相同的。

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

0 个答案:

没有答案