-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_fusion.py
105 lines (74 loc) · 3.29 KB
/
test_fusion.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
#!/usr/bin/python
import argparse
import time
import numpy as np
import matplotlib.pyplot as plt
from monodepth.monodepth_runner import monodepth_runner
import utils
import cv2
from models.icnet_fusion import ICNet
import configs
#### Test ####
# define global variables
model_type = 'cross_fusion'
checkpoint_path = '/home/neil/Workspace/semantic-segmentation/monodepth/models/cityscape/model_cityscapes.data-00000-of-00001'
model_path = 'icnet_' + model_type + '_030_0.869.h5'
test_img_path = "./testing_imgs/731.png"
# ==== create monodepth runner ====
depth_runner = monodepth_runner(checkpoint_path)
# ====== Model ======
net = ICNet(width=configs.img_width, height=configs.img_height, n_classes=34, weight_path="output/" + model_path,
mode=model_type)
print(net.model.summary())
def visualization_result(y, mid):
y = cv2.resize(y, (configs.img_width / 2, configs.img_height / 2))
image = utils.convert_class_to_rgb(y, threshold=0.50)
viz = cv2.addWeighted(mid, 0.8, image, 0.8, 0)
plt.figure(1)
plt.imshow(viz)
plt.show()
cv2.imwrite('seg_result_overlay.png', cv2.resize(cv2.cvtColor(viz, cv2.COLOR_RGB2BGR), (1024, 512)))
def test_fusion():
# ======== Testing ========
x = cv2.resize(cv2.imread(test_img_path, 1), (configs.img_width, configs.img_height))
x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)
x_depth = depth_runner.run_depth(image_path=test_img_path, out_height=configs.img_height,
out_width=configs.img_width)
x_depth = np.dstack((x_depth, x_depth, x_depth))
mid = cv2.resize(x, (configs.img_width / 2, configs.img_height / 2))
X_color = np.zeros((1, configs.img_height, configs.img_width, 3), dtype='float32')
X_depth = np.zeros((1, configs.img_height, configs.img_width, 3), dtype='float32')
X_color[0, :, :, :] = x
X_depth[0, :, :, :] = x_depth
y = net.model.predict([X_color, X_depth], verbose=1)[0]
# ====== running... ======
start_time = time.time()
for i in range(10):
y = net.model.predict([X_color, X_depth])[0]
duration = time.time() - start_time
print('Generated segmentations in %s seconds -- %s FPS' % (duration / 10, 1.0 / (duration / 10)))
visualization_result(y=y, mid=mid)
def test_early_fusion():
# ======== Testing ========
x = cv2.resize(cv2.imread(test_img_path, 1), (configs.img_width, configs.img_height))
x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)
x_depth = depth_runner.run_depth(image_path=test_img_path, out_height=configs.img_height,
out_width=configs.img_width)
x_depth = np.dstack((x_depth, x_depth, x_depth))
plt.imshow(x_depth)
plt.show()
mid = cv2.resize(x, (configs.img_width / 2, configs.img_height / 2))
x = np.array([np.concatenate((x, x_depth), axis=2)])
y = net.model.predict(x)[0]
# ===== running... =====
start_time = time.time()
for i in range():
y = net.model.predict(x)[0]
duration = time.time() - start_time
print('Generated segmentations in %s seconds -- %s FPS' % (duration / 10, 1.0 / (duration / 10)))
visualization_result(y=y, mid=mid)
if __name__ == "__main__":
if model_type == 'mid_fusion' or model_type == 'cross_fusion':
test_fusion()
elif model_type == 'early_fusion':
test_early_fusion()