-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.py
258 lines (199 loc) · 7.37 KB
/
test.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
248
249
250
251
252
253
254
255
from __future__ import print_function
"Fingerprint Image denoising using M-net"
import os
import glob
import numpy as np
from PIL import Image
import datetime
import scipy as sp
import sys
import keras as keras
from keras.models import Model
from keras.layers import Input, Activation, Conv2D, MaxPooling2D, UpSampling2D, Dropout
from keras.layers.merge import concatenate
from keras.layers.normalization import BatchNormalization
from keras import backend as K
from keras.optimizers import SGD
# image_data_format = channels_last
K.set_image_data_format('channels_last')
# Parameter
##############################################################
modelName = 'FPD_M_net_weights'
lrate = 0.1
decay_Rate = 1e-6
height = 400
width = 275
padSz = 88
d = 8
padX = (d - height%d)
padY = (d - width%d)
ipDepth = 3
ipHeight = height + padX + padSz
ipWidth = width + padY + padSz
outDepth = 1
is_zeroMean = False
##############################################################
# Loss function
def my_loss(y_true, y_pred):
l1_loss = K.mean(K.abs(y_pred - y_true))
return l1_loss
# Define the neural network
def getFPDMNet(patchHeight, patchWidth, ipCh, outCh):
# Input
input1 = Input((patchHeight, patchWidth, ipCh))
# Encoder
conv1 = Conv2D(16, (3, 3), padding='same')(input1)
conv1 = BatchNormalization()(conv1)
conv1 = Activation('relu')(conv1)
conv1 = Dropout(0.2)(conv1)
conv1 = concatenate([input1, conv1], axis=-1)
conv1 = Conv2D(16, (3, 3), padding='same')(conv1)
conv1 = BatchNormalization()(conv1)
conv1 = Activation('relu')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
#
input2 = MaxPooling2D(pool_size=(2, 2))(input1)
conv21 = concatenate([input2, pool1], axis=-1)
conv2 = Conv2D(32, (3, 3), padding='same')(conv21)
conv2 = BatchNormalization()(conv2)
conv2 = Activation('relu')(conv2)
conv2 = Dropout(0.2)(conv2)
conv2 = concatenate([conv21, conv2], axis=-1)
conv2 = Conv2D(32, (3, 3), padding='same')(conv2)
conv2 = BatchNormalization()(conv2)
conv2 = Activation('relu')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
#
input3 = MaxPooling2D(pool_size=(2, 2))(input2)
conv31 = concatenate([input3, pool2], axis=-1)
conv3 = Conv2D(64, (3, 3), padding='same')(conv31)
conv3 = BatchNormalization()(conv3)
conv3 = Activation('relu')(conv3)
conv3 = Dropout(0.2)(conv3)
conv3 = concatenate([conv31, conv3], axis=-1)
conv3 = Conv2D(64, (3, 3), padding='same')(conv3)
conv3 = BatchNormalization()(conv3)
conv3 = Activation('relu')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
#
input4 = MaxPooling2D(pool_size=(2, 2))(input3)
conv41 = concatenate([input4, pool3], axis=-1)
conv4 = Conv2D(128, (3, 3), padding='same')(conv41)
conv4 = BatchNormalization()(conv4)
conv4 = Activation('relu')(conv4)
conv4 = Dropout(0.2)(conv4)
conv4 = concatenate([conv41, conv4], axis=-1)
conv4 = Conv2D(128, (3, 3), padding='same')(conv4)
conv4 = BatchNormalization()(conv4)
conv4 = Activation('relu')(conv4)
conv4 = Dropout(0.2)(conv4)
conv4 = Conv2D(128, (3, 3), padding='same')(conv4)
conv4 = BatchNormalization()(conv4)
conv4 = Activation('relu')(conv4)
# Decoder
conv5 = UpSampling2D(size=(2, 2))(conv4)
conv51 = concatenate([conv3, conv5], axis=-1)
conv5 = Conv2D(64, (3, 3), padding='same')(conv51)
conv5 = BatchNormalization()(conv5)
conv5 = Activation('relu')(conv5)
conv5 = Dropout(0.2)(conv5)
conv5 = concatenate([conv51, conv5], axis=-1)
conv5 = Conv2D(64, (3, 3), padding='same')(conv5)
conv5 = BatchNormalization()(conv5)
conv5 = Activation('relu')(conv5)
#
conv6 = UpSampling2D(size=(2, 2))(conv5)
conv61 = concatenate([conv2, conv6], axis=-1)
conv6 = Conv2D(32, (3, 3), padding='same')(conv61)
conv6 = BatchNormalization()(conv6)
conv6 = Activation('relu')(conv6)
conv6 = Dropout(0.2)(conv6)
conv6 = concatenate([conv61, conv6], axis=-1)
conv6 = Conv2D(32, (3, 3), padding='same')(conv6)
conv6 = BatchNormalization()(conv6)
conv6 = Activation('relu')(conv6)
#
conv7 = UpSampling2D(size=(2, 2))(conv6)
conv71 = concatenate([conv1, conv7], axis=-1)
conv7 = Conv2D(16, (3, 3), padding='same')(conv71)
conv7 = BatchNormalization()(conv7)
conv7 = Activation('relu')(conv7)
conv7 = Dropout(0.2)(conv7)
conv7 = concatenate([conv71, conv7], axis=-1)
conv7 = Conv2D(16, (3, 3), padding='same')(conv7)
conv7 = BatchNormalization()(conv7)
conv7 = Activation('relu')(conv7)
# Final
conv81 = UpSampling2D(size=(8, 8))(conv4)
conv82 = UpSampling2D(size=(4, 4))(conv5)
conv83 = UpSampling2D(size=(2, 2))(conv6)
conv8 = concatenate([conv81, conv82, conv83, conv7], axis=-1)
conv8 = Conv2D(outCh, (1, 1), activation='sigmoid')(conv8)
############
model = Model(inputs=input1, outputs=conv8)
sgd = SGD(lr=lrate, decay=decay_Rate, momentum=0.75, nesterov=True)
model.compile(optimizer=sgd, loss=my_loss)
return model
####################### Load & Preprocess Data #######################
# pad Data
def padData(img):
# For grayscale image
if len(img.shape) == 3:
H, W, nB = img.shape
img = np.transpose(img, (2,1,0))
img = np.reshape(img, (nB, W, H, 1))
# Pad X, Y such that it will be divisible by 8
nB, H, W, D = img.shape
temp = np.zeros((nB, H + padX, W + padY, D))
temp[:, :H, :W, :] = img
nB, H, W, D = temp.shape
# Pad extra for network
img = np.zeros((nB, H + padSz, W + padSz, D))
for i in range(0, nB):
for j in range(0, D):
img[i,:,:,j] = np.lib.pad(temp[i,:,:,j], (np.int(padSz/2)), 'edge')
img = (img.astype('float32'))
return img
def load_data(dataPath):
imgsX = imread(dataPath)
if is_zeroMean:
imgsX = np.array(imgsX)/127.5 - 1.
else:
imgsX = np.array(imgsX)/255.0
# Pad images
imgsX = padData(imgsX)
return imgsX
def imread(path, is_gt=False):
temp = sp.misc.imread(path).astype(np.float)
if is_gt:
temp = np.expand_dims(temp, -1)
temp = np.expand_dims(temp, 0)
return temp
####################################
def test_FPDMNet(dataPath):
# Get FPD_M_Net
loadWeightsPath = os.path.join("./weights", modelName + ".hdf5")
fpDenNet = getFPDMNet(ipHeight, ipWidth, ipDepth, outDepth)
# load weights
fpDenNet.load_weights(loadWeightsPath)
imgDataFiles = glob.glob(os.path.join(dataPath, '*.jpg'))
imgDataFiles.sort()
savePath = os.path.join(dataPath, "Results")
if not os.path.exists(savePath):
os.makedirs(savePath)
for i in range(0, len(imgDataFiles)):
print('-----------------------------> Test: ', i, ' <-----------------------------')
# Read fundus data from imgDataFiles
imTest = load_data(imgDataFiles[i])
imPred = fpDenNet.predict(imTest, batch_size=1)
#print('Before imPred: ', imPred.shape)
imPred = np.squeeze(imPred)
unpad = np.int(padSz/2)
imPred = imPred[unpad:-unpad, unpad:-unpad]
imPred = imPred[:-padX, :-padY]
#print('After imPred: ', imPred.shape)
Image.fromarray(np.uint8(imPred * 255)).save(os.path.join(savePath, os.path.basename(imgDataFiles[i])))
if __name__ == '__main__':
dataPath = sys.argv[1]
#dataPath = './test'
test_FPDMNet(dataPath)