Can you copy the weights from just the first 3 layers of a network? Not exactly finetuning, but almost reshaping
在 caffe 中,我希望仅将预训练的权重用于使用 ImageNet 数据集训练的 alexnet 架构,仅用于前两层,并且我想在这两层之后添加一个 softmax 分类器。我想知道如何才能从包含更大网络结构(真正的”深度”Alexnet 结构)的权重文件中仅提取前两层的权重。
补充 Shai 的答案-
如果您不想要完整的权重文件,
为了提取所需层的权重,请使用网络手术:
1
2 3 4 5 6 7 8 |
net = caffe.Net(prototxt, caffemodel, caffe.TRAIN)
outnet = caffe.Net(predefined_prototxt_with_desired_layers_only, caffe.TRAIN) layers_to_copy = [‘conv1’, ‘conv2’, ‘conv3’] for layer in layers_to_copy: for i in range(0, len(net.params[layer])): #this is for copying both weights and bias, in case bias exists outnet.params[layer][i].data[…]=np.copy(net.params[layer][i].data[…]) outnet.save(new_caffemodel_name) |
Caffe 使用层的 “name” 将权重分配给层的 blobs。
如果您更改顶层的 “name”,则 caffe 不会从原始 .caffemodel 文件中复制权重。
来源:https://www.codenong.com/46420040/