-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdataset.py
executable file
·78 lines (72 loc) · 3.24 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
import torch
from torch.utils.data import Dataset
import numpy as np
import os
import random
class Normal_Loader(Dataset):
"""
is_train = 1 <- train, 0 <- test
"""
def __init__(self, is_train=1, path='/workspace/DATA/UCF-Crime/'):
super(Normal_Loader, self).__init__()
self.is_train = is_train
self.path = path
if self.is_train == 1:
data_list = os.path.join(path, 'train_normal.txt')
with open(data_list, 'r') as f:
self.data_list = f.readlines()
else:
data_list = os.path.join(path, 'test_normalv2.txt')
with open(data_list, 'r') as f:
self.data_list = f.readlines()
random.shuffle(self.data_list)
self.data_list = self.data_list[:-10]
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
if self.is_train == 1:
rgb_npy = np.load(os.path.join(self.path+'all_rgbs', self.data_list[idx][:-1]+'.npy'))
flow_npy = np.load(os.path.join(self.path+'all_flows', self.data_list[idx][:-1]+'.npy'))
concat_npy = np.concatenate([rgb_npy, flow_npy], axis=1)
return concat_npy
else:
name, frames, gts = self.data_list[idx].split(' ')[0], int(self.data_list[idx].split(' ')[1]), int(self.data_list[idx].split(' ')[2][:-1])
rgb_npy = np.load(os.path.join(self.path+'all_rgbs', name + '.npy'))
flow_npy = np.load(os.path.join(self.path+'all_flows', name + '.npy'))
concat_npy = np.concatenate([rgb_npy, flow_npy], axis=1)
return concat_npy, gts, frames
class Anomaly_Loader(Dataset):
"""
is_train = 1 <- train, 0 <- test
"""
def __init__(self, is_train=1, path='/workspace/DATA/UCF-Crime/'):
super(Anomaly_Loader, self).__init__()
self.is_train = is_train
self.path = path
if self.is_train == 1:
data_list = os.path.join(path, 'train_anomaly.txt')
with open(data_list, 'r') as f:
self.data_list = f.readlines()
else:
data_list = os.path.join(path, 'test_anomalyv2.txt')
with open(data_list, 'r') as f:
self.data_list = f.readlines()
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
if self.is_train == 1:
rgb_npy = np.load(os.path.join(self.path+'all_rgbs', self.data_list[idx][:-1]+'.npy'))
flow_npy = np.load(os.path.join(self.path+'all_flows', self.data_list[idx][:-1]+'.npy'))
concat_npy = np.concatenate([rgb_npy, flow_npy], axis=1)
return concat_npy
else:
name, frames, gts = self.data_list[idx].split('|')[0], int(self.data_list[idx].split('|')[1]), self.data_list[idx].split('|')[2][1:-2].split(',')
gts = [int(i) for i in gts]
rgb_npy = np.load(os.path.join(self.path+'all_rgbs', name + '.npy'))
flow_npy = np.load(os.path.join(self.path+'all_flows', name + '.npy'))
concat_npy = np.concatenate([rgb_npy, flow_npy], axis=1)
return concat_npy, gts, frames
if __name__ == '__main__':
loader2 = Normal_Loader(is_train=0)
print(len(loader2))
#print(loader[1], loader2[1])