-
Notifications
You must be signed in to change notification settings - Fork 34
/
msgpack_viewer.py
166 lines (131 loc) · 4.59 KB
/
msgpack_viewer.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
158
159
160
161
162
163
164
165
166
import os
import argparse
import re
from typing import Union
from io import BytesIO
import random
from pathlib import Path
from PIL import Image
import torchvision
import torch
import msgpack
class MsgPackIterableDataset(torch.utils.data.IterableDataset):
def __init__(
self,
path: str,
key_img_id: str = "id",
key_img_encoded: str = "image",
transformation=None,
shuffle=False,
cache_size=6 * 4096,
):
super(MsgPackIterableDataset, self).__init__()
self.path = path
self.cache_size = cache_size
self.transformation = transformation
self.shuffle = shuffle
self.seed = random.randint(1, 100)
self.key_img_id = key_img_id.encode("utf-8")
self.key_img_encoded = key_img_encoded.encode("utf-8")
if not isinstance(self.path, (list, set)):
self.path = [self.path]
self.shards = self.__init_shards(self.path)
@staticmethod
def __init_shards(path: Union[str, Path]) -> list:
shards = []
for i, p in enumerate(path):
shards_re = r"shard_(\d+).msg"
shards_index = [
int(re.match(shards_re, x).group(1))
for x in os.listdir(p)
if re.match(shards_re, x)
]
shards.extend(
[
{
"path_index": i,
"path": p,
"shard_index": s,
"shard_path": os.path.join(p, f"shard_{s}.msg"),
}
for s in shards_index
]
)
if len(shards) == 0:
raise ValueError("No shards found")
return shards
def _process_sample(self, x):
# decode and initial resize if necessary
img = Image.open(BytesIO(x[self.key_img_encoded]))
if img.mode != "RGB":
img = img.convert("RGB")
if img.width > 320 and img.height > 320:
img = torchvision.transforms.Resize(320)(img)
# apply all user specified image transformations
if self.transformation is not None:
img = self.transformation(img)
_id = x[self.key_img_id].decode("utf-8")
return img, _id
def __iter__(self):
shard_indices = list(range(len(self.shards)))
if self.shuffle:
random.seed(self.seed)
random.shuffle(shard_indices)
worker_info = torch.utils.data.get_worker_info()
if worker_info is not None:
def split_list(alist, splits=1):
length = len(alist)
return [
alist[i * length // splits : (i + 1) * length // splits]
for i in range(splits)
]
shard_indices_split = split_list(shard_indices, worker_info.num_workers)[
worker_info.id
]
else:
shard_indices_split = shard_indices
cache = []
for shard_index in shard_indices_split:
shard = self.shards[shard_index]
with open(
os.path.join(shard["path"], f"shard_{shard['shard_index']}.msg"), "rb"
) as f:
unpacker = msgpack.Unpacker(
f, max_buffer_size=1024 * 1024 * 1024, raw=True
)
for x in unpacker:
if x is None:
continue
if len(cache) < self.cache_size:
cache.append(x)
if len(cache) == self.cache_size:
if self.shuffle:
random.shuffle(cache)
while cache:
yield self._process_sample(cache.pop())
if self.shuffle:
random.shuffle(cache)
while cache:
yield self._process_sample(cache.pop())
if __name__ == "__main__":
args = argparse.ArgumentParser()
args.add_argument("--data", type=str, default="resources/images/mp16")
args = args.parse_args()
tfm = torchvision.transforms.Compose(
[
torchvision.transforms.ToTensor(),
]
)
dataset = MsgPackIterableDataset(path=args.data, transformation=tfm)
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size=1,
num_workers=6,
pin_memory=False,
)
num_images = 0
for x, image_id in dataloader:
if num_images == 0:
print(x.shape, image_id)
num_images +=1
print(f"{num_images=}")