你能复制网络前 3 层的权重吗?不完全是微调,但几乎是重塑
Can you copy the weights from just the first 3 layers of a network? Not exactly finetuning, but almost reshaping
在 caffe 中,我希望仅对前两层使用使用 ImageNet 数据集训练的 alexnet 架构的预训练权重,我想在这两层之后添加一个 softmax 分类器。我想知道如何从包含更大网络结构(真正的 "deep" Alexnet 结构)的权重文件中仅提取前两层的权重。
Caffe 使用层的 "name"
将权重分配给层的 blobs
。
如果您更改顶层的 "name"
,caffe 将不会从原始 .caffemodel
文件复制权重。
添加到 的回答 -
如果您不想要完整的权重文件,
为了提取所需层的权重,请使用 net surgery:
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 中,我希望仅对前两层使用使用 ImageNet 数据集训练的 alexnet 架构的预训练权重,我想在这两层之后添加一个 softmax 分类器。我想知道如何从包含更大网络结构(真正的 "deep" Alexnet 结构)的权重文件中仅提取前两层的权重。
Caffe 使用层的 "name"
将权重分配给层的 blobs
。
如果您更改顶层的 "name"
,caffe 将不会从原始 .caffemodel
文件复制权重。
添加到
如果您不想要完整的权重文件,
为了提取所需层的权重,请使用 net surgery:
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)