voxel51 / fiftyone

Refine high-quality datasets and visual AI models
https://fiftyone.ai
Apache License 2.0
8.89k stars 563 forks source link

[FR] Add support for subclassing `Sample` #1643

Open marijnl opened 2 years ago

marijnl commented 2 years ago

Im trying to sublcass fo.Sample such that during evaluation i can put some custom logic on top Can someone help me out here?

My non-working attempt:

class CustomSample(fo.Sample):

    def custom_method(self):
        # do work
        self.save()

custom_sample = CustomSample(filepath="tmp.png")
# <CustomSample: {
#     'id': None,
#     'media_type': 'image',
#     'filepath': 'tmp.png',
#     'tags': [],
#     'metadata': None,
# }>
dataset = fo.Dataset()
dataset.add_sample(custom_sample)

for sample in dataset:
    print(sample)
    # <Sample: {
    #     'id': '6231c273fe91a4da8d0dd0e1',
    #     'media_type': 'image',
    #     'filepath': 'tmp.png',
    #     'tags': BaseList([]),
    #     'metadata': None,
    # }>
    sample.custom_method()
    # AttributeError: Sample has no field 'custom_method'
benjaminpkane commented 2 years ago

Hi @marijnl. Subclassing Sample is not possible at moment. Defining functions that operate on a Sample is enough in on our experience.

import fiftyone on as of

dataset = fo.Dataset()
sample = fo.Sample(filepath="image.png")
dataset.add_sample(sample)

def my_method(sample):
    pass

for sample in dataset:
    my_method(sample)

My own interpretation of Sample is something similar to a dataclass which functions can operate on (not class/instance methods).

Perhaps this feature could be added if there is community support for it. So I will defer to community feedback for the time being. If users want OOP Samples that they can define, we will listen. But this is a large consideration.