Skip to content

Commit

Permalink
Uploaded models and simple test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jannemus authored Jan 10, 2019
1 parent 7fe8314 commit 1c4619e
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 0 deletions.
151 changes: 151 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
from keras.models import Model
from keras.layers import Input, concatenate, Conv2D, Conv2DTranspose, MaxPooling2D, ZeroPadding2D
from keras import backend as keras

class modelsClass(object):

def __init__(self, img_rows = 272, img_cols = 480):

self.img_rows = img_rows
self.img_cols = img_cols

def addPadding(self, layer, level): #height, width, level):

w1, h1 = self.img_cols, self.img_rows
w2, h2 = int(w1/2), int(h1/2)
w3, h3 = int(w2/2), int(h2/2)
w4, h4 = int(w3/2), int(h3/2)
h = [h1,h2,h3,h4]
w = [w1,w2,w3,w4]

# Target width and height
tw = w[level-1]
th = h[level-1]

# Source width and height
lsize = keras.int_shape(layer)
sh = lsize[1]
sw = lsize[2]

pw = (0, tw - sw)
ph = (0, th - sh)

layer = ZeroPadding2D(padding=(ph,pw),data_format="channels_last")(layer)

return layer

def getDeepGyro(self):

input_blurred = Input((self.img_rows, self.img_cols,3))
input_blurx = Input((self.img_rows, self.img_cols,1))
input_blury = Input((self.img_rows, self.img_cols,1))

inputs = concatenate([input_blurred,input_blurx,input_blury])

conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(conv5)

up6 = Conv2DTranspose(512, (2, 2), strides=(2, 2), padding='same')(conv5)
up6 = self.addPadding(up6,level=4)
up6 = concatenate([up6,conv4], axis=3)
conv6 = Conv2D(512, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv6)

up7 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv6)
up7 = self.addPadding(up7,level=3)
up7 = concatenate([up7,conv3], axis=3)
conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv7)

up8 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv7)
up8 = self.addPadding(up8,level=2)
up8 = concatenate([up8,conv2], axis=3)
conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv8)

up9 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv8)
up9 = self.addPadding(up9,level=1)
up9 = concatenate([up9,conv1], axis=3)
conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv9)

conv10 = Conv2D(3, (1, 1), activation='linear')(conv9)

model = Model(inputs=[input_blurred,input_blurx,input_blury], outputs=conv10)

#adam = optimizers.Adam(lr=0.00005, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
#model.compile(optimizer = adam, loss = 'mean_squared_error')

return model

def getDeepBlind(self):

input_blurred = Input((self.img_rows, self.img_cols,3))

conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(input_blurred)
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same')(conv5)

up6 = Conv2DTranspose(512, (2, 2), strides=(2, 2), padding='same')(conv5)
up6 = self.addPadding(up6,level=4)
up6 = concatenate([up6,conv4], axis=3)
conv6 = Conv2D(512, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv6)

up7 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv6)
up7 = self.addPadding(up7,level=3)
up7 = concatenate([up7,conv3], axis=3)
conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv7)

up8 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv7)
up8 = self.addPadding(up8,level=2)
up8 = concatenate([up8,conv2], axis=3)
conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv8)

up9 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv8)
up9 = self.addPadding(up9,level=1)
up9 = concatenate([up9,conv1], axis=3)
conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv9)

conv10 = Conv2D(3, (1, 1), activation='linear')(conv9)

model = Model(inputs=input_blurred, outputs=conv10)

#adam = optimizers.Adam(lr=0.0000125, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
#model.compile(optimizer = adam, loss = 'mean_squared_error')

return model
44 changes: 44 additions & 0 deletions test_deepblind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os, os.path, errno
from PIL import Image
import numpy as np
from keras.models import Model
from keras.preprocessing.image import array_to_img
from models import modelsClass

# Put your data to the input folder
inpath = "input"
outpath = "output/DeepBlind"

try:
os.makedirs(outpath)
except OSError as e:
if e.errno != errno.EEXIST:
raise

blurred_path = inpath + "/blurred/blurred/"
blurred_names = os.listdir(blurred_path)

num_images = len(blurred_names)
print("Found %d inputs" %(num_images))

for i in range(0, num_images):

print("Deblurring %s with DeepBlind" %(blurred_names[i]))

path_blurred = blurred_path + blurred_names[i]
blurred_img = Image.open(path_blurred)
blurred_np = (1./255)*np.array(blurred_img)

width, height = blurred_img.size
models = modelsClass(height,width)
model = models.getDeepBlind()
model.load_weights("checkpoints/DeepBlind.hdf5")

x = np.reshape(blurred_np,[1,height,width,3])
prediction = model.predict(x, batch_size=1,verbose=0,steps=None)
prediction = prediction[0,:,:,:]

deblurred_img = array_to_img(prediction)
deblurred_img.save(outpath+"/%s"%(blurred_names[i]))

print("DONE!")
61 changes: 61 additions & 0 deletions test_deepgyro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os, os.path, errno
from PIL import Image
import numpy as np
from keras.models import Model
from keras.preprocessing.image import array_to_img
from models import modelsClass

# Put your data to the input folder
inpath = "input"
outpath = "output/DeepGyro"

try:
os.makedirs(outpath)
except OSError as e:
if e.errno != errno.EEXIST:
raise

blurred_path = inpath + "/blurred/blurred/"
blurx_path = inpath + "/blurx/blurx/"
blury_path = inpath + "/blury/blury/"

blurred_names = os.listdir(blurred_path)
blurx_names = os.listdir(blurx_path)
blury_names = os.listdir(blury_path)

num_images = len(blurred_names)
print("Found %d inputs" %(num_images))

for i in range(0, num_images):

print("Deblurring %s with DeepGyro" %(blurred_names[i]))

path_blurred = blurred_path + blurred_names[i]
path_blurx = blurx_path + blurx_names[i]
path_blury = blury_path + blury_names[i]

blurred_img = Image.open(path_blurred)
blurx_img = Image.open(path_blurx)
blury_img = Image.open(path_blury)

blurred_np = (1./255)*np.array(blurred_img)
blurx_np = (1./255)*np.array(blurx_img)
blury_np = (1./255)*np.array(blury_img)

width, height = blurred_img.size
models = modelsClass(height,width)
model = models.getDeepGyro()
model.load_weights("checkpoints/DeepGyro.hdf5")

b = np.reshape(blurred_np,[1,height,width,3])
bx = np.reshape(blurx_np,[1,height,width,1])
by = np.reshape(blury_np,[1,height,width,1])
x = [b,bx,by]

prediction = model.predict(x, batch_size=1,verbose=0,steps=None)
prediction = prediction[0,:,:,:]

deblurred_img = array_to_img(prediction)
deblurred_img.save(outpath+"/%s"%(blurred_names[i]))

print("DONE!")

0 comments on commit 1c4619e

Please sign in to comment.