-
Notifications
You must be signed in to change notification settings - Fork 38
/
dataset.py
74 lines (63 loc) · 2.71 KB
/
dataset.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
import os
import json
import torch
from torch.utils.data import Dataset
import numpy as np
from PIL import Image
import PIL.Image
try:
import pyspng
except ImportError:
pyspng = None
class CustomDataset(Dataset):
def __init__(self, data_dir):
PIL.Image.init()
supported_ext = PIL.Image.EXTENSION.keys() | {'.npy'}
self.images_dir = os.path.join(data_dir, 'images')
self.features_dir = os.path.join(data_dir, 'vae-sd')
# images
self._image_fnames = {
os.path.relpath(os.path.join(root, fname), start=self.images_dir)
for root, _dirs, files in os.walk(self.images_dir) for fname in files
}
self.image_fnames = sorted(
fname for fname in self._image_fnames if self._file_ext(fname) in supported_ext
)
# features
self._feature_fnames = {
os.path.relpath(os.path.join(root, fname), start=self.features_dir)
for root, _dirs, files in os.walk(self.features_dir) for fname in files
}
self.feature_fnames = sorted(
fname for fname in self._feature_fnames if self._file_ext(fname) in supported_ext
)
# labels
fname = 'dataset.json'
with open(os.path.join(self.features_dir, fname), 'rb') as f:
labels = json.load(f)['labels']
labels = dict(labels)
labels = [labels[fname.replace('\\', '/')] for fname in self.feature_fnames]
labels = np.array(labels)
self.labels = labels.astype({1: np.int64, 2: np.float32}[labels.ndim])
def _file_ext(self, fname):
return os.path.splitext(fname)[1].lower()
def __len__(self):
assert len(self.image_fnames) == len(self.feature_fnames), \
"Number of feature files and label files should be same"
return len(self.feature_fnames)
def __getitem__(self, idx):
image_fname = self.image_fnames[idx]
feature_fname = self.feature_fnames[idx]
image_ext = self._file_ext(image_fname)
with open(os.path.join(self.images_dir, image_fname), 'rb') as f:
if image_ext == '.npy':
image = np.load(f)
image = image.reshape(-1, *image.shape[-2:])
elif image_ext == '.png' and pyspng is not None:
image = pyspng.load(f.read())
image = image.reshape(*image.shape[:2], -1).transpose(2, 0, 1)
else:
image = np.array(PIL.Image.open(f))
image = image.reshape(*image.shape[:2], -1).transpose(2, 0, 1)
features = np.load(os.path.join(self.features_dir, feature_fname))
return torch.from_numpy(image), torch.from_numpy(features), torch.tensor(self.labels[idx])