【发布时间】:2019-01-25 05:54:36
【问题描述】:
我在猫和狗之间编译了一个图像分类器,但是在测试一批图片上对其进行测试时,预测结果与预期的 1 或 0 不同:(
我从 google 获得了训练狗和猫的图片,我可能会将一些 png 图片重命名为 jpg,也可能没有,这是问题所在吗?
In [78]:
train_path = 'train'
test_path = 'test'
valid_path = 'valid'
In [79]:
train_batches = ImageDataGenerator().flow_from_directory(train_path, target_size=(224,224), classes=['dogs', 'cats'], batch_size=10)
test_batches = ImageDataGenerator().flow_from_directory(test_path, target_size=(224,224), classes=['dogs', 'cats'], batch_size=10)
valid_batches = ImageDataGenerator().flow_from_directory(valid_path, target_size=(224,224), classes=['dogs', 'cats'], batch_size=4)
Found 40 images belonging to 2 classes.
Found 10 images belonging to 2 classes.
Found 16 images belonging to 2 classes.
In [81]:
imgs, labels = next(train_batches)
In [84]:
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(224,224,3)),
Flatten(),
Dense(2, activation='softmax'), ])
In [85]:
model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
In [86]:
model.fit_generator(train_batches, steps_per_epoch=4, validation_data=valid_batches, validation_steps=4, epochs=5, verbose=2)
Epoch 1/5
- 3s - loss: 6.8502 - acc: 0.5750 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 2/5
- 2s - loss: 8.4620 - acc: 0.4750 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 3/5
- 2s - loss: 8.8650 - acc: 0.4500 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 4/5
- 2s - loss: 7.6561 - acc: 0.5250 - val_loss: 8.0590 - val_acc: 0.5000
Epoch 5/5
- 2s - loss: 8.0590 - acc: 0.5000 - val_loss: 8.0590 - val_acc: 0.5000
<keras.callbacks.History at 0x23a3a6bd2b0>
In [87]:
test_imgs, test_labels = next(test_batches)
In [88]:
test_labels = test_labels[:,0]
test_labels
array([1., 1., 0., 0., 1., 0., 1., 1., 0., 0.], dtype=float32)
In [91]:
predictionz = model.predict_generator(test_batches, steps=1, verbose=0)
In [92]: predictionz
array([[6.3490617e-33, 1.0000000e+00],
[0.0000000e+00, 1.0000000e+00],
[0.0000000e+00, 1.0000000e+00],
[0.0000000e+00, 1.0000000e+00],
[2.1344874e-26, 1.0000000e+00],
[0.0000000e+00, 1.0000000e+00],
[1.4632116e-31, 1.0000000e+00],
[2.2157095e-33, 1.0000000e+00],
[0.0000000e+00, 1.0000000e+00],
[0.0000000e+00, 1.0000000e+00]], dtype=float32)
它们应该以 1 或 0 的形式出现,但它们给出的预测几乎是随机的。
【问题讨论】:
-
也许使用更多的 epoch 来训练模型就可以了
-
我认为您使用的模型非常简单。而且训练的时代很少……随便我做了一个猫和狗的教程。并且这种架构的图像输入非常大,复杂性驻留在全连接中。 github.com/adriaciurana/medium-cnn
标签: keras deep-learning