mmdnn tensorflow 转 caffe

参考

https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/tensorflow/README.md#how-to-prepare-your-model-and-conversion-parameters

查看可用模型:

How to prepare your model and conversion parameters

You can refer Slim Model Extractor to extract your own tensorflow model, which is a sample tool to extract both architecture and weights from slim pre-trained models.

$ mmdownload -f tensorflow

Support frameworks: ['inception_v3_frozen', 'resnet_v2_200', 'inception_v1', 'mobilenet_v1_1.0', 'mobilenet_v2_1.0_224', 'resnet_v2_152', 'vgg16', 'mobilenet_v1_1.0_frozen', 'resnet_v1_50', 'resnet_v2_50', 'inception_v3', 'inception_resnet_v2', 'resnet_v1_152', 'inception_v1_frozen', 'vgg19', 'nasnet-a_large']

有几条路:

1. ckpt 转

Checkpoint File Conversion

We will give an example to convert TensorFlow resnet slim model with checkpoint files to caffe.

# Download TensorFlow pre-trained model first
$ mmdownload -f tensorflow -n resnet_v2_152

Downloading file [./resnet_v2_152_2017_04_14.tar.gz] from [http://download.tensorflow.org/models/resnet_v2_152_2017_04_14.tar.gz]
100% [......................................................................] 675629399 / 675629399
Model saved in file: ./imagenet_resnet_v2_152.ckpt

# Convert the TensorFlow model to Caffe
$ mmconvert -sf tensorflow -in imagenet_resnet_v2_152.ckpt.meta -iw imagenet_resnet_v2_152.ckpt --dstNodeName MMdnn_Output -df caffe -om tf_resnet
.
.
Caffe model files are saved as [tf_resnet.prototxt] and [tf_resnet.caffemodel], generated by [203e03ef187a42f59942737dace8773d.py] and [203e03ef187a42f59942737dace8773d.npy].

-in is used to specify the ".ckpt.meta" file.

-iw is used to specify the ".ckpt" file.

--dstNodeName is used to specify the output node of your model, which can be found in your code or tensorboard graph. We provide a tool vis_meta to help visualize your meta graph.

2. frozen pb 转

We will give an example to convert TensorFlow mobilenet slim model with frozen_pb to caffe

# Download TensorFlow pre-trained model first
$ mmdownload -f tensorflow -n mobilenet_v1_1.0_frozen

Downloading file [mobilenet_v1_1.0_224_frozen.tgz] from [https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_1.0_224_frozen.tgz]
progress: 18712.0 KB downloaded, 100%k

# Convert the TensorFlow model to Caffe
$ mmconvert -sf tensorflow -iw mobilenet_v1_1.0_224/frozen_graph.pb --inNodeName input --inputShape 224,224,3 --dstNodeName MobilenetV1/Predictions/Softmax -df caffe -om tf_mobilenet
.
.
Caffe model files are saved as [tf_mobilenet.prototxt] and [tf_mobilenet.caffemodel], generated by [e96550a4c55141afa8cd94372b858613.py] and [e96550a4c55141afa8cd94372b858613.npy].

For frozen graph parser, --inNodeName and --inputShape are required, and don't need to set -in.

3. debug 模式一步一步来

Step-by-step conversion for debugging

We will give an example to convert TensorFlow mobilenet slim model with frozen_pb to caffe.

# Convert frozen graph to IR
$ mmtoir -f tensorflow -w mobilenet_v1_1.0_224/frozen_graph.pb --inNodeName input --inputShape 224,224,3 --dstNodeName MobilenetV1/Predictions/Softmax -o mobilenet_v1

IR network structure is saved as [mobilenet_v1.json].
IR network structure is saved as [mobilenet_v1.pb].
IR weights are saved as [mobilenet_v1.npy].

# Convert IR to Caffe network building code
$ mmtocode -f caffe -n mobilenet_v1.pb -w mobilenet_v1.npy -o tf_mobilenet.py -ow tf_mobilenet.npy

Parse file [mobilenet_v1.pb] with binary format successfully.
Target network code snippet is saved as [tf_mobilenet.py].
Target weights are saved as [tf_mobilenet.npy].

# Use Caffe network building code to generate an original Caffe model
$ mmtomodel -f caffe -in tf_mobilenet.py -iw tf_mobilenet.npy -o tf_mobilenet
.
.
.
Caffe model files are saved as [tf_mobilenet.prototxt] and [tf_mobilenet.caffemodel], generated by [tf_mobilenet.py] and [tf_mobilenet.npy].

The you can use tf_mobilenet.prototxt and tf_mobilenet.caffemodel in Caffe directly.

第三个方法的好处是,如果前两条路出现cuda error,可以用debug模式一步一步来减轻GPU的负担从而解决问题。

常见问题是

DummyData 出现 "'LayerParameter' object has no attribute 'shape'" 在 _to_proto 的时候,

解决办法:caffe在dummydata 进行了更新,我们也需要更新caffe并重新 make pycaffe

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值