caffe模型转tensorflow模型

caffe转tensorflow的github地址,但是该仓库年久失修,好久不更新。在pull requests中我找到了这个版本。该版本增加了Python3.6和tensorflow1.10的支持。

将alexnet.prototxt文件(deploy.prototxt)和alexnet_cvgj_iter_320000.caffemodel文件放到caffe-tensorflow文件夹的根目录。

当使用命令./convert.py alexnet.prototxt --code-output-path=zdaiot.py转换的时候

遇到了如下错误:

1
2
Error encountered: Cannot determine dimensions of data layer.
See comments in function shape_data for more info.

解决方法为:

将原本的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
name: "AlexNet"
input: "data"
layer {
name: "input"
type: "Input"
top: "data"
input_param {
shape {
dim: 10
dim: 3
dim: 227
dim: 227
}
}
}

中的input: "data"删除

然后遇到了下面错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Converting data...
Saving source...
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 37, in convert
src_out.write(transformer.transform_source().encode('utf-8'))
File "/home/zdaiot/Code/caffe-tensorflow/kaffe/tensorflow/transformer.py", line 282, in transform_source
chains = mapper.map()
File "/home/zdaiot/Code/caffe-tensorflow/kaffe/graph.py", line 288, in map
mapped_chains.append(self.map_chain(chain))
File "/home/zdaiot/Code/caffe-tensorflow/kaffe/graph.py", line 292, in map_chain
return [self.map_node(node) for node in chain]
File "/home/zdaiot/Code/caffe-tensorflow/kaffe/graph.py", line 292, in <listcomp>
return [self.map_node(node) for node in chain]
File "/home/zdaiot/Code/caffe-tensorflow/kaffe/graph.py", line 296, in map_node
mapped_node = map_func(node)
File "/home/zdaiot/Code/caffe-tensorflow/kaffe/tensorflow/transformer.py", line 149, in map_batch_norm
scale_offset = len(node.data) == 4
TypeError: object of type 'NoneType' has no len()

解决方法为
I got it to work by replacing scale_offset= len(node.data) == 4 with scale_offset = len(node.output_shape) == 4 in transformer.py

使用Python2的时候,报错

1
TypeError: __new__() got an unexpected keyword argument 'serialized_options'

解决方法,出现该问题,很有可能是,终端上的 protoc 版本 与python库内的protobuf版本不一样。

1
sudo pip2 install -U  protobuf

接着使用./convert.py alexnet.prototxt --caffemodel alexnet_cvgj_iter_320000.caffemodel --data-output-path=zdaiot.npy将权重文件转为npy格式。

------ 本文结束------
坚持原创技术分享,您的支持将鼓励我继续创作!

欢迎关注我的其它发布渠道