-
Notifications
You must be signed in to change notification settings - Fork 77
/
dataset.py
89 lines (74 loc) · 2.88 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
import torch.utils.data as data
import numpy as np
from utils import process_feat
import torch
from torch.utils.data import DataLoader
torch.set_default_tensor_type('torch.FloatTensor')
class Dataset(data.Dataset):
def __init__(self, args, is_normal=True, transform=None, test_mode=False):
self.modality = args.modality
self.is_normal = is_normal
self.dataset = args.dataset
if self.dataset == 'shanghai':
if test_mode:
self.rgb_list_file = 'list/shanghai-i3d-test-10crop.list'
else:
self.rgb_list_file = 'list/shanghai-i3d-train-10crop.list'
else:
if test_mode:
self.rgb_list_file = 'list/ucf-i3d-test.list'
else:
self.rgb_list_file = 'list/ucf-i3d.list'
self.tranform = transform
self.test_mode = test_mode
self._parse_list()
self.num_frame = 0
self.labels = None
def _parse_list(self):
self.list = list(open(self.rgb_list_file))
if self.test_mode is False:
if self.dataset == 'shanghai':
if self.is_normal:
self.list = self.list[63:]
print('normal list for shanghai tech')
print(self.list)
else:
self.list = self.list[:63]
print('abnormal list for shanghai tech')
print(self.list)
elif self.dataset == 'ucf':
if self.is_normal:
self.list = self.list[810:]
print('normal list for ucf')
print(self.list)
else:
self.list = self.list[:810]
print('abnormal list for ucf')
print(self.list)
def __getitem__(self, index):
label = self.get_label() # get video level label 0/1
features = np.load(self.list[index].strip('\n'), allow_pickle=True)
features = np.array(features, dtype=np.float32)
if self.tranform is not None:
features = self.tranform(features)
if self.test_mode:
return features
else:
# process 10-cropped snippet feature
features = features.transpose(1, 0, 2) # [10, B, T, F]
divided_features = []
for feature in features:
feature = process_feat(feature, 32) # divide a video into 32 segments
divided_features.append(feature)
divided_features = np.array(divided_features, dtype=np.float32)
return divided_features, label
def get_label(self):
if self.is_normal:
label = torch.tensor(0.0)
else:
label = torch.tensor(1.0)
return label
def __len__(self):
return len(self.list)
def get_num_frames(self):
return self.num_frame