【问题标题】:How do I know what the output of model.predict() correspond to?我怎么知道 model.predict() 的输出对应什么?
【发布时间】:2020-10-27 08:52:25
【问题描述】:

我正在尝试制作一个对猫和狗进行分类的 CNN,我正在使用 flow_from_directory() 为模型准备数据。

from keras import Sequential
from keras_preprocessing.image import ImageDataGenerator
from keras.layers import *
from keras.callbacks import ModelCheckpoint
from keras.optimizers import *
import keras
import numpy as np
import os

img_size = 250 # number of pixels for width and height

#Random Seed
np.random.seed(123456789)


training_path = os.getcwd() + "/cats and dogs images/train"
testing_path = os.getcwd() + "/cats and dogs images/test"

#Defines the Model
model = Sequential([
        Conv2D(filters=128, kernel_size=(3,3), activation="relu", padding="same", input_shape=(img_size,img_size,3)),
        MaxPool2D(pool_size=(2,2), strides=2),
        Conv2D(filters=64, kernel_size=(3,3), activation="relu", padding="same"),
        Flatten(),
        Dense(32, activation="relu"),
        Dense(2, activation="softmax")
])


#Scales the pixel values to between 0 to 1
datagen = ImageDataGenerator(rescale=1.0/255.0)

Batch_size = 10

#Prepares Training Data
training_dataset = datagen.flow_from_directory(directory = training_path,
                                               target_size=(img_size,img_size),
                                               classes = ["cat","dog"],
                                               class_mode = "categorical",
                                               batch_size = Batch_size)

#Prepares Testing Data
testing_dataset = datagen.flow_from_directory(directory = testing_path,
                                              target_size=(img_size,img_size),
                                              classes = ["cat","dog"],
                                              class_mode = "categorical",
                                              batch_size = Batch_size)


#Compiles the model
#model.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=['accuracy'])
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])
#model.compile(loss="mse", optimizer="sgd", metrics=[keras.metrics.MeanSquaredError()])

#Checkpoint
filepath = os.getcwd() + "/trained_model.h5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min', save_freq=1)

#Fitting the model to the dataset (Training the Model)
model.fit(x = training_dataset, steps_per_epoch = 400,
          validation_data=testing_dataset, validation_steps=100,
          epochs = 10, callbacks=[checkpoint], verbose = 1)


# evaluate model on training dataset
_,acc = model.evaluate_generator(training_dataset, steps=len(training_dataset), verbose=0)
print("Accuracy on training dataset:")
print('> %.3f' % (acc * 100.0))


#evaluate model on testing dataset
_,acc = model.evaluate_generator(testing_dataset, steps=len(testing_dataset), verbose=0)
print("Accuracy on testing dataset:")
print('> %.3f' % (acc * 100.0))

我想知道model.predict() 的输出将如何对应于标签猫和狗,以及输出中的两个数字中哪一个是猫,哪个是狗? 这是我加载模型并给出预测的代码:

from keras.models import Sequential
from keras_preprocessing.image import *
from keras.layers import *
import tensorflow as tf
import numpy as np
from keras.layers.experimental.preprocessing import Rescaling
import os
import cv2
from keras.models import *

img_size = 250

#Load weights into new model
filepath = os.getcwd() + "/trained_model.h5"

model = load_model(filepath)
print("Loaded model from disk")

#Scales the pixel values to between 0 to 1
#datagen = ImageDataGenerator(rescale=1.0/255.0)

#Prepares Testing Data

testing_dataset = cv2.imread(os.getcwd() + "/cats and dogs images/single test sample/507.png")
#img = datagen.flow_from_directory(testing_dataset, target_size=(img_size,img_size))

img = cv2.resize(testing_dataset, (img_size,img_size))
newimg = np.asarray(img)
pixels = newimg.astype('float32')
pixels /= 255.0
print(pixels.shape)
pixels = np.expand_dims(pixels, axis=0)
print(pixels.shape)
prediction = model.predict(pixels)
print(prediction)

这是上面预测代码的输出:

Loaded model from disk
(250, 250, 3)
(1, 250, 250, 3)
[[5.4904184e-27 1.0000000e+00]]

如您所见,预测给出了一个包含两个数字的数组,但哪个对应于狗标签,哪个对应于猫标签?顺便说一句,该模型没有完全训练,所以我只是测试代码看看它是否有效。

【问题讨论】:

  • np.argmax(answered_correctly, axis=1) 为了获得 0 类或 1 类。从您的代码中,猫似乎是 0 类而狗 1
  • 您能解释一下我如何实现您的代码以及 answer_correctly 的来源吗?
  • 抱歉,answered_correctly 是您的预测数组
  • 您的设置有些有趣:如果您使用多个输出(每个类一个)和 softmax 输出激活,请确保 categorical_crossentropy 损失。如果你只使用一个输出,假设一个输出意味着 0 代表猫,1 代表狗,那么请确保使用 binary_crossentropy 损失函数。
  • 很好,我一定会改变的。我把它弄错了的唯一原因是因为我想看看有什么不同

标签: keras neural-network conv-neural-network


【解决方案1】:

模型输出取决于您如何加载数据并在您提供的代码中指定如何对类进行排序/标记:

training_dataset = datagen.flow_from_directory(directory = training_path,
                                           target_size=(img_size,img_size),
                                           classes = ["cat","dog"],
                                           class_mode = "categorical",
                                           batch_size = Batch_size)

#Prepares Testing Data
testing_dataset = datagen.flow_from_directory(directory = testing_path,
                                              target_size=(img_size,img_size),
                                              classes = ["cat","dog"],
                                              class_mode = "categorical",
                                              batch_size = Batch_size)

您在加载数据期间指定类将在 classes 参数中按 Cat 然后 Dog 排序。

因此输出将被排序为两个概率(总和为 1) 第一个概率是指输入图像是猫的百分比,第二个概率是指输入图像是狗的百分比。

你使用这条线:

output_class = np.argmax(prediction, axis=1)

这一行将比较列表的元素,并以 [1](或 [0, 1] 的形式输出列表元素的哪个索引最大(在我们的例子中是包含两个概率的列表)取决于输出的形状)这意味着所述图像是一只狗,因为输出列表中的第二个元素是 1 如果它是 [0] (或 [1, 0] 取决于输出的形状)那么这意味着输入图像的输出类是猫。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    相关资源
    最近更新 更多