MCPcopy Create free account
hub / github.com/NVlabs/SegFormer / slide_inference

Method slide_inference

mmseg/models/segmentors/encoder_decoder.py:169–212  ·  view source on GitHub ↗

Inference by sliding-window with overlap. If h_crop > h_img or w_crop > w_img, the small patch will be used to decode without padding.

(self, img, img_meta, rescale)

Source from the content-addressed store, hash-verified

167
168 # TODO refactor
169 def slide_inference(self, img, img_meta, rescale):
170 """Inference by sliding-window with overlap.
171
172 If h_crop > h_img or w_crop > w_img, the small patch will be used to
173 decode without padding.
174 """
175
176 h_stride, w_stride = self.test_cfg.stride
177 h_crop, w_crop = self.test_cfg.crop_size
178 batch_size, _, h_img, w_img = img.size()
179 num_classes = self.num_classes
180 h_grids = max(h_img - h_crop + h_stride - 1, 0) // h_stride + 1
181 w_grids = max(w_img - w_crop + w_stride - 1, 0) // w_stride + 1
182 preds = img.new_zeros((batch_size, num_classes, h_img, w_img))
183 count_mat = img.new_zeros((batch_size, 1, h_img, w_img))
184 for h_idx in range(h_grids):
185 for w_idx in range(w_grids):
186 y1 = h_idx * h_stride
187 x1 = w_idx * w_stride
188 y2 = min(y1 + h_crop, h_img)
189 x2 = min(x1 + w_crop, w_img)
190 y1 = max(y2 - h_crop, 0)
191 x1 = max(x2 - w_crop, 0)
192 crop_img = img[:, :, y1:y2, x1:x2]
193 crop_seg_logit = self.encode_decode(crop_img, img_meta)
194 preds += F.pad(crop_seg_logit,
195 (int(x1), int(preds.shape[3] - x2), int(y1),
196 int(preds.shape[2] - y2)))
197
198 count_mat[:, :, y1:y2, x1:x2] += 1
199 assert (count_mat == 0).sum() == 0
200 if torch.onnx.is_in_onnx_export():
201 # cast count_mat to constant while exporting to ONNX
202 count_mat = torch.from_numpy(
203 count_mat.cpu().detach().numpy()).to(device=img.device)
204 preds = preds / count_mat
205 if rescale:
206 preds = resize(
207 preds,
208 size=img_meta[0]['ori_shape'][:2],
209 mode='bilinear',
210 align_corners=self.align_corners,
211 warning=False)
212 return preds
213
214 def whole_inference(self, img, img_meta, rescale):
215 """Inference with full image."""

Callers 1

inferenceMethod · 0.95

Calls 2

encode_decodeMethod · 0.95
resizeFunction · 0.90

Tested by

no test coverage detected