下载模型转换代码:
git clone https://github.com/ethereon/caffe-tensorflow
caffe-tensorflow模型转换文件为convert.py,可以将caffe的模型定义文件.prototxt和训练好的模型分别转换为tensorflow类,和tensorflow模型文件.在example文件夹下提供了examples/minist和examples/imagenet两个转换示例.
以手写字体为例,模型为lenet,examples/minist目录下提供了lenet.prototxt和训练好的caffe模lenet_iter_10000.caffemodel,caffe转换为tensorflow命令为:
python convert.py --def_path examples/mnist/lenet.prototxt --caffemodel examples/mnist/lenet_iter_10000.caffemodel --data-output-path lenet.npy --code-output-path lenet.py
运行命令后报错:
TypeError: Descriptors should not be created directly, but only retrieved from their parent.
Process finished with exit code 1
这是因为找不到caffe安装目录,解决方法是kaffe/caffe/resolver.py的第12行添加caffe安装目录:
import sys
caffe_root = '/home/qinghua/program/caffe'
sys.path.insert(0, caffe_root + '/python')
import caffe
转换后得到lenet网络结构文件lenet.py:
from kaffe.tensorflow import Network
class LeNet(Network):
def setup(self):
(self.feed('data')
.conv(5, 5, 20, 1, 1, padding='VALID', relu=False, name='conv1')
.max_pool(2, 2, 2, 2, name='pool1')
.conv(