【发布时间】:2018-06-13 05:55:07
【问题描述】:
我开发了一个 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)
【问题讨论】:
-
能否提供一些模型代码以便更好地理解?
-
我已将模型定义添加到问题中
-
Ceci n'est pas un chat ????
标签: python classification conv-neural-network