-
Notifications
You must be signed in to change notification settings - Fork 12
/
dataset.py
153 lines (127 loc) · 5.14 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
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
from io import BytesIO
import lmdb
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms
from torch.utils import data
import numpy as np
import os
def data_sampler(dataset, shuffle, distributed):
if distributed:
return data.distributed.DistributedSampler(dataset, shuffle=shuffle)
if shuffle:
return data.RandomSampler(dataset)
else:
return data.SequentialSampler(dataset)
class MultiResolutionDataset(Dataset):
def __init__(self, path, transform, resolution=256):
self.env = lmdb.open(
path,
max_readers=32,
readonly=True,
lock=False,
readahead=False,
meminit=False,
)
if not self.env:
raise IOError('Cannot open lmdb dataset', path)
with self.env.begin(write=False) as txn:
self.length = int(txn.get('length'.encode('utf-8')).decode('utf-8'))
self.resolution = resolution
self.transform = transform
def __len__(self):
return self.length
def __getitem__(self, index):
with self.env.begin(write=False) as txn:
key = f'{self.resolution}-{str(index).zfill(5)}'.encode('utf-8')
img_bytes = txn.get(key)
buffer = BytesIO(img_bytes)
img = Image.open(buffer)
img = self.transform(img)
return img
class MultiResolutionDataset_drum(Dataset):
def __init__(self, path, transform, resolution=None):
self.path_list = []
for file in os.listdir(path):
if file.endswith('.npy') == True:
if file.startswith('std') == False and file.startswith('mean') == False:
self.path_list.append(os.path.join(path, file))
self.resolution = resolution
self.transform = transform
def __len__(self):
return len(self.path_list)
def __getitem__(self, index):
img = np.load(self.path_list[index])
img = self.transform(img)
return img
class MultiResolutionDataset_drum_with_filename(Dataset):
def __init__(self, path, transform, resolution=None):
self.path_list = []
for file in os.listdir(path):
if file.endswith('.npy') == True:
if file.startswith('std') == False and file.startswith('mean') == False:
self.path_list.append(os.path.join(path, file))
self.resolution = resolution
self.transform = transform
def __len__(self):
return len(self.path_list)
def __getitem__(self, index):
img = np.load(self.path_list[index])
img = self.transform(img)
return img, self.path_list[index].split('/')[-1]
class MultiResolutionDataset_drum_with_label(Dataset):
def __init__(self, path, transform, label_dictionary, resolution=None):
self.path_list = []
for file in os.listdir(path):
if file.endswith('.npy') == True:
if file.startswith('std') == False and file.startswith('mean') == False:
self.path_list.append(os.path.join(path, file))
self.resolution = resolution
self.transform = transform
class MultiResolutionDataset_drum_with_label(Dataset):
def __init__(self, path, transform, label_dictionary, resolution=None):
self.path_list = []
for file in os.listdir(path):
if file.endswith('.npy') == True:
if file.startswith('std') == False and file.startswith('mean') == False:
self.path_list.append(os.path.join(path, file))
self.resolution = resolution
self.transform = transform
## read label dictionary
import pickle as pickle
with open(label_dictionary, 'rb') as f:
self.label_dictionary = pickle.load(f)
## genre to int dictionary
self.genre_to_int = {}
count = 0
for _, genre in self.label_dictionary.items():
if self.genre_to_int.get(genre) == None:
self.genre_to_int[genre] = count
count += 1
def __len__(self):
return len(self.path_list)
def __getitem__(self, index):
img = np.load(self.path_list[index])
img = self.transform(img)
file_name = self.path_list[index].split('/')[-1]
label = self.genre_to_int[self.label_dictionary[file_name]]
return img, label
if __name__ == '__main__':
transform = transforms.Compose(
[
#transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
#transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True),
]
)
path = '/home/allenhung/nas189/home/style-based-gan-drum/training_data_one_bar_all/mel_80_320_genre_more_than_600'
label_dictionary = '/home/allenhung/nas189/home/style-based-gan-drum/training_data_one_bar_all/dict_one_bar_more_than_600.pickle'
dataset = MultiResolutionDataset_drum_with_label(path, transform, label_dictionary)
loader = data.DataLoader(
dataset,
batch_size=2,
sampler=data_sampler(dataset, shuffle=True, distributed=False),
drop_last=True,
)
for data in loader:
import pdb; pdb.set_trace()