五种卷积神经网络解决猫狗分类问题(四):V4 预训练-特征提取-合并式

版本描述epochs训练精度验证精度
V1.0简单线性卷积神经网络10099%75%
V2.0添加了数据增强和dropout层10083%83%
V3.0使用预训练-特征提取-分步式10099%90%
V4.0使用预训练-特征提取-合并式10090%90%
V5.0使用预训练-微调模型10099%94%

五种卷积神经网络解决猫狗分类问题(零):总概要
五种卷积神经网络解决猫狗分类问题(一):V1 简单线性网络
五种卷积神经网络解决猫狗分类问题(二):V2 简单线性网络上添加数据增强和dropout层
五种卷积神经网络解决猫狗分类问题(三):V3 预训练-特征提取-分步式
五种卷积神经网络解决猫狗分类问题(四):V4 预训练-特征提取-合并式
五种卷积神经网络解决猫狗分类问题(五):V5 预训练-微调模型

1. 预训练网络

关于预训练网络可看上一篇文章:五种卷积神经网络解决猫狗分类问题(三):预训练-特征提取-分步式

2. 合并式

我们将数据输入整个网络(卷积基+新分类器),而不再像分步式那样保存中间结果再训练单独分类器。合并式是对整个网络进行训练,只不过在反向传播更新权值时只更新新分类器,而不更新卷积基。

3. 代码

3.1 导入预训练网络
  • 设置导入的卷积基不可被训练,从而保护其在训练时不被更新
from keras.applications import VGG16
from keras import layers
from keras import models

conv_base=VGG16(include_top=False,
                weights='imagenet',
                input_shape=(150,150,3))
conv_base.trainable=False  # 冻结参数,使之不被更新
3.2 定义网络模型
  • 使用add()函数堆叠即可
model=models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))
model.summary()

网络结构如下(非代码):

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
vgg16 (Functional)           (None, 4, 4, 512)         14714688  
_________________________________________________________________
flatten (Flatten)            (None, 8192)              0         
_________________________________________________________________
dense (Dense)                (None, 256)               2097408   
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 257       
=================================================================
Total params: 16,812,353
Trainable params: 2,097,665
Non-trainable params: 14,714,688
_________________________________________________________________
3.3 数据准备(数据生成器)
from keras.preprocessing.image import ImageDataGenerator

train_dir='../dogs-vs-cats_small/train/'
validation_dir='../dogs-vs-cats_small/validation/'
test_dir='../dogs-vs-cats_small/test/'

train_datagen=ImageDataGenerator(rescale=1./255,
                                rotation_range=40,
                                width_shift_range=0.2,
                                height_shift_range=0.2,
                                shear_range=0.2,
                                zoom_range=0.2,
                                horizontal_flip=True,
                                fill_mode='nearest')
test_datagen=ImageDataGenerator(rescale=1./255)

train_generator=train_datagen.flow_from_directory(train_dir,
                                                 target_size=(150,150),
                                                 batch_size=20,
                                                 class_mode='binary')
validation_generator=test_datagen.flow_from_directory(validation_dir,
                                                     target_size=(150,150),
                                                     batch_size=20,
                                                     class_mode='binary')
3.4 配置模型
from keras import optimizers
model.compile(loss='binary_crossentropy',
             optimizer=optimizers.RMSprop(lr=2e-5),
             metrics=['acc'])
3.5 开始训练
history=model.fit_generator(train_generator,
                           steps_per_epoch=100,
                           epochs=100,
                           validation_data=validation_generator,
                           validation_steps=50)
3.6 可视化
import matplotlib.pyplot as plt

train_acc=history.history['acc']
train_loss=history.history['loss']

val_acc=history.history['val_acc']
val_loss=history.history['val_loss']

epoch=range(1,len(train_acc)+1)

plt.plot(epoch,train_acc,'bo',label='Train_acc')
plt.plot(epoch,val_acc,'b',label='val_acc')
plt.title("training and validation accuracy")
plt.legend()

plt.figure()
plt.plot(epoch,train_loss,'bo',label='Train_losx')
plt.plot(epoch,val_loss,'b',label='val_loss')
plt.title("training and validation loss")
plt.legend()
plt.show()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是一个对称矩阵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值