samzirbo commited on
Commit
2ca1e9b
1 Parent(s): b313e8a

Upload set-classification.py

Browse files
Files changed (1) hide show
  1. set-classification.py +64 -0
set-classification.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import pandas as pd
3
+ from datasets import DownloadManager
4
+
5
+ class SetClassification(datasets.GeneratorBasedBuilder):
6
+ """Set-Classification Images dataset"""
7
+
8
+ def __init__(self, data_path, *args, **kwargs):
9
+ super(SetClassification, self).__init__(*args, **kwargs)
10
+ self.data_path = data_path
11
+ self.labels = pd.read_csv(f'{self.data_path}/labels.csv')
12
+ self.train = self.labels[self.labels['split'] == 'train']
13
+ self.test = self.labels[self.labels['split'] == 'test']
14
+ self.dl_manager = DownloadManager()
15
+
16
+ def _info(self):
17
+ return datasets.DatasetInfo(
18
+ description='Set Classification Images dataset',
19
+ )
20
+
21
+
22
+ def _split_generators(self, dl_manager):
23
+
24
+ return [
25
+ datasets.SplitGenerator(
26
+ name=datasets.Split.TRAIN,
27
+ gen_kwargs={
28
+ 'images': [f"{self.data_path}/images/{image.filename}" for image in self.train.itertuples()],
29
+ 'labels': {
30
+ 'no': [image.no for image in self.train.itertuples()],
31
+ 'shape': [image.shape for image in self.train.itertuples()],
32
+ 'color': [image.color for image in self.train.itertuples()],
33
+ 'shading': [image.shading for image in self.train.itertuples()]
34
+ }
35
+ }
36
+ ),
37
+ datasets.SplitGenerator(
38
+ name=datasets.Split.TEST,
39
+ gen_kwargs={
40
+ 'images': [f"{self.data_path}/images/{image.filename}" for image in self.test.itertuples()],
41
+ 'labels': {
42
+ 'no': [image.no for image in self.test.itertuples()],
43
+ 'shape': [image.shape for image in self.test.itertuples()],
44
+ 'color': [image.color for image in self.test.itertuples()],
45
+ 'shading': [image.shading for image in self.test.itertuples()]
46
+ }
47
+ }
48
+ )
49
+ ]
50
+
51
+ def _generate_examples(self, images, labels):
52
+ for img, label in zip(images, zip(*labels.values())):
53
+ try:
54
+ with open(img, 'rb') as img_obj:
55
+ no, shape, color, shading = label
56
+ yield img, {
57
+ 'image': {"path": img, "bytes": img_obj.read()},
58
+ 'no': no,
59
+ 'shape': shape,
60
+ 'color': color,
61
+ 'shading': shading
62
+ }
63
+ except Exception as e:
64
+ print(f"Error processing image {img}: {e}")