How to add meta-features so that I can access them during training?

Hi,

I’m doing some image classification, and for each image, I’ve a single boolean meta-feature.

How should I encode the meta-feature to my dataset so that I could access it during the training
when I fetch a batch from the dataloader? I’ll not pass this feature to the network that I’m training, but rather reweight the instance btw.

Hi,

I think making Dataset instance to return a batch of metadata with images (and targets) is a way if the meta-features can be represented in torch.Tensor.

For example, if we use the dataset defined below, in each iteration a DataLoader will generate a tuple of (image, target, metas), where each item is a torch.Tensor.

import torch

class DatasetWithMeta(torch.utils.data.IterableDataset):

    def __init__(self, inputs, targets, metas):
        super().__init__()
        self.inputs = inputs
        self.targets = targets
        self.metas = metas

    def __getitem__(self, index):
        return (self.inputs[index], self.targets[index], self.metas[index])
1 Like

Perfect. Just one thing though: I believe it needs to inherit from torch.utils.data.Dataset, i.e.,
class DatasetWithMeta(Dataset)

1 Like