Upload 2 files
Browse files- cityscape.py +130 -0
- create_jsonl_file.py +53 -0
cityscape.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from huggingface_hub import hf_hub_url # hf_hub_url is used to construct the URL of a file from the given information.
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
import os
|
6 |
+
|
7 |
+
#_VERSION = datasets.Version("0.0.2")
|
8 |
+
|
9 |
+
_DESCRIPTION = "TODO"
|
10 |
+
_HOMEPAGE = "TODO"
|
11 |
+
_LICENSE = "TODO"
|
12 |
+
_CITATION = "TODO"
|
13 |
+
|
14 |
+
_FEATURES = datasets.Features(
|
15 |
+
{
|
16 |
+
"image": datasets.Image(),
|
17 |
+
"conditioning_image": datasets.Image(),
|
18 |
+
"text": datasets.Value("string"),
|
19 |
+
},
|
20 |
+
)
|
21 |
+
|
22 |
+
METADATA_URL = hf_hub_url(
|
23 |
+
"MoaazId/cityscape",
|
24 |
+
filename="train.jsonl",
|
25 |
+
repo_type="dataset",
|
26 |
+
)
|
27 |
+
|
28 |
+
IMAGES_URL = hf_hub_url(
|
29 |
+
"MoaazId/cityscape",
|
30 |
+
filename="leftImg8bit.zip/train/"+f"{city}", #M
|
31 |
+
repo_type="dataset",
|
32 |
+
)
|
33 |
+
|
34 |
+
CONDITIONING_IMAGES_URL = hf_hub_url(
|
35 |
+
"MoaazId/cityscape",
|
36 |
+
filename="gtCoarse.zip/train/"+f"{city}", #M
|
37 |
+
repo_type="dataset",
|
38 |
+
)
|
39 |
+
|
40 |
+
_DEFAULT_CONFIG = datasets.BuilderConfig(name="default")
|
41 |
+
|
42 |
+
|
43 |
+
class Cityscape(datasets.GeneratorBasedBuilder):
|
44 |
+
BUILDER_CONFIGS = [_DEFAULT_CONFIG]
|
45 |
+
DEFAULT_CONFIG_NAME = "default"
|
46 |
+
|
47 |
+
def _info(self):
|
48 |
+
return datasets.DatasetInfo(
|
49 |
+
description=_DESCRIPTION,
|
50 |
+
features=_FEATURES,
|
51 |
+
supervised_keys=None,
|
52 |
+
homepage=_HOMEPAGE,
|
53 |
+
license=_LICENSE,
|
54 |
+
citation=_CITATION,
|
55 |
+
)
|
56 |
+
|
57 |
+
def _split_generators(self, dl_manager):
|
58 |
+
metadata_path = dl_manager.download(METADATA_URL)
|
59 |
+
images_dir = dl_manager.download_and_extract(IMAGES_URL)
|
60 |
+
conditioning_images_dir = dl_manager.download_and_extract(
|
61 |
+
CONDITIONING_IMAGES_URL
|
62 |
+
)
|
63 |
+
|
64 |
+
|
65 |
+
# Iterate through the city folders in the image directory
|
66 |
+
for city_folder in os.listdir(images_dir):
|
67 |
+
city_dir = os.path.join(images_dir, city_folder)
|
68 |
+
|
69 |
+
# Iterate through image files in the city directory
|
70 |
+
for image_filename in os.listdir(city_dir):
|
71 |
+
# Extract relevant information from the image filename
|
72 |
+
parts = image_filename.split("_")
|
73 |
+
city = parts[0]
|
74 |
+
seq = parts[1]
|
75 |
+
frame = parts[2]
|
76 |
+
|
77 |
+
# Construct the paths to the image and conditioning image
|
78 |
+
image_path = os.path.join(city_dir, image_filename)
|
79 |
+
conditioning_image_filename = f"{city}_{seq}_{frame}_gtCoarse_color.png"
|
80 |
+
conditioning_image_path = os.path.join(conditioning_dir, city_folder+"/"+conditioning_image_filename)
|
81 |
+
|
82 |
+
# Create a dataset entry as a dictionary
|
83 |
+
entry = {
|
84 |
+
"text": "A view to a street from a car's front window",
|
85 |
+
"image": image_path,
|
86 |
+
"conditioning_image": conditioning_image_path,
|
87 |
+
}
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
return [
|
93 |
+
datasets.SplitGenerator(
|
94 |
+
name=datasets.Split.TRAIN,
|
95 |
+
# These kwargs will be passed to _generate_examples
|
96 |
+
gen_kwargs={
|
97 |
+
"metadata_path": metadata_path,
|
98 |
+
"images_dir": images_dir,
|
99 |
+
"conditioning_images_dir": conditioning_images_dir,
|
100 |
+
},
|
101 |
+
),
|
102 |
+
]
|
103 |
+
|
104 |
+
def _generate_examples(self, metadata_path, images_dir, conditioning_images_dir):
|
105 |
+
metadata = pd.read_json(metadata_path, lines=True)
|
106 |
+
|
107 |
+
for _, row in metadata.iterrows():
|
108 |
+
text = row["text"]
|
109 |
+
|
110 |
+
image_path = row["image"]
|
111 |
+
image_path = os.path.join(images_dir, image_path)
|
112 |
+
image = open(image_path, "rb").read()
|
113 |
+
|
114 |
+
conditioning_image_path = row["conditioning_image"]
|
115 |
+
conditioning_image_path = os.path.join(
|
116 |
+
conditioning_images_dir, row["conditioning_image"]
|
117 |
+
)
|
118 |
+
conditioning_image = open(conditioning_image_path, "rb").read()
|
119 |
+
|
120 |
+
yield row["image"], {
|
121 |
+
"text": text,
|
122 |
+
"image": {
|
123 |
+
"path": image_path,
|
124 |
+
"bytes": image,
|
125 |
+
},
|
126 |
+
"conditioning_image": {
|
127 |
+
"path": conditioning_image_path,
|
128 |
+
"bytes": conditioning_image,
|
129 |
+
},
|
130 |
+
}
|
create_jsonl_file.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
|
4 |
+
# Define the directory where your dataset images are located
|
5 |
+
#dataset_directory = "cityscape_dataset"
|
6 |
+
|
7 |
+
# Define the directories for images and conditioning images
|
8 |
+
#image_dir = os.path.join(dataset_directory, "leftImg8bit/train")
|
9 |
+
#conditioning_dir = os.path.join(dataset_directory, "gtCoarse/train")
|
10 |
+
|
11 |
+
image_dir = "leftImg8bit/train"
|
12 |
+
conditioning_dir = "gtCoarse/train"
|
13 |
+
|
14 |
+
# Define the output JSONL filename
|
15 |
+
jsonl_filename = "train.jsonl"
|
16 |
+
|
17 |
+
# Initialize an empty list to store the dataset entries
|
18 |
+
dataset_entries = []
|
19 |
+
|
20 |
+
# Iterate through the city folders in the image directory
|
21 |
+
for city_folder in os.listdir(image_dir):
|
22 |
+
city_dir = os.path.join(image_dir, city_folder)
|
23 |
+
|
24 |
+
# Iterate through image files in the city directory
|
25 |
+
for image_filename in os.listdir(city_dir):
|
26 |
+
# Extract relevant information from the image filename
|
27 |
+
parts = image_filename.split("_")
|
28 |
+
city = parts[0]
|
29 |
+
seq = parts[1]
|
30 |
+
frame = parts[2]
|
31 |
+
|
32 |
+
# Construct the paths to the image and conditioning image
|
33 |
+
image_path = os.path.join(city_dir, image_filename)
|
34 |
+
conditioning_image_filename = f"{city}_{seq}_{frame}_gtCoarse_color.png"
|
35 |
+
conditioning_image_path = os.path.join(conditioning_dir, city_folder+"/"+conditioning_image_filename)
|
36 |
+
|
37 |
+
# Create a dataset entry as a dictionary
|
38 |
+
entry = {
|
39 |
+
"text": "A view to a street from a car's front window",
|
40 |
+
"image": image_path,
|
41 |
+
"conditioning_image": conditioning_image_path,
|
42 |
+
}
|
43 |
+
|
44 |
+
# Append the entry dictionary to the list
|
45 |
+
dataset_entries.append(entry)
|
46 |
+
|
47 |
+
# Open the JSONL file for writing
|
48 |
+
with open(jsonl_filename, "w") as jsonl_file:
|
49 |
+
# Write each entry as a JSON string followed by a newline character
|
50 |
+
for entry in dataset_entries:
|
51 |
+
jsonl_file.write(json.dumps(entry) + "\n")
|
52 |
+
|
53 |
+
print(f"JSON Lines file '{jsonl_filename}' has been created.")
|