2

I want to use a pretrained neural network and just fine-tune it to my specific needs. I wanted to use Python and the Lasagne framework for this. On:

https://github.com/Lasagne/Recipes/blob/master/examples/ImageNet%20Pretrained%20Network%20%28VGG_S%29.ipynb

I found an example of how to use a pretrained network for specific images. My problem is that I would like to use the network described in the link above as a starting point and add a final layer to it that makes it implement a TWO CLASS classifier which is what I need. I therefore wanted to keep all the layers in the network frozen and allow training ONLY in my last added layer.

Apparently there is a way to indicate that layers should be "nontrainable" in lasagne, but I have found no expemples of how to do this on the web.

Any thoughts on this would be highly appreciated.

Lars Aurdal
  • 103
  • 1
  • 5

2 Answers2

2

Set those layers that you want to frozen with lr to be 0 and only set those layer you want to fine tune lr nonzero. There is not a online example yet. But you should check this thread https://groups.google.com/forum/#!topic/lasagne-users/2z-6RrgiHkE

Zaikun Xu
  • 1,433
  • 1
  • 12
  • 7
0

Remove trainable tag from all parameters of the layers that you want to keep frozen:

def freeze_layer(layer):
    for param in layer.params.values():
         param.remove('trainable')

To freeze all your network up to a certain layer you can simply iterate over its lower layers:

from lasagne.layers import get_all_layers

def freeze_net(net):
    layers = get_all_layers(net)
    for l in layers:
        freeze_layer(l)

Code untested. See this discussion for more info.

Nicolas Ivanov
  • 798
  • 9
  • 15