Error with CutMix callback

Hi guys! Hope you’re doing well.

I am trying to implement an image classifier with augmentations of CutMix and Mixup. I successfully trained my model with the Mixup callback but with CutMix, I get the below error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-37-3bf775d5b609> in <module>()
----> 1 lr_min,lr_steep = learn.lr_find()

20 frames
/usr/local/lib/python3.6/dist-packages/torch/distributions/dirichlet.py in forward(ctx, concentration)
     16     @staticmethod
     17     def forward(ctx, concentration):
---> 18         x = torch._sample_dirichlet(concentration)
     19         ctx.save_for_backward(x, concentration)
     20         return x

RuntimeError: "dirichlet" not implemented for 'Long'

Is something wrong with my code? or is “Dirichlet” not being for long is something from Pytorch that I can’t change?

Apologies if the question seems lame, I am just starting out.
Best,
Mayuresh

EDIT:
Just calling cutmix = CutMix(1) wasn’t working as it was not being recognized. Hence I used from fastai2.callback.cutmix import * . Which then gave this error.

Can you show us the code so we can reproduce it possibly or see what you’re doing? :slight_smile: (How you built your Learner, etc)

Here it goes: And I am using it on the Food-101 dataset.

def get_dls(bs, size):

dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),

               get_items=get_image_files,

               get_y=parent_label,

               splitter = GrandparentSplitter(valid_name='test',train_name='train'),

               item_tfms=Resize(460),

               batch_tfms=[*aug_transforms(size=size, min_scale=0.75, ),

                           Normalize.from_stats(*imagenet_stats)])

return dblock.dataloaders(path, bs=bs)

dls = get_dls(128, 128)
cutmix= CutMix(alpha=0.5)

learn = cnn_learner(dls, resnet50, loss_func=LabelSmoothingCrossEntropy(),

            metrics=accuracy,

            cbs=cutmix)

lr_min,lr_steep = learn.lr_find()

EDIT:
Just calling cutmix = CutMix(1) wasn’t working as it was not being recognized. Hence I used from fastai2.callback.cutmix import * . Which then gave this error.

Check out here, this may be a hint: Implementing `CutMix` in FastaiV2

Can you try simply fitting? (not doing lr_find())

According to @amritv running learn twice solved the issue for him. Unfortunately, I got the same error again :sob: :worried:
And I tried fitting and fine-tuning the learner as well, but no luck.

Also, I forgot to mention. Just calling cutmix = CutMix(1) wasn’t working as it was not being recognized. Hence I used from fastai2.callback.cutmix import *. Which then gave this error.

Try implementing it this way and see if it works https://github.com/asvcode/medical_imaging/blob/master/Cutmix_covid.ipynb

Thanks for the reply @amritv. But the problem was entirely with my code. The callback should’ve been done by passing a Float.
cutmix = CutMix(1) was where it went wrong .
Changing it to cutmix = CutMix(1.) solved it.

1 Like

Thank you @muellerzr. Simply fitting with learn.fit_one_cycle() helped in my case. And it gave me errors when I did lr_find() before learn.fit_one_cycle().

Thanks again!