How can I load a pretrained model on Kaggle using fastai?


(Kal) #24

Struggled with this for a while, some tips for Kaggle kernels where you don’t have internet access to download pre-trained weights:

  1. Under draft-environment in your kernel, click the “add-data” button, search for the relevant pytorch model. For example I wanted to use the Resnet-50, so I added the Resnet-50 Pytorch (not the Keras) model to my kernel (click “Add”).
  2. This will give you a new Resnet-50 directory and the *.pth weight file inside of that directory. Now you need to copy the *.pth file into the torch model directory using the same filename it’s looking for. One way to do this is to try to run the model without copying anything. I would get an error like:

failed download to /tmp/.torch/models/resnet50-19c8e357.pth

  1. So we need to copy the resnet50.pth file from the ResNet50 directory that was just automatically added we to the directory where it’s just errored out. My resnet50 file was in:

../input/resnet50/resnet50.pth

  1. Therefore, run a copy command that takes the file you have and put in the place where it’s looking for it, using the same model-sha_hash naming convention.

!cp ../input/resnet50/resnet50.pth /tmp/.torch/models/resnet50-19c8e357.pth

  1. Make sure you remember that your …/input directory is read only and the models are going to be changed during the learning process so you need to go up one level when creating your learner:
    ./ : put path here
    ./input : read only, don’t put path here

(Kirill) #25

Thanks a lot! I will just mention that in order to change learner’s directory one should add model_dir kwarg to create_cnn function so it looks like that:
learn = create_cnn(data, models.resnet34, metrics=error_rate, model_dir='/tmp/models')


(JianWen Yang) #26

There’s a kaggle dataset for wt103
https://www.kaggle.com/mnpinto/fastai-wt103-1
But when I use used hyper-parameter fnames="…/input/fastai-wt103-1/wt103-1.tgz" for language model_learner, it reported “FileNotFoundError: [Errno 2] No such file or directory: ‘…/models/…pkl’” How can I deal with that?