MCPcopy Create free account
hub / github.com/VCIP-RGBD/DFormer / scale_process

Method scale_process

utils/engine/evaluator.py:226–271  ·  view source on GitHub ↗
(self, img, ori_shape, crop_size, stride_rate, device=None)

Source from the content-addressed store, hash-verified

224 return pred
225
226 def scale_process(self, img, ori_shape, crop_size, stride_rate, device=None):
227 new_rows, new_cols, c = img.shape
228 long_size = new_cols if new_cols > new_rows else new_rows
229
230 if long_size <= crop_size:
231 input_data, margin = self.process_image(img, crop_size)
232 score = self.val_func_process(input_data, device)
233 score = score[:, margin[0] : (score.shape[1] - margin[1]), margin[2] : (score.shape[2] - margin[3])]
234 else:
235 stride = int(np.ceil(crop_size * stride_rate))
236 img_pad, margin = pad_image_to_shape(img, crop_size, cv2.BORDER_CONSTANT, value=0)
237
238 pad_rows = img_pad.shape[0]
239 pad_cols = img_pad.shape[1]
240 r_grid = int(np.ceil((pad_rows - crop_size) / stride)) + 1
241 c_grid = int(np.ceil((pad_cols - crop_size) / stride)) + 1
242 data_scale = torch.zeros(self.class_num, pad_rows, pad_cols).cuda(device)
243 count_scale = torch.zeros(self.class_num, pad_rows, pad_cols).cuda(device)
244
245 for grid_yidx in range(r_grid):
246 for grid_xidx in range(c_grid):
247 s_x = grid_xidx * stride
248 s_y = grid_yidx * stride
249 e_x = min(s_x + crop_size, pad_cols)
250 e_y = min(s_y + crop_size, pad_rows)
251 s_x = e_x - crop_size
252 s_y = e_y - crop_size
253 img_sub = img_pad[s_y:e_y, s_x:e_x, :]
254 count_scale[:, s_y:e_y, s_x:e_x] += 1
255
256 input_data, tmargin = self.process_image(img_sub, crop_size)
257 temp_score = self.val_func_process(input_data, device)
258 temp_score = temp_score[
259 :,
260 tmargin[0] : (temp_score.shape[1] - tmargin[1]),
261 tmargin[2] : (temp_score.shape[2] - tmargin[3]),
262 ]
263 data_scale[:, s_y:e_y, s_x:e_x] += temp_score
264 # score = data_scale / count_scale
265 score = data_scale
266 score = score[:, margin[0] : (score.shape[1] - margin[1]), margin[2] : (score.shape[2] - margin[3])]
267
268 score = score.permute(1, 2, 0)
269 data_output = cv2.resize(score.cpu().numpy(), (ori_shape[1], ori_shape[0]), interpolation=cv2.INTER_LINEAR)
270
271 return data_output
272
273 def val_func_process(self, input_data, device=None):
274 input_data = np.ascontiguousarray(input_data[None, :, :, :], dtype=np.float32)

Callers 1

sliding_evalMethod · 0.95

Calls 3

process_imageMethod · 0.95
val_func_processMethod · 0.95
pad_image_to_shapeFunction · 0.90

Tested by

no test coverage detected