-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_to_array_record.py
More file actions
256 lines (220 loc) · 7.25 KB
/
Copy pathconvert_to_array_record.py
File metadata and controls
256 lines (220 loc) · 7.25 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import argparse
import json
import os
import random
from pathlib import Path
from typing import Dict, List, Union
import tensorflow as tf
from array_record.python.array_record_module import ArrayRecordWriter
DOMAINS = {
"cars": 0,
"sop": 1,
"inshop": 2,
"inat": 3,
"food2k": 6,
"imagenet": 8,
}
def load_json_info(info_file: str) -> List[Dict[str, Union[str, int, List[int]]]]:
with open(info_file, "r") as infile:
return json.load(infile)
def build_info_from_image_folders(base_dir: str) -> List[Dict[str, Union[str, int]]]:
"""Builds split metadata from a class-per-folder image directory."""
info_data = []
classes = sorted(
d for d in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, d))
)
class_to_idx = {cls: idx for idx, cls in enumerate(classes)}
for cls in classes:
cls_dir = os.path.join(base_dir, cls)
for filename in sorted(os.listdir(cls_dir)):
if filename.lower().endswith((".jpg", ".jpeg", ".png")):
info_data.append(
{
"path": os.path.join(cls, filename),
"class_id": class_to_idx[cls],
}
)
return info_data
def maybe_subset_info_data(
info_data: List[Dict[str, Union[str, int, List[int]]]],
*,
split: str,
percentage: int | None,
shuffle: bool,
seed: int,
) -> List[Dict[str, Union[str, int, List[int]]]]:
if shuffle:
random.seed(seed)
random.shuffle(info_data)
if percentage is None:
return info_data
if not 0 <= percentage <= 100:
raise ValueError("percentage must be between 0 and 100.")
subset_size = len(info_data) * percentage // 100
if split == "val":
return info_data[-subset_size:]
return info_data[:subset_size]
def create_example(
*,
index: int,
image_file: str,
label: Union[int, str, List[int], List[str]],
files_dir: str,
domain: str,
) -> tf.train.Example:
if not isinstance(label, list):
label = [label]
image_path = os.path.join(files_dir, image_file)
feature = {
"index": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
"domain": tf.train.Feature(
int64_list=tf.train.Int64List(value=[DOMAINS[domain]])
),
"image_bytes": tf.train.Feature(
bytes_list=tf.train.BytesList(value=[tf.io.read_file(image_path).numpy()])
),
"class_id": tf.train.Feature(int64_list=tf.train.Int64List(value=label)),
"key": tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_file.encode()])),
}
return tf.train.Example(features=tf.train.Features(feature=feature))
def write_array_record_shards(
*,
info_data: List[Dict[str, Union[str, int, List[int]]]],
output_file: str,
files_dir: str,
num_shards: int,
domain: str,
) -> None:
writers = [
ArrayRecordWriter(
f"{output_file}-{i:05d}-of-{num_shards:05d}",
"group_size:1",
)
for i in range(num_shards)
]
num_examples = len(info_data)
examples_per_shard = num_examples // num_shards
remainder = num_examples % num_shards
shard_counts = [
examples_per_shard + (1 if i < remainder else 0)
for i in range(num_shards)
]
shard_index = 0
shard_limit = shard_counts[0] if shard_counts else 0
for i, file_info in enumerate(info_data):
if i >= shard_limit and shard_index < num_shards - 1:
shard_index += 1
shard_limit += shard_counts[shard_index]
print(f"Processing: {file_info['path']}")
example = create_example(
index=i,
image_file=file_info["path"],
label=file_info["class_id"],
files_dir=files_dir,
domain=domain,
)
writers[shard_index].write(example.SerializeToString())
for writer in writers:
writer.close()
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Convert image datasets into ArrayRecord shards.",
)
parser.add_argument(
"--input_format",
choices=("json", "folder"),
default="json",
help="Use 'json' for split metadata files or 'folder' for class-per-folder scans.",
)
parser.add_argument(
"--info_file",
help="Path to JSON split metadata. Required when --input_format=json.",
)
parser.add_argument(
"--base_dir",
help="Base directory of class-per-folder images. Required when --input_format=folder.",
)
parser.add_argument(
"--files_dir",
required=True,
help="Root directory used to resolve relative image paths.",
)
parser.add_argument(
"--output_file",
required=True,
help="Output ArrayRecord path prefix.",
)
parser.add_argument(
"--domain",
required=True,
choices=sorted(DOMAINS),
help="Dataset domain identifier.",
)
parser.add_argument(
"--num_shards",
type=int,
required=True,
help="Number of ArrayRecord shards to write.",
)
parser.add_argument(
"--split",
default="train",
help="Split name used when subsetting folder-based inputs.",
)
parser.add_argument(
"--percentage",
type=int,
help="Keep only this percentage of folder-based inputs after optional shuffling.",
)
parser.add_argument(
"--shuffle",
action="store_true",
help="Shuffle folder-based inputs before applying --percentage.",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="Random seed used with --shuffle.",
)
parser.add_argument(
"--save_info_json",
action="store_true",
help="Save the resolved input metadata next to the output prefix.",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
Path(args.output_file).parent.mkdir(parents=True, exist_ok=True)
if args.input_format == "json":
if not args.info_file:
raise ValueError("--info_file is required when --input_format=json.")
info_data = load_json_info(args.info_file)
else:
if not args.base_dir:
raise ValueError("--base_dir is required when --input_format=folder.")
info_data = build_info_from_image_folders(args.base_dir)
info_data = maybe_subset_info_data(
info_data,
split=args.split,
percentage=args.percentage,
shuffle=args.shuffle,
seed=args.seed,
)
if args.save_info_json:
info_data_json_path = os.path.splitext(args.output_file)[0] + "_info_data.json"
with open(info_data_json_path, "w") as json_file:
json.dump(info_data, json_file, indent=2)
print(f"Info data saved to {info_data_json_path}")
print(f"Output File: {args.output_file}")
print(f"Files Directory: {args.files_dir}")
print(f"Examples: {len(info_data)}")
write_array_record_shards(
info_data=info_data,
output_file=args.output_file,
files_dir=args.files_dir,
num_shards=args.num_shards,
domain=args.domain,
)
if __name__ == "__main__":
main()