I could have sworn I saw a post where this was mentionted a long time ago but I cannot seem to find it. I am not sure what this technique is called and have been unable to search for it effectively.
What I am looking to do is instead of having my y labels be [0,0,0,0,0,1] as the goal of prediction (and what the loss function uses), I want it to be something like [0,0,0,0.1,0.1,0.8], so that if I get a category ‘close’ to the right answer it is penalized less than if I classify it completely wrong. For example, if I am predicting pet breeds:
- If I pick the right breed that’s ideal
- If the image is a cat and I pick the wrong breed of cat, that is penalized some
- If the image is a cat and I guess that it’s some breed of dog, that is penalized more heavily than bullet 2
I am trying to figure out how I can use a custom defined matrix as my labels. I can’t seem to figure out either how to specify this and how to access and change the y matrix that has to be generated for the model to work generated. In the pets dataset, what I would like to do is change my y values so that if it is a cat and I predict the wrong breed of cat, that is a more acceptable error than if I predict a dog. My thought on how to do this would be to change the y matrix, though I am open to other ideas on how to get this result.
Here is the datablock and dataloader.
from fastai2.vision.all import *
path = untar_data(URLs.PETS) #Sample dataset from fastai2
dls = pets.dataloaders(path/"images")
pets = DataBlock(
blocks = (ImageBlock, CategoryBlock),
get_items = get_image_files,
splitter= RandomSplitter(valid_pct = 0.2, seed=seed),
get_y= using_attr(RegexLabeller(r'(.+)_\d+.jpg$'),'name'),
item_tfms=Resize(460),
batch_tfms=aug_transforms(min_scale = 0.9,size=224)
)
dls = pets.dataloaders(path/"images")