Tensorflow 部分恢复模型

来源:互联网 发布:信捷plc xc3编程软件 编辑:程序博客网 时间:2024/04/30 14:10

It is often desirable to fine-tune a pre-trained model on an entirely new dataset or even a new task. In these situations, one can use TF-Slim’s helper functions to select a subset of variables to restore:

# Create some variables.v1 = slim.variable(name="v1", ...)v2 = slim.variable(name="nested/v2", ...)...# Get list of variables to restore (which contains only 'v2'). These are all# equivalent methods:variables_to_restore = slim.get_variables_by_name("v2")# orvariables_to_restore = slim.get_variables_by_suffix("2")# orvariables_to_restore = slim.get_variables(scope="nested")# orvariables_to_restore = slim.get_variables_to_restore(include=["nested"])# or# 举个例子 vgg/conv6 vgg 都可以作为exclude的参数传入variables_to_restore = slim.get_variables_to_restore(exclude=["v1"])# Create the saver which will be used to restore the variables.restorer = tf.train.Saver(variables_to_restore)with tf.Session() as sess:  # Restore variables from disk.  restorer.restore(sess, "/tmp/model.ckpt")  print("Model restored.")  # Do some work with the model  ...
阅读全文
0 0
原创粉丝点击