"""定制resnet后面的层"""
def custom(input_size,num_classes,pretrain):
# 引入初始化resnet50模型
base_model = ResNet50(weights=pretrain,
include_top=False,
pooling=None,
input_shape=(input_size,input_size, 3),
classes=num_classes)
#由于有预权重,前部分冻结,后面进行迁移学习
for layer in base_model.layers:
layer.trainable = False
#添加后面的层
x = base_model.output
x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
x = layers.Dropout(0.5,name='dropout1')(x)
#regularizers正则化层,正则化器允许在优化过程中对层的参数或层的激活情况进行惩罚
#对损失函数进行最小化的同时,也需要让对参数添加限制,这个限制也就是正则化惩罚项,使用l2范数
x = layers.Dense(512,activation='relu',kernel_regularizer= regularizers.l2(0.0001),name='fc2')(x)
x = layers.BatchNormalization(name='bn_fc_01')(x)
x = layers.Dropout(0.5,name='dropout2')(x)
#40个分类
x = layers.Dense(num_classes,activation='softmax')(x)
model = Model(inputs=base_model.input,outputs=x)
#模型编译
model.compile(optimizer="adam",loss = 'categorical_crossentropy',metrics=['accuracy'])
return model
网络的训练是迁移学习过程,使用已有的初始resnet50权重(5个stage已经训练过,卷积层已经能够提取特征),我们只训练后面的全连接层部分,4个epoch后再对较后面的层进行训练微调一下,获得更高准确率,训练过程如下:
class Net():
def __init__(self,img_size,gar_num,data_dir,batch_size,pretrain):
self.img_size=img_size
self.gar_num=gar_num
self.data_dir=data_dir
self.batch_size=batch_size
self.pretrain=pretrain
def build_train(self):
"""迁移学习"""
model = resnet.custom(self.img_size, self.gar_num, self.pretrain)
model.summary()
train_sequence, validation_sequence = genit.gendata(self.data_dir, self.batch_size, self.gar_num, self.img_size)
epochs=4
model.fit_generator(train_sequence,steps_per_epoch=len(train_sequence),epochs=epochs,verbose=1,validation_data=validation_sequence,
max_queue_size=10,shuffle=True)
#微调,在实际工程中,激活函数也被算进层里,所以总共181层,微调是为了重新训练部分卷积层,同时训练最后的全连接层
layers=149
learning_rate=1e-4
for layer in model.layers[:layers]:
layer.trainable = False
for layer in model.layers[layers:]:
layer.trainable = True
Adam =adam(lr=learning_rate, decay=0.0005)
model.compile(optimizer=Adam, loss='categorical_crossentropy', metrics=['accuracy'])
model.fit_generator(train_sequence,steps_per_epoch=len(train_sequence),epochs=epochs * 2,verbose=1,
callbacks=[
callbacks.ModelCheckpoint('./models/garclass.h5',monitor='val_loss', save_best_only=True, mode='min'),
callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1,patience=10, mode='min'),
callbacks.EarlyStopping(monitor='val_loss', patience=10),],
validation_data=validation_sequence,max_queue_size=10,shuffle=True)
print('finish train,look for garclass.h5')