预测结果是什么?CNN Keras

我创建了一个CNN模型,试图预测图像是狗还是猫,但在输出结果中我不知道它预测了什么。请看下面的代码:

import pandas as pdfrom keras.models import Sequentialfrom keras.preprocessing.image import ImageDataGeneratorfrom keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2Dfrom scipy import miscimport numpy as npdef build_classifier():    # 模型基于 'https://www.researchgate.net/profile/Le_Lu/publication/277335071/figure/fig8/AS:294249976352779@1447166069905/Figure-8-The-proposed-CNN-model-architecture-is-composed-of-five-convolutional-layers.png'    # 由于处理原因,最好在不创建变量的情况下添加层,但对于小数据集来说影响不大。    classifier = Sequential()    conv1 = Conv2D(filters=64, kernel_size=(2,2), activation='relu', input_shape=(64,64,3))    conv2 = Conv2D(filters=192, kernel_size=(2,2), activation='relu')    conv3 = Conv2D(filters=384, kernel_size=(2,2), activation='relu')    conv4 = Conv2D(filters=256, kernel_size=(2,2), activation='relu')    conv5 = Conv2D(filters=256, kernel_size=(2,2), activation='relu')    pooling1 = MaxPooling2D(pool_size=(2,2))    pooling2 = MaxPooling2D(pool_size=(2,2))    pooling3 = MaxPooling2D(pool_size=(2,2))    fcl1 = Dense(1024, activation='relu')    fcl2 = Dense(1024, activation='relu')    fcl3 = Dense(2, activation='softmax')    dropout1= Dropout(0.5)    dropout2 = Dropout(0.5)    flatten = Flatten()    layers = [conv1, pooling1, conv2, pooling2, conv3, conv4, conv5,             pooling3, flatten, fcl1, dropout1, fcl2, dropout2, fcl3]    for l in layers:        classifier.add(l)    return classifiermodel = build_classifier()model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])train_datagen = ImageDataGenerator(        rescale=1./255,        shear_range=0.2,        zoom_range=0.2,        horizontal_flip=True)test_datagen = ImageDataGenerator(rescale=1./255)train_generator = train_datagen.flow_from_directory(        'dataset/training_set',        target_size=(64, 64),        batch_size=32,        class_mode='categorical')validation_generator = test_datagen.flow_from_directory(        'dataset/test_set',        target_size=(64, 64),        batch_size=32,        class_mode='categorical')model.fit_generator(        train_generator,        steps_per_epoch=200,        epochs=32,        validation_data=validation_generator,        validation_steps=100)model.save('model.h5')model.save_weights('model_weights.h5')

我在另一个文件中打开了保存的模型:

from keras.models import load_modelfrom scipy import miscimport numpy as npdef single_pred(filepath, model):    classifier = load_model(model)    img = misc.imread(filepath)    img = misc.imresize(img, (64,64,3))    img = np.expand_dims(img, 0)    print(classifier.predict(img))if __name__ == '__main__':    single_pred('/home/leonardo/Desktop/Help/dataset/single_prediction/cat_or_dog_2.jpg', 'model.h5')

输出结果如下:

Using TensorFlow backend.2017-10-09 14:06:25.520018: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.2017-10-09 14:06:25.520054: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.[[ 0.  1.]]

但是,我如何知道预测结果是狗还是猫呢?有了这个结果,我仍然不知道图像是狗还是猫。


回答:

除非你指定标签,否则你的生成器会自动为你创建分类标签。你可以使用 train_generator.class_indices 来检查这些标签。类标签的顺序是按字母数字排序的,所以猫=0,狗=1

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注