import datasets import pandas as pd from datasets import DownloadManager class SetClassification(datasets.GeneratorBasedBuilder): """Set-Classification Images dataset""" def __init__(self, data_path='data', *args, **kwargs): super(SetClassification, self).__init__(*args, **kwargs) self.data_path = data_path self.labels = pd.read_csv(f'{self.data_path}/labels.csv') self.train = self.labels[self.labels['split'] == 'train'] self.test = self.labels[self.labels['split'] == 'test'] self.dl_manager = DownloadManager() def _info(self): return datasets.DatasetInfo( description='Set Classification Images dataset', ) def _split_generators(self, dl_manager): return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ 'images': [f"{self.data_path}/images/{image.filename}" for image in self.train.itertuples()], 'labels': { 'no': [image.no for image in self.train.itertuples()], 'shape': [image.shape for image in self.train.itertuples()], 'color': [image.color for image in self.train.itertuples()], 'shading': [image.shading for image in self.train.itertuples()] } } ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ 'images': [f"{self.data_path}/images/{image.filename}" for image in self.test.itertuples()], 'labels': { 'no': [image.no for image in self.test.itertuples()], 'shape': [image.shape for image in self.test.itertuples()], 'color': [image.color for image in self.test.itertuples()], 'shading': [image.shading for image in self.test.itertuples()] } } ) ] def _generate_examples(self, images, labels): for img, label in zip(images, zip(*labels.values())): try: with open(img, 'rb') as img_obj: no, shape, color, shading = label yield img, { 'image': {"path": img, "bytes": img_obj.read()}, 'no': no, 'shape': shape, 'color': color, 'shading': shading } except Exception as e: print(f"Error processing image {img}: {e}")