Custom an ensemble metric

Hi, I am trying to design a custom metric during training. So here are some information:

  1. I am training a classification model with images, which the input is stored in a dataframe.
  2. for each image I have a label and the trained model will give a prediction per image.
  3. in the dataframe, I also have another column named address, one address might has several images. If the images have same address, than they will have same labels.
  4. Instead of the traditional accuracy, I want to create a custom accuracy which we will ensemble same address’ images’ predictions, and then calculating the average probabilities for each class, and select the class with the highest probability as the answer for address.
    In this case, I wonder if it is possible to create a custom accuracy in fastai Metrics/Callback?
    Any suggestions will be super helpful. Thank you!
    My step 1 is creating an DataBlock
db = DataBlock(
    blocks=(ImageBlock, CategoryBlock),
    get_x=partial(get_x_from_df_row, img_col="images"),
        mask=False if model_type == "cnn" else True,
    item_tfms=Resize(img_size, method="squish"),

my step 2:

dls = db.dataloaders(df, bs=BATCH_SIZE, num_workers=num_workers)

my step3:

learner = vision_learner(
    metrics=[accuracy, FBeta(beta=1)]

Now my question is if I can create a custom metric/callback?

I think you will find Jeremy’s notebook on multi-target classification helpful for this problem. He also walks through his approach to multi-classification in this live coding video.