Understanding net_surgery in caffe
<p>I'm following the <a href="http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/net_surgery.ipynb" rel="nofollow">net_surgery.ipynb</a> example of caffe which explains how to modify the weights of a saved <code>.caffemodel</code>. However since I'm quite new to python I can't understand some of the syntax.</p>

<p>Can someone explain me what the 7th line starting with <code>conv_params = {pr: ...</code> means in the code example given below? (example is from <a href="http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/net_surgery.ipynb" rel="nofollow">net_surgery.ipynb</a> - step 8). Especially what is <code>pr:</code>? Is it a key in a (key,value) like structure?</p>

<pre><code># 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)
</code></pre>

<p>The line you are struggling with:</p>

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

<p>defines a dictionary <code>conv_params</code> with keys <code>'fc6-conv'</code>, <code>'fc7-conv'</code> and  <code>'fc8-conv'</code>.<br>
The construction of the dictionary using <code>for</code> statement (<code>... for pr in ...</code>) is called 'Dictionary comprehension" and you can find more information about this construct <a href="http://stackoverflow.com/q/14507591/1714410">here</a>.</p>

python neural-network deep-learning caffe pycaffe