• TensorFlow模型转为caffe模型


    最近由于要将训练好的模型移植到硬件上,因此需要将TensorFlow转为caffe模型。

    caffe模型需要两个文件,一个是定义网络结构的prototxt,一个是存储了参数的caffemodel文件。只要生成这两个文件,caffe模型就算转好了。

    在模型转换的过程中,我主要参考了https://github.com/lFatality/tensorflow2caffe

    首先根据已有的tensorflow模型定义caffe模型需要的网络结构prototxt文件,这个可以参考一些现有经典的prototxt。

    然后生成caffe模型需要的模型参数caffemodel。基本过程是:把ckpt中的参数读出来,因为tensorflow和caffe对特征的维度处理不一样,tensorflow中特征的维度是NHWC(Number of filters * Height * Width * Channel),caffe中特征的维度是NCWH(Number of filters * Channel * width * height),所以,需要将参数从NHWC转为NCWH的顺序,然后保存为caffe模型。

    具体过程,参考资料中讲得比较清楚,在此不做太多说明。在模型转换的过程中,在batch norm层的转换时碰到了一些问题,在caffe中batch norm中除了存储mean和variance之外还有scale_factor,在测试过程中需要将其设置为1。

    下面是其中一层全连接层的转换代码。

    net.params['fc15'][0].data[...] = w_15_new
    net.params['fc15'][1].data[...] = b_15
    net.params['fc15_bn'][0].data[...] = mean_15
    net.params['fc15_bn'][1].data[...] = variance_15
    net.params['fc15_bn'][2].data[...] = 1
    net.params['fc15_scale'][0].data[...] = gamma_15
    net.params['fc15_scale'][1].data[...] = beta_15
  • 相关阅读:
    The "with" keyword in Javascript
    [导入][Software]SourceForge Enterprise Edition
    Font in Google Adsense
    JavaScript, We Hardly new Ya
    [导入][Fun]Total广告歌
    Prototype 1.5.0_rc2
    Ajax Web Part
    jquery数组(创建对象数组)
    jquery数组(拆分)
    jquery数组(排序)
  • 原文地址:https://www.cnblogs.com/Peyton-Li/p/10797002.html
Copyright © 2020-2023  润新知