简而言之 我正在使用我的数据集与Keras迈出第一步。 当我尝试加载模型并在测试图像上进行测试时 - 尽管在训练状态下的先前输出,我只获得了一个类,但准确度为77%。你能指出,预测代码中的错误配置是什么?
详细说明: 数据集是灰度的标记生物细胞,大小为64x64。有两种类型的细胞p和c。 我按照cat / dogs示例中的建议创建了数据集目录。 火车,测试,valrerereies with panca,canca in each。 canca,canca目录包含图像。 e.g:
...
80/80 [==============================] - 8s 101ms/step - loss: 0.2995 - acc: 0.8910 - val_loss: 0.5150 - val_acc: 0.7560
Using TensorFlow backend.
Finished saving
Test loss: 0.46428223699331284
Test accuracy: 0.7759999990463257
我主要也是从cat / dog示例中导出代码并更改了图像大小。
运行代码后,我获得了输出:
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
# dimensions of the images.
img_width, img_height = 64, 64
train_dir = 'c:/tmp/anca_pc/train'
val_dir = 'c:/tmp/anca_pc/val'
test_dir = 'c:/tmp/anca_pc/test'
nb_train_samples = 2000
nb_validation_samples = 500
nb_test_samples = 500
epochs = 5
batch_size = 25
#Is right got gray-scale images? Shouldn't be 1 instead of 3
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
#Define model as proposed in keras tutorials
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))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
datagen = ImageDataGenerator( rescale=1. / 255 )
train_generator = datagen.flow_from_directory(
train_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = datagen.flow_from_directory(
val_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
test_generator = datagen.flow_from_directory(
test_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
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)
#Save model architecture
model_json = model.to_json()
json_file = open("anca_pc.json", "w")
json_file.write(model_json)
json_file.close()
#Save model weights
model.save_weights("anca_pc.h5")
print("Finished saving")
score = model.evaluate_generator(test_generator, nb_test_samples // batch_size)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
print('Scode', score)
这看起来合情合理且很有前途 - 77%的准确率,非常小的数据和短的训练时间(约3分钟)令人难以置信。 我还测试了通过列车目录替换测试目录并获得>的准确性。 90%。所以模型看起来很实用。
然而,当我尝试加载它并测试测试图像时 - 我只获得了一个类。
train.py
import numpy as np
from keras.models import model_from_json
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import matplotlib.pyplot as plt
from scipy.misc import toimage
import os
classes = ['panca', 'canca']
directory = 'C:/tmp/anca_pc/test/'
json_file = open("anca_pc.json", "r")
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("anca_pc.h5")
loaded_model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
for c in classes:
aktdir=directory+c
print(aktdir)
for filename in os.listdir(aktdir):
fn=os.path.join(aktdir, filename)
#print(fn)
img = image.load_img(fn, target_size=(64, 64))
#plt.imshow(img)
#plt.show()
x = image.img_to_array(img)
x = x.astype('float32')
x /= 255
x = np.expand_dims(x, axis=0)
#x = preprocess_input(x)
prediction = loaded_model.predict(x)
#print(prediction)
print(c + " : " + classes[np.argmax(prediction)], " : ", prediction)
predict.py
model.predict_generator(test_generator, nb_test_samples // batch_size)
修改
Test loss: 0.5996998563408852
Test accuracy: 0.7060000032186509
Score [[0.72468185]
[0.07171335]
[0.06702321]
[0.04176971]
[0.76247555]
[0.07743845]
[0.07435916]
[0.9960306 ]
[0.9270018 ]
[0.04746262]
[0.05305129]
[0.9993339 ]
[0.9986149 ]
[0.63527316]
[0.08033804]
[0.3816172 ]
[0.97601706]
[0.83666223]
[0.7226989 ]
[0.5351326 ]
[0.8407803 ]
[0.6953097 ]
[0.89651984]
[0.44985726]
[0.30889446]
[0.16406931]
[0.6346773 ]
[0.13678996]
[0.51343983]
[0.97438985]
[0.9981396 ]
[0.5485193 ]
[0.05270131]
[0.8029713 ]
[0.3295382 ]
[0.1865853 ]
[0.94497275]
[0.07609159]
[0.67434824]
[0.18562992]
[0.53442085]
[0.06662691]
[0.0388172 ]
[0.8763066 ]
[0.9875164 ]
...
的得分输出以3个时期运行
Dim rs As Recordset: Set rs = Me.Recordset
With rs
If Not rs.Updatable Then
'RS is not updateable, delete will fail
Exit Sub
End If
If Not .EOF And Not .BOF Then '0 records
If Me.NewRecord Then
If Not Me.Dirty Then
'Do nothing, unchanged new record so won't be saved
Else
'Revert changes on new record
Me.Undo
End If
Else
.Delete
End If
End If
End With
答案 0 :(得分:4)
np.argmax(预测)将始终返回0,因为预测只包含一个值。
由于你有二进制输出,你需要用这样的东西替换np.argmax:
def get_class(prediction):
return 1 if prediction > 0.5 else 0