keras CNN model.predict_classes return 3 元素数组
keras CNN model.predict_classes return array of 3 element
我在 CNN 的 Keras 上训练了一个模型,它应该得到具有形状 (-1,32,32,1)
的图像的输入
训练后,我通过向模型输入新图像来测试模型并使用 model.predict_class 获得预测并检查预测是否与所需标签匹配,但是模型 return 是一个包含 3 个元素的数组([3 12 22])
为什么它没有 return 一个 class '
这是预测前图像的预处理代码
read_img = cv2.imread('test-images/' + file + '/' + img)
read_img = read_img.reshape( -1,32, 32, 1)
read_img = read_img.astype('float32')/255
maxind = model.predict_classes(read_img)
这是预测的输出
[22 12 4]
用于构建模型的代码:
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=2,padding='same',activation='relu',input_shape=(32,32,1)))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(filters=64, kernel_size=2, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(24, activation='softmax'))
model.summary()
模型输入、输出和图像形状
model.inputs = [tf.Tensor 'conv2d_1_input:0' shape=(?, 32, 32, 1) dtype=float32]
model.outputs = [tf.Tensor 'dense_2/Softmax:0' shape=(?, 24) dtype=float32]
read_img.shape = (3, 32, 32, 1)
简短的回答是:你有 3 个矢量 类 因为你在网络中放入了 3 张图像。请查看输入张量的形状/
read_img.shape = (3, 32, 32, 1)
第一个维度 - 3 - 它是批量大小,这意味着你的网络对示例进行预测 - 这就是你的预测有 3 类 的原因。请检查输入图像的尺寸以确定您预测了多少张照片。
如果您想对一张照片进行预测,您的输入必须具有 (1,32,32,1)
.
的形状
我在 CNN 的 Keras 上训练了一个模型,它应该得到具有形状 (-1,32,32,1)
的图像的输入
训练后,我通过向模型输入新图像来测试模型并使用 model.predict_class 获得预测并检查预测是否与所需标签匹配,但是模型 return 是一个包含 3 个元素的数组([3 12 22])
为什么它没有 return 一个 class '
这是预测前图像的预处理代码
read_img = cv2.imread('test-images/' + file + '/' + img)
read_img = read_img.reshape( -1,32, 32, 1)
read_img = read_img.astype('float32')/255
maxind = model.predict_classes(read_img)
这是预测的输出
[22 12 4]
用于构建模型的代码:
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=2,padding='same',activation='relu',input_shape=(32,32,1)))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(filters=64, kernel_size=2, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(24, activation='softmax'))
model.summary()
模型输入、输出和图像形状
model.inputs = [tf.Tensor 'conv2d_1_input:0' shape=(?, 32, 32, 1) dtype=float32]
model.outputs = [tf.Tensor 'dense_2/Softmax:0' shape=(?, 24) dtype=float32]
read_img.shape = (3, 32, 32, 1)
简短的回答是:你有 3 个矢量 类 因为你在网络中放入了 3 张图像。请查看输入张量的形状/
read_img.shape = (3, 32, 32, 1)
第一个维度 - 3 - 它是批量大小,这意味着你的网络对示例进行预测 - 这就是你的预测有 3 类 的原因。请检查输入图像的尺寸以确定您预测了多少张照片。
如果您想对一张照片进行预测,您的输入必须具有 (1,32,32,1)
.