-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyn_datagen.py
157 lines (129 loc) · 4.44 KB
/
syn_datagen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
""" Generate UISketch synthetic dataset from labelled UI element sketches. """
import warnings
warnings.filterwarnings("ignore", message=r"Passing", category=FutureWarning)
import argparse
import os
from uisketch_dataset_module.generate_tf_record import Generate_TF_Records
from uisketch_dataset_module.uisketch_synthetic_datagen import UISketch_Synthetic_Datagen
from uisketch_dataset_module.xml_to_csv import XML_to_CSV
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate UISketch synthetic dataset from labelled UI element sketches."
)
parser.add_argument(
"-d",
"--directory",
required=True,
dest="directory",
help="Directory containing labelled folders of UI element sketches",
)
parser.add_argument(
"-o",
"--output",
required=True,
dest="output_folder",
help="Output folder of cropped images",
)
parser.add_argument(
"-l",
"--limit",
type=int,
required=True,
dest="limit",
help="Number of dataset images to generate",
)
parser.add_argument(
"-t",
"--timeout",
type=int,
dest="timeout",
default=10,
help="Subprocess timeout limit",
)
parser.add_argument(
"-s",
"--test-split",
default=0.2,
type=float,
dest="test_split",
help="Test split percentage (default: 0.2)",
)
parser_group = parser.add_mutually_exclusive_group()
parser_group.add_argument(
"--datagen-only",
dest="datagen_only",
action="store_true",
help="Generate images and annotations without csv/tf shards",
)
parser_group.add_argument(
"--eval-only",
dest="eval_only",
action="store_true",
help="Generate only evaluation data without train/test",
)
parser_group.add_argument(
"--conversion-only",
dest="conversion_only",
action="store_true",
help="Converts data to csv & tf shards without train/test",
)
parser.set_defaults(eval_only=False)
args = parser.parse_args()
eval_only = args.eval_only
if eval_only:
print("** Evaluation data generation mode **")
datagen_only = args.datagen_only
if datagen_only:
print("** Data generation mode **")
conversion_only = args.conversion_only
if datagen_only:
print("** Data generation mode **")
directory = args.directory
directory = directory.strip(os.sep)
print(f"Directory with UI element sketches: {directory}")
output_folder = args.output_folder
output_folder = output_folder.strip(os.sep)
print(f"Output folder: {output_folder}")
limit = args.limit
print(f"{limit} images will be generated")
timeout = args.timeout
# Create Datagen instance
uisketch_datagen = UISketch_Synthetic_Datagen(directory, output_folder)
if not conversion_only:
# Generate dataset
uisketch_datagen.generate(limit=limit, timeout=timeout)
if not datagen_only:
if not eval_only:
test_split = args.test_split
print(f"Test split: {test_split}")
xml_to_csv = XML_to_CSV(
uisketch_datagen.annotations_folder,
uisketch_datagen.data_folder,
)
xml_to_csv.convert(test_split=test_split)
generate_tf_records = Generate_TF_Records(
uisketch_datagen.image_folder,
uisketch_datagen.data_folder,
uisketch_datagen.labels,
)
generate_tf_records.generate_records(
xml_to_csv.training_labels_csv, record_name="train.record"
)
generate_tf_records.generate_records(
xml_to_csv.test_labels_csv, record_name="test.record"
)
else:
xml_to_csv = XML_to_CSV(
uisketch_datagen.annotations_folder,
uisketch_datagen.data_folder,
)
XML_DF = xml_to_csv.xml_to_csv()
XML_DF.to_csv(xml_to_csv.uisketch_labels_csv, index=None)
generate_tf_records = Generate_TF_Records(
uisketch_datagen.image_folder,
uisketch_datagen.data_folder,
uisketch_datagen.labels,
)
generate_tf_records.generate_records(
xml_to_csv.uisketch_labels_csv, record_name="eval.record"
)