gpt4 book ai didi

python - Keras 预测中缺少类标签

转载 作者:行者123 更新时间:2023-11-30 09:00:13 27 4
gpt4 key购买 nike

我正在尝试使用 Keras 解决猫狗问题。这是我正在使用的模型。

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

我还观察到一件有趣的事情,图像似乎没有重新缩放。我打印了 xm.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引入正则化器和重新缩放。

我做错了什么?我想要的只是根据他们的分数获得猫和狗的标签。

最佳答案

唯一错误的是期望来自分类器的类名称。分类器不知道类名,这是一个后处理步骤,类似于:

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)。

关于python - Keras 预测中缺少类标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43107736/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com