Tuning a network, subset of data, one non-frozen layer

For the sake of this post, say I am training googlenet using 500 classes from imagenet, and I have done some number of epochs and then looked at visualizations for each layer. And I have determined that I want to use one particular class from my 500 to train exclusively one layer.

Let’s say I want to train layer model.inception5b.branch2[0].conv using only imagenet class n04026813.

I froze all the other layers using this idiom:

 # layers_to_freeze contains refs to every layer except 
 # model.inception5b.branch2[0].conv and the final fc layer.
 for layer in layers_to_freeze: 
    for param in layer.parameters():
       param.requires_grad = False

Now, I would like to train the model using only, say, imagenet class n04026813, so as to (try to) make the chosen unfrozen layer learn this class really well.

Problem I have it this:

If I set up a suite of directories data/train/[500 class dirs] and data/val/[500 class dirs] with all but */n04026813/ empty, then the data loader chokes on not finding any files in 499 of the class dirs.

If I instead stick one image in each of the 499 class dirs that I want to ignore (so that the data loader is happy) then I think I must be teaching that unfrozen layer mainly to memorize those 499 files.

Question: how can I get the dataloader to be happy with empty class directories? Or put it another way - how can I make pytorch just ignore the other 499 classes?


IDK if my thinking here is just wrong in the context of pytorch. I’ve done this kind of thing using Caffe in the past, but in Caffe you say in the prototxt that there are X number of classes, and if (X-1) of the corresponding train & val directories happen to not contain any data then that’s fine, those classes just are not represented.