我开发了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)
答案 0 :(得分:2)
您在具有2个单位的输出图层上使用softmax激活,这使您的模型成为二元分类器。如果没有先前的数据集知识,很难给出正确的解决方案,但归结为两个可能的情况: