以下是我最近的 SO 帖子中的文本/代码,希望您能发表评论:
使用 Keras/Theano,我使用CIFAR10 的 Keras 示例代码成功地训练了一个模型。它在带有 GTX98-Ti GPU 和 Theano(最新版本)后端的 MacPro 上运行了大约 2 个小时。
所有原始参数都没有变化,模型达到了约 81% 的准确度。在脚本结束时,权重(weights.h5,磁盘上的 5MB)和架构(arch.json)被导出。
在另一个脚本中,我恢复了模型和权重,并尝试使用以下方法对单个图像进行分类:
from skimage.data import imread
import os
from keras.models import model_from_json
from keras.optimizers import SGD
model = model_from_json(open('./arch.json').read())
model.load_weights('./weights.h5')
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
for root, dirs, files in os.walk('./input/singles/'):
for idx, file in enumerate(files):
if not file.startswith('.'):
file_path = root + os.sep + file
img = imread(file_path)
img = fn_resize(img, 32) # this is a resize function in another file
# I have plotted images at this point and they are
# correctly sized smaller version of the original
# normalized between 0-1 as in training
img = img.reshape((1, 3, 32, 32))
y_pred = int(model.predict_classes(img, 1))
pred_labels = ['airplane', 'automobile', ' bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
print(pred_labels[y_pred], "prediction \\\", file, "truth")
从本质上讲,它恢复了模型和架构,然后在一个文件夹中循环浏览来自网络的 10 张图像,将它们调整为 32x32 像素,重塑数组,并将数据转发到model.predict_classes().
我很惊讶地看到下面的输出,其中没有一个图像被正确分类。有没有人做过这样的实验并知道模型构建是否存在问题或者我的程序是否不正确?
Using Theano backend.
Using gpu device 0: GeForce GTX 980 Ti (CNMeM is disabled, cuDNN 5005)
1/1 [==============================] - 0s
truck predicted \\\ airplane.jpeg truth
1/1 [==============================] - 0s
airplane predicted \\\ bird.jpeg truth
1/1 [==============================] - 0s
airplane predicted \\\ car.jpg truth
1/1 [==============================] - 0s
airplane predicted \\\ cat.jpeg truth
1/1 [==============================] - 0s
frog predicted \\\ deer.jpeg truth
1/1 [==============================] - 0s
ship predicted \\\ frog.jpeg truth
1/1 [==============================] - 0s
automobile predicted \\\ horse.jpeg truth
1/1 [==============================] - 0s
automobile predicted \\\ dog.tif truth
1/1 [==============================] - 0s
frog predicted \\\ ship.jpeg truth
1/1 [==============================] - 0s
airplane predicted \\\ truck.jpeg truth
Process finished with exit code 0