查看TensorFlow checkpoint文件中的变量名和对应值
from tensorflow.python import pywrap_tensorflow
checkpoint_path = os.path.join(model_dir, "model.ckpt")
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
for key in var_to_shape_map:
print("tensor_name: ", key)
print(reader.get_tensor(key)) # Remove this is you want to print only variable names
TensorFlow加载部分模型
#得到该网络中,所有可以加载的参数
variables = tf.contrib.framework.get_variables_to_restore()
for v in variables:
print(v.name,v.shape)
#删除vgg_16_text层中的参数
variables_to_restore = [v for v in variables if v.name.split('/')[0]!='vgg_16_text']
#构建这部分参数的saver
saver = tf.train.Saver(variables_to_restore)
saver.restore(sess,checkpoint_path)
参考资料: