Keras —— 基于InceptionV3模型(不含全连接层)的迁移学习应用
2018-02-25 15:56
459 查看
一、ImageDataGenerator
def image_preprocess(): # 训练集的图片生成器,通过参数的设置进行数据扩增 train_datagen = ImageDataGenerator( preprocessing_function=preprocess_input, rotation_range=30, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) # 验证集的图片生成器,不进行数据扩增,只进行数据预处理 val_datagen = ImageDataGenerator( preprocessing_function=preprocess_input, ) # 训练数据与测试数据 train_generator = train_datagen.flow_from_directory( train_dir, target_size=(IM_WIDTH, IM_HEIGHT), batch_size=batch_size, class_mode='categorical') validation_generator = val_datagen.flow_from_directory( val_dir, target_size=(IM_WIDTH, IM_HEIGHT), batch_size=batch_size, class_mode='categorical') return train_generator, validation_generator
二、加载InceptionV3模型(不含全连接层)
使用带有预训练权重的InceptionV3模型,但不包括顶层分类器(顶层分类器即全连接层。)base_model = InceptionV3(weights='imagenet', include_top=False)
三、添加新的顶层分类器
def add_new_last_layer(base_model, nb_classes): x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(FC_SIZE, activation='relu')(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(input=base_model.input, output=predictions) return model
四、训练顶层分类器
冻结base_model所有层,这样就可以正确获得bottleneck特征def setup_to_transfer_learn(model, base_model): for layer in base_model.layers: layer.trainable = False model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) setup_to_transfer_learn(model, base_model) history_tl = model.fit_generator( train_generator, epochs=nb_epoch, steps_per_epoch=nb_train_samples // batch_size, validation_data=validation_generator, validation_steps=nb_val_samples // batch_size, class_weight='auto')
五、对顶层分类器进行fine_tuning
冻结部分层,对顶层分类器进行Fine-tuneFine-tune以一个预训练好的网络为基础,在新的数据集上重新训练一小部分权重。fine-tune应该在很低的学习率下进行,通常使用SGD优化
def setup_to_finetune(model): for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]: layer.trainable = False for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]: layer.trainable = True model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) setup_to_finetune(model) # 冻结model的部分层 history_ft = model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=nb_epoch, validation_data=validation_generator, validation_steps=nb_val_samples // batch_size, class_weight='auto')
源码地址:
https://github.com/Zheng-Wenkai/Keras_Demo
相关文章推荐
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-05-模型
- Deep learning基于theano的keras学习笔记(1)-Sequential模型
- TensorFlow学习笔记(八) 如何训练自己的图片模型-基于inception模型(未完成)使用迁移学习和从头学习
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-05-模型
- Keras —— 基于Vgg16模型(含全连接层)的图片识别
- Keras 入门课6 -- 使用Inception V3模型进行迁移学习
- kaggle competition 实践学习 文本分类 keras实现 模型基于yoon kim 的 Convolutional Neural Networks for Sentence Class
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-05-模型
- 《最小割模型在信息学竞赛中的应用》——学习笔记
- keras:保存keras学习好的深度神经网络模型参数为二进制和txt文件
- ADO.NET的数据提供程序和数据连接——ADO.NET学习&应用笔记之二
- Keras: 基于Theano和TensorFlow的快速深度学习库
- 【keras】函数式(Functional)模型学习以LSTM为例构建多输入和多输出模型及完整实例(二)
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-04-目标函数
- 深度学习——keras训练RNN模型
- 基于MNIST数据集的深度学习库keras的学习
- 深度学习和自然语言处理的应用和脉络2-复杂模型,最大熵-隐马尔科夫模型-条件随机场
- 统计学习导论:基于R应用——第三章习题
- TensorFlow实现经典深度学习网络(6):TensorFlow实现基于LSTM的语言模型
- 基于TCP连接的C S模式的最简单模型代码