转载请注明出处:

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(, 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模型的更多相关文章

  1. Linux 下的五种 IO 模型

    概念说明 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方).操作系统的核心是内核,独立于普通的应用程序,可以访问受保护的 ...

  2. Linux 下 简单客户端服务器通讯模型(TCP)

    原文:Linux 下 简单客户端服务器通讯模型(TCP) 服务器端:server.c #include<stdio.h> #include<stdlib.h> #include ...

  3. linux下Anaconda安装使用Tensorflow

    # linux下Anaconda安装使用Tensorflow ### 环境------------------------------ Ubuntu 18.04 ### 环境准备----------- ...

  4. Linux下多进程服务端客户端模型一(单进程与多进程模型)

    本文将会简单介绍Linux下如何利用C库函数与系统调用编写一个完整的.初级可用的C-S模型. 一.基本模型: 1.1   首先服务器调用socket()函数建立一个套接字,然后bind()端口,开始l ...

  5. linux下caffe的命令运行脚本

    参考:https://www.cnblogs.com/denny402/p/5076285.html 首先编译: make -j8 make pycaffe 注:下面的--solver=.... 等价 ...

  6. (转)Darknet模型与Tensorflow模型相互转换

    目前darknet框架下的模型训练都是在C环境下训练的,难免较为晦涩,如果能将模型转换到Tensorflow环境下完成模型的训练,在将训练好的权重转为Darknet可以识别的权重部署到实际应用中.这样 ...

  7. Linux上多次restore Tensorflow模型报错

    环境:python3,tensotflow 在恢复了预先训练好的模型进行预测时,第一次是能够成功执行的,但我多次restore模型时,出现了以下问题: 1.ValueError: Variable c ...

  8. Linux下多进程服务端客户端模型二(粘包问题与一种解决方法)

    一.Linux发送网络消息的过程 (1) 应用程序调用write()将消息发送到内核中 ( 2)内核中的缓存达到了固定长度数据后,一般是SO_SNDBUF,将发送到TCP协议层 (3)IP层从TCP层 ...

  9. 【泡咖啡1】linux下caffe编译以及python环境配置手记

    caffe是一个深度学习的库,相信搞深度学习的话,不是用这个库就是用theano吧.要想使用caffe首先第一步就是要配置好caffe的环境.在这里,我主要说的是在debian的linux环境下如何配 ...

随机推荐

  1. Java系列: 如何在Eclipse中安装Memory Analyzer插件

    一.找到eclipse的插件安装对话框: help->install new software ->work with 二.输入Memory Analyzer的安装路径 具体可以到http ...

  2. 九度oj-1001-Java

    题目描述:     This time, you are supposed to find A+B where A and B are two matrices, and then count the ...

  3. 文本相似度-BM25算法

    BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms app ...

  4. jsp table 表格单元格编辑示例

    列表单元格: //两个 隐藏的 input, 第一个存 记录 id, 单元格内容是排序码 : <td id="ordinal"><%=ordinal%> & ...

  5. c# event Action 判断事件列表中是否存在这个委托

    using UnityEngine; using System.Collections; using System; public class eventTest : MonoBehaviour { ...

  6. 上机题目(0基础)- Java网络操作-打印网页(Java)

    打印一个网页,熟悉Java网络编程: import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt ...

  7. IDEA实现序列号接口

    idea自动生成serialVersionUID (2013-12-15 08:12:09)转载▼ Setting->Plugins 找到一个叫 GenerateSerialVersionUID ...

  8. SuperMap入门3——Hello World

    Hello World程序很重要,对于入门来说,它可以检测我们的环境.配置是否正确,感受程序的易用性等. 添加工具 由于我是使用的VS2017+ SuperMap iObject绿色免安装版,所以新建 ...

  9. PPT模板中的”书签”

    引言 在项目中生成文档报告经常需要word中,其中的关键就是书签,通过定位和替换书签中的值来达到生成定制的报告(详见Word模板中的表格处理):但在PPT中却没有书签这个概念,所以,不能采用这种方式. ...

  10. Android 之 应用未捕获异常处理

    最近开发一款低功耗蓝牙通讯的 Android 应用,安装使用时多次出现“ 抱歉,xxx已停止 ”.现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出 ...