-
Notifications
You must be signed in to change notification settings - Fork 9
/
datasets.py
268 lines (203 loc) · 8.73 KB
/
datasets.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from ntu_rgb import NTU
from feature_manager import FeatureManager
import numpy as np
import scipy
import itertools
import cv2
import torch
from torch.utils.data import Dataset
import torchvision.transforms as transforms
from config import *
vox_size=54
all_tups = np.array(list(itertools.product(range(vox_size), repeat=2)))
rot_array = np.arange(vox_size*vox_size).reshape([vox_size,vox_size])
class NTURGBDataset(Dataset):
'''
NTURGB dataset - inherited from pytorch Datset class
...
Attributes
----------
op_flow : bool
Whether to return the 3D optical flow
op_flow_2D : bool
Whether to return the 2D optical flow
images : bool
Whether to return the images
images_3D : bool
Whether to return the 3D images
single_feature : bool
Whether to return a single feature per video or multiple
test : bool
Return test set videos
validation : bool
Return validation set videos
full_train : bool
No validation set - all training data is used
cross_view : bool
Whether to return the cross-view split instead of cross-subject
augmentation : bool
Whether to perform data augmentation
'''
def __init__(self, test=False, validation=False, full_train=False):
# Underlying dataset and features
dataset = NTU()
self.id_to_action = dataset.id_to_action.copy()
# What to return
self.images = DATA_IMAGES
self.images_3D = DATA_IMAGES_3D
self.op_flow = DATA_OP_FLOW
self.op_flow_2D = DATA_OP_FLOW_2D
self.single_feature = DATA_SINGLE_FEAT
self.augmentation = DATA_AUGMENTATION
# Train, validation, test split
self.train = (test == False) and (validation == False)
if DATA_CROSS_VIEW == False:
if test: self.vid_ids = dataset.test_split_subject.copy()
elif validation: self.vid_ids = dataset.validation_split_subject.copy()
elif full_train: self.vid_ids = dataset.train_split_subject.copy()
else: self.vid_ids = dataset.train_split_subject_with_validation.copy()
else:
if test: self.vid_ids = dataset.test_split_camera.copy()
else: self.vid_ids = dataset.train_split_camera.copy()
def __len__(self):
return len(self.vid_ids)
def image_transforms(self, numpy_imgs):
''' Transformations on a list of images
Returns
-------
images : Torch Tensor
Stacked tensor of all images with the transformations applied
'''
# Get random parameters to apply same transformation to all images in list
color_jitter = transforms.ColorJitter.get_params(.25,.25,.25,.25)
rotation_param = transforms.RandomRotation.get_params((-15,15))
crop_params = None
# Apply transformations
images = []
for numpy_img in numpy_imgs:
i = transforms.functional.to_pil_image(numpy_img)
i = transforms.functional.resize(i, (224,224))
if self.train:
i = color_jitter(i)
i = transforms.functional.rotate(i, rotation_param)
i = transforms.functional.to_tensor(i)
i = transforms.functional.normalize(i, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
images.append(i)
return torch.stack(images)
def op_flow_transforms(self, op_flow):
''' Transformations on a tensor of optical flow voxel grids
Parameters
----------
op_flow : ndarray
Returns
-------
op_flow : Torch Tensor
A torch tensor of an optical flow voxel grid with the
transformations (rotation, scale, translation) applied to it
'''
def translate(op_flow):
# op_flow[:,0::3,:,:,:] ---> x axis vectors
# op_flow = scipy.ndimage.interpolation.shift(op_flow, [0,0,x_move,y_move,z_move], cval=0, order=0) # Slower alternative
# Get amount to shift
max_shift = int(op_flow.shape[2] * 0.10)
x_move, y_move, z_move = np.random.randint(-max_shift, max_shift, 3)
# Translate values
if x_move > 0:
op_flow[:,:,x_move:,:,:] = op_flow[:,:,:-x_move,:,:]
op_flow[:,:,:x_move,:,:] = 0
elif x_move < 0:
op_flow[:,:,:x_move,:,:] = op_flow[:,:,-x_move:,:,:]
op_flow[:,:,x_move:,:,:] = 0
if y_move > 0:
op_flow[:,:,:,y_move:,:] = op_flow[:,:,:,:-y_move,:]
op_flow[:,:,:,:y_move,:] = 0
elif y_move < 0:
op_flow[:,:,:,:y_move,:] = op_flow[:,:,:,-y_move:,:]
op_flow[:,:,:,y_move:,:] = 0
if z_move > 0:
op_flow[:,:,:,:,z_move:] = op_flow[:,:,:,:,:-z_move]
op_flow[:,:,:,:,:z_move] = 0
elif z_move < 0:
op_flow[:,:,:,:,:z_move] = op_flow[:,:,:,:,-z_move:]
op_flow[:,:,:,:,z_move:] = 0
return op_flow
def rotate(op_flow):
''' Rotate an optical flow tensor a random amount about the y axis '''
# Get angle
angle = np.random.randint(-45, 45)
# Rotate positions - Lawrence's algorithm
rot_mat = scipy.ndimage.interpolation.rotate(rot_array, angle, (0,1), reshape=False, order=0)
op_flow_new = np.zeros(op_flow.shape, dtype=np.float32)
tup = all_tups[rot_mat]
op_flow_new = op_flow[:,:,tup[:, :, 0],:,tup[:, :, 1]].transpose(2,3,0,4,1)
# Rotate flow vectors
cos = np.cos(np.radians(-angle))
sin = np.sin(np.radians(-angle))
x_copy = op_flow_new[:,0].copy()
z_copy = op_flow_new[:,2].copy()
op_flow_new[:,0] = x_copy * cos + z_copy * sin
op_flow_new[:,2] = x_copy * -sin + z_copy * cos
return op_flow_new
def scale(op_flow):
return op_flow
if self.train:
op_flow = translate(op_flow)
# op_flow = rotate(op_flow)
# op_flow = scale(op_flow)
return op_flow
def __getitem__(self, idx):
to_return = []
vid_id = self.vid_ids[idx]
# Images
if self.images:
images = np.load('{}/{:05}.npy'.format(CACHE_2D_IMAGES, vid_id))
images = self.image_transforms(images)
to_return.append(images)
# 3D images
if self.images_3D:
feat_nonzero = np.load("{}/{:05}.nonzeros.npy".format(CACHE_3D_IMAGES, vid_id))
images_3D = np.zeros([5, 108, 108, 108], np.float32)
images_3D[tuple(feat_nonzero)] = 1
to_return.append(images_3D)
# Optical flow 3D
if self.op_flow:
feat_values = np.load("{}/{:05}.npy".format(CACHE_FEATURES_VOX_FLOW, vid_id))
feat_nonzero = np.load("{}/{:05}.nonzeros.npy".format(CACHE_FEATURES_VOX_FLOW, vid_id))
feat_shape = np.load("{}/{:05}.shape.npy".format(CACHE_FEATURES_VOX_FLOW, vid_id))
op_flow = np.zeros(feat_shape, np.float32)
op_flow[tuple(feat_nonzero)] = feat_values
if self.augmentation:
op_flow = self.op_flow_transforms(op_flow)
to_return.append(op_flow)
# Optical flow 2D
if self.op_flow_2D:
op_flow_2D = np.load('/hdd/Datasets/NTU/nturgb+d_op_flow_2D_small/{:05}.npy'.format(vid_id))
to_return.append(op_flow_2D)
# Label
y = self.id_to_action[vid_id]
to_return.append(y)
return to_return
def get_train_valid_loader():
# Create the dataset
train_dataset = NTURGBDataset()
valid_dataset = NTURGBDataset(validation=True)
# Seed the shuffler
np.random.seed(149)
torch.manual_seed(149)
train_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=DATA_BATCH_SIZE, shuffle=True,
num_workers=NUM_WORKERS, pin_memory=True)
valid_loader = torch.utils.data.DataLoader(valid_dataset,
batch_size=DATA_BATCH_SIZE, shuffle=True,
num_workers=NUM_WORKERS, pin_memory=True)
return (train_loader, valid_loader)
def get_train_loader():
dataset = NTURGBDataset(full_train=True)
return torch.utils.data.DataLoader(dataset, batch_size=DATA_BATCH_SIZE,
shuffle=True, num_workers=NUM_WORKERS,
pin_memory=True)
def get_test_loader():
dataset = NTURGBDataset(test=True)
return torch.utils.data.DataLoader(dataset, batch_size=DATA_BATCH_SIZE,
shuffle=False, num_workers=NUM_WORKERS,
pin_memory=True)