-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualization.py
209 lines (172 loc) · 6.75 KB
/
visualization.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
# imports
import numpy as np
import tensorflow as tf
import cv2
import argparse
import matplotlib.pyplot as plt
from Misc.tf_dataset import get_tf_dataset
from Network.Network import get_model
from Test import Mean_Corner_error
def plot_box(im, pts, color=1):
if color == 1:
c = (0,0,255)
elif color == 2:
c = (255,0,0)
elif color == 3:
c = (255,166,0)
for i in range(4):
cv2.line(im,
(np.flip(pts[i,:].astype(int))),
(np.flip(pts[int((i+1)%4)].astype(int))),
color=c, thickness=2)
def plot_result(h4pt_us,
h4pt_s,
im_ori,
h4pt,
upper_left,
crop_sz=128,):
ch = crop_sz
cw = crop_sz
h,w = im_ori.shape[1:3]
B = im_ori.shape[0]
plt.figure(figsize=(8,14))
for b in range(B):
upper_left_coord = upper_left[[b],:]
corners = upper_left_coord + np.array([[0,0],
[ch-1,0],
[ch-1,cw-1],
[0,cw-1]])
corners_shift = corners - np.reshape(h4pt[[b],:],(4,2))
corners_shift_pred_s = corners - np.reshape(h4pt_s[[b]],(4,2))
corners_shift_pred_us = corners - np.reshape(h4pt_us[[b]],(4,2))
error_s = Mean_Corner_error(corners_shift, corners_shift_pred_s)
error_us = Mean_Corner_error(corners_shift, corners_shift_pred_us)
im_ori_this = np.squeeze(im_ori[b,:,:,:])
# plot the corners on the original image
im_ori0 = im_ori_this.copy()
plot_box(im_ori0, corners, color=1)
# supervised performance
im_ori1 = im_ori_this.copy()
fs = 0.7
c = (255,0,0)
org = (185,30)
cv2.putText(im_ori1, text=f"Error: {error_s:.2f}", org=org,
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=fs,
color=c,
thickness=2)
plot_box(im_ori1, corners_shift, color=1)
plot_box(im_ori1, corners_shift_pred_s, color=2)
# unsupervised performance
im_ori2 = im_ori_this.copy()
cv2.putText(im_ori2, text=f"Error: {error_us:.2f}", org=org,
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=fs,
color=c,
thickness=2)
plot_box(im_ori2, corners_shift, color=1)
plot_box(im_ori2, corners_shift_pred_us, color=2)
plt.subplot(B,1,b+1)
plt.imshow(np.hstack((im_ori0, im_ori1, im_ori2)))
plt.axis('off')
plt.tight_layout()
plt.savefig(f"visualization.",dpi=300)
plt.show()
def plot_transform(h4pt_s,
im_crop1,
im_crop2,
im_ori,
h4pt,
h4pt_us,
upper_left,
crop_sz=128,):
ch = crop_sz
cw = crop_sz
h,w = im_ori.shape[1:3]
B = im_ori.shape[0]
plt.figure(figsize=(10,6))
b = 0
upper_left_coord = upper_left[[b],:]
corners = upper_left_coord + np.array([[0,0],
[ch-1,0],
[ch-1,cw-1],
[0,cw-1]])
corners_shift = corners - np.reshape(h4pt[[b],:],(4,2))
corners_shift_pred_s = corners - np.reshape(h4pt_s[[b]],(4,2))
corners_shift_pred_us = corners - np.reshape(h4pt_us[[b]],(4,2))
error_s = Mean_Corner_error(corners_shift, corners_shift_pred_s)
error_us = Mean_Corner_error(corners_shift, corners_shift_pred_us)
im_ori_this = np.squeeze(im_ori[b,:,:,:])
# plot the corners on the original image
im_ori0 = im_ori_this.copy()
plot_box(im_ori0, corners, color=3) # plot in orange
plot_box(im_ori0, corners_shift, color=1)
plt.subplot(1,3,1)
plt.imshow(im_ori0)
plt.axis("off")
plt.subplot(1,3,2)
imc1,imc2 = np.squeeze(im_crop1[b,:,:,:]/255),\
np.squeeze(im_crop2[b,:,:,:]/255)
plt.imshow(np.hstack((imc1,np.ones((128,10,3)),imc2)))
plt.axis("off")
# supervised performance
im_ori1 = im_ori_this.copy()
fs = 0.7
c = (255,0,0)
org = (185,30)
cv2.putText(im_ori1, text=f"Error: {error_s:.2f}", org=org,
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=fs,
color=c,
thickness=2)
plot_box(im_ori1, corners_shift, color=1)
plot_box(im_ori1, corners_shift_pred_s, color=2)
plt.subplot(1,3,3)
plt.imshow(im_ori1)
plt.axis("off")
plt.tight_layout()
plt.savefig(f"transform_illustration.",dpi=300)
plt.show()
def main():
Parser = argparse.ArgumentParser()
Parser.add_argument('--TestPath', default='/home/ychen921/733/project1/Phase2_Data/Test', help='Base path of images, Default:/home/ychen921/733/project1/Phase2_Data/Test')
Parser.add_argument('--SupCheckPointPath', default='./chkpt_weight/cp_0100.ckpt', help='Path to save Checkpoints, Default: ../chkpt_weight/cp_0100.ckpt')
Parser.add_argument('--UnsupCheckPointPath', default='./chkpt_weight/cp_0050.ckpt', help='Path to save Checkpoints, Default: ../chkpt_weight/cp_0050.ckpt')
Args = Parser.parse_args()
TestPath = Args.TestPath
SupCheckPointPath = Args.SupCheckPointPath
UnsupCheckPointPath = Args.UnsupCheckPointPath
# Test Dataset
ds = get_tf_dataset(path=TestPath, batch_size=8, mode="unsupervised")
sample_input, sample_output = next(iter(ds))
im_crop1, im_crop2, im_ori, upper_left_coord = sample_input
im_warp, h4pt = sample_output
# convert to numpy arrays
im_crop1 = im_crop1.numpy()
im_crop2 = im_crop2.numpy()
im_ori = im_ori.numpy()
upper_left_coord = upper_left_coord.numpy().astype(int)
im_warp = im_warp.numpy()
h4pt = h4pt.numpy().astype(int)
# Load supervised model
model_s = get_model(mode="supervised")
model_s.load_weights(SupCheckPointPath).expect_partial()
# Load unsupervised model
model_us = get_model(mode="unsupervised")
model_us.load_weights(UnsupCheckPointPath).expect_partial()
# call model
h4pt_s = model_s([im_crop1,im_crop2])
h4pt_s = np.round(h4pt_s.numpy())
model_out_us = model_us(sample_input)
im_warp_pred_us, h4pt_us = model_out_us
im_warp_pred_us = np.round(im_warp_pred_us.numpy()*255)
h4pt_us = np.round(h4pt_us.numpy())
plot_result(h4pt_s=h4pt_s, h4pt_us=h4pt_us,
im_ori=im_ori, h4pt=h4pt,
upper_left=upper_left_coord)
plot_transform(im_crop1=im_crop1, im_crop2=im_crop2,
h4pt_s=h4pt_s, im_ori=im_ori,
h4pt=h4pt, h4pt_us=h4pt_us,
upper_left=upper_left_coord)
if __name__ == '__main__':
main()