-
Notifications
You must be signed in to change notification settings - Fork 11
/
data_manager.py
200 lines (168 loc) · 7.12 KB
/
data_manager.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
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from PIL import Image
import numpy as np
import pims
import subprocess as sp
import re
import os, sys
import pickle
import pdb
fsize = 128 # 256
#root_dataset = '../dataset-mp4/' # Download from competition url
# root_dataset = '../dataset-sample/'
root_dataset = '/data1/vid_inpaint/Track2/dataset'
'''
data generator used for baseline1
load video clip and randomly choose 2 frames for training
'''
def generate_data(max_samples, batchsize, part): #part = train|dev|test
while 1:
samples = list(range(0, max_samples, batchsize))
#np.random.shuffle(samples)
for i in samples:
X = []
Y = []
#Read a batch of clips from files
j = 0
while len(X) < batchsize:
if part == 'train':
idxs = list(range(25*5))
np.random.shuffle(idxs)
idxs = idxs[:2] # keep only 2 random frames per clip on train mode
else:
idxs = [50, 100] # only evaluate frames 50 and 100 on eval mode
ok = True
path = root_dataset+'/'+part+'/X/X'+str(i+j)+'.mp4'
# pdb.set_trace()
try:
Xj = pims.Video(root_dataset+'/'+part+'/X/X'+str(i+j)+'.mp4')[idxs]
Xj = np.array(Xj, dtype='float32') / 255.
Yj = pims.Video(root_dataset+'/'+part+'/Y/Y'+str(i+j)+'.mp4')[idxs]
Yj = np.array(Yj, dtype='float32') / 255.
except:
print('Error clip number '+ str(i+j) + ' at '+root_dataset+'/train/X/X'+str(i+j)+'.mp4'+ ' OR '+root_dataset+'/train/Y/Y'+str(i+j)+'.mp4')
ok = False
if i+j >= max_samples: j = 0
if ok:
X.append(Xj)
Y.append(Yj)
j = j + 1
# make numpy and reshape
X = np.asarray(X)
X = X.reshape((X.shape[0]*X.shape[1], X.shape[2], X.shape[3], X.shape[4]))
Y = np.asarray(Y)
Y = Y.reshape((Y.shape[0]*Y.shape[1], Y.shape[2], Y.shape[3], Y.shape[4]))
yield (X, Y)
# return all frames from video clip
# returned frames are normalized
def getAllFrames(clipname):
print(clipname)
# open one video clip sample
try:
data = pims.Video(root_dataset+'/'+clipname)
except:
data = pims.Video(clipname)
data = np.array(data, dtype='float32')
length = data.shape[0]
return data[:125] / 255.
# create video clip using 'ffmpeg' command
# clip: input data, supposed normalized (between 0 and 1)
# name: basename of output file
def createVideoClip(clip, folder, name):
#clip = clip * 255.
#clip = clip.astype('uint8')
# write video stream #
command = [ 'ffmpeg',
'-y', # overwrite output file if it exists
'-f', 'rawvideo',
'-s', '128x128', #'256x256', # size of one frame
'-pix_fmt', 'rgb24',
'-r', '25', # frames per second
'-an', # Tells FFMPEG not to expect any audio
'-i', '-', # The input comes from a pipe
'-vcodec', 'libx264',
'-b:v', '100k',
'-vframes', '125', # 5*25
'-s', '128x128', #'256x256', # size of one frame
folder+'/'+name ]
pipe = sp.Popen( command, stdin=sp.PIPE, stderr=sp.PIPE)
out, err = pipe.communicate(clip.tostring())
pipe.wait()
pipe.terminate()
print(err)
################################### baseline2 #################################
# for baseline2, we precompute mini batches.
# don't need a generator since inputs are small dimension (patches)
def build_and_save_batches(max_samples, batchsize): #part = train|dev|test
different_clips_per_batch = 10
number_of_frames_per_clips = 2
samples = list(range(max_samples))
np.random.shuffle(samples)
num_batch = 0
for i in range(0, max_samples, different_clips_per_batch):
X = []
Y = []
#Read a batch of clips from files
j = 0
while len(X) < different_clips_per_batch:
idxs = list(range(25*5))
np.random.shuffle(idxs)
idxs = idxs[:number_of_frames_per_clips] # keep only 2 random frames per clip
print('read clip '+str(samples[i+j])+' at idxs '+str(idxs))
ok = True
try:
Xj = pims.Video(root_dataset+'/train/X/X'+str(samples[i+j])+'.mp4')[idxs]
Xj = np.array(Xj, dtype='float32') / 255.
Yj = pims.Video(root_dataset+'/train/Y/Y'+str(samples[i+j])+'.mp4')[idxs]
Yj = np.array(Yj, dtype='float32') / 255.
except:
print('Error clip number '+ str(samples[i+j]) + ' at '+root_dataset+'/train/X/X'+str(samples[i+j])+'.mp4'+ ' OR '+root_dataset+'/train/Y/Y'+str(samples[i+j])+'.mp4')
ok = False
if ok:
X.append(Xj)
Y.append(Yj)
j = j + 1
# get random non-overlapped patches
X = np.asarray(X)
X = X.reshape((X.shape[0]*X.shape[1], X.shape[2], X.shape[3], X.shape[4]))
X = X.reshape(-1, fsize//32,32,fsize//32,32, 3).swapaxes(2,3).reshape(-1,32,32,3)
Y = np.asarray(Y)
Y = Y.reshape((Y.shape[0]*Y.shape[1], Y.shape[2], Y.shape[3], Y.shape[4]))
Y = Y.reshape(-1, fsize//32,32,fsize//32,32, 3).swapaxes(2,3).reshape(-1,32,32,3)
# compute differnce to look for patches including text
# wrong image comparison, should use opencv or PILLOW, but ok..
Tt = abs(X - Y)
T=np.array([np.max(t) for t in Tt])
T[T>0.2] = 1
T[T<0.2] = 0
# get random positive and negative patches
Tpos_idxs = np.where(T>0)[0]
np.random.shuffle(Tpos_idxs)
Tneg_idxs = np.where(T==0)[0]
np.random.shuffle(Tneg_idxs)
# try to make nbpos = nbneg = batchsize/2
nbpos = int(batchsize/2)
if len(Tpos_idxs) < nbpos: nbpos = len(Tpos_idxs)
# shuffle idxs
patch_idxs = np.concatenate([Tpos_idxs[:nbpos], Tneg_idxs[:int(batchsize-nbpos)]])
np.random.shuffle(patch_idxs)
X = X[patch_idxs]
Y = Y[patch_idxs]
T = T[patch_idxs]
# save in pickle
data = (X,Y,T)
with open('batches/batch_'+str(num_batch)+'.pkl', 'wb') as f:
print('write batch '+str(num_batch))
pickle.dump(data, f)
num_batch = num_batch + 1
# load and return minibatches for training
def load_batches(idxfrom, idxto): # 0, 3500
train_batches = []
for i in range(idxfrom, idxto):
with open('batches/batch_'+str(i)+'.pkl', 'rb') as f:
train_batches.append(pickle.load(f))
return train_batches
if __name__ == "__main__":
if sys.argv[1] == 'build_and_save_batches': build_and_save_batches(40000, 128)