princeton-vl / CornerNet

BSD 3-Clause "New" or "Revised" License
2.36k stars 475 forks source link

How to fine-tunning your pretrained cooco model? #23

Open YiLiangNie opened 6 years ago

YiLiangNie commented 6 years ago

How to fine tune your dataset on your trained coco model? Could you provide the json file for fine-tunning params and update the load_pretrained_params() function in file : ./nnet/py_factory.py?

heilaw commented 6 years ago

All you need to do is add "pretrain": "/path/to/the/pretrained/model" in the system section of the configuration file. The code will load the pretrained file before it starts training.

heilaw commented 6 years ago

I just saw your another post.

The size match happens in the convolution layers for tl_heats and br_heats. The sizes of weights and biases in the pretrained model are [80, 256, 1, 1] and [80], which do not match your new dataset.

To prepare the model for fine-tuning on a different datasets, you can do the followings.

  1. In train.py, add nnet.save_params(0) after line 114. This forces the code to save a randomly initialized model with correct sizes before the training starts. After the model is saved, terminate the code by Ctrl + c.
  2. Replace the weights and biases in the trained model with the ones in the randomly initialized model. A PyTorch model is a dictionary, where the keys are the parameter names and values are the parameters. They can be loaded with torch.load. After loading, the parameters can be replaced by newmodel['param_name'] = oldmodel['param_name']. The parameter names you are looking for are:
    • 'module.tl_heats.0.1.weight'
    • 'module.tl_heats.0.1.bias'
    • 'module.tl_heats.1.1.weight'
    • 'module.tl_heats.1.1.bias'
    • 'module.br_heats.0.1.weight'
    • 'module.br_heats.0.1.bias'
    • 'module.br_heats.1.1.weight'
    • 'module.br_heats.1.1.bias'
  3. Save the model.
  4. Remove nnet.save_params(0) in step 1.
  5. Add "pretrain": "/path/to/the/pretrained/model" in the system section of the configuration file.
YiLiangNie commented 6 years ago

Hi heilaw, I find this function in /models/CornerNet.py, and I want to know what does the n=5 means? thank you.

class model(kp): def init(self, db): n = 5 dims = [256, 256, 384, 384, 384, 512] modules = [2, 2, 2, 2, 2, 4] out_dim = 80 super(model, self).init( n, 2, dims, modules, out_dim, make_tl_layer=make_tl_layer, make_br_layer=make_br_layer, make_pool_layer=make_pool_layer, make_hg_layer=make_hg_layer, kp_layer=residual, cnv_dim=256 )

heilaw commented 6 years ago

The number of downsamplings in each hourglass module.

YiLiangNie commented 6 years ago

Hi heilaw, If I want to change the project to python2.7, what files need to be modified? thx!