keras 迁移学习, 微调, model的predict函数定义
来源:互联网 发布:js drag拖拽 编辑:程序博客网 时间:2024/05/17 04:10
点击这里:猫狗大战keras实例
def add_new_last_layer(base_model, nb_classes): """Add last layer to the convnet Args: base_model: keras model excluding top nb_classes: # of classes Returns: new keras model with last layer """ 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
载入预训练模型作为前端的网络,在自己的数据集上进行微调,最好按照以下两步进行:
- Transfer learning:freeze all but the penultimate layer and re-train the last
Dense
layer - Fine-tuning:un-freeze the lower convolutional layers and retrain more layers
Doing both, in that order, will ensure a more stable and consistent training. This is because the large gradient updates triggered by randomly initialized weights could wreck the learned weights in the convolutional base if not frozen. Once the last layer has stabilized (transfer learning), then we move onto retraining more layers (fine-tuning).
Transfer learning
def setup_to_transfer_learn(model, base_model): """Freeze all layers and compile the model""" for layer in base_model.layers: layer.trainable = False model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
Fine-tune
def setup_to_finetune(model): """Freeze the bottom NB_IV3_LAYERS and retrain the remaining top layers. note: NB_IV3_LAYERS corresponds to the top 2 inception blocks in the inceptionv3 architecture Args: model: keras 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')
When fine-tuning, it’s important to lower your learning rate relative to the rate that was used when training from scratch (lr=0.0001
), otherwise, the optimization could destabilize and the loss diverge.
Training
Now we’re all set for training. Usefit_generator
for both transfer learning and fine-tuning. 分两个阶段依次进行训练
history = model.fit_generator( train_generator, samples_per_epoch=nb_train_samples, nb_epoch=nb_epoch, validation_data=validation_generator, nb_val_samples=nb_val_samples, class_weight='auto')
model.save(args.output_model_file)
在keras2.0版本以上时,函数参数做了改变
datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range=0.1, # randomly shift images horizontally (fraction of total width) height_shift_range=0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False) # randomly flip images # Compute quantities required for feature-wise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) # Fit the model on the batches generated by datagen.flow(). model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=x_train.shape[0] // batch_size, epochs=epochs, validation_data=(x_test, y_test))
预测函数:
def predict(model, img, target_size, top_n=3): """Run model prediction on image Args: model: keras model img: PIL format image target_size: (width, height) tuple top_n: # of top predictions to return Returns: list of predicted labels and their probabilities """ if img.size != target_size: img = img.resize(target_size)
x = image.img_to_array(img) x = np.expand_dims(x, axis=0) # 插入这一个轴是关键,因为keras中的model的tensor的shape是(bath_size, h, w, c),如果是tf后台 x = preprocess_input(x) preds = model.predict(x) return decode_predictions(preds, top=top_n)[0]
- keras 迁移学习, 微调, model的predict函数定义
- 迁移学习与微调的关系
- 迁移学习和微调深度卷积神经网络
- 迁移学习和微调深度卷积神经网络
- keras的基本用法(五)——图像predict
- CS231n 卷积神经网络与计算机视觉 11 卷积神经网络的 迁移学习 和微调
- 迁移学习(transfer learning)和微调(fine-tune)的几点认识
- Keras学习笔记---保存model文件和载入model文件
- keras.model的保存与打开
- deep learning 深度学习网络的微调
- keras迁移学习 使用vgg16进行手写数字识别
- 深度学习系列Part2:迁移学习和微调深度卷积神经网络
- 深度学习系列Part2:迁移学习和微调深度卷积神经网络
- 深度学习系列Part2:迁移学习和微调深度卷积神经网络
- Keras <一> 可视化model
- Keras 可视化 model visualization
- keras merged model
- YII Framework学习教程-YII的Model-数据库操作5-数据迁移官方翻译
- 推荐几个安卓开发者常用并且实用的网站
- Github使用之clone项目
- bzoj2049Cave 洞穴勘测
- 屏蔽ios弹性滑动
- hibernate加载策略session.load()与session.get()---【小白系列】0基础到熟练应用hibernate框架(五)
- keras 迁移学习, 微调, model的predict函数定义
- python_爬虫http协议
- 流程控制
- 并查集 排列组合 In Chinese Restaurant:URAL
- Conky--linux自定义桌面监控
- 2017身残志坚的定西考生魏祥上清华
- windows部署iBase4J
- H5滚动翻页
- Android4.4蓝牙耳机HFP流程分析-3