我开发了一个CNN分类模型来分类猫和狗。当我使用任何既不是猫也不是狗的图像(例如天空或椅子的图片)测试模型时,它以0.99的概率预测它们为狗。知道为什么会这样吗?这是否意味着这一模式过于夸张了?
模型定义如下,
from keras.layers import Input, Conv2D, Dense,MaxPooling2D, Flatten, Activation,Dense, Dropout, BatchNormalization
from keras.models import Model
from keras.backend import tf as ktf
import numpy as np
np.random.seed(123)
def mean_subtract(img):
#img = T.set_subtensor(img[:,0,:,:],img[:,0,:,:] - 123.68)
#img = T.set_subtensor(img[:,1,:,:],img[:,1,:,:] - 116.779)
#img = T.set_subtensor(img[:,2,:,:],img[:,2,:,:] - 103.939)
return img / 255.0
def cats_dogs_model():
input_shape = (3, 256, 256)
x_input = Input(input_shape)
x = Lambda(mean_subtract, name='mean_subtraction')(x_input)
# Conv Layer 1
x = Convolution2D(96, 7, 7, subsample=(4,4), activation='relu',
name='conv_1', init='he_normal')(x_input)
x = MaxPooling2D((3, 3), strides=(2,2))(x)
x = BatchNormalization()(x)
x = ZeroPadding2D((2,2))(x)
# Conv Layer 2
x = Convolution2D(256, 5, 5, activation='relu', name='conv_2', init='he_normal')(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
x = BatchNormalization()(x)
x = ZeroPadding2D((2, 2))(x)
# Conv Layer 3
x = Convolution2D(384, 3, 3, activation='relu',
name='conv_3', init='he_normal')(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
x = Flatten()(x)
x = Dense(512, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(512, activation="relu")(x)
x = Dropout(0.5)(x)
predictions = Dense(2, activation="softmax")(x)
return Model(inputs=x_input, outputs=predictions)发布于 2018-01-03 15:06:22
您正在使用的软件最大激活输出层有2个单位,这使您的模型二进制分类器。在没有事先了解数据集的情况下,很难给出正确的解决方案,但可以归结为两种可能的情况:
https://stackoverflow.com/questions/48079392
复制相似问题