Ejemplo n.º 1
0
def evaluate_mAP(val_loader, model, configs, logger):
    batch_time = AverageMeter('Time', ':6.3f')
    data_time = AverageMeter('Data', ':6.3f')

    progress = ProgressMeter(len(val_loader), [batch_time, data_time],
                             prefix="Evaluation phase...")
    labels = []
    sample_metrics = []  # List of tuples (TP, confs, pred)
    # switch to evaluate mode
    model.eval()
    with torch.no_grad():
        start_time = time.time()
        for batch_idx, batch_data in enumerate(tqdm(val_loader)):
            data_time.update(time.time() - start_time)
            _, imgs, targets = batch_data
            # Extract labels
            labels += targets[:, 1].tolist()
            # Rescale x, y, w, h of targets ((box_idx, class, x, y, z, h, w, l, im, re))
            targets[:, 2:4] *= configs.img_size
            targets[:, 5:8] *= configs.img_size
            imgs = imgs.to(configs.device, non_blocking=True)

            outputs = model(imgs)
            outputs = post_processing_v2(outputs,
                                         conf_thresh=configs.conf_thresh,
                                         nms_thresh=configs.nms_thresh)

            sample_metrics += get_batch_statistics_rotated_bbox(
                outputs, targets, iou_threshold=configs.iou_thresh)

            # measure elapsed time
            # torch.cuda.synchronize()
            batch_time.update(time.time() - start_time)

            # Log message
            if logger is not None:
                if ((batch_idx + 1) % configs.print_freq) == 0:
                    logger.info(progress.get_message(batch_idx))

            start_time = time.time()

        # Concatenate sample statistics
        true_positives, pred_scores, pred_labels = [
            np.concatenate(x, 0) for x in list(zip(*sample_metrics))
        ]
        precision, recall, AP, f1, ap_class = ap_per_class(
            true_positives, pred_scores, pred_labels, labels)

    return precision, recall, AP, f1, ap_class
Ejemplo n.º 2
0
    def callback(self,data):
        rospy.loginfo("detection")
        with torch.no_grad():
            gen = point_cloud2.read_points(data)
            for idx, p in enumerate(gen):
                print(idx)
                print(p)

            b = kitti_bev_utils.removePoints(gen, cnf.boundary)
            imgs_bev = kitti_bev_utils.makeBVFeature(b, cnf.DISCRETIZATION, cnf.boundary)

            input_imgs = imgs_bev.to(device=configs.device).float()
            t1 = time_synchronized()
            outputs = self.model(input_imgs)
            t2 = time_synchronized()
            detections = post_processing_v2(outputs, conf_thresh=configs.conf_thresh, nms_thresh=configs.nms_thresh)

            img_detections = []  # Stores detections for each image index
            img_detections.extend(detections)

            img_bev = imgs_bev.squeeze() * 255
            img_bev = img_bev.permute(1, 2, 0).numpy().astype(np.uint8)
            img_bev = cv2.resize(img_bev, (configs.img_size, configs.img_size))
            for detections in img_detections:
                if detections is None:
                    continue
                # Rescale boxes to original image
                detections = rescale_boxes(detections, configs.img_size, img_bev.shape[:2])
                for x, y, w, l, im, re, *_, cls_pred in detections:
                    yaw = np.arctan2(im, re)
                    # Draw rotated box
                    kitti_bev_utils.drawRotatedBox(img_bev, x, y, w, l, yaw, cnf.colors[int(cls_pred)])

            img_bev = cv2.flip(cv2.flip(img_bev, 0), 1)
            out_img = img_bev
            cv2.imshow('test-img', out_img)
            cv2.waitKey(1)
Ejemplo n.º 3
0
        'cpu' if configs.no_cuda else 'cuda:{}'.format(configs.gpu_idx))
    model = model.to(device=configs.device)

    out_cap = None

    model.eval()

    test_dataloader = create_test_dataloader(configs)
    with torch.no_grad():
        for batch_idx, (img_paths, imgs_bev) in enumerate(test_dataloader):
            input_imgs = imgs_bev.to(device=configs.device).float()
            t1 = time_synchronized()
            outputs = model(input_imgs)
            t2 = time_synchronized()
            detections = post_processing_v2(outputs,
                                            conf_thresh=configs.conf_thresh,
                                            nms_thresh=configs.nms_thresh)

            img_detections = []  # Stores detections for each image index
            img_detections.extend(detections)

            img_bev = imgs_bev.squeeze() * 255
            img_bev = img_bev.permute(1, 2, 0).numpy().astype(np.uint8)
            img_bev = cv2.resize(img_bev, (configs.img_size, configs.img_size))
            for detections in img_detections:
                if detections is None:
                    continue
                # Rescale boxes to original image
                detections = rescale_boxes(detections, configs.img_size,
                                           img_bev.shape[:2])
                for x, y, w, l, im, re, *_, cls_pred in detections:
Ejemplo n.º 4
0
    def client_recv(self, client, address):
        while True:
            # read message from socket
            # client_msg_0\x00\x00\x00\x00\x00...
            msg = client.recv(1024).decode("utf-8")
            msg = msg.rstrip("\x00")
            if msg == '':
                return
            if msg == "EOF":
                return
            elif msg == "quit_client":
                client.close()
                # self.sock.close()
                print("> client  exit...")
                return
            elif msg == "quit_server":
                client.close()
                self.sock.close()
                print("> server  exit...")
                sys.exit(0)
            else:
                print("> -------", time.strftime('%Y-%m-%d %H:%M:%S',
                      time.localtime(time.time())), "-------")
                print("> receive the msg from client : {0}".format(msg))
                print('> inference for {0}'.format(msg))
                if(self.need_create_window):
                    # NOTE ObjSLAM
                    cv2.namedWindow("YOLO", flags=cv2.WINDOW_GUI_NORMAL)
                    self.need_create_window = False
                # Inference
                with torch.no_grad():
                    # img_paths, imgs_bev = self.test_dataloader_iter.next()
                    img_paths, imgs_bev = self.test_dataset[int(msg)]
                    img_paths = [img_paths]
                    imgs_bev = torch.from_numpy(
                        np.expand_dims(imgs_bev, axis=0))
                    input_imgs = imgs_bev.to(
                        device=self.configs.device).float()
                    outputs = self.model(input_imgs)
                    detections = post_processing_v2(
                        outputs, conf_thresh=self.configs.conf_thresh, nms_thresh=self.configs.nms_thresh)

                    img_detections = []  # Stores detections for each image index
                    img_detections.extend(detections)

                    img_bev = imgs_bev.squeeze() * 255
                    img_bev = img_bev.permute(1, 2, 0).numpy().astype(np.uint8)
                    img_bev = cv2.resize(
                        img_bev, (self.configs.img_size, self.configs.img_size))
                    for detections in img_detections:
                        if detections is None:
                            continue
                        # Rescale boxes to original image
                        detections = rescale_boxes(
                            detections, self.configs.img_size, img_bev.shape[:2])
                        for x, y, w, l, im, re, *_, cls_pred in detections:
                            yaw = np.arctan2(im, re)
                            # Draw rotated box
                            kitti_bev_utils.drawRotatedBox(
                                img_bev, x, y, w, l, yaw, cnf.colors[int(cls_pred)])

                    img_rgb = cv2.imread(img_paths[0])
                    calib = kitti_data_utils.Calibration(img_paths[0].replace(
                        ".png", ".txt").replace("image_2", "calib"))
                    objects_pred = predictions_to_kitti_format(
                        img_detections, calib, img_rgb.shape, self.configs.img_size)
                    # NOTE: 输出json的代码
                    frame_object_list = []
                    for i in objects_pred:
                        frame_object = dict()
                        frame_object['type'] = i.type
                        frame_object['center'] = i.t
                        frame_object['length'] = i.l
                        frame_object['width'] = i.w
                        frame_object['height'] = i.h
                        frame_object['theta'] = i.ry
                        box3d_pts_2d, _ = kitti_data_utils.compute_box_3d(
                            i, calib.P)
                        if box3d_pts_2d is None:
                            frame_object['box3d_pts_2d'] = box3d_pts_2d
                        elif box3d_pts_2d.size == 16:
                            frame_object['box3d_pts_2d'] = box3d_pts_2d
                        else:
                            frame_object['box3d_pts_2d'] = box3d_pts_2d[:8, :]
                        frame_object_list.append(frame_object)
                    result = json.dumps(frame_object_list, cls=NumpyEncoder)
                    img_bev = cv2.flip(cv2.flip(img_bev, 0), 1)
                    scale = 1.5
                    cv2.resizeWindow("YOLO",
                                     width=int(img_bev.shape[1] * scale),
                                     height=int(img_bev.shape[0] * scale))
                    cv2.imshow('YOLO', img_bev)
                    cv2.waitKey(10)
                    self.batch_idx += 1
                if len(result) > self.configs.max_length:
                    print("> WARNING: STRING IS TOO LONG! (MAX_LENGTH {0})".format(
                        self.configs.max_length))
                client.send(result.encode(encoding='utf-8'))
                print("> send the responce back to client, string length: {0}".format(
                    len(result)))
        return