-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_data.py
137 lines (86 loc) · 3.67 KB
/
read_data.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
import requests
import cv2
import os
import tarfile
import numpy as np
import torch
from PIL import Image
class Dashcam_data():
def __init__(self,
dataset='Dashcam', dir='./data/', batch_size=64, frame_size=[112, 112],
train="train", mean_file='mean_file.npy'):
self.dataset = dataset
self.dir = dir
self.batch_size = batch_size
self.frame_size = frame_size
self.train = train
self.mean_file = mean_file
self.dir_structure = {}
self.im_names = []
self.im_pointer = 0
self.batch = []
self.aug_steps = [1]
self.im_pointer = 0
self.paths = []
self.labels = []
self.paths_abnormal =[]
self.labels_abnormal = []
if (self.train == "train"):
self.cat_path = "/hdd/local/sda/mishal/Anticipating-Accidents-master/dataset/videos/training/frames_train"
else:
self.cat_path = "/hdd/local/sda/mishal/Anticipating-Accidents-master/dataset/videos/testing/frames/negative"
for folder in os.listdir(self.cat_path):
path_abnormal = os.path.join(self.cat_path, folder)
self.paths.append(path_abnormal)
self.labels.append(0)
for folder in os.listdir(self.cat_path):
path_normal = os.path.join(self.cat_path, folder)
self.paths.append(path_normal)
self.labels.append(1)
print(len(self.paths))
print(len(self.labels))
self.total_folders=len(self.paths)
self.im_ind = list(range(len(self.paths)))
def get_next_batch(self,batch_size,clip_len):
if (self.im_pointer == 0):
np.random.shuffle(self.im_ind)
self.total_folders = len(self.paths)
self.batch = np.zeros((batch_size, clip_len, self.frame_size[0],
self.frame_size[1], 3))
self.l = np.zeros((batch_size))
for idx in range(batch_size):
images = np.zeros((clip_len, self.frame_size[0],
self.frame_size[1], 3))
video = self.paths[self.im_ind[self.im_pointer]]
label = self.labels[self.im_ind[self.im_pointer]]
path, dirs, files = next(os.walk(video))
paths = [video + "/" + x for x in files]
frames = np.sort(paths)
num_frames = len(frames)
time_index = np.random.randint(num_frames - clip_len)
sequence = frames[time_index:time_index + clip_len]
if (label == 1):
time_index_1 = np.random.randint(clip_len)
time_index_2 = np.random.randint(num_frames-clip_len)
sequence[time_index_1] = frames[time_index_2]
#For fixed location
#sequence[8] = frames[time_index_2]
for file in range(len(sequence)):
img=cv2.imread(sequence[file])/255
img = cv2.resize(img,(self.frame_size[0], self.frame_size[1]))
images[file,:,:,:] = img
self.batch[idx,:,:,:,:] = images
self.l[idx] = label
self.im_pointer+=1
if (self.im_pointer==len(self.paths)):
self.im_pointer = 0
np.random.shuffle(self.im_ind)
self.batch = np.moveaxis(self.batch,4,1)
return torch.from_numpy(self.batch).float(), torch.from_numpy(self.l).long()
if __name__ == '__main__':
dataset = Dashcam_data("val")
im_names = (dataset.total_folders)
tot_batches = int(im_names / 10)
for i in range(tot_batches):
print(i, "out of", tot_batches)
batch,labels = dataset.get_next_batch(10,16)