东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 14533|回复: 0
打印 上一主题 下一主题

[课堂笔记] caffe模型转tensorflow的笔记记录

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14431
QQ
跳转到指定楼层
楼主
发表于 2020-9-8 09:10:51 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

caffe模型转tensorflow的笔记记录

首先搜索github发现标星最多的:
https://github.com/ethereon/caffe-tensorflow

要求是新版的caffe(先用caffe的源码编译环境)
./build/tools/upgrade_net_proto_text
./build/tools/upgrade_net_proto_binary

输出2个文件: numpy   py文件(网络结构)


先跑一下例子:
python convert.py --def_path examples/mnist/lenet.prototxt --caffemodel examples/mnist/lenet_iter_10000.caffemodel --data-output-path lenet_dfy.npy --code-output-path lenet_dfy.py

ImportError: No module named tensorflow
pip install tensorflow-gpu==1.15

python examples/mnist/finetune_mnist.py


下一步:开始转换自己的caffe模型:
Error encountered: Unknown layer type encountered: PReLU
https://github.com/davidsandberg/facenet/issues/81


自己加这个层(太多了):
1、 ./kaffe/layers.py 第52行 'PReLU': shape_identity,
2、 ./kaffe/tensorflow/network.py

直接用别人改好的:https://github.com/davidsandberg/caffe-tensorflow
davidsandberg Some changes needed to convert MTCNN model



  1. 旧的softmax:
  2.   @layer
  3.     def softmax(self, input, name):
  4.         input_shape = map(lambda v: v.value, input.get_shape())
  5.         if len(input_shape) > 2:
  6.             # For certain models (like NiN), the singleton spatial dimensions
  7.             # need to be explicitly squeezed, since they're not broadcast-able
  8.             # in TensorFlow's NHWC ordering (unlike Caffe's NCHW).
  9.             if input_shape[1] == 1 and input_shape[2] == 1:
  10.                 input = tf.squeeze(input, squeeze_dims=[1, 2])
  11.             else:
  12.                 raise ValueError('Rank 2 tensor input expected for softmax!')
  13.         return tf.nn.softmax(input, name=name)
  14. softmax 也改了
  15. """
  16.     Multi dimensional softmax,
  17.     refer to https://github.com/tensorflow/tensorflow/issues/210
  18.     compute softmax along the dimension of target
  19.     the native softmax only supports batch_size x dimension
  20.     """

  21.     @layer
  22.     def softmax(self, target, axis, name=None):
  23.         print('Softmax: %s' % name)
  24.         max_axis = tf.reduce_max(target, axis, keep_dims=True)
  25.         target_exp = tf.exp(target - max_axis)
  26.         normalize = tf.reduce_sum(target_exp, axis, keep_dims=True)
  27.         softmax = tf.div(target_exp, normalize, name)
  28.         return softmax
复制代码



Error encountered: Output shape computation not implemented for type: Flatten

Added support for reshape and flatten layers
https://github.com/ethereon/caffe-tensorflow/pull/147   参考这个修改文件 Files Changed


'module' object has no attribute 'mul'
确认该问题为 tensorflow 版本升级导致。
升级后,原有的 tensorflow.mul( ) 更新为 tensorflow.multiply( ) 接口,
并将原有接口取消,故报出不存在 mul 的 AttributeError


注意:
1、将模型保存为tensorflow ckpt时,输入的占位符batch_size=None 方便后面使用模型时结果与batch_size大小无关
2、tensorflow ckpt冻结为pb文件时只需要指定输出的节点名称,该节点名称必须是原模型中存在的节点,最后输出的节点,可以在tensorboard中查找到
3、测试pb文件 结果一定与batch_size无关,如果有关需要看定义的层是否关系到batch_size的输入大小  比如:prelu层的实现
4、pb前向计算时,需要指定input_name和output_name 在tensorboard中查找到 格式:Tensor names must be of the form "<op_name>:<output_index>"
  比如:output_name='softmax:0'




PReLU(Parametric Rectified Linear Unit), 顾名思义:带参数的ReLU
PReLU的几点说明
(1) PReLU只增加了极少量的参数,也就意味着网络的计算量以及过拟合的危险性都只增加了一点点。特别的,当不同channels使用相同的ai时,参数就更少了。

(2) BP更新ai时,采用的是带动量的更新方式





softmax层定义变了.png (197.92 KB, 下载次数: 121)

softmax层定义变了.png

prelu.png (48.97 KB, 下载次数: 127)

prelu.png

prelu_mtcnn.png (81.65 KB, 下载次数: 123)

prelu_mtcnn.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-25 10:02 , Processed in 0.201914 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表