Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'CrossEntropyLoss' object has no attribute 'label_smoothing' #1

Closed
iimog opened this issue Jan 18, 2023 · 3 comments
Closed

Comments

@iimog
Copy link
Member

iimog commented Jan 18, 2023

When trying to use the provided fastai2 model for anything but making single predictions using predict, I get the above error message. This happens in fastai version 2.7.10 and pytorch version 1.13.1. I assume that the loss function in the saved model is too old, so it does not yet have the label_smoothing attribute.

@iimog
Copy link
Member Author

iimog commented Jan 18, 2023

I was able to fix this problem by replacing the loss_func with the current implementation like this:

from fastai.vision.all import *
trainedModel = load_learner("resnet34_5percent_size256_extremeTfms_ceLoss_fastai2.pkl")
trainedModel.loss_func = CrossEntropyLossFlat(axis=1)

As the previous loss function was also CrossEntropyLossFlat, this should not make any difference in training.

After that, it should be possible, to replace the dataloaders like this to continue training on own data:

def label_func(x):
    return str(x).replace("image","mask")

def get_parent(x):
    return Path(x).parent.name == 'val'

dbl = DataBlock(blocks=(ImageBlock, MaskBlock(codes = np.array(["background","left_ventricle","myocardium"]))),
        get_items = get_image_files,
        get_y = label_func,
        splitter = FuncSplitter(get_parent),
        item_tfms=Resize(512, method='crop'),
        batch_tfms=aug_transforms(do_flip=True,max_rotate=90,max_lighting=.4,max_zoom=1.2,size=256))

dls = dbl.dataloaders("data/images", bs=16)
trainedModel.dls = dls

@iimog
Copy link
Member Author

iimog commented Jan 18, 2023

I'll re-upload a version with fixed loss in a new release soon. Documentation for re-training should go to the repository and not into an (unrelated) issue.

@iimog
Copy link
Member Author

iimog commented Jan 18, 2023

New model is uploaded and documentation in the proper location.

@iimog iimog closed this as completed Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant