-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_mifcn_helpers.py
230 lines (198 loc) · 9.21 KB
/
model_mifcn_helpers.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
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np
import cv2
# **********************************
# ********** SETTINGS **************
# **********************************
hp=400 # This constant is used for the weighted averaging module (Default: 400)
N_train=10 # number of training images
N_patches=400 # number of patches that was extracted from each image
N_blks=5 # number of similar patches for each patch
N_test=18 # test dataset size
N_epochs=60
N_FM=24 # Number of feature maps
k_size=3 # kernel size for convolution layers
# **********************************
def lrelu(x):
return tf.maximum(x*0.2,x)
def identity_initializer():
def _initializer(shape, dtype=tf.float32, partition_info=None):
array = np.zeros(shape, dtype=float)
cx, cy = shape[0]//2, shape[1]//2
for i in range(shape[2]):
array[cx, cy, i, i] = 1
return tf.constant(array, dtype=dtype)
return _initializer
def build(input):
# input has 5 channels
i1,i2,i3,i4,i5=tf.split(input,5,axis=3)
# branch 1 (Main branch)
net1 = slim.conv2d(i1, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net1_1')
net1 = slim.conv2d(net1, N_FM, [k_size, k_size], rate=2,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net1_2')
net1 = slim.conv2d(net1, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net1_3')
net1 = slim.conv2d(net1, 1, [1, 1], rate=1,
activation_fn=None,
scope='net1_last')
# branch 2
net2 = slim.conv2d(i2, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net2_1')
net2 = slim.conv2d(net2, N_FM, [k_size, k_size], rate=2,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net2_2')
net2 = slim.conv2d(net2, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net2_3')
net2 = slim.conv2d(net2, 1, [1, 1], rate=1,
activation_fn=None,
scope='net2_last')
# branch 3
net3 = slim.conv2d(i3, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net3_1')
net3 = slim.conv2d(net3, N_FM, [k_size, k_size], rate=2,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net3_2')
net3 = slim.conv2d(net3, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net3_3')
net3 = slim.conv2d(net3, 1, [1, 1], rate=1,
activation_fn=None,
scope='net3_last')
# branch 4
net4 = slim.conv2d(i4, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net4_1')
net4 = slim.conv2d(net4, N_FM, [k_size, k_size], rate=2,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net4_2')
net4 = slim.conv2d(net4, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net4_3')
net4 = slim.conv2d(net4, 1, [1, 1], rate=1,
activation_fn=None,
scope='net4_last')
# branch 5
net5 = slim.conv2d(i5, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net5_1')
net5 = slim.conv2d(net5, N_FM, [k_size, k_size], rate=2,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net5_2')
net5 = slim.conv2d(net5, N_FM, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
scope='net5_3')
net5 = slim.conv2d(net5, 1, [1, 1], rate=1,
activation_fn=None,
scope='net5_last')
# total=tf.concat([net1,net2,net3,net4,net5],axis=3)
a = tf.minimum(tf.maximum(net1, 0.0), 1.0) * 255.0 # @@
b = tf.minimum(tf.maximum(net2, 0.0), 1.0) * 255.0
c = tf.minimum(tf.maximum(net3, 0.0), 1.0) * 255.0
d = tf.minimum(tf.maximum(net4, 0.0), 1.0) * 255.0
e = tf.minimum(tf.maximum(net5, 0.0), 1.0) * 255.0
# --------------------
d1 = (a - a) ** 2
d2 = (a - b) ** 2
d3 = (a - c) ** 2
d4 = (a - d) ** 2
d5 = (a - e) ** 2
d1 = tf.exp(-d1 / hp)
d2 = tf.exp(-d2 / hp)
d3 = tf.exp(-d3 / hp)
d4 = tf.exp(-d4 / hp)
d5 = tf.exp(-d5 / hp)
wsum = d1 + d2 + d3 + d4 + d5
w1 = tf.divide(d1, wsum)
w2 = tf.divide(d2, wsum)
w3 = tf.divide(d3, wsum)
w4 = tf.divide(d4, wsum)
w5 = tf.divide(d5, wsum)
network_total = tf.multiply(w1, a) + tf.multiply(w2, b) + tf.multiply(w3, c) + tf.multiply(w4, d) + tf.multiply(w5,
e)
total = network_total / 255.0 # @@
net6 = slim.conv2d(total, 24, [k_size, k_size], rate=1,
activation_fn=lrelu,
weights_initializer=identity_initializer(),
# weights_regularizer=slim.l1_regularizer(0.07),
scope='nett_1')
net6 = slim.conv2d(net6, 1, [1, 1], rate=1,
activation_fn=None,
scope='nett_last')
return tf.concat([net1,net2,net3,net4,net5,net6],axis=3)
def prepare_data():
# test set
val_inp_fnames = []
val_lbl_fnames = []
for dirname in ['test/Synthetic']:
for i in range(1,N_test+1):
val_inp_fnames.append("./data/%s/%d/test.tif"%(dirname,i))
val_inp_fnames.append("./data/%s/%d/1.tif" % (dirname, i))
val_inp_fnames.append("./data/%s/%d/2.tif" % (dirname, i))
val_inp_fnames.append("./data/%s/%d/3.tif" % (dirname, i))
val_inp_fnames.append("./data/%s/%d/4.tif" % (dirname, i))
val_lbl_fnames.append("./data/%s/%d/Average.tif" % (dirname, i))
return val_inp_fnames, val_lbl_fnames
def save_data():
# USAGE:
# # For saving:
# from model_mifcn_helpers import save_data
# save_data()
#
# # For loading:
# import numpy as np
# inputs_ndarray = np.load('data/train15_inputs.npy')
# labels_ndarray = np.load('data/train15_labels.npy')
# training set
input_fnames = []
# label_fnames = []
for dirname in ['train15']:
for i in range(1, N_train + 1): # training images
for jj in range(1, N_patches + 1): # patches
for kk in range(1, N_blks + 1): # similar patches for each patch
input_fnames.append("./data/%s/L%0.2d_%0.3d_%0.2d.tif" % (dirname, i, jj, kk))
# label_fnames.append("./data/%s/H%0.2d_%0.3d_%0.2d.tif" % (dirname, i, jj, kk))
img_indices=range(0,len(input_fnames),N_blks)
N_images = len(img_indices)
inputs_ndarray = np.empty(shape=[N_train*N_patches,N_blks,15,15], dtype=np.float32)
labels_ndarray = np.empty(shape=[N_train*N_patches,N_blks,15,15], dtype=np.float32)
dirname = 'train15'
for i in range(1, N_train + 1):
print('Saving patches of the training image number %d ...'%i)
for jj in range(1, N_patches + 1):
for kk in range(1, N_blks + 1):
# Low-SNR Images (noisy)
file = "./data/%s/L%0.2d_%0.3d_%0.2d.tif" % (dirname, i, jj, kk)
tmp = np.float32(cv2.imread(file, cv2.IMREAD_GRAYSCALE)) / 255.0
index = (i-1)*N_patches+(jj-1)
inputs_ndarray[index,kk-1] = tmp
# The Corresponding High-SNR Images (clean)
file = "./data/%s/H%0.2d_%0.3d_%0.2d.tif" % (dirname, i, jj, kk)
tmp = np.float32(cv2.imread(file, cv2.IMREAD_GRAYSCALE)) / 255.0
labels_ndarray[index,kk-1] = tmp
print(inputs_ndarray.shape)
print(labels_ndarray.shape)
np.save('data/train15_inputs.npy',inputs_ndarray)
np.save('data/train15_labels.npy', labels_ndarray)