我训练了一个模型来对来自两个类别的图像进行分类,并使用model.save()
保存了它。以下是我使用的代码:
from keras.preprocessing.image import ImageDataGeneratorfrom keras.models import Sequentialfrom keras.layers import Conv2D, MaxPooling2Dfrom keras.layers import Activation, Dropout, Flatten, Densefrom keras import backend as K# dimensions of our images.img_width, img_height = 320, 240train_data_dir = 'data/train'validation_data_dir = 'data/validation'nb_train_samples = 200 #totalnb_validation_samples = 10 # totalepochs = 6batch_size = 10if 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))model.add(Activation('sigmoid'))model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])# this is the augmentation configuration we will use for trainingtrain_datagen = ImageDataGenerator( rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)# this is the augmentation configuration we will use for testing:# only rescalingtest_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')model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=5)model.save('model.h5')
它成功地以0.98的准确率进行了训练,效果相当不错。为了在新图像上加载和测试这个模型,我使用了下面的代码:
from keras.models import load_modelimport cv2import numpy as npmodel = load_model('model.h5')model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])img = cv2.imread('test.jpg')img = cv2.resize(img,(320,240))img = np.reshape(img,[1,320,240,3])classes = model.predict_classes(img)print classes
它的输出是:
[[0]]
为什么它不会输出实际的类名,而只是[[0]]
?
回答:
keras的predict_classes函数(文档)输出的是类预测的numpy数组。在你的模型中,这是最后(softmax)层中激活度最高的神经元的索引。[[0]]
意味着你的模型预测你的测试数据属于类别0。(通常你会传递多张图像,结果看起来像[[0], [1], [1], [0]]
)
你必须将实际的标签(例如'cancer', 'not cancer'
)转换为二进制编码(0
代表’cancer’,1
代表’not cancer’)用于二分类。然后你可以将[[0]]
的序列输出解释为类别标签'cancer'