Keras Predictions中没有类别标签

时间:2017-03-30 03:00:55

标签: python machine-learning classification keras

我正在尝试使用Keras解决Cats vs Dogs问题。这是我正在使用的模型。

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
from keras import regularizers
from keras.utils import plot_model

img_width, img_height = 150, 150
train_data_dir = 'kateVSdoge/train'
validation_data_dir = 'kateVSdoge/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16
if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

model = Sequential()

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

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

model.add(Conv2D(64, (3, 3)))
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,kernel_regularizer=regularizers.l2(0.01),
                activity_regularizer=regularizers.l1(0.01)))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              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)
train_generator = train_datagen.flow_from_directory(
                    train_data_dir,
                    target_size=(img_width, img_height),
                    batch_size=batch_size,
                    class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
                        validation_data_dir,
                        target_size=(img_width, img_height),
                        batch_size=batch_size,
                        class_mode='binary',
                        )
xm=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('first_try3.h5')
model_json=model.to_json()
with open("model3.json","w+") as json_file:
    json_file.write(model_json)
plot_model(model,to_file="model.jpeg")

该模型最终精确度为0.79-0.80。但是当我尝试在预测器脚本中加载模型并使用 model.predict_generator()预测时,我似乎做错了,因为我无法在预测中获得类名。我尝试过 .predict() .predict_proba()

以下是预测器脚本:

from keras.models import Sequential, model_from_json
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
import numpy as np


p_model = Sequential();
jsonfile = open('model3.json','r')
model_json = jsonfile.read()
p_model = model_from_json(model_json)
p_model.load_weights('first_try3.h5')
p_model.compile(loss='binary_crossentropy',
                optimizer='rmsprop',
                metrics=['accuracy'])

img = image.load_img('do.jpg', target_size=(150,150))
x=image.img_to_array(img)
x=x.reshape((1,)+x.shape)
test_datagen = ImageDataGenerator(rescale=1. /255)
m=test_datagen.flow(x,batch_size=1)


preds = p_model.predict_generator(m,1,verbose=1)
print preds

此外,我观察到一个有趣的事情,图像似乎没有重新缩放。 我打印出 x m.x ,两个矩阵似乎都相等,且值不会转换为介于0和1之间。

这是猫和狗图片的输出。

(myenv)link@zero-VirtualBox:~/myenv/keras_app$ python predictor.py 
Using Theano backend.
1/1 [==============================] - 0s
[[ 0.29857877]]
(myenv)link@zero-VirtualBox:~/myenv/keras_app$ python predictor.py 
Using Theano backend.
1/1 [==============================] - 0s
[[ 0.77536112]]

我已经使用了https://stackoverflow.com/a/41833076/4159447给出的建议来介绍正则化和重新缩放。

我做错了什么?我想要的只是让猫狗标签与他们的分数相符。

1 个答案:

答案 0 :(得分:2)

唯一错误的是期望来自分类器的类名。分类器不知道类名,即后处理步骤,如:

preds = p_model.predict_generator(m,1,verbose=1)[0]

if preds > 0.5:
    output = "cat"
else:
   output = "dog"

请注意,0.5可能不是最佳阈值,您也可以选择概率最大的类(p vs 1 - p)。