-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimateDistances.py
99 lines (73 loc) · 3.99 KB
/
estimateDistances.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
##################################################################################
# This python code includes the function call to estimate the distance maps
# using a trained DeepDistanceModel or DeepDistanceExtendedModel on a given
# test set 'x' and returns the estimated output maps.
# When extendedModel = False, it uses DeepDistanceModel and returns an empty
# list for the segmentation output.
##################################################################################
import numpy as np
from keras.models import load_model
def test(modelpath = '', x = [],
extendedModel = False, # is DeepDistanceExtendedModel used
patch_height=512, patch_width=512, increment = 64 ):
segmentationMaps = [] # result
innerDstMaps = [] # result
outerDstMaps = [] # result
model = load_model(modelpath)
for ind in range(len(x)): #index for each image
print ('img ind: %d / %d' % (ind+1, len(x)))
shape = (x[ind].shape[0],x[ind].shape[1])
segmentation = np.zeros(shape)
inner_dst = np.zeros(shape)
outer_dst = np.zeros(shape)
counts = np.zeros(shape)
i=0
while (i < x[ind].shape[0] +1):
j=0
while (j < x[ind].shape[1] +1):
if( i < (x[ind].shape[0] - patch_height +1) and j < (x[ind].shape[1] - patch_width +1)):
x_start = i
x_end = i+patch_height
y_start = j
y_end = j+patch_width
else:
if(i < (x[ind].shape[0] - patch_height +1)):
x_start = i
x_end = i+patch_height
y_start = x[ind].shape[1] - patch_width
y_end = x[ind].shape[1]
j = x[ind].shape[1] +1
elif(j < (x[ind].shape[1] - patch_width +1)):
x_start = x[ind].shape[0] - patch_height
x_end = x[ind].shape[0]
y_start = j
y_end = j+patch_width
i = x[ind].shape[0] +1
else:
x_start = x[ind].shape[0] - patch_height
x_end = x[ind].shape[0]
y_start = x[ind].shape[1] - patch_width
y_end = x[ind].shape[1]
i = x[ind].shape[0] +1
j = x[ind].shape[1] +1
counts[x_start:x_end , y_start:y_end] += 1
patch = x[ind][x_start:x_end , y_start:y_end , :]
#Model - dist
pred = model.predict( patch.reshape( (1,) + patch.shape ) )
if(extendedModel):
segmentation[x_start:x_end , y_start:y_end] += pred[0][0,:,:,0]
inner_dst[x_start:x_end , y_start:y_end] += pred[1][0,:,:,0]
outer_dst[x_start:x_end , y_start:y_end] += pred[2][0,:,:,0]
else:
inner_dst[x_start:x_end , y_start:y_end] += pred[0][0,:,:,0]
outer_dst[x_start:x_end , y_start:y_end] += pred[1][0,:,:,0]
j+= increment
i += increment
if(extendedModel):
mask = np.divide(segmentation,counts) #average
segmentationMaps.append(mask)
inner_dstmap = np.divide(inner_dst,counts) #average
innerDstMaps.append(inner_dstmap)
outer_dstmap = np.divide(outer_dst,counts) #average
outerDstMaps.append(outer_dstmap)
return[innerDstMaps, outerDstMaps, segmentationMaps]