Can ImageDataLoaders.from_df be used for regression?

I’m trying to predict how soon a pump will fail (in days) based on images. So I’ve been trying to use ImageDataLoaders.from_df and I’m running into all sorts of problems. It seems to be trying to force a classification model?

The model appears to train fine, but when it tries to validate the first epoch I get: KeyError: "Label '1291' was not included in the training dataset". This is confusing because 1291 isn’t really supposed to be a label. It’s a number for the regression to predict.

Here’s the code:

dls = ImageDataLoaders.from_df(card_df_sample, path=image_folder, fn_col="Id", suff=".png", valid_col="is_valid", yblock=RegressionBlock(), label_col="days_to_fail")
learn = vision_learner(dls, resnet18, metrics=mae)
learn.fit_one_cycle(3)

I went through this blog post, it was very helpful.
I think the problem might be with your loss function, you need to set it to MSEloss, L1loss, or any other that goes for regression because fastai might have been setting it to a loss function used for classification.
(I might be wrong)

Interesting idea! But explicitly setting the loss function didn’t seem to help.

I managed to fix this with the following:

dblock = DataBlock(blocks=(ImageBlock, RegressionBlock),
                           get_x=ColReader("Id", pref=image_folder, suff=".png"),
                           get_y=ColReader("days_to_fail"),
                           splitter=ColSplitter("is_valid"))
dls = dblock.dataloaders(card_df)
learn = vision_learner(dls, resnet18, metrics=mae, cbs=ShowGraphCallback())
learn.fine_tune(5)
1 Like