fastai icon indicating copy to clipboard operation
fastai copied to clipboard

FastAI appends `models/` prefix and `.pth` suffix to weights file path

Open danielbellsa opened this issue 1 year ago • 5 comments

Goal: Override FastAI appending models/ prefix and .pth suffix to the weights file path.

Note: this behaviour does not happen to the model file path.

How can I override this behaviour?

Turning:

model.load('data/models/my_weights.pth')

Into:

FileNotFoundError: [Errno 2] No such file or directory: 'models/data/models/my_weights.pth.pth'

Code:

def inference(model_params: ModelParams, device: str, dataloader: DataLoader, id: str):
    model = load_learner(model_params.model, cpu=device=='cpu')
    model.load('data/models/my_weights.pth')

Traceback:

Traceback (most recent call last):
  File "/home/me/BitBucket/project/app/container/main.py", line 145, in <module>
    prediciton = inference(model_params, device, dataloader, id)
  File "/home/me/BitBucket/project/app/container/main.py", line 116, in inference
    model.load('data/models/my_weights.pth')
  File "/home/me/miniconda3/envs/venv/lib/python3.9/site-packages/fastai/learner.py", line 387, in load
    load_model(file, self.model, self.opt, device=device, **kwargs)
  File "/home/me/miniconda3/envs/venv/lib/python3.9/site-packages/fastai/learner.py", line 51, in load_model
    state = torch.load(file, map_location=device)
  File "/home/me/miniconda3/envs/venv/lib/python3.9/site-packages/torch/serialization.py", line 791, in load
    with _open_file_like(f, 'rb') as opened_file:
  File "/home/me/miniconda3/envs/venv/lib/python3.9/site-packages/torch/serialization.py", line 271, in _open_file_like
    return _open_file(name_or_buffer, mode)
  File "/home/me/miniconda3/envs/venv/lib/python3.9/site-packages/torch/serialization.py", line 252, in __init__
    super().__init__(open(name, mode))
FileNotFoundError: [Errno 2] No such file or directory: 'models/data/models/my_weights.pth.pth'

danielbellsa avatar May 11 '23 09:05 danielbellsa

Hey @danielbellsa I cannot reproduce the issue can you please provide more context? I have tested the following code on Colab:


from fastai.vision.all import *
import torch

# Define your dataset
path = untar_data(URLs.PETS)/'images'
fnames = get_image_files(path)[:10] # use only first 10 images
def label_func(x): return x[0].isupper()
dls = ImageDataLoaders.from_name_func(
    path, fnames, label_func, item_tfms=Resize(224))

# Define your model
learn = cnn_learner(dls, resnet18, metrics=accuracy)

# Train your model
learn.fine_tune(1)

# Save your model
model_path = Path("/content/test/my_model.pth")
learn.export(model_path)

# Load your model
learn_loaded = load_learner(model_path)

It is working correctly for me and it is not adding any prefix or suffix to the model path.

namanmistry avatar May 16 '23 07:05 namanmistry

So strange that only I seem to be getting this error. Whenever I pass a string literal directly: model.load('data/models/my_weights.pth') FastAI still throws: FileNotFoundError: [Errno 2] No such file or directory: 'models/data/models/my_weights.pth.pth'

danielbellsa avatar May 17 '23 12:05 danielbellsa

What version of FastAI are you using? I'm using fastai==2.7.7

danielbellsa avatar May 17 '23 12:05 danielbellsa

@danielbellsa I am using fastai==2.7.12.

namanmistry avatar May 19 '23 00:05 namanmistry

Hi, just dropping in to confirm that I see the same behaviour @danielbellsa explains, i.e. learn.save adds a prefix models/. to the save path:

image

I have in fastai==2.7.12 . I though this was intended behaviour somehow, but it would actually helpful to be able to override this.

gorkamunoz avatar Jan 12 '24 09:01 gorkamunoz