mar*_*man 4 python deep-learning caffe pycaffe vgg-net
我正在使用PyCaffe来实现受VGG 16层网络启发的神经网络.我想使用GitHub页面提供的预训练模型.通常,这通过匹配图层名称来实现.
对于我的"fc6"图层,我在train.prototxt文件中有以下定义:
layer {
name: "fc6"
type: "InnerProduct"
bottom: "pool5"
top: "fc6"
inner_product_param {
num_output: 4096
}
}
Run Code Online (Sandbox Code Playgroud)
以下是VGG-16部署体系结构的prototxt文件.请注意,"fc6"他们的原型文本与我的相同(除了学习率,但这是无关紧要的).值得注意的是,我的模型中的输入大小也相同:3通道224x224px图像.
我一直非常密切地关注本教程,并且给我一个问题的代码块如下:
solver = caffe.SGDSolver(osp.join(model_root, 'solver.prototxt'))
solver.net.copy_from(model_root + 'VGG_ILSVRC_16_layers.caffemodel')
solver.test_nets[0].share_with(solver.net)
solver.step(1)
Run Code Online (Sandbox Code Playgroud)
第一行加载我的求解器原型,然后第二行从预先训练的模型中复制权重(VGG_ILSVRC_16_layers.caffemodel).解算器运行时,我收到此错误:
Cannot copy param 0 weights from layer 'fc6'; shape mismatch. Source param
shape is 1 1 4096 25088 (102760448); target param shape is 4096 32768 (134217728).
To learn this layer's parameters from scratch rather than copying from a saved
net, rename the layer.
Run Code Online (Sandbox Code Playgroud)
它的要点是他们的模型期望该层的大小为1x1x4096而我的只有4096.但我不知道如何改变它?
我在用户Google小组中找到了这个答案,指示我在复制之前进行网络手术以重塑预先训练的模型,但为了做到这一点,我需要lmdb来自原始架构的数据层的文件,我没有这个(它当我尝试运行网络手术脚本时抛出错误).
问题不在于4096而在于25088.您需要根据输入要素图计算网络中每个层的输出要素图.请注意,fc图层采用固定大小的输入,因此前conv一层的输出必须与图层所需的输入大小相匹配fc.conv使用前一层的输入要素贴图大小计算fc6输入要素贴图大小(这是上一层的输出要素贴图)conv.这是公式:
H_out = ( H_in + 2 x Padding_Height - Kernel_Height ) / Stride_Height + 1
W_out = (W_in + 2 x Padding_Width - Kernel_Width) / Stride_Width + 1
Run Code Online (Sandbox Code Playgroud)