修改Anaconda3权限,
右键C:\ProgramData\Anaconda3 文件夹
属性->安全->点击“组或用户名”中的Users->编辑
把“完全控制”打钩->应用->确定
在anaconda 条件下,激活tensorflow
下载
git clone https://github.com/onnx/tensorflow-onnx.git
cd到目录,安装
python setup.py install
python -m tf2onnx.convert --checkpoint ./train_model.ckpt.meta --output ./frozen_model.onnx --inputs img1:0,roi1:0,phase:0 --outputs BiasAdd:0,fc2_f:0 --fold_const --opset 10
遇到错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Beta input to batch norm has bad shape: [32]
这是因为保存的模型存在问题,此时需要做调整。
步骤如下:
使用batchnorm的时候,在训练的时候将training 设置为true,
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
train_op = tf.train.AdamOptimizer(learn_alpha).minimize(loss)
在保存模型的时候:
saver = tf.train.Saver(var_list=tf.global_variables())
savepath = saver.save(sess, 'path/to/model’)
ckpt 转tflite报错:
File "conv_lite.py", line 101, in <module>
tflite_model = converter.convert()
File "C:\ProgramData\Anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\lite\python\lite.py", line 413, in convert
self._set_batch_size(batch_size=1)
File "C:\ProgramData\Anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\lite\python\lite.py", line 498, in _set_batch_size
shape = tensor.get_shape().as_list()
File "C:\ProgramData\Anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 1079, in as_list
raise ValueError("as_list() is not defined on an unknown TensorShape.")
ValueError: as_list() is not defined on an unknown TensorShape.
同样得,再把ckpt转换为onnx得时候,也会报错:
[09/02/2020-17:34:24] [W] [TRT] onnx2trt_utils.cpp:220: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32.
ERROR: builtin_op_importers.cpp:1554 In function importIf:
[8] Assertion failed: cond.is_weights() && cond.weights().count() == 1 && “If condition must be a initializer!”
解决办法:
最大得可能是,输入用tf.placeholder做占位符,但是这个占位符没有指定维度,或者占位符不确定维度过多,导致报错是none.
比如说,phase = tf.placeholder(tf.bool, name='phase')
想办法把这个替换掉
基本能解决问题。
tflite只能接受占位符得第一个维度不确定,也就是batch_size 不确定,[none, 224,224,3]是可以的,但是[none,224,224,none]是不行的。
关注他,获取跟多干货: