-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tfrecord_converters.py
43 lines (35 loc) · 1.18 KB
/
run_tfrecord_converters.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
import os
import glob
from transformers import BertTokenizerFast
from dataset import convert_coco_captions_to_tfrecord, convert_images_to_tfrecord
if __name__ == "__main__":
# 1. Convert images dataset to tfrecord files (for training autoencoder)
root_path = "/path/to/coco_root"
part = "train2017"
# `out_path`: path to output directory where tfrecord files will be located
out_path = "/path/to/tfrecord/images"
# `filenames`: a list of image file paths
filenames = glob.glob(os.path.join(root_path, part, "*.jpg"))
convert_images_to_tfrecord(
filenames,
out_path,
num_shards=100,
)
# 2. Convert coco captions dataset to tfrecord files (for training ldm)
root_path = "/path/to/coco_root"
part = "val2017"
ann_filename = "captions_val2017.json"
tokenizer = BertTokenizerFast.from_pretrained("bert_model/")
# `out_path`: path to output directory where tfrecord files will be located
out_path = "/path/to/tfrecord/images_captions"
max_length = 77
num_shards = 20
convert_coco_captions_to_tfrecord(
root_path,
part,
ann_filename,
tokenizer,
out_path,
max_length=max_length,
num_shards=num_shards,
)