为什么我的预测结果与准确性相比非常差

时间:2017-05-24 07:16:50

标签: python deep-learning keras

我使用Keras在python中编写了一个图像分类代码(2个类别' good'' bad'),训练数据的准确率约为99%,并且验证数据约为95%。 (图像是60倍30像素灰度png文件)

img_width, img_height = 60, 30
nb_train_samples = 4000
nb_validation_samples = 600
epochs = 30
batch_size = 32

if K.image_data_format() == 'channels_first':
    input_shape = (1, img_width, img_height)
else:
    input_shape = (img_width, img_height, 1)

model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=input_shape,padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['binary_accuracy'])

train_datagen = ImageDataGenerator(rescale= 1. /255)
validation_datagen = ImageDataGenerator(rescale= 1. /255)

train_generator = train_datagen.flow_from_directory(
    '/home/admin/Desktop/dataset/train',
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary',
    color_mode='grayscale')

validation_generator = validation_datagen.flow_from_directory(
    '/home/admin/Desktop/dataset/validation',
    target_size=(img_width, img_height),
    batch_size= batch_size,
    class_mode='binary',
    color_mode='grayscale')

model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

model.save_weights('test_weights.h5')

现在我想使用此代码预测验证目录中的所有600个图像。

path='/home/admin/Desktop/dataset/validation/good'
for file in os.listdir(path):
    im=cv2.imread(os.path.join(path,file))
    im=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    im=np.reshape(im,[1, img_width,img_height,1])*(1. /255)
    result=model.predict_classes(im)

然而,300张图片的结果很糟糕'目录是9 [1],291 [0],300个图像在'好'目录是42 [1] 258 [0],这显然是错误的。

我认为我的数据处理方式与 ImageDataGenerator flow_from_directory 不同,这会导致错误,但我不确定。

******** ********更新

当我使用以下代码时,结果是正确的

predict_data=ImageDataGenerator(rescale=1. / 255).flow_from_directory(
    '/home/admin/Desktop/dataset/validation/good',
    target_size=(img_width, img_height),
    batch_size= 300,
    class_mode=None,
    color_mode='grayscale')

result=model.predict_generator(
    predict_data,
    steps=1)

但我仍然不知道这个错误在哪里。

0 个答案:

没有答案