birgermoell commited on
Commit
0a0b68e
1 Parent(s): 76707ac

Upload data.py

Browse files
Files changed (1) hide show
  1. data.py +94 -0
data.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import datasets
4
+ import pandas as pd
5
+
6
+ _VERSION = datasets.Version("0.0.2")
7
+
8
+ _DESCRIPTION = "TODO"
9
+ _HOMEPAGE = "TODO"
10
+ _LICENSE = "TODO"
11
+ _CITATION = "TODO"
12
+
13
+ _FEATURES = datasets.Features(
14
+ {
15
+ "image": datasets.Image(),
16
+ "conditioning_image": datasets.Image(),
17
+ "text": datasets.Value("string"),
18
+ },
19
+ )
20
+
21
+
22
+ _DEFAULT_CONFIG = datasets.BuilderConfig(name="default", version=_VERSION)
23
+ DATA_DIR = "/home/birgermoell/data"
24
+
25
+
26
+ class coyo(datasets.GeneratorBasedBuilder):
27
+ BUILDER_CONFIGS = [_DEFAULT_CONFIG]
28
+ DEFAULT_CONFIG_NAME = "default"
29
+
30
+ def _info(self):
31
+ return datasets.DatasetInfo(
32
+ description=_DESCRIPTION,
33
+ features=_FEATURES,
34
+ supervised_keys=None,
35
+ homepage=_HOMEPAGE,
36
+ license=_LICENSE,
37
+ citation=_CITATION,
38
+ )
39
+
40
+ def _split_generators(self, dl_manager):
41
+ metadata_path = f"{DATA_DIR}/output.csv"
42
+ images_dir = f"{DATA_DIR}/base_images"
43
+ conditioning_images_dir = f"{DATA_DIR}/segmented_images"
44
+
45
+ return [
46
+ datasets.SplitGenerator(
47
+ name=datasets.Split.TRAIN,
48
+ # These kwargs will be passed to _generate_examples
49
+ gen_kwargs={
50
+ "metadata_path": metadata_path,
51
+ "images_dir": images_dir,
52
+ "conditioning_images_dir": conditioning_images_dir,
53
+ },
54
+ ),
55
+ ]
56
+
57
+ def _generate_examples(self, metadata_path, images_dir, conditioning_images_dir):
58
+ #metadata = pd.read_json(metadata_path, lines=True)
59
+ metadata = pd.read_csv(metadata_path)
60
+
61
+ for _, row in metadata.iterrows():
62
+ text = row["description"]
63
+ print("the row is", row)
64
+
65
+ try:
66
+ image_path = row["image"]
67
+ #image_path = os.path.join(images_dir, image_path)
68
+ image_path = os.path.join("/home/birgermoell/data/" + image_path)
69
+ image = open(image_path, "rb").read()
70
+
71
+
72
+ conditioning_image_path = row["conditioning_image"]
73
+
74
+ # conditioning_image_path = os.path.join(
75
+ # conditioning_images_dir, row["conditioning_image"]
76
+ # )
77
+
78
+ conditioning_image_path = os.path.join("/home/birgermoell/data/" + row["conditioning_image"])
79
+
80
+ conditioning_image = open(conditioning_image_path, "rb").read()
81
+
82
+ yield row["image"], {
83
+ "text": text,
84
+ "image": {
85
+ "path": image_path,
86
+ "bytes": image,
87
+ },
88
+ "conditioning_image": {
89
+ "path": conditioning_image_path,
90
+ "bytes": conditioning_image,
91
+ },
92
+ }
93
+ except Exception as e:
94
+ print(e)