我遵循caffe的net_surgery.ipynb示例,该示例说明了如何修改已保存的.caffemodel
的权重。但是,由于我是python的新手,所以我无法理解某些语法。
有人可以解释一下下面的代码示例中以conv_params = {pr: ...
开头的第七行是什么意思吗? (示例来自net_surgery.ipynb-步骤8)。特别是pr:
是什么?它是像(key,value)这样的结构中的键吗?
# Load the fully convolutional network to transplant the parameters.
net_full_conv = caffe.Net('net_surgery/bvlc_caffenet_full_conv.prototxt',
'../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',
caffe.TEST)
params_full_conv = ['fc6-conv', 'fc7-conv', 'fc8-conv']
# conv_params = {name: (weights, biases)}
conv_params = {pr: (net_full_conv.params[pr][0].data, net_full_conv.params[pr][1].data) for pr in params_full_conv}
for conv in params_full_conv:
print '{} weights are {} dimensional and biases are {} dimensional'.format(conv, conv_params[conv][0].shape, conv_params[conv][1].shape)
最佳答案
您正在努力的那条线:
conv_params = {pr: (net_full_conv.params[pr][0].data, net_full_conv.params[pr][1].data) for pr in params_full_conv}
用键
conv_params
,'fc6-conv'
和'fc7-conv'
定义字典'fc8-conv'
。使用
for
语句(... for pr in ...
)构造字典的过程称为“字典理解”,您可以找到有关该构造here的更多信息。关于python - 了解caffe中的net_surgery,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39793953/