-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_dataloader_RGB.py
125 lines (94 loc) · 3.88 KB
/
create_dataloader_RGB.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
import torch
import h5py
import numpy as np
import pandas as pd
import pickle
from torchvision import datasets, models, transforms
from torchvision.transforms import ToTensor, Lambda
# device setting
device = 'cuda' if torch.cuda.is_available() else 'cpu'
class RGBDataset(torch.utils.data.Dataset):
'''
__getitem__ return each data, target
data : torch.size([num_frames, RGB channels, image_width, image_height]) # example : torch.Size([20, 3, 224, 224])
target : one hot vector(torch.tensor) which len is len(label_map)
'''
def __init__(self, train, file_path, transform=None, target_transform=None):
super(RGBDataset, self).__init__()
self.f = h5py.File(file_path, 'r')
self.train = train
self.transform = transform
self.target_transform = target_transform
self.train_root = self.f['data/train']
self.test_root = self.f['data/test']
self.train_data_list = []
self.train_label_list = []
self.test_data_list = []
self.test_label_list = []
if train:
for i in self.train_root:
self.train_label_list.extend(
list(self.train_root[f'{i}/labels']))
for j in list(self.train_root[i])[:-1]:
self.train_data_list.append(f'data/train/{i}/{j}')
else:
for i in self.test_root:
self.test_label_list.extend(
list(self.test_root[f'{i}/labels']))
for j in list(self.test_root[i])[:-1]:
self.test_data_list.append(f'data/test/{i}/{j}')
def __getitem__(self, idx):
data_path = self.train_data_list[idx]
data = self.f[data_path]
data = torch.tensor(data).permute(0, 3, 1, 2)
if self.transform:
data = self.transform(data)
label = self.train_label_list[idx]
label = torch.tensor(label, dtype=torch.int64)
if self.target_transform:
label = self.target_transform(label)
return data.to(device), label.to(device)
def __len__(self):
return len(self.train_data_list)
def fill_zero(num, l):
fill_num = str(num).zfill(l)
return fill_num
def create_datasets(
hdf5_file_path,
im_size, # I set 224
transform=None,
target_transform=None):
with open('label_map.pickle', 'rb') as f:
label_map = pickle.load(f)
if transform is None:
transform = transforms.Compose([
transforms.Resize((im_size, im_size)),
# transforms.ToTensor(),#])
# transforms.Normalize(mean,std)
])
if target_transform is None:
target_transform = Lambda(lambda y: torch.zeros(len(
label_map), dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1))
train_ds = RGBDataset(hdf5_file_path, train=True,
transform=transform,
target_transform=target_transform,
)
test_ds = RGBDataset(hdf5_file_path, train=False,
transform=transform,
target_transform=target_transform,
)
return train_ds, test_ds
def create_dataloader(batch_size,
hdf5_file_path,
transform=None,
target_transform=None):
train_d, test_d = create_datasets(
hdf5_file_path=hdf5_file_path,
batch_size=batch_size,
transform=transform,
target_transform=target_transform)
train_dl = torch.utils.data.DataLoader(
train_d, shuffle=True, batch_size=batch_size)
test_dl = torch.utils.data.DataLoader(
test_d, shuffle=False, batch_size=batch_size)
return train_dl, test_dl