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

Hi James,

Thanks for the help!

Here’s what I did:

Create the models directory:

cache_dir = expanduser(join('~', '.torch'))
if not exists(cache_dir):
    makedirs(cache_dir)
models_dir = join(cache_dir, 'models')
if not exists(models_dir):
    makedirs(models_dir)

Copy the weights over:

!cp ../input/resnet34/resnet34.pth /tmp/.torch/models/resnet34-333f7ec4.pth

Now I get this error

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-73-b0bc3f5d394a> in <module>()
----> 1 learn = ConvLearner.pretrained(f_model, md, metrics=[accuracy])
      2 learn.opt_fn = optim.Adam

/opt/conda/lib/python3.6/site-packages/fastai-0.6-py3.6.egg/fastai/conv_learner.py in pretrained(cls, f, data, ps, xtra_fc, xtra_cut, custom_head, precompute, **kwargs)
     99         models = ConvnetBuilder(f, data.c, data.is_multi, data.is_reg,
    100             ps=ps, xtra_fc=xtra_fc, xtra_cut=xtra_cut, custom_head=custom_head)
--> 101         return cls(data, models, precompute, **kwargs)
    102 
    103     @property

/opt/conda/lib/python3.6/site-packages/fastai-0.6-py3.6.egg/fastai/conv_learner.py in __init__(self, data, models, precompute, **kwargs)
     85     def __init__(self, data, models, precompute=False, **kwargs):
     86         self.precompute = False
---> 87         super().__init__(data, models, **kwargs)
     88         if hasattr(data, 'is_multi'):
     89             self.crit = F.binary_cross_entropy if data.is_multi else F.nll_loss

/opt/conda/lib/python3.6/site-packages/fastai-0.6-py3.6.egg/fastai/learner.py in __init__(self, data, models, opt_fn, tmp_name, models_name, metrics, clip)
     22         self.tmp_path = os.path.join(self.data.path, tmp_name)
     23         self.models_path = os.path.join(self.data.path, models_name)
---> 24         os.makedirs(self.tmp_path, exist_ok=True)
     25         os.makedirs(self.models_path, exist_ok=True)
     26         self.crit,self.reg_fn = None,None

/opt/conda/lib/python3.6/os.py in makedirs(name, mode, exist_ok)
    218             return
    219     try:
--> 220         mkdir(name, mode)
    221     except OSError:
    222         # Cannot rely on checking for EEXIST, since the operating system

OSError: [Errno 30] Read-only file system: '../input/tmp'

So fastai tries to write to the data directory, which is read-only.

I set the path to the working directory

md.path = pathlib.Path('.')

and now it runs.

7 Likes