|
| 1 | +from typing import Optional, List, Union |
| 2 | +import os |
| 3 | +import torch |
| 4 | +import logging |
| 5 | +import backend |
| 6 | +from collections import namedtuple |
| 7 | +from model.painter import Painter |
| 8 | +from model.pointpillars import PointPillars |
| 9 | +import numpy as np |
| 10 | +from tools.process import keep_bbox_from_image_range |
| 11 | +from waymo import Waymo |
| 12 | + |
| 13 | + |
| 14 | +logging.basicConfig(level=logging.INFO) |
| 15 | +log = logging.getLogger("backend-pytorch") |
| 16 | + |
| 17 | + |
| 18 | +def change_calib_device(calib, cuda): |
| 19 | + result = {} |
| 20 | + if cuda: |
| 21 | + device = 'cuda' |
| 22 | + else: |
| 23 | + device = 'cpu' |
| 24 | + result['R0_rect'] = calib['R0_rect'].to(device=device, dtype=torch.float) |
| 25 | + for i in range(5): |
| 26 | + result['P' + str(i)] = calib['P' + str(i) |
| 27 | + ].to(device=device, dtype=torch.float) |
| 28 | + result['Tr_velo_to_cam_' + |
| 29 | + str(i)] = calib['Tr_velo_to_cam_' + |
| 30 | + str(i)].to(device=device, dtype=torch.float) |
| 31 | + return result |
| 32 | + |
| 33 | + |
| 34 | +class BackendDeploy(backend.Backend): |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + segmentor_path, |
| 38 | + lidar_detector_path, |
| 39 | + data_path |
| 40 | + ): |
| 41 | + super(BackendDeploy, self).__init__() |
| 42 | + self.segmentor_path = segmentor_path |
| 43 | + self.lidar_detector_path = lidar_detector_path |
| 44 | + # self.segmentation_classes = 18 |
| 45 | + self.detection_classes = 3 |
| 46 | + self.data_root = data_path |
| 47 | + CLASSES = Waymo.CLASSES |
| 48 | + self.LABEL2CLASSES = {v: k for k, v in CLASSES.items()} |
| 49 | + |
| 50 | + def version(self): |
| 51 | + return torch.__version__ |
| 52 | + |
| 53 | + def name(self): |
| 54 | + return "python-SUT" |
| 55 | + |
| 56 | + def load(self): |
| 57 | + device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") |
| 58 | + PaintArgs = namedtuple( |
| 59 | + 'PaintArgs', [ |
| 60 | + 'training_path', 'model_path', 'cam_sync']) |
| 61 | + painting_args = PaintArgs( |
| 62 | + os.path.join( |
| 63 | + self.data_root, |
| 64 | + 'training'), |
| 65 | + self.segmentor_path, |
| 66 | + False) |
| 67 | + self.painter = Painter(painting_args) |
| 68 | + self.segmentor = self.painter.model |
| 69 | + model = PointPillars( |
| 70 | + nclasses=self.detection_classes, |
| 71 | + painted=True).to( |
| 72 | + device=device) |
| 73 | + model.eval() |
| 74 | + checkpoint = torch.load(self.lidar_detector_path) |
| 75 | + model.load_state_dict(checkpoint["model_state_dict"]) |
| 76 | + self.lidar_detector = model |
| 77 | + |
| 78 | + return self |
| 79 | + |
| 80 | + def predict(self, inputs): |
| 81 | + # TODO: implement predict |
| 82 | + dimensions, locations, rotation_y, box2d, class_labels, class_scores, ids = [ |
| 83 | + ], [], [], [], [], [], [] |
| 84 | + with torch.inference_mode(): |
| 85 | + device = torch.device( |
| 86 | + "cuda:0" if torch.cuda.is_available() else "cpu") |
| 87 | + format_results = {} |
| 88 | + model_input = inputs[0] |
| 89 | + batched_pts = model_input['pts'] |
| 90 | + scores_from_cam = [] |
| 91 | + for i in range(len(model_input['images'])): |
| 92 | + segmentation_score = self.segmentor( |
| 93 | + model_input['images'][i].to(device))[0] |
| 94 | + scores_from_cam.append( |
| 95 | + self.painter.get_score(segmentation_score).cpu()) |
| 96 | + points = self.painter.augment_lidar_class_scores_both( |
| 97 | + scores_from_cam, batched_pts, model_input['calib_info']) |
| 98 | + batch_results = self.lidar_detector( |
| 99 | + batched_pts=[points.to(device=device)], mode='val') |
| 100 | + for j, result in enumerate(batch_results): |
| 101 | + format_result = { |
| 102 | + 'class': [], |
| 103 | + 'truncated': [], |
| 104 | + 'occluded': [], |
| 105 | + 'alpha': [], |
| 106 | + 'bbox': [], |
| 107 | + 'dimensions': [], |
| 108 | + 'location': [], |
| 109 | + 'rotation_y': [], |
| 110 | + 'score': [], |
| 111 | + 'idx': -1 |
| 112 | + } |
| 113 | + |
| 114 | + calib_info = model_input['calib_info'] |
| 115 | + image_info = model_input['image_info'] |
| 116 | + idx = model_input['image_info']['image_idx'] |
| 117 | + |
| 118 | + calib_info = change_calib_device(calib_info, False) |
| 119 | + result_filter = keep_bbox_from_image_range( |
| 120 | + result, calib_info, 5, image_info, False) |
| 121 | + |
| 122 | + lidar_bboxes = result_filter['lidar_bboxes'] |
| 123 | + labels, scores = result_filter['labels'], result_filter['scores'] |
| 124 | + bboxes2d, camera_bboxes = result_filter['bboxes2d'], result_filter['camera_bboxes'] |
| 125 | + for lidar_bbox, label, score, bbox2d, camera_bbox in \ |
| 126 | + zip(lidar_bboxes, labels, scores, bboxes2d, camera_bboxes): |
| 127 | + format_result['class'].append(label.item()) |
| 128 | + format_result['truncated'].append(0.0) |
| 129 | + format_result['occluded'].append(0) |
| 130 | + alpha = camera_bbox[6] - \ |
| 131 | + np.arctan2(camera_bbox[0], camera_bbox[2]) |
| 132 | + format_result['alpha'].append(alpha.item()) |
| 133 | + format_result['bbox'].append(bbox2d.tolist()) |
| 134 | + format_result['dimensions'].append(camera_bbox[3:6]) |
| 135 | + format_result['location'].append(camera_bbox[:3]) |
| 136 | + format_result['rotation_y'].append(camera_bbox[6].item()) |
| 137 | + format_result['score'].append(score.item()) |
| 138 | + format_results['idx'] = idx |
| 139 | + |
| 140 | + # write_label(format_result, os.path.join(saved_submit_path, f'{idx:06d}.txt')) |
| 141 | + |
| 142 | + if len(format_result['dimensions']) > 0: |
| 143 | + format_result['dimensions'] = torch.stack( |
| 144 | + format_result['dimensions']) |
| 145 | + format_result['location'] = torch.stack( |
| 146 | + format_result['location']) |
| 147 | + dimensions.append(format_result['dimensions']) |
| 148 | + locations.append(format_result['location']) |
| 149 | + rotation_y.append(format_result['rotation_y']) |
| 150 | + class_labels.append(format_result['class']) |
| 151 | + class_scores.append(format_result['score']) |
| 152 | + box2d.append(format_result['bbox']) |
| 153 | + ids.append(format_results['idx']) |
| 154 | + # return Boxes, Classes, Scores # Change to desired output |
| 155 | + return dimensions, locations, rotation_y, box2d, class_labels, class_scores, ids |
0 commit comments