近期要因为学习的需要要从caffe框架转为tensorflow的学习,但是通过在网上搜索后发现,网上关于训练好的模型却几乎找不到,因此心里在想,有没有相关的代码将caffe训练好的模型转换为tensorflow的模型呢?说干就干,在网上搜索了一下,还真的找到了一个相关的开源代码,可以实现这个功能.其在github上为开源工程,链接为:https://github.com/ethereon/caffe-tensorflow
于是从网页上git下来了这个项目源码,按照工程中提示的信息一步一步的进行操作,可是在刚开始的时候就给了当头一棒,泼了一盆冷水,当按照教程中提示的指令输入
./convert.py examples/mnist/lenet.prototxt --code-output-path=mynet.py
最不想开到的情况还是出现了,程序报错.
Traceback (most recent call last):
File "./convert.py", line 60, in < module> main()
File "./convert.py", line 56, in main args.phase)
File "./convert.py", line 27, in convert transformer = TensorFlowTransformer(def_path, caffemodel_path, phase=phase)
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/tensorflow/transformer.py", line 221, in __init__ self.load(def_path, data_path, phase)
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/tensorflow/transformer.py", line 227, in load graph = GraphBuilder(def_path, phase).build()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/graph.py", line 140, in __init__ self.load()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/graph.py", line 144, in load self.params = get_caffe_resolver().NetParameter()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/caffe/resolver.py", line 32, in get_caffe_resolver SHARED_CAFFE_RESOLVER = CaffeResolver()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/caffe/resolver.py", line 7, in __init__ self.import_caffe()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/caffe/resolver.py", line 17, in import_caffe from . import caffepb
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/caffe/caffepb.py", line 28, in type=None),
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/descriptor.py", line 654, in __new__ message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors should not be created directly, but only retrieved from their parent
在网上搜索了一下,并没有发现合适的方法,最终还是选择了google大神来帮忙解决这个问题,网上给出了这样一个解决方法,就时将kaffe/caffe文件夹中的caffepb.py改为caffe_pb2.py,原本以为这样感觉不太合理啊,只是改一下名字就能解决这个问题?但是也没别的什么办法,就试一下看看具体会报什么错误吧.
于是又重新输入了一遍指令,程序果然报错了,提示:
Traceback (most recent call last):
File "./convert.py", line 60, in main()
File "./convert.py", line 56, in main args.phase
File "./convert.py", line 27, in convert
transformer = TensorFlowTransformer(def_path, caffemodel_path, phase=phase)
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/tensorflow/transformer.py", line 221, in __init__ self.load(def_path, data_path, phase)
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/tensorflow/transformer.py", line 227, in load graph = GraphBuilder(def_path, phase).build()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/graph.py", line 140, in __init__ self.load()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/graph.py", line 144, in load
self.params = get_caffe_resolver().NetParameter()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/caffe/resolver.py", line 32, in get_caffe_resolver SHARED_CAFFE_RESOLVER = CaffeResolver()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/caffe/resolver.py", line 7, in __init__ self.import_caffe()
File "/home/li/liyahui/caffe/caffe-tensorflow/kaffe/caffe/resolver.py", line 17, in import_caffe
from . import caffepb
ImportError: cannot import name caffepb
按照程序提示的错误信息,找到文件reolver.py这个文件,并定位到from . import caffepb的位置,由于文件名修改了,那必定找不到这个文件,更不用谈导入相关程序代码了,所以程序必定报错.在明白了这个之后将from . import caffepb改为
# Fall back to the protobuf implementation
from . import caffe_pb2
self.caffepb = caffe_pb2
show_fallback_warning()
再运行命令,发现成功了.程序运行出了结果,生成了mynet.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(5, 5, 50, 1, 1, padding='VALID', relu=False, name='conv2')
.max_pool(2, 2, 2, 2, name='pool2')
.fc(500, name='ip1')
.fc(10, relu=False, name='ip2')
.softmax(name='prob'))