Example #1
0
def inference_no_model(
        data_loader,
        iou_types=("bbox", ),
        motion_specific=False,
        box_only=False,
        expected_results=(),
        expected_results_sigma_tol=4,
        output_folder=None,
):
    dataset = data_loader.dataset

    predictions = torch.load(os.path.join(output_folder, "predictions.pth"))
    print("prediction loaded.")

    extra_args = dict(
        box_only=box_only,
        iou_types=iou_types,
        motion_specific=motion_specific,
        expected_results=expected_results,
        expected_results_sigma_tol=expected_results_sigma_tol,
    )

    return evaluate(dataset=dataset,
                    predictions=predictions,
                    output_folder=output_folder,
                    **extra_args)
Example #2
0
def inference(
        cfg,
        model,
        data_loader,
        dataset_name,
        iou_types=("bbox", ),
        box_only=False,
        bbox_aug=False,
        device="cuda",
        expected_results=(),
        expected_results_sigma_tol=4,
        output_folder=None,
):
    # convert to a torch.device for efficiency
    device = torch.device(device)
    num_devices = get_world_size()
    logger = logging.getLogger("mega_core.inference")
    dataset = data_loader.dataset
    logger.info("Start evaluation on {} dataset({} images).".format(
        dataset_name, len(dataset)))
    total_timer = Timer()
    inference_timer = Timer()
    total_timer.tic()
    predictions = compute_on_dataset(model, data_loader, device, bbox_aug,
                                     cfg.MODEL.VID.METHOD, inference_timer)
    # wait for all processes to complete before measuring the time
    synchronize()
    total_time = total_timer.toc()
    total_time_str = get_time_str(total_time)
    logger.info(
        "Total run time: {} ({} s / img per device, on {} devices)".format(
            total_time_str, total_time * num_devices / len(dataset),
            num_devices))
    total_infer_time = get_time_str(inference_timer.total_time)
    logger.info(
        "Model inference time: {} ({} s / img per device, on {} devices)".
        format(
            total_infer_time,
            inference_timer.total_time * num_devices / len(dataset),
            num_devices,
        ))

    predictions = _accumulate_predictions_from_multiple_gpus(predictions)
    if not is_main_process():
        return

    if output_folder:
        torch.save(predictions, os.path.join(output_folder, "predictions.pth"))

    extra_args = dict(
        box_only=box_only,
        iou_types=iou_types,
        expected_results=expected_results,
        expected_results_sigma_tol=expected_results_sigma_tol,
    )

    return evaluate(dataset=dataset,
                    predictions=predictions,
                    output_folder=output_folder,
                    **extra_args)
Example #3
0
def inference(
        cfg,
        model,
        data_loader,
        dataset_name,
        iou_types=("bbox", ),
        motion_specific=False,
        box_only=False,
        bbox_aug=False,
        device="cuda",
        expected_results=(),
        expected_results_sigma_tol=4,
        output_folder=None,
):
    # convert to a torch.device for efficiency
    device = torch.device(device)
    num_devices = get_world_size()
    logger = logging.getLogger("mega_core.inference")
    dataset = data_loader.dataset
    logger.info("Start evaluation on {} dataset({} images).".format(
        dataset_name, len(dataset)))
    total_timer = Timer()
    inference_timer = Timer()
    total_timer.tic()
    predictions = compute_on_dataset(model, data_loader, device, bbox_aug,
                                     cfg.MODEL.VID.METHOD, inference_timer)
    # wait for all processes to complete before measuring the time
    synchronize()
    total_time = total_timer.toc()
    total_time_str = get_time_str(total_time)
    logger.info(
        "Total run time: {} ({} s / img per device, on {} devices)".format(
            total_time_str, total_time * num_devices / len(dataset),
            num_devices))
    total_infer_time = get_time_str(inference_timer.total_time)
    logger.info(
        "Model inference time: {} ({} s / img per device, on {} devices)".
        format(
            total_infer_time,
            inference_timer.total_time * num_devices / len(dataset),
            num_devices,
        ))

    predictions = _accumulate_predictions_from_multiple_gpus(predictions)
    ##########################################################################
    ## save result img as a video
    # out_video='result.avi'
    # fourcc = cv2.VideoWriter_fourcc(*'XVID')
    # fgs=25
    # size = (560, 220)
    # video_writer = cv2.VideoWriter(out_video, fourcc, fgs, size)
    outputset = output_folder.split('/')[-1]
    if hasattr(predictions, "copy"):
        score_thr = [0.5, 0.7, 0.9]
        for _score in score_thr:
            file_data = ""
            with open(f'{outputset}_video_result_{_score}.txt', 'w') as f:
                f.write(file_data)
            predictionstmp = predictions.copy()
            video_list = {}
            alarmvideo_list = {}
            frame_record = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            frame_num = 0
            for image_id, prediction in enumerate(predictionstmp):
                img, target, filename = dataset.get_visualization(image_id)
                frame_id = int(filename.split("/")[-1])
                ## every 12 frames
                # if frame_id%12 != 0:
                #     continue
                img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
                img_info = dataset.get_img_info(image_id)
                image_width = img_info["width"]
                image_height = img_info["height"]
                prediction = prediction.resize((image_width, image_height))
                pred_bbox = prediction.bbox.numpy()
                pred_label = prediction.get_field("labels").numpy()
                pred_score = prediction.get_field("scores").numpy()
                # keep = pred_score.gt(score_thr)
                keep = pred_score > _score
                boxes = pred_bbox[keep]
                classes = pred_label[keep]
                scores = pred_score[keep]
                template = "{}: {:.2f}"
                boxnum = 0
                for box, score, label in zip(boxes, scores, classes):
                    if label == 1:
                        label = 'person'
                        boxnum += 1
                    elif label == 2:
                        label = 'head'
                    else:
                        label = 'unknown'
                    rect = [int(k) for k in box[:4]]
                    cv2.rectangle(img, (rect[0], rect[1]), (rect[2], rect[3]),
                                  (0, 0, 255), 2)
                    s = template.format(label, score)
                    cv2.putText(img, s, (rect[0], rect[1]),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)

                video_dictname = filename.split(
                    '/')[-3] + '/' + filename.split('/')[-2]
                if video_dictname not in video_list:
                    frame_record = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
                    frame_num = 0
                    video_result = {}
                    video_list[video_dictname] = video_result
                    alarmvideo_list[video_dictname] = 0
                    file_data_tmp = ""
                    for single_video in alarmvideo_list:
                        file_data_tmp += str(single_video) + ', value: ' + str(
                            alarmvideo_list[single_video]) + '\n'
                    with open(f'{outputset}_video_result_{_score}_tmp.txt',
                              'w') as f:
                        f.write(file_data_tmp)

                #every 12 frames
                # if alarmvideo_list[video_dictname] ==0 and boxnum>1:
                #     alarmvideo_list[video_dictname] =1
                #every frame 8 warning
                if boxnum > 1:
                    frame_record[frame_num % 10] = 1
                else:
                    frame_record[frame_num % 10] = 0

                if alarmvideo_list[video_dictname] == 0 and sum(
                        frame_record) > 7:
                    alarmvideo_list[video_dictname] = 1

                frame_num += 1
                if filename.split('/')[-1] not in video_result:
                    video_result[filename.split('/')[-1]] = boxnum

                imgdir = os.path.join(output_folder, os.path.dirname(filename))
                if not os.path.exists(imgdir):
                    os.makedirs(imgdir)
                output_name = os.path.join(
                    imgdir, '{}_{}.jpg'.format(_score,
                                               os.path.basename(filename)))
                cv2.imwrite(output_name, img)
                # video_writer.write(img)
                #xml
                create_tree(output_name)
                for box, score, label in zip(boxes, scores, classes):
                    rect = [int(k) for k in box[:4]]
                    create_object(annotation, label, rect[0], rect[1], rect[2],
                                  rect[3])

                # for coordinate_list in coordinates_list:
                #     create_object(annotation, coordinate_list[0], coordinate_list[1], coordinate_list[2], coordinate_list[3])
                # if coordinates_list==[]:
                #     break
                # 将树模型写入xml文件
                tree = ET.ElementTree(annotation)
                # tree.write('%s.xml' % output_name.rstrip('.jpg'))

                # tree = ET.ElementTree.parse('%s.xml' % output_name.rstrip('.jpg'))  # 解析movies.xml这个文件
                root = tree.getroot()  # 得到根元素,Element类
                pretty_xml(root, '\t', '\n')  # 执行美化方法
                tree.write('%s.xml' % output_name.rstrip('.jpg'))
            # file_data=str(alarmvideo_list)
            for single_video in alarmvideo_list:
                file_data += str(single_video) + ', value: ' + str(
                    alarmvideo_list[single_video]) + '\n'
            with open(f'{outputset}_video_result_{_score}.txt', 'a') as f:
                f.write(file_data)
    # video_writer.release()

    ##########################################################################
    if not is_main_process():
        return

    if output_folder:
        torch.save(predictions, os.path.join(output_folder, "predictions.pth"))

    extra_args = dict(
        box_only=box_only,
        iou_types=iou_types,
        motion_specific=motion_specific,
        expected_results=expected_results,
        expected_results_sigma_tol=expected_results_sigma_tol,
    )

    return evaluate(dataset=dataset,
                    predictions=predictions,
                    output_folder=output_folder,
                    **extra_args)