-
Notifications
You must be signed in to change notification settings - Fork 251
/
motion_dataloader.py
189 lines (152 loc) · 6.11 KB
/
motion_dataloader.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
import numpy as np
import pickle
from PIL import Image
import time
import shutil
import random
import argparse
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
import torchvision.models as models
import torch.nn as nn
import torch
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
from torch.optim.lr_scheduler import ReduceLROnPlateau
from split_train_test_video import *
class motion_dataset(Dataset):
def __init__(self, dic, in_channel, root_dir, mode, transform=None):
#Generate a 16 Frame clip
self.keys=dic.keys()
self.values=dic.values()
self.root_dir = root_dir
self.transform = transform
self.mode=mode
self.in_channel = in_channel
self.img_rows=224
self.img_cols=224
def stackopf(self):
name = 'v_'+self.video
u = self.root_dir+ 'u/' + name
v = self.root_dir+ 'v/'+ name
flow = torch.FloatTensor(2*self.in_channel,self.img_rows,self.img_cols)
i = int(self.clips_idx)
for j in range(self.in_channel):
idx = i + j
idx = str(idx)
frame_idx = 'frame'+ idx.zfill(6)
h_image = u +'/' + frame_idx +'.jpg'
v_image = v +'/' + frame_idx +'.jpg'
imgH=(Image.open(h_image))
imgV=(Image.open(v_image))
H = self.transform(imgH)
V = self.transform(imgV)
flow[2*(j-1),:,:] = H
flow[2*(j-1)+1,:,:] = V
imgH.close()
imgV.close()
return flow
def __len__(self):
return len(self.keys)
def __getitem__(self, idx):
#print ('mode:',self.mode,'calling Dataset:__getitem__ @ idx=%d'%idx)
if self.mode == 'train':
self.video, nb_clips = self.keys[idx].split('-')
self.clips_idx = random.randint(1,int(nb_clips))
elif self.mode == 'val':
self.video,self.clips_idx = self.keys[idx].split('-')
else:
raise ValueError('There are only train and val mode')
label = self.values[idx]
label = int(label)-1
data = self.stackopf()
if self.mode == 'train':
sample = (data,label)
elif self.mode == 'val':
sample = (self.video,data,label)
else:
raise ValueError('There are only train and val mode')
return sample
class Motion_DataLoader():
def __init__(self, BATCH_SIZE, num_workers, in_channel, path, ucf_list, ucf_split):
self.BATCH_SIZE=BATCH_SIZE
self.num_workers = num_workers
self.frame_count={}
self.in_channel = in_channel
self.data_path=path
# split the training and testing videos
splitter = UCF101_splitter(path=ucf_list,split=ucf_split)
self.train_video, self.test_video = splitter.split_video()
def load_frame_count(self):
#print '==> Loading frame number of each video'
with open('dic/frame_count.pickle','rb') as file:
dic_frame = pickle.load(file)
file.close()
for line in dic_frame :
videoname = line.split('_',1)[1].split('.',1)[0]
n,g = videoname.split('_',1)
if n == 'HandStandPushups':
videoname = 'HandstandPushups_'+ g
self.frame_count[videoname]=dic_frame[line]
def run(self):
self.load_frame_count()
self.get_training_dic()
self.val_sample19()
train_loader = self.train()
val_loader = self.val()
return train_loader, val_loader, self.test_video
def val_sample19(self):
self.dic_test_idx = {}
#print len(self.test_video)
for video in self.test_video:
n,g = video.split('_',1)
sampling_interval = int((self.frame_count[video]-10+1)/19)
for index in range(19):
clip_idx = index*sampling_interval
key = video + '-' + str(clip_idx+1)
self.dic_test_idx[key] = self.test_video[video]
def get_training_dic(self):
self.dic_video_train={}
for video in self.train_video:
nb_clips = self.frame_count[video]-10+1
key = video +'-' + str(nb_clips)
self.dic_video_train[key] = self.train_video[video]
def train(self):
training_set = motion_dataset(dic=self.dic_video_train, in_channel=self.in_channel, root_dir=self.data_path,
mode='train',
transform = transforms.Compose([
transforms.Scale([224,224]),
transforms.ToTensor(),
]))
print '==> Training data :',len(training_set),' videos',training_set[1][0].size()
train_loader = DataLoader(
dataset=training_set,
batch_size=self.BATCH_SIZE,
shuffle=True,
num_workers=self.num_workers,
pin_memory=True
)
return train_loader
def val(self):
validation_set = motion_dataset(dic= self.dic_test_idx, in_channel=self.in_channel, root_dir=self.data_path ,
mode ='val',
transform = transforms.Compose([
transforms.Scale([224,224]),
transforms.ToTensor(),
]))
print '==> Validation data :',len(validation_set),' frames',validation_set[1][1].size()
#print validation_set[1]
val_loader = DataLoader(
dataset=validation_set,
batch_size=self.BATCH_SIZE,
shuffle=False,
num_workers=self.num_workers)
return val_loader
if __name__ == '__main__':
data_loader =Motion_DataLoader(BATCH_SIZE=1,num_workers=1,in_channel=10,
path='/home/ubuntu/data/UCF101/tvl1_flow/',
ucf_list='/home/ubuntu/cvlab/pytorch/ucf101_two_stream/github/UCF_list/',
ucf_split='01'
)
train_loader,val_loader,test_video = data_loader.run()
#print train_loader,val_loader