-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmsr.py
79 lines (64 loc) · 2.83 KB
/
msr.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
import os
import sys
import numpy as np
from torch.utils.data import Dataset
class MSRAction3D(Dataset):
def __init__(self, root, frames_per_clip=16, frame_interval=1, num_points=2048, train=True):
super(MSRAction3D, self).__init__()
self.videos = []
self.labels = []
self.index_map = []
index = 0
for video_name in os.listdir(root):
if train and (int(video_name.split('_')[1].split('s')[1]) <= 5):
video = np.load(os.path.join(root, video_name), allow_pickle=True)['point_clouds']
self.videos.append(video)
label = int(video_name.split('_')[0][1:])-1
self.labels.append(label)
nframes = video.shape[0]
for t in range(0, nframes-frame_interval*(frames_per_clip-1)):
self.index_map.append((index, t))
index += 1
if not train and (int(video_name.split('_')[1].split('s')[1]) > 5):
video = np.load(os.path.join(root, video_name), allow_pickle=True)['point_clouds']
self.videos.append(video)
label = int(video_name.split('_')[0][1:])-1
self.labels.append(label)
nframes = video.shape[0]
for t in range(0, nframes-frame_interval*(frames_per_clip-1)):
self.index_map.append((index, t))
index += 1
self.frames_per_clip = frames_per_clip
self.frame_interval = frame_interval
self.num_points = num_points
self.train = train
self.num_classes = max(self.labels) + 1
def __len__(self):
return len(self.index_map)
def __getitem__(self, idx):
index, t = self.index_map[idx]
video = self.videos[index]
label = self.labels[index]
clip = [video[t+i*self.frame_interval] for i in range(self.frames_per_clip)]
for i, p in enumerate(clip):
if p.shape[0] > self.num_points:
r = np.random.choice(p.shape[0], size=self.num_points, replace=False)
else:
repeat, residue = self.num_points // p.shape[0], self.num_points % p.shape[0]
r = np.random.choice(p.shape[0], size=residue, replace=False)
r = np.concatenate([np.arange(p.shape[0]) for _ in range(repeat)] + [r], axis=0)
clip[i] = p[r, :]
clip = np.array(clip)
if self.train:
# scale the points
scales = np.random.uniform(0.9, 1.1, size=3)
clip = clip * scales
clip = clip / 300
return clip.astype(np.float32), label, index
if __name__ == '__main__':
dataset = MSRAction(root='../data/msr_action', frames_per_clip=16)
clip, label, video_idx = dataset[0]
print(clip)
print(label)
print(video_idx)
print(dataset.num_classes)