tensorflow 选择性fine-tune(微调)

'''
多种选择fine-tune部分权重的方法

Multiple selection load weights for fine-tune

注:slim的fine-tune 适用slim模块conv bn(尽量一致对应模块)

第一种没有试验过:个人理解:可以不用管 不需要fine-tune的权重 直接tf.global_variables_initializer即可 (slim模块就是这样实验的)

'''
import tensorflow as tf
from tensorflow.contrib import slim

model_path=r'**/model.ckpt'

# 1
sess=tf.Session()
var = tf.global_variables() # all weights' name dict
var_to_restore = [val  for val in var if 'conv1' in val.name or 'conv2'in val.name] # select which to fine-tune
saver = tf.train.Saver(var_to_restore)
saver.restore(sess, model_path)# fine-tune those weights (names must be same)
var_to_init = [val  for val in var if 'conv1' not in val.name or 'conv2'not in val.name] # need to init
tf.variables_initializer(var_to_init)
# when save model ,need to create a new saver

# 2
sess=tf.Session()
exclude = ['conv1', 'conv2'] # need write the actual name
variables_to_restore = slim.get_variables_to_restore(exclude=exclude)
saver = tf.train.Saver(variables_to_restore)
saver.restore(sess, model_path)
# when save model ,need to create a new saver

# 3
exclude = ['weight1','weight2']
variables_to_restore = slim.get_variables_to_restore(exclude=exclude)
init_fn = slim.assign_from_checkpoint_fn(model_path, variables_to_restore)

init_fn(sess)
具体实现代码: https://blog.csdn.net/leilei18a/article/details/80189917
'''
个人见解: 如果是要fine-tune前面连续的层权重,可以先建立网络,然后在需要fine-tune层
最后位置添加init_fn = slim.assign_from_checkpoint_fn(model_path, slim.get_model_variables('model_name')) # model_name: eg resnet_v2_152
然后,在tf.global_variables_initializer 执行后,添加init_fn(sess),即: 只fine-tune
前面某些层权重。【当然也可以在最后弄,选择加载即可其他的自动会init】
别人点醒我:init_fn在网络哪个位置,此位置之前会自动从get_model_variables中提取相同名字的
权重。(尽量名字一致)
'''
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值