Python 有线电视新闻网分类
我开发了一个CNN分类模型来对猫和狗进行分类。当我使用任何既不是猫也不是狗的图像(例如天空或椅子的图像)测试模型时,它以0.99的概率预测它们是狗。知道为什么会这样吗?。这是否意味着该车型过度装修 模型定义如下:Python 有线电视新闻网分类,python,classification,conv-neural-network,Python,Classification,Conv Neural Network,我开发了一个CNN分类模型来对猫和狗进行分类。当我使用任何既不是猫也不是狗的图像(例如天空或椅子的图像)测试模型时,它以0.99的概率预测它们是狗。知道为什么会这样吗?。这是否意味着该车型过度装修 模型定义如下: from keras.layers import Input, Conv2D, Dense,MaxPooling2D, Flatten, Activation,Dense, Dropout, BatchNormalization from keras.models import Mod
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)
您在输出层上使用的softmax激活有2个单元,这使您的模型成为一个二进制分类器。在事先不了解数据集的情况下,很难给出正确的解决方案,但可以归结为两种可能的情况:
- 您的分类标签是相互排斥的(狗或猫,而不是两者)。在这种情况下,您应该添加第三个类,前提是可以相应地转换受监控的数据集
- 标签不是相互排斥的(两个类可以不存在或同时存在)。在这种情况下,应将softmax替换为sigmoid,并确保数据集的格式正确(用于多标签分类)。这相当于同时解决两个独立的二进制分类问题
- 您的分类标签是相互排斥的(狗或猫,而不是两者)。在这种情况下,您应该添加第三个类,前提是可以相应地转换受监控的数据集
- 标签不是相互排斥的(两个类可以不存在或同时存在)。在这种情况下,应将softmax替换为sigmoid,并确保数据集的格式正确(用于多标签分类)。这相当于同时解决两个独立的二进制分类问题