python cnn代码详解 keras_python – CNN keras中图像的混淆矩阵

我已经使用keras训练了我的CNN模型(多类分类),现在我想在我的测试图像集上评估模型.

除了准确性,精确度和召回率之外,评估我的模型有哪些可能的选择?我知道如何从自定义脚本中获得精确度和召回率.但我无法找到一种方法来获取我的12类图像的混淆矩阵.

Scikit-learn显示way,但不显示图像.

我正在使用model.fit_generator()

有没有办法为我的所有课程创建混淆矩阵或在我的课程中找到分类信心?我正在使用谷歌Colab,虽然我可以下载模型并在本地运行它.

任何帮助,将不胜感激.

码:

train_data_path = 'dataset_cfps/train'

validation_data_path = 'dataset_cfps/validation'

#Parametres

img_width, img_height = 224, 224

vggface = VGGFace(model='resnet50', include_top=False, input_shape=(img_width, img_height, 3))

#vgg_model = VGGFace(include_top=False, input_shape=(224, 224, 3))

last_layer = vggface.get_layer('avg_pool').output

x = Flatten(name='flatten')(last_layer)

xx = Dense(256, activation = 'sigmoid')(x)

x1 = BatchNormalization()(xx)

x2 = Dropout(0.3)(x1)

y = Dense(256, activation = 'sigmoid')(x2)

yy = BatchNormalization()(y)

y1 = Dropout(0.6)(yy)

x3 = Dense(12, activation='sigmoid', name='classifier')(y1)

custom_vgg_model = Model(vggface.input, x3)

# Create the model

model = models.Sequential()

# Add the convolutional base model

model.add(custom_vgg_model)

model.summary()

#model = load_model('facenet_resnet_lr3_SGD_sameas1.h5')

def recall(y_true, y_pred):

true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))

possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))

recall = true_positives / (possible_positives + K.epsilon())

return recall

def precision(y_true, y_pred):

true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))

predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))

precision = true_positives / (predicted_positives + K.epsilon())

return precision

train_datagen = ImageDataGenerator(

rescale=1./255,

rotation_range=20,

width_shift_range=0.2,

height_shift_range=0.2,

horizontal_flip=True,

fill_mode='nearest')

validation_datagen = ImageDataGenerator(rescale=1./255)

# Change the batchsize according to your system RAM

train_batchsize = 32

val_batchsize = 32

train_generator = train_datagen.flow_from_directory(

train_data_path,

target_size=(img_width, img_height),

batch_size=train_batchsize,

class_mode='categorical')

validation_generator = validation_datagen.flow_from_directory(

validation_data_path,

target_size=(img_width, img_height),

batch_size=val_batchsize,

class_mode='categorical',

shuffle=True)

# Compile the model

model.compile(loss='categorical_crossentropy',

optimizer=optimizers.SGD(lr=1e-3),

metrics=['acc', recall, precision])

# Train the model

history = model.fit_generator(

train_generator,

steps_per_epoch=train_generator.samples/train_generator.batch_size ,

epochs=100,

validation_data=validation_generator,

validation_steps=validation_generator.samples/validation_generator.batch_size,

verbose=1)

# Save the model

model.save('facenet_resnet_lr3_SGD_new_FC.h5')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值