-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_with_infer.py
257 lines (209 loc) · 8.32 KB
/
export_with_infer.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
256
257
import warnings
import argparse
import numpy as np
import cv2
import torch
from base_tracker import BaseTracker
from export_model import *
parser = argparse.ArgumentParser(description='welcome to Xmem exporter v0.3')
parser.add_argument('-i', '--input_dir', required=True, help='Xmem checkpoint file path')
parser.add_argument('-o', '--output_dir', required=False, default='./export/', help='export folder path. default is [ export/ ]')
parser.add_argument('--width', required=False, default=640, help='model input width. default is [ 640 ] ')
parser.add_argument('--height', required=False, default=480, help='model input height. default is [ 480 ] ')
parser.add_argument('--mask_num', required=False, default=1, help='max number of mask. default is [ 1 ] ')
args = parser.parse_args()
print()
print("=================================================================================")
print('initialize model')
print("=================================================================================")
xmem_checkpoint = args.input_dir
device = "cpu"
Btrack = BaseTracker(xmem_checkpoint, device)
Btrack.clear_memory()
print("PyTorch version:", torch.__version__)
print("CUDA is available:", torch.cuda.is_available())
print()
print("=================================================================================")
print("set dummy inputs")
print("=================================================================================")
# set padding image shape
def pad_divide_by(in_img, d=16):
h, w = in_img.shape[0], in_img.shape[1]
new_w, new_h = 0, 0
if h % d > 0:
new_h = h + d - h % d
else:
new_h = h
if w % d > 0:
new_w = w + d - w % d
else:
new_w = w
lh, uh = int((new_h-h) / 2), int(new_h-h) - int((new_h-h) / 2)
lw, uw = int((new_w-w) / 2), int(new_w-w) - int((new_w-w) / 2)
pad_array = (int(lw), int(uw), int(lh), int(uh)) # (padding_left,padding_right,padding_top,padding_bottom)
pad_val = None
if len(in_img.shape) == 3:
pad_val = (0, 0, 0)
elif len(in_img.shape) == 2:
pad_val = 0
out = cv2.copyMakeBorder(in_img, top=lh,bottom=uh,left=lw,right=uw,borderType=cv2.BORDER_CONSTANT, value=pad_val)
return np.array(out,dtype=np.float32), pad_array
w_set = abs(int(args.width))
h_set = abs(int(args.height))
dummy_img = np.zeros((h_set, w_set, 3))
print("setting dummy image shape: w={}, h={}".format(w_set, h_set))
dummy_img, _ = pad_divide_by(dummy_img)
print("padding dummy image shape: w={}, h={}".format(dummy_img.shape[1], dummy_img.shape[0]))
h_new, w_new, ch_new = dummy_img.shape
h_pad = int(h_new)
w_pad = int(w_new)
ch_pad = 3
# read sample video
video_path = './sample/test-sample1.mp4'
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("please set sample video file path")
raise IOError
fn = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# read mask image
masks = cv2.imread('sample/test-sample1-1frame-mask.png', cv2.IMREAD_GRAYSCALE)
print("sample image shape: w={}, h={}".format(masks.shape[1], masks.shape[0]))
masks = cv2.resize(masks, dsize=(w_set, h_set))
# color clustering
mask_num = int(args.mask_num)
mask_num = max(1, min(256, mask_num+1))
best_mask = np.zeros_like(masks)
if 1 < mask_num:
masks = np.floor(masks/(256.0/mask_num))*256.0/mask_num
best_mask = masks.astype(np.uint8)
prelabels = np.unique(best_mask).astype(np.uint8)
prelabels = prelabels[prelabels!=0].tolist()
if (len(prelabels)) != mask_num:
warnings.warn("The number of masks did not match the number specified. \nThe number of masks will be Re-specified: {}".format(len(prelabels)))
else:
_, best_mask = cv2.threshold(masks, 10, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
warnings.warn("The number of masks did not match the number specified. \nThe number of masks will be Re-specified: 1")
best_mask = best_mask.astype(np.uint8)
print()
print("=================================================================================")
print("infer test")
print("=================================================================================")
cv2.namedWindow('result', cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow("result", w_pad, h_pad)
first_frame = True
print("sample eval start.")
try_count = 10 if fn>10 else fn
for i in range(try_count):
try:
ret, frame = cap.read()
if ret is False:
raise IOError
frame = cv2.resize(frame, dsize=(w_set, h_set))
print("===================================")
print("frame #" + str(i))
if first_frame:
mask, prob, painted_frame = Btrack.track(frame, best_mask)
first_frame = False
else:
mask, prob, painted_frame = Btrack.track(frame)
best_mask = cv2.normalize(src=mask, dst=None, alpha=1.0, beta=0.0, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
best_mask = cv2.convertScaleAbs(best_mask, dst=None, alpha=255.0, beta=0.0)
cvtImg = np.hstack((frame, cv2.cvtColor(best_mask, cv2.COLOR_GRAY2BGR)))
cv2.imshow("result", cv2.resize(cvtImg, dsize=None, fx=0.5, fy=0.5))
cv2.waitKey(1)
except KeyboardInterrupt:
break
cv2.destroyAllWindows()
cap.release()
print("===================================")
print("sample eval end.\n")
print("=================================================================================")
print("export model")
print("=================================================================================")
# get value
key, shrinkage, selection, f16, f8, f4 = Btrack.tracker.export_val_enc_key
memory_readout, hidden_1, logits, prob = Btrack.tracker.export_val_dec
value, hidden_2, all_labels = Btrack.tracker.export_val_enc_val
do_export_flag = { "encode_key": True, "encode_value": True, "decode": True }
for k, v in do_export_flag.items():
print("{}: {}".format(k, v))
print("\nset value")
print("===================================")
print("model: key encoder + key projection")
model_encode_key = EncodeKey(
Btrack.tracker.network.key_encoder,
Btrack.tracker.network.key_proj
).eval().cpu()
if do_export_flag["encode_key"]:
print("\nexport\n")
dummy_inputs = (
torch.randn((1, ch_pad, h_pad, w_pad)),
torch.randn((1)),
torch.randn((1))
)
torch.onnx.export(
model_encode_key,
dummy_inputs,
"./export/XMem-encode_key.onnx",
export_params=True,
opset_version=17,
do_constant_folding=False,
input_names= ["image", "need_sk", "need_ek"],
output_names=["key", "shrinkage", "selection", "f16", "f8", "f4"],
verbose=False
)
print("\nexport done!\n")
print("===================================")
print("model: value encoder")
model_encode_value = EncodeValue(Btrack.tracker.network.value_encoder).eval().cpu()
if do_export_flag["encode_value"]:
print("\nexport\n")
dummy_inputs = (
torch.randn((1, ch_new, h_new, w_new)),
torch.randn_like(f16),
torch.randn_like(hidden_2),
torch.randn_like(prob[0, 1:].unsqueeze(0)),
torch.randn_like(prob[0, 1:].unsqueeze(0)),
torch.randn((1))
)
torch.onnx.export(
model_encode_value,
dummy_inputs,
f"./export/XMem-encode_value.onnx",
export_params=True,
opset_version=17,
do_constant_folding=True,
input_names= ["image","f16", "h16_in", "masks", "others", "is_deep_update"],
output_names=["g16", "h16_out"],
verbose=False
)
print("\nexport done!\n")
print("===================================")
print("model: decoder")
model_segment = Segment(
Btrack.tracker.network.decoder,
Btrack.tracker.network.value_dim
).eval().cpu()
if do_export_flag["decode"]:
print("\nexport\n")
dummy_inputs = (
torch.randn_like(f16),
torch.randn_like(f8),
torch.randn_like(f4),
torch.randn_like(hidden_1),
torch.randn_like(memory_readout),
torch.randn((1))
)
torch.onnx.export(
model_segment,
dummy_inputs,
"./export/XMem-decode.onnx",
export_params=True,
opset_version=17,
do_constant_folding=False,
input_names= ["f16","f8","f4", "h16_in", "memory_readout", "h_out"],
output_names=["h16_out", "logits", "prob"],
verbose=False
)
print("\nexport done!\n")
print("=================================================================================")