-
Notifications
You must be signed in to change notification settings - Fork 4
/
traindata.py
247 lines (204 loc) · 8.3 KB
/
traindata.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# http://learnandshare645.blogspot.in/2016/06/3d-cnn-in-keras-action-recognition.html
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, load_model, Model
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D, Conv3D
from keras.layers import Input, BatchNormalization
from keras.callbacks import ModelCheckpoint
import keras
from numpy.testing import assert_allclose
from keras.optimizers import SGD, RMSprop
from keras.utils import np_utils, generic_utils
from keras.layers.convolutional import Convolution3D
import matplotlib
import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split
from sklearn import cross_validation
from sklearn import preprocessing
import os
import cv2
import numpy as np
import random
from keras import backend as K
from imgaug import augmenters as iaa
import imgaug as ia
#image specification
img_rows,img_cols,img_depth = 128,128,5 #using only Y channel of YCrCb
def show(img):
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def returnpred(i):
out=X_train[i]
out=np.expand_dims(out,axis=0)
out=np.expand_dims(out,axis=0)
ypred=model.predict(out)
ypred=ypred.reshape((128,128,3))
show(ypred)
show(y_train[i].reshape((128,128,3)))
def customLoss(yTrue, yPred):
val = K.sum(K.sum(K.sum(K.sum(K.sum(K.square(yTrue-yPred))))))
return val
def returnlist(N):
l=os.listdir("blurred_sharp/blurred/")
b=[int(x.split('.')[0]) for x in l]
b.sort(key=int)
l=[str(x)+".png" for x in b]
return l[:N]
def createdata():
aug = iaa.CropAndPad(px=((-300,0), (-300,0), (-300, 0), (-300, 0)), pad_mode=ia.ALL, pad_cval=(0,128), keep_size=False)
l=returnlist(1151)
x_tr=[]
y_tr=[]
for i in range(len(l)-5):
frames=[]
for item in l[i:i+5]:
imgYCC = cv2.cvtColor(cv2.imread("blurred_sharp/blurred/"+item), cv2.COLOR_BGR2YCR_CB)
frames.append(imgYCC[:,:,0]) #append the Y component
outY = cv2.cvtColor(cv2.imread("blurred_sharp/sharp/"+l[i+2]), cv2.COLOR_BGR2YCR_CB)[:,:,0]
frames = np.array(frames)
print frames.shape
frames=np.rollaxis(frames,0,3)
y_tr.append(outY)
x_tr.append(frames)
print i
images_augX=x_tr[:]
images_augY=y_tr[:]
seq_det = aug.to_deterministic()
x_tr = seq_det.augment_images(x_tr)
y_tr = seq_det.augment_images(y_tr)
for i in range(len(images_augX)):
images_augY[i] = cv2.resize(y_tr[i],(128,128))
images_augX[i] = cv2.resize(x_tr[i],(128,128))
print i
return images_augX, images_augY
def generator(batch_size):
aug = iaa.CropAndPad(px=((-100,0), (-50,0), (-100, 0), (-50, 0)), pad_mode=ia.ALL, pad_cval=(0,128), keep_size=False)
l=os.listdir("blurred_sharp/blurred/")
l=random.sample(l,batch_size)
x_tr=[]
y_tr=[]
while True:
for i in range(len(l)-5):
frames=[]
for item in l[i:i+5]:
imgRGB = cv2.imread("blurred_sharp/blurred/"+item)
frames.append(imgRGB)
#imgYCC = cv2.cvtColor(cv2.imread("blurred_sharp/blurred/"+item), cv2.COLOR_BGR2YCR_CB)
#frames.append(imgYCC[:,:,0]) #append the Y component
#outY = cv2.cvtColor(cv2.imread("blurred_sharp/sharp/"+l[i+2]), cv2.COLOR_BGR2YCR_CB)[:,:,0]
#outY = cv2.resize(outY,(128,128))
frames=np.array(frames)
frames=frames.reshape((720,720,15))
out = cv2.imread("blurred_sharp/sharp/"+l[i+2])
out=cv2.resize(out,(128,128))
y_tr.append(out)
x_tr.append(frames)
print i
images_aug=aug.augment_images(x_tr)
for i in range(len(images_aug)):
images_aug[i]=cv2.resize(images_aug[i],(128,128))
images_aug=np.expand_dims(np.array(images_aug),1)
yield images_aug, np.array(y_tr)
def spatempblock(inputshape):
l1 = Conv3D(64, (3,3,1), padding="same")(inputshape)
bn1 = BatchNormalization()(l1)
ac1 = Activation('relu')(bn1)
l2 = Conv3D(64, (3,3,1), padding="same")(ac1)
bn2 = BatchNormalization()(l2)
return bn2
X_train,y_train=createdata()
np.save("x_train.npy",X_train)
np.save("y_trin.npy",y_train)
exit(0)
#X_train=np.load("x_train.npy")
#y_train=np.load("y_trin.npy")
X_train=np.array(X_train)
y_train=np.array(y_train)
print y_train.shape
#X_train=np.array(X_train)
num_samples=len(X_train)
input_shape=(img_rows,img_cols,img_depth)
print X_train.shape
X_train = X_train/255.0
y_train = y_train/255.0
out = np.zeros((num_samples,1,img_rows,img_cols,img_depth))
yt = np.zeros((num_samples,1,img_rows,img_cols,1))
for h in xrange(num_samples):
out[h,0,:,:,:]=X_train[h,:,:,:]
yt[h,0,:,:,0]=y_train[h,:,:]
print out.shape
print yt.shape
#X_train=np.swapaxes(X_train, 2,3)
#print X_train.shape
#X_train=np.swapaxes(X_train, 1, 2)
input_img = Input(shape=(1,img_rows,img_cols,img_depth))
l1 = Conv3D(32, (3,3,3), input_shape=(1, img_rows, img_cols, img_depth),padding="same", activation='relu',data_format="channels_last")(input_img)
l2 = Conv3D(64, (3,3,3), activation='relu', padding="same",data_format="channels_last" )(l1)
bn4 = spatempblock(l2)
out1 = keras.layers.add([l2, bn4])
bn6 = spatempblock(out1)
out2 = keras.layers.add([out1, bn6])
bn8 = spatempblock(out2)
out3 = keras.layers.add([out2, bn8])
bn10 = spatempblock(out3)
out4 = keras.layers.add([out3, bn10])
bn12 = spatempblock(out4)
out5 = keras.layers.add([out4, bn12])
bn14 = spatempblock(out5)
out6 = keras.layers.add([out5, bn14])
bn16 = spatempblock(out6)
out7 = keras.layers.add([out6, bn16])
bn18 = spatempblock(out7)
out8 = keras.layers.add([out7, bn18])
bn20 = spatempblock(out8)
out9 = keras.layers.add([out8, bn20])
bn22 = spatempblock(out9)
out10 = keras.layers.add([out9, bn22])
bn24 = spatempblock(out10)
out11 = keras.layers.add([out10, bn24])
bn26 = spatempblock(out11)
out12 = keras.layers.add([out11, bn26])
bn28 = spatempblock(out12)
out13 = keras.layers.add([out12, bn28])
bn30 = spatempblock(out13)
out14 = keras.layers.add([out13, bn30])
bn32 = spatempblock(out14)
out15 = keras.layers.add([out14, bn32])
out15 = keras.layers.add([out15, l2])
l33 = Conv3D(256, (3,3,1), activation='relu', padding="same")(out15)
l34 = Conv3D(256, (3,3,1), activation='relu', padding="same")(l33)
l35 = Conv3D(1, (3,3,1), padding="same")(l34)
model = Model(inputs=input_img, outputs=l35)
model.compile(optimizer='rmsprop', loss=customLoss)
model.summary()
model.fit(out, yt, batch_size=5, epochs=10)
show(X_train[0,:,:,0])
show(X_train[1,:,:,1])
show(X_train[0,:,:,2])
#filepath = "model.h5"
#checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
#callbacks_list = [checkpoint]
#model.fit_generator(generator(1), samples_per_epoch=50, epochs=10, shuffle=True, verbose=True, callbacks=callbacks_list)
"""
model = Sequential()
model.add(Convolution3D(32, kernel_dim1=3, kernel_dim2=3, kernel_dim3=3,
input_shape=(1, img_rows, img_cols, img_depth),padding="same", activation='relu',data_format="channels_last"))
model.add(Convolution3D(64, kernel_dim1=3, kernel_dim2=3, kernel_dim3=3, activation='relu', padding="same", data_format="channels_last"))
model.add(Convolution3D(64, kernel_dim1=3, kernel_dim2=3, kernel_dim3=3, activation='relu', padding="same", data_format="channels_last"))
model.add(Convolution3D(64, kernel_dim1=3, kernel_dim2=3, kernel_dim3=3, activation='relu', padding="same", data_format="channels_last"))
model.add(Convolution3D(64, kernel_dim1=3, kernel_dim2=3, kernel_dim3=3, activation='relu', padding="same", data_format="channels_last"))
model.add(Convolution3D(3, kernel_dim1=3, kernel_dim2=3, kernel_dim3=3, activation='relu', padding="same", data_format="channels_last"))
model.add(Flatten())
"""
#model.compile(loss="mean_squared_error", optimizer="sgd", metrics=['accuracy'])
#filepath = "model.h5"
#checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
#callbacks_list = [checkpoint]
#print y_train[0]
#model.fit_generator(generator(1), samples_per_epoch=50, epochs=10, shuffle=True, verbose=True, callbacks=callbacks_list)
#new_model = load_model("model.h5")
"""assert_allclose(model.predict(X_train),
new_model.predict(X_train),
1e-5)"""
#returnpred(0) #display output