Created
July 16, 2021 09:46
-
-
Save Chris-hughes10/f967ea4e1236d9c910e17e692e7fd471 to your computer and use it in GitHub Desktop.
Effdet_blog_inference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _run_inference(self, images_tensor, image_sizes): | |
dummy_targets = self._create_dummy_inference_targets( | |
num_images=images_tensor.shape[0] | |
) | |
detections = self.model(images_tensor.to(self.device), dummy_targets)[ | |
"detections" | |
] | |
( | |
predicted_bboxes, | |
predicted_class_confidences, | |
predicted_class_labels, | |
) = self.post_process_detections(detections) | |
scaled_bboxes = self.__rescale_bboxes( | |
predicted_bboxes=predicted_bboxes, image_sizes=image_sizes | |
) | |
return scaled_bboxes, predicted_class_labels, predicted_class_confidences | |
def _create_dummy_inference_targets(self, num_images): | |
dummy_targets = { | |
"bbox": [ | |
torch.tensor([[0.0, 0.0, 0.0, 0.0]], device=self.device) | |
for i in range(num_images) | |
], | |
"cls": [torch.tensor([1.0], device=self.device) for i in range(num_images)], | |
"img_size": torch.tensor( | |
[(self.img_size, self.img_size)] * num_images, device=self.device | |
).float(), | |
"img_scale": torch.ones(num_images, device=self.device).float(), | |
} | |
return dummy_targets | |
def post_process_detections(self, detections): | |
predictions = [] | |
for i in range(detections.shape[0]): | |
predictions.append( | |
self._postprocess_single_prediction_detections(detections[i]) | |
) | |
predicted_bboxes, predicted_class_confidences, predicted_class_labels = run_wbf( | |
predictions, image_size=self.img_size, iou_thr=self.wbf_iou_threshold | |
) | |
return predicted_bboxes, predicted_class_confidences, predicted_class_labels | |
def _postprocess_single_prediction_detections(self, detections): | |
boxes = detections.detach().cpu().numpy()[:, :4] | |
scores = detections.detach().cpu().numpy()[:, 4] | |
classes = detections.detach().cpu().numpy()[:, 5] | |
indexes = np.where(scores > self.prediction_confidence_threshold)[0] | |
boxes = boxes[indexes] | |
return {"boxes": boxes, "scores": scores[indexes], "classes": classes[indexes]} | |
def __rescale_bboxes(self, predicted_bboxes, image_sizes): | |
scaled_bboxes = [] | |
for bboxes, img_dims in zip(predicted_bboxes, image_sizes): | |
im_h, im_w = img_dims | |
if len(bboxes) > 0: | |
scaled_bboxes.append( | |
( | |
np.array(bboxes) | |
* [ | |
im_w / self.img_size, | |
im_h / self.img_size, | |
im_w / self.img_size, | |
im_h / self.img_size, | |
] | |
).tolist() | |
) | |
else: | |
scaled_bboxes.append(bboxes) | |
return scaled_bboxes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment