ckpt转pb,batch normalzition 出现的ValueError问题

出现的问题:

如果是有batch normalzition,或者残差网络层,会出现:

ValueError: Input 0 of node vgg_16/conv1/conv1_1/BatchNorm/cond_1/AssignMovingAvg/Switch was passed float from vgg_16/conv1/conv1_1/BatchNorm/moving_mean:0 incompatible with expected float_ref.

类似问题

则需要在restore模型后加入:


 
 
  1. # fix batch norm nodes
  2. for node in gd.node:
  3. if node.op == 'RefSwitch':
  4. node.op = 'Switch'
  5. for index in xrange(len(node.input)):
  6. if 'moving_' in node.input[index]:
  7. node.input[index] = node.input[index] + '/read'
  8. elif node.op == 'AssignSub':
  9. node.op = 'Sub'
  10. if 'use_locking' in node.attr: del node.attr[ 'use_locking']

参考:https://github.com/tensorflow/tensorflow/issues/3628

参考:https://www.cnblogs.com/bonelee/p/8445261.htm

 

1,这段代码可以加在ckpt2pb里面

2,也可以加在使用pb模型时候

tf.import_graph_def

之前。

 

附:ckpt2pb.py 参考


 
 
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Aug 27 15:26:44 2018
  4. @author: me
  5. """
  6. #https://blog.csdn.net/michael_yt/article/details/74737489
  7. #https://blog.csdn.net/yjl9122/article/details/78341689
  8. import tensorflow as tf
  9. import os.path
  10. import argparse
  11. from tensorflow.python.framework import graph_util
  12. MODEL_DIR = "D:/T_mytest/clsTest/checkpoint/pb"
  13. MODEL_NAME = "frozen_model.pb"
  14. if not tf.gfile.Exists(MODEL_DIR): #创建目录
  15. tf.gfile.MakeDirs(MODEL_DIR)
  16. def freeze_graph(model_folder):
  17. checkpoint = tf.train.get_checkpoint_state(model_folder) #检查目录下ckpt文件状态是否可用
  18. input_checkpoint = checkpoint.model_checkpoint_path #得ckpt文件路径
  19. output_graph = os.path.join(MODEL_DIR, MODEL_NAME) #PB模型保存路径
  20. #output_node_names = "predictions" #原模型输出操作节点的名字
  21. output_node_names = "predicted_val_top_k,predicted_index_top_k" #原模型输出操作节点的名字
  22. saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices= True) #得到图、clear_devices :Whether or not to clear the device field for an `Operation` or `Tensor` during import.
  23. graph = tf.get_default_graph() #获得默认的图
  24. input_graph_def = graph.as_graph_def() #返回一个序列化的图代表当前的图
  25. with tf.Session() as sess:
  26. saver.restore(sess, input_checkpoint) #恢复图并得到数据
  27. #sess.run(tf.global_variables_initializer())
  28. # fix batch norm nodes
  29. for node in input_graph_def.node:
  30. if node.op == 'RefSwitch':
  31. node.op = 'Switch'
  32. for index in range(len(node.input)):
  33. if 'moving_' in node.input[index]:
  34. node.input[index] = node.input[index] + '/read'
  35. elif node.op == 'AssignSub':
  36. node.op = 'Sub'
  37. if 'use_locking' in node.attr: del node.attr[ 'use_locking']
  38. #print ("predictions : ", sess.run("predictions:0", feed_dict={"input_holder:0": [10.0]})) # 测试读出来的模型是否正确,注意这里传入的是输出 和输入 节点的 tensor的名字,不是操作节点的名字
  39. output_graph_def = graph_util.convert_variables_to_constants( #模型持久化,将变量值固定
  40. sess,
  41. input_graph_def,
  42. output_node_names.split( ",") #如果有多个输出节点,以逗号隔开
  43. )
  44. with tf.gfile.GFile(output_graph, "wb") as f: #保存模型
  45. f.write(output_graph_def.SerializeToString()) #序列化输出
  46. print( "%d ops in the final graph." % len(output_graph_def.node)) #得到当前图有几个操作节点
  47. #for op in graph.get_operations():
  48. #print(op.name, op.values())
  49. if __name__ == '__main__':
  50. parser = argparse.ArgumentParser()
  51. parser.add_argument( "--model_folder",default= "D:/T_mytest/clsTest/checkpoint", type=str, help= "input ckpt model dir") #命令行解析,help是提示符,type是输入的类型,
  52. # 这里运行程序时需要带上模型ckpt的路径,不然会报 error: too few arguments
  53. aggs = parser.parse_args()
  54. freeze_graph(aggs.model_folder)
  55. # freeze_graph("model/ckpt") #模型目录

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值