1

I'm following the net_surgery.ipynb example of caffe which explains how to modify the weights of a saved .caffemodel. However since I'm quite new to python I can't understand some of the syntax.

Can someone explain me what the 7th line starting with conv_params = {pr: ... means in the code example given below? (example is from net_surgery.ipynb - step 8). Especially what is pr:? Is it a key in a (key,value) like structure?

# 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)
Shai
  • 111,146
  • 38
  • 238
  • 371
mcExchange
  • 6,154
  • 12
  • 57
  • 103

1 Answers1

1

The line you are struggling with:

conv_params = {pr: (net_full_conv.params[pr][0].data, net_full_conv.params[pr][1].data) for pr in params_full_conv}

defines a dictionary conv_params with keys 'fc6-conv', 'fc7-conv' and 'fc8-conv'.
The construction of the dictionary using for statement (... for pr in ...) is called 'Dictionary comprehension" and you can find more information about this construct here.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371