Changing default directory for torch models

Hi,

If I run for example

learn = create_cnn(data, models.resnet18, metrics=accuracy)

the model is loaded into /root/.torch/models as it is shown here:

Downloading: "https://download.pytorch.org/models/resnet18-5c106cde.pth" to /root/.torch/models/resnet18-5c106cde.pth 100%|██████████| 46827520/46827520 [00:00<00:00, 73938844.51it/s]

My question is: can I change the directory where the model is loaded?

Regards

Jarek

Pl try this.

I have ~/.fastai/config.yml setup so the data and model are loaded into the /workspace/fastai/data and /workspace/fastai/model respectively.

It works properly for path = untar_data(URLs.PETS); path for example. The result is PosixPath('/workspace/fastai/data/oxford-iiit-pet') but still create_cnn loads the model into ~/.torch/models/ :frowning:

You can override the default pytorch model_dir by changing the env variable,
os.environ['TORCH_MODEL_ZOO'] = '<path>'

2 Likes

That’s what I need. Thank you!

You can also use os.rename(old_path, new_path) if your files are uploaded incorrectly. This will move your files to the new directory. For example:

folders = [‘black’, ‘teddys’, ‘grizzly’]
files = [‘urls_black.csv’, ‘urls_teddys.csv’, ‘urls_grizzly.csv’]
for folder, file in zip(folders, files):
path = ‘data/bears’
os.rename(os.path.join(os.getcwd(), file),
os.path.join(os.getcwd(), path, file))

Great advice!
Please notice that “TORCH_MODEL_ZOO” is deprecated, use “TORCH_HOME” instead.

1 Like