我对猫和狗之间的图像进行了分类,但是在测试一批图片上进行测试时,得出的预测结果不同于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出现,但他们给出的预测几乎是随机的。