(原)linux下caffe模型转tensorflow模型
转载请注明出处:
http://www.cnblogs.com/darkknightzh/p/7419352.html
参考网址:
https://github.com/ethereon/caffe-tensorflow
https://github.com/ethereon/caffe-tensorflow/issues/53
http://blog.csdn.net/zchang81/article/details/76229017
提醒:1. 目前该开源程序不支持caffe中全连接层的bias=false的情况。如果caffe模型全连接层有这种情况,暂时不用往下看了。。。
2. 增加的或者修改的代码用红色字体标注了,因而本文没有使用代码功能。
具体步骤如下:
1. caffe-tensorflow-master\kaffe\caffe\resolver.py下,增加如下两句:
import sys
sys.path.insert(0, "/home/xxx/caffe/caffe-master/python")
注意:使用的系统是ubuntu14.04。如果caffe和tensorflow都配置好了,这步可能不需要吧。我这边由于caffe配置的有点问题,直接import caffe的话,失败,因而增加了caffe的路径。
2. 默认该开源程序无法处理caffe中的global pooling,默认会提示:
ValueError: Unable to determine kernel parameter!
因而,在caffe-tensorflow-master\kaffe\layers.py中,修改class LayerAdapter(object):
class LayerAdapter(object):
def __init__(self, layer, kind):
self.layer = layer
self.kind = kind
self._input_shape = None
@property
def parameters(self):
name = NodeDispatch.get_handler_name(self.kind)
name = '_'.join((name, 'param'))
try:
return getattr(self.layer, name)
except AttributeError:
raise NodeDispatchError('Caffe parameters not found for layer kind: %s' % (self.kind))
@staticmethod
def get_kernel_value(scalar, repeated, idx, default=None):
if scalar:
return scalar
if repeated:
if isinstance(repeated, numbers.Number):
return repeated
if len(repeated) == 1:
# Same value applies to all spatial dimensions
return int(repeated[0])
assert idx < len(repeated)
# Extract the value for the given spatial dimension
return repeated[idx]
if default is None:
raise ValueError('Unable to determine kernel parameter!')
return default
def set_input_shape(self, input_shape):
self._input_shape = input_shape
@property
def kernel_parameters(self):
assert self.kind in (NodeKind.Convolution, NodeKind.Pooling)
params = self.parameters
global_pool = hasattr(params, 'global_pooling')
if params.kernel_size:
k_h = self.get_kernel_value(params.kernel_h, params.kernel_size, 0)
k_w = self.get_kernel_value(params.kernel_w, params.kernel_size, 1)
elif self._input_shape:
k_h, k_w = [self._input_shape.height, self._input_shape.width]
else: #errors out in get_kernel_value function
k_h = self.get_kernel_value(params.kernel_h, params.kernel_size, 0)
k_w = self.get_kernel_value(params.kernel_w, params.kernel_size, 1)
s_h = self.get_kernel_value(params.stride_h, params.stride, 0, default=1)
s_w = self.get_kernel_value(params.stride_w, params.stride, 1, default=1)
p_h = self.get_kernel_value(params.pad_h, params.pad, 0, default=0)
p_w = self.get_kernel_value(params.pad_h, params.pad, 1, default=0)
return KernelParameters(k_h, k_w, s_h, s_w, p_h, p_w)
3. 在caffe-tensorflow-master\kaffe\shapes.py中第18行,增加下面代码:
node.layer.set_input_shape(input_shape)
4. 如果要在caffe-tensorflow-master\examples\imagenet目录下测试转换后的模型,需要将转换后的模型的py文件放到examples\imagenet\models文件夹内,生成的npy文件放到caffe-tensorflow-master\examples\imagenet文件夹内,并修改caffe-tensorflow-master\examples\imagenet\models\helper.py文件:
1)开头增加:
from aaa import bbb
其中,aaa为生成的py文件(不需要后缀)。bbb为aaa.py文件内生成的class名,如class bbb(Network)
2)根据需要,在std_spec函数结束后,增加新的函数:
def bbb_spec(batch_size=500):
'''Parameters used by bbb and its variants.'''
return DataSpec(batch_size=batch_size, scale_size=256, crop_size=128, isotropic=False)
3)在MODELS中增加新生成的模型名字:
MODELS = (AlexNet, CaffeNet, GoogleNet, NiN, ResNet50, ResNet101, ResNet152, VGG16, bbb)
4)在MODEL_DATA_SPECS中增加新生成模型的初始化信息:
MODEL_DATA_SPECS = {
AlexNet: alexnet_spec(),
CaffeNet: alexnet_spec(),
GoogleNet: std_spec(batch_size=200, isotropic=False),
ResNet50: std_spec(batch_size=25),
ResNet101: std_spec(batch_size=25),
ResNet152: std_spec(batch_size=25),
NiN: std_spec(batch_size=500),
VGG16: std_spec(batch_size=25),
bbb: bbb_spec(batch_size=25)
}
5. 模型转换完之后,如果caffe模型最后还有一个concat层(用于concat之前每个层的特征),而输入的特征均为batch size*featDim类型的2D数组(之前层的concat输入的均为4D的数据,则正常),需要将转换后的模型的这层由
.concat(3, name='concat_feature')
手动改成
.concat(1, name='concat_feature')
原因是,该开源程序貌似无法判断输入数据维度(都没数据呢,哪里去判断。。。),因而默认都设置了从第3维来concat(tensorflow中第0维为batch,第1维为高,第2维为宽,第3维为特征维度)。如果concat的每维均为特征,则此时只是将多个batch size*featDim进行concat,故需要手动将此时的3改为1。不过不涉及到concat多个特征,则不需要修改这里。
6. caffe-tensorflow-master\kaffe\tensorflow中map_batch_norm函数修改如下:
def map_batch_norm(self, node):
if not (node.data is None):
scale_offset = len(node.data) == 4
kwargs = {} if scale_offset else {'scale_offset': False}
return MaybeActivated(node, default=False)('batch_normalization', **kwargs)
else:
return MaybeActivated(node, default=False)('batch_normalization', {'scale_offset': False})
7. 目前该开源程序不支持全连接层bias=false的情况,即便修改代码,能够设置bias=false,但是在真正使用模型时,依旧失败。因而这种情况下,或者等大神继续修改代码,或者自己修改代码,或者。。。放弃吧。
8. 转换模型的代码(在caffe-tensorflow-master打开终端):
./convert.py aaa.prototxt --caffemodel aaa.caffemodel --code-output-path=aaa.py --data-output-path=aaa.npy
注意:
1)prototxt一定要输入。
2)code-output-path若指定则会输出生成的模型文件。
3)caffemodel若指定,则会从该模型中读取模型参数;data-output-path若指定,则会输出对应的模型参数,且需使用该开源程序来载入模型参数。
8. 使用的话(这部分代码不记得能不能用。。。看着修改吧),可以结合caffe-tensorflow-master\examples\imagenet\validate.py进行修改。
比如,增加:
import sys
sys.path.insert(0, "/home/xxx/caffe-tensorflow-master/kaffe")
from kaffe.tensorflow import Network
并且:
import models
主程序中:
spec = models.get_data_spec(model_class=models.bbb) # Get the data specifications for the bbb model
input_node = tf.placeholder(tf.float32, shape=(None, img_size, img_size, img_channels)) # Create a placeholder for the input image
net = models.bbb({'data': input_node}) # Construct the network
当使用该模型时:
with tf.Graph().as_default():
with tf.Session() as sess:
net.load(args.model_path, sess) # Load the converted parameters
for i in range(nrof_batches):
# get images
# Perform a forward pass through the network to get the class probabilities
feat = sess.run(net.get_output(), feed_dict={input_node: images})
(原)linux下caffe模型转tensorflow模型相关推荐
- Linux 下的五种 IO 模型
Linux 下的五种 IO 模型 来源:decaywood's Blog 概念说明 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2 ...
- linux下的五种io模型,Linux下的五种IO模型
Java中提供的IO有关的API,在文件处理的时候,其实依赖操作系统层面的IO操作实现的(关于Java对IO的三种封装,可见我的另一篇博客) 开门见山,Linux下的如中IO模型:阻塞IO模型,非阻塞 ...
- linux 网络io命令详解,Linux下五种网络IO模型详解
本文我们主要来了解一下Unix/Linux下5种网络IO模型:blocking IO, nonblocking IO, IO multiplexing, signal driven IO, async ...
- linux 进程sockfd fork,Linux下多进程服务端客户端模型一(单进程与多进程模型)...
本文将会简单介绍Linux下如何利用C库函数与系统调用编写一个完整的.初级可用的C-S模型. 一.基本模型: 1.1 首先服务器调用socket()函数建立一个套接字,然后bind()端口,开始l ...
- linux 上自动执行caffe,linux下caffe的命令运行脚本
参考:https://www.cnblogs.com/denny402/p/5076285.html 首先编译: make -j8 make pycaffe 注:下面的--solver=.... 等价 ...
- onnx模型转tensorflow模型
ONNX是开源神经网络交换平台,有了它基本上不用纠结用什么深度学习框架的问题了.我现在记录一下怎么将onnx模型转换成tensorflow模型. 1.安装tensorflow和onnx 我是通过Ana ...
- keras模型转TensorFlow模型-tensorrt UFF格式
最近在学习tensorrt,需要将keras训练好保存的.hdf5格式模型转为tensorflow的.pb模型,然后转为tensorrt支持的uff格式. 做个记录. 代码如下: 转为tensorfl ...
- Linux下多进程服务端客户端模型二(粘包问题与一种解决方法)
一.Linux发送网络消息的过程 (1) 应用程序调用write()将消息发送到内核中 ( 2)内核中的缓存达到了固定长度数据后,一般是SO_SNDBUF,将发送到TCP协议层 (3)IP层从TCP层 ...
- Linux下toco命令压缩Tensorflowlite模型
备忘: toco工具目前不支持windows bazel run --config=opt tensorflow/contrib/lite/toco:toco --help 环境变量 export I ...
最新文章
- 10.python网络编程(解决粘包问题 part 2)
- setsockopt
- 使用photoview+viewpager实现图片缩放切换(类似微信朋友圈图片查看)
- asp.net webform 使用 html partial,ASP.NET Web Forms
- 存储过程Procedure
- How to enable multiple text type for Product
- java 和c 比优势_Java和C++各有什么优势。
- 企业级php第三方支付平台,ThinkPHP新版企业级php第三方api第四方支付平台程序源码商业版 带接口文件等 某宝售价3000元...
- Storm系列(四)Topology提交校验过程
- 软件工程概论第二周 开课作业
- 设计一个完善可用的服务框架
- swift 项目_谷歌为何要养苹果的亲儿子Swift?原来意在可微分编程
- 调整Word中英文与汉字之间的空隙
- python怎么创建变量_创建一个新变量,它是python中另一个变量的...
- Django入门:第十一章、基础模板
- qpython怎么使用_SL4A、QPython学习笔记
- FireFox浏览器渗透测试插件
- 用记事本编写ajax,用记事本轻松制作ajax+xml在线聊天室
- 段码式显示屏和背光电路怎么设计?
- 插入法排序c语言程序,C语言直接插入排序算法