-
Notifications
You must be signed in to change notification settings - Fork 152
/
onnx_models.py
166 lines (148 loc) · 5.34 KB
/
onnx_models.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
#Onnx export code is from [labelme annotation tool](https://github.com/labelmeai/efficient-sam). Huge thanks to Kentaro Wada.
import torch
import torch.nn.functional as F
class OnnxEfficientSam(torch.nn.Module):
def __init__(self, model):
super().__init__()
self.model = model
@property
def decoder_max_num_input_points(self):
return self.model.decoder_max_num_input_points
@property
def image_encoder(self):
return self.model.image_encoder
@property
def get_image_embeddings(self):
return self.model.get_image_embeddings
@property
def prompt_encoder(self):
return self.model.prompt_encoder
@property
def mask_decoder(self):
return self.model.mask_decoder
def forward(
self,
batched_images: torch.Tensor,
batched_points: torch.Tensor,
batched_point_labels: torch.Tensor,
):
batch_size, _, input_h, input_w = batched_images.shape
image_embeddings = self.get_image_embeddings(batched_images)
return self.predict_masks(
image_embeddings,
batched_points,
batched_point_labels,
multimask_output=True,
input_h=input_h,
input_w=input_w,
output_h=input_h,
output_w=input_w,
)
def get_rescaled_pts(
self, batched_points: torch.Tensor, input_h: int, input_w: int
):
return torch.stack(
[
batched_points[..., 0] * self.image_encoder.img_size / input_w,
batched_points[..., 1] * self.image_encoder.img_size / input_h,
],
dim=-1,
)
def predict_masks(
self,
image_embeddings: torch.Tensor,
batched_points: torch.Tensor,
batched_point_labels: torch.Tensor,
multimask_output: bool,
input_h: int,
input_w: int,
output_h: int = -1,
output_w: int = -1,
):
batch_size, max_num_queries, num_pts, _ = batched_points.shape
num_pts = batched_points.shape[2]
rescaled_batched_points = self.get_rescaled_pts(
batched_points, input_h, input_w
)
if num_pts > self.decoder_max_num_input_points:
rescaled_batched_points = rescaled_batched_points[
:, :, : self.decoder_max_num_input_points, :
]
batched_point_labels = batched_point_labels[
:, :, : self.decoder_max_num_input_points
]
elif num_pts < self.decoder_max_num_input_points:
rescaled_batched_points = F.pad(
rescaled_batched_points,
(0, 0, 0, self.decoder_max_num_input_points - num_pts),
value=-1.0,
)
batched_point_labels = F.pad(
batched_point_labels,
(0, self.decoder_max_num_input_points - num_pts),
value=-1.0,
)
sparse_embeddings = self.prompt_encoder(
rescaled_batched_points.reshape(
batch_size * max_num_queries, self.decoder_max_num_input_points, 2
),
batched_point_labels.reshape(
batch_size * max_num_queries, self.decoder_max_num_input_points
),
)
sparse_embeddings = sparse_embeddings.view(
batch_size,
max_num_queries,
sparse_embeddings.shape[1],
sparse_embeddings.shape[2],
)
low_res_masks, iou_predictions = self.mask_decoder(
image_embeddings,
self.prompt_encoder.get_dense_pe(),
sparse_prompt_embeddings=sparse_embeddings,
multimask_output=multimask_output,
)
_, num_predictions, low_res_size, _ = low_res_masks.shape
if output_w > 0 and output_h > 0:
output_masks = F.interpolate(
low_res_masks,
(output_h, output_w),
# NOTE: "bicubic" is inefficient on onnx
mode="bilinear",
)
output_masks = torch.reshape(
output_masks,
(batch_size, max_num_queries, num_predictions, output_h, output_w),
)
else:
output_masks = torch.reshape(
low_res_masks,
(
batch_size,
max_num_queries,
num_predictions,
low_res_size,
low_res_size,
),
)
iou_predictions = torch.reshape(
iou_predictions, (batch_size, max_num_queries, num_predictions)
)
return output_masks, iou_predictions, low_res_masks
class OnnxEfficientSamEncoder(OnnxEfficientSam):
def forward(self, batched_images: torch.Tensor):
return self.model.get_image_embeddings(batched_images)
class OnnxEfficientSamDecoder(OnnxEfficientSam):
def forward(
self, image_embeddings, batched_points, batched_point_labels, orig_im_size
):
return self.predict_masks(
image_embeddings=image_embeddings,
batched_points=batched_points,
batched_point_labels=batched_point_labels,
multimask_output=True,
input_h=orig_im_size[0],
input_w=orig_im_size[1],
output_h=orig_im_size[0],
output_w=orig_im_size[1],
)