【Tensorflow实战系列】Caffe: 为什么你的tf模型转caffemodel老不对?(其中一个重要原因)
发布时间:2026/10/22 1:13:07 锦皓数字建站
`)
使用训练好的tensorflow模型转换成caffemodel的时候需要对tensorflow中接口设置和caffe中接口设置都非常了解才能对其参数进行对应设置举个简单的例子在我们使用slim库的时候只需要按照如下方式进行书写netslim.conv2d(inputs,16,3,stride1,paddingVALID,scopeConv1)但是如果我们搭建prototxt的结构是按照下面搭建cnncaffe.layers.Convolution(inputs,num_output16,kernel_size3,stride1,pad0,nameConv1)即使上述两段代码设置完全一样我们经过inference之后就是得不到正确的结构这是为什么呢我们打开tensorflow slim中的conv2d的函数定义defconvolution2d(inputs,num_outputs,kernel_size,stride1,paddingSAME,data_formatNone,rate1,activation_fnnn.relu,###注意这行normalizer_fnNone,normalizer_paramsNone,weights_initializerinitializers.xavier_initializer(),weights_regularizerNone,biases_initializerinit_ops.zeros_initializer(),biases_regularizerNone,reuseNone,variables_collectionsNone,outputs_collectionsNone,trainableTrue,scopeNone):我们再看下caffe对应conv2d的定义message ConvolutionParameter{optional uint32 num_output1;// The number of outputs for the layeroptional bool bias_term2[defaulttrue];// whether to have bias terms// Pad, kernel size, and stride are all given as a single value for equal// dimensions in all spatial dimensions, or once per spatial dimension.repeated uint32 pad3;// The padding size; defaults to 0repeated uint32 kernel_size4;// The kernel sizerepeated uint32 stride6;// The stride; defaults to 1// Factor used to dilate the kernel, (implicitly) zero-filling the resulting// holes. (Kernel dilation is sometimes referred to by its use in the// algorithme à trous from Holschneider et al. 1987.)repeated uint32 dilation18;// The dilation; defaults to 1// For 2D convolution only, the *_h and *_w versions may also be used to// specify both spatial dimensions.optional uint32 pad_h9[default0];// The padding height (2D only)optional uint32 pad_w10[default0];// The padding width (2D only)optional uint32 kernel_h11;// The kernel height (2D only)optional uint32 kernel_w12;// The kernel width (2D only)optional uint32 stride_h13;// The stride height (2D only)optional uint32 stride_w14;// The stride width (2D only)optional uint32 group5[default1];// The group size for group convoptional FillerParameter weight_filler7;// The filler for the weightoptional FillerParameter bias_filler8;// The filler for the biasenumEngine{DEFAULT0;CAFFE1;CUDNN2;}optional Engine engine15[defaultDEFAULT];// The axis to interpret as channels when performing convolution.// Preceding dimensions are treated as independent inputs;// succeeding dimensions are treated as spatial.// With (N, C, H, W) inputs, and axis 1 (the default), we perform// N independent 2D convolutions, sliding C-channel (or (C/g)-channels, for// groups g1) filters across the spatial axes (H, W) of the input.// With (N, C, D, H, W) inputs, and axis 1, we perform// N independent 3D convolutions, sliding (C/g)-channels// filters across the spatial axes (D, H, W) of the input.optional int32 axis16[default1];// Whether to force use of the general ND convolution, even if a specific// implementation for blobs of the appropriate number of spatial dimensions// is available. (Currently, there is only a 2D-specific convolution// implementation; for input blobs with num_axes ! 2, this option is// ignored and the ND implementation will be used.)optional bool force_nd_im2col17[defaultfalse];}当然这个太详细了我们可以发现slim中默认是卷积后进行relu激活的在caffe中并没有这一个操作一个经过relu激活一个没有这个操作你转换后的模型肯定不会和转换前一样得到正确的结果所以要想将tensorflow模型正确的转成caffemodel那就老老实实的了解每个接口后面的参数设置并对应的写到你的caffe模型中。慢工出细活有些事儿吧你急不来。。。
锦
锦皓数字建站
深耕本土企业品牌数字化升级,专注原创端正雅致商务官网,从视觉设计到稳定运维全程保驾护航。