-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmodels.py
180 lines (141 loc) · 7.22 KB
/
models.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import tensorflow as tf
import numpy as np
import cv2
from ops import *
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
class MSCNN(object):
"""Single Image Dehazing via Multi-Scale Convolutional Neural Networks"""
def __init__(self, model_path):
self.model_path = model_path
self.graph_path = model_path+"/tf_graph/"
self.save_path = model_path + "/saved_model/"
self.output_path = model_path + "/results/"
if not os.path.exists(model_path):
os.makedirs(self.graph_path+"train/")
os.makedirs(self.graph_path+"val/")
def _debug_info(self):
variables_names = [[v.name, v.get_shape().as_list()] for v in tf.trainable_variables()]
print "Trainable Variables:"
tot_params = 0
for i in variables_names:
var_params = np.prod(np.array(i[1]))
tot_params += var_params
print i[0], i[1], var_params
print "Total number of Trainable Parameters: ", str(tot_params/1000.0)+"K"
def _coarseNet(self, x):
with tf.variable_scope("coarseNet") as var_scope:
conv1 = Conv_2D(x, output_chan=5, kernel=[11,11], stride=[1,1], padding="SAME", name="Conv1")
pool1 = max_pool(conv1, 2, 2, "max_pool1")
upsample1 = max_unpool(pool1, "upsample1")
conv2 = Conv_2D(upsample1, output_chan=5, kernel=[9,9], stride=[1,1], padding="SAME", name="Conv2")
pool2 = max_pool(conv2, 2, 2, "max_pool2")
upsample2 = max_unpool(pool2, "upsample2")
conv3 = Conv_2D(upsample2, output_chan=10, kernel=[7,7], stride=[1,1], padding="SAME", name="Conv3")
pool3 = max_pool(conv3, 2, 2, "max_pool3")
upsample3 = max_unpool(pool3, "upsample3")
linear = Conv_2D(upsample3, output_chan=1, kernel=[1,1], stride=[1,1], padding="SAME", activation=tf.sigmoid,
add_summary=True, name="linear_comb")
return linear
def _fineNet(self, x, coarseMap):
with tf.variable_scope("fineNet") as var_scope:
conv1 = Conv_2D(x, output_chan=4, kernel=[7,7], stride=[1,1], padding="SAME", name="Conv1")
pool1 = max_pool(conv1, 2, 2, "max_pool1")
upsample1 = max_unpool(pool1, "upsample1")
concat = tf.concat([upsample1, coarseMap], axis=3, name="CorMapConcat")
conv2 = Conv_2D(concat, output_chan=5, kernel=[5,5], stride=[1,1], padding="SAME", name="Conv2")
pool2 = max_pool(conv2, 2, 2, "max_pool2")
upsample2 = max_unpool(pool2, "upsample2")
conv3 = Conv_2D(upsample2, output_chan=10, kernel=[3,3], stride=[1,1], padding="SAME", name="Conv3")
pool3 = max_pool(conv3, 2, 2, "max_pool3")
upsample3 = max_unpool(pool3, "upsample3")
linear = Conv_2D(upsample3, output_chan=1, kernel=[1,1], stride=[1,1], padding="SAME", activation=tf.sigmoid,
add_summary=True, name="linear_comb")
return linear
def build_model(self):
with tf.name_scope("Inputs") as scope:
self.x = tf.placeholder(tf.float32, shape=[None,216,240,3], name="Haze_Image")
self.y = tf.placeholder(tf.float32, shape=[None,216,240,1], name="TMap")
hazy_summ = tf.summary.image("Hazy image", self.x)
map_summ = tf.summary.image("Trans Map", self.y)
with tf.name_scope("Model") as scope:
self.coarseMap = self._coarseNet(self.x)
self.transMap = self._fineNet(self.x, self.coarseMap)
with tf.name_scope("Loss") as scope:
self.coarseLoss = tf.losses.mean_squared_error(self.y, self.coarseMap)
self.fineLoss = tf.losses.mean_squared_error(self.y, self.transMap)
self.coarse_loss_summ = tf.summary.scalar("Coarse Loss", self.coarseLoss)
self.fine_loss_summ = tf.summary.scalar("Fine Loss", self.fineLoss)
with tf.name_scope("Optimizers") as scope:
train_vars = tf.trainable_variables()
self.coarse_vars = [var for var in train_vars if "coarse" in var.name]
self.fine_vars = [var for var in train_vars if "fine" in var.name]
self.coarse_solver = tf.train.AdamOptimizer(learning_rate=1e-04).minimize(self.coarseLoss, var_list=self.coarse_vars)
self.fine_solver = tf.train.AdamOptimizer(learning_rate=1e-04).minimize(self.fineLoss, var_list=self.fine_vars)
self.merged_summ = tf.summary.merge_all()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
self.sess = tf.Session(config=config)
self.train_writer = tf.summary.FileWriter(self.graph_path+"train/")
self.train_writer.add_graph(self.sess.graph)
self.val_writer = tf.summary.FileWriter(self.graph_path+"val/")
self.val_writer.add_graph(self.sess.graph)
self.saver = tf.train.Saver()
self.sess.run(tf.global_variables_initializer())
self._debug_info()
def train_model(self, train_imgs, val_imgs, learning_rate=1e-5, batch_size=32, epoch_size=50):
print "Training Images: ", train_imgs[0].shape[0]
print "Validation Images: ", val_imgs[0].shape[0]
print "Learning_rate: ", learning_rate, "Batch_size", batch_size, "Epochs", epoch_size
raw_input("Training will start above configuration. Press Enter to Start....")
with tf.name_scope("Training") as scope:
for epoch in range(epoch_size):
for itr in xrange(0, train_imgs[0].shape[0]-batch_size, batch_size):
in_images = train_imgs[0][itr:itr+batch_size][:,0,:,:,:]
out_images = train_imgs[1][itr:itr+batch_size]
sess_in = [self.coarse_solver, self.coarseLoss, self.merged_summ]
coarse_out = self.sess.run(sess_in, {self.x:in_images, self.y:out_images})
self.train_writer.add_summary(coarse_out[2])
sess_in = [self.fine_solver, self.fineLoss, self.merged_summ]
fine_out = self.sess.run(sess_in, {self.x:in_images, self.y:out_images})
self.train_writer.add_summary(fine_out[2])
if itr%5==0:
print "Epoch: ", epoch, "Iteration: ", itr, " Coarse Loss: ", coarse_out[1], "Fine Loss: ", fine_out[1], \
"Loss: ", coarse_out[1]+fine_out[1]
for itr in xrange(0, val_imgs[0].shape[0]-batch_size, batch_size):
in_images = val_imgs[0][itr:itr+batch_size][:,0,:,:,:]
out_images = val_imgs[1][itr:itr+batch_size]
c_val_loss, f_val_loss, summ = self.sess.run([self.coarseLoss,self.fineLoss ,self.merged_summ], {self.x: in_images, self.y: out_images})
self.val_writer.add_summary(summ)
print "Epoch: ", epoch, "Iteration: ", itr, "Validation Loss: ", c_val_loss+f_val_loss
if epoch%10==0:
self.saver.save(self.sess, self.save_path+"MSCNN", global_step=epoch)
print "Checkpoint saved"
# random_img = train_imgs[0][np.random.randint(1, train_imgs[0].shape[0], 1)]
# gen_imgs = self.sess.run([self.coarseMap], {self.x: random_img[:,0,:,:,:],self.train_phase:False})
# print gen_imgs[0][0].shape
# cv2.imwrite(self.output_path +str(epoch)+"_train_img.jpg", 255.0*gen_imgs[0][0])
def test(self, input_imgs, batch_size):
sess=tf.Session()
saver = tf.train.import_meta_graph(self.save_path+'MSCNN-90.meta')
saver.restore(sess,tf.train.latest_checkpoint(self.save_path))
graph = tf.get_default_graph()
x = graph.get_tensor_by_name("Inputs/Haze_Image:0")
y = graph.get_tensor_by_name("Model/fineNet/linear_comb/Sigmoid:0")
print "start"
for itr in xrange(0, input_imgs.shape[0], batch_size):
if itr+batch_size<=input_imgs.shape[0]:
end = itr+batch_size
else:
end = input_imgs.shape[0]
input_img = input_imgs[itr:end]
maps = sess.run(y, {x:input_img})
if itr==0:
tot_out = maps
else:
tot_out = np.concatenate((tot_out, maps))
print "Output Shape: ", tot_out.shape
return tot_out