Example #1
0
def task_31():
    gt_annot_file = 'datasets/ai_challenge_s03_c010-full_annotation.xml'
    frames_path = 'datasets/AICity_data/train/S03/c010/data'


    det_bb = read_detections_file("datasets/AICity_data/train/S03/c010/det/our_results_finetune_faster.txt")
    gt_bb = read_xml_gt_options(gt_annot_file, False, False)
    gt_bb = filter_gt(gt_bb, ["car"])

    video_n_frames = number_of_images_jpg(frames_path)
    det_bb_max_iou, idd = tracking_iou(frames_path, copy.deepcopy(det_bb), video_n_frames, mode='of')
    ap_max_iou = calculate_ap(det_bb_max_iou, gt_bb, 0, video_n_frames, mode='sort')

    print("Ap after tracking with maximum IoU: {}".format(ap_max_iou))

    det_bb_max_iou, idd = tracking_iou(frames_path, copy.deepcopy(det_bb), video_n_frames, mode='other')
    ap_max_iou = calculate_ap(det_bb_max_iou, copy.deepcopy(gt_bb), 0, video_n_frames, mode='sort')
    print("Ap after tracking with maximum IoU: {}".format(ap_max_iou))


    new_tracks = restore_tracks(frames_path, det_bb_max_iou)
    ap_max_iou = calculate_ap(new_tracks, copy.deepcopy(gt_bb), 0, video_n_frames, mode='sort')
    print("Ap after tracking with maximum IoU and of: {}".format(ap_max_iou))



    ini_frame = 700
    end_frame = 900
    animation_tracks(new_tracks, idd, ini_frame, end_frame, frames_path)
Example #2
0
def task1(gt_file):
    gt = read_xml_gt(gt_file)
    classes_to_keep = ['car']
    gt = filter_gt(gt, classes_to_keep)

    det_bb = generate_noisy_annotations(gt)

    lst_gt = [item[0] for item in gt]
    last_frame = np.max(lst_gt)

    miou = 0
    for f_val in range(0, last_frame):
        frame_gt_bb = [gt[i] for i, num in enumerate(gt) if num[0] == f_val]

        frame_det_bb = [det_bb[i] for i, num in enumerate(det_bb) if num[0] == f_val]
        miou += frame_miou(frame_det_bb, frame_gt_bb, confidence=False)

    miou = miou/last_frame
    print("Noisy GT (Sorted Random): ", calculate_ap(det_bb, gt, 'random'))
    print("Noisy GT (Sorted by Area): ", calculate_ap(det_bb, gt, 'area'))


    preds_mask = read_detections_file("datasets/AICity_data/train/S03/c010/det/det_mask_rcnn.txt")
    print("maskrcnn ap: {}".format(calculate_ap(preds_mask, gt, 'sort')))

    preds_ssd = read_detections_file("datasets/AICity_data/train/S03/c010/det/det_ssd512.txt")
    print("ssd ap: {}".format(calculate_ap(preds_ssd, gt, 'sort')))

    preds_yolo = read_detections_file("datasets/AICity_data/train/S03/c010/det/det_yolo3.txt")
    print("yolo ap: {}".format(calculate_ap(preds_yolo, gt, 'sort')))
Example #3
0
def task11(images_path, gt_annot_file, config_file):
    files = get_files_from_dir(images_path, 'jpg')
    # we remove bikes and compute ap only for car class
    gt_bb = read_xml_gt(gt_annot_file)
    classes_to_keep = ['car']
    gt_bb = filter_gt(gt_bb, classes_to_keep)

    filename = "our_results_coco_{}.txt".format("faster" if "faster" in
                                                config_file else "retina")
    preds = inference(config_file,
                      files,
                      save_results=True,
                      out_filename=filename)

    ap50 = calculate_ap(preds, gt_bb, 0, len(files), mode='area')
    print(ap50)

    filename_gif = "faster_on_coco" if "faster" in config_file else "retina_on_coco"
    animation_2bb(
        filename_gif,
        '.gif',
        gt_bb,
        preds,
        images_path,
        ini=800,
    )
Example #4
0
def task22(gt_annot_file, detections_file, frames_path):
    ap_mode = 'area'
    #Read and filter detections
    det_bb = read_detections_file(detections_file)
    det_bb = filter_det_confidence(det_bb, threshold=0.5)
    #Read and filter gt
    gt_bb = read_xml_gt_options(gt_annot_file, False, False)
    gt_bb = filter_gt(gt_bb, ["car"])
    #Calculate original ap
    video_n_frames = number_of_images_jpg(frames_path)
    # Constant velocity
    det_bb_vel = kalman_filter_tracking(det_bb, video_n_frames, 0)
    # Constant acceleration
    det_bb_acc = kalman_filter_tracking(det_bb, video_n_frames, 1)
    #Print ap results
    print("Original ap: {}".format(
        calculate_ap(det_bb, gt_bb, 0, video_n_frames, mode=ap_mode)))
    print("Ap after tracking with constant velocity: {}".format(
        calculate_ap(det_bb_vel, gt_bb, 0, video_n_frames, mode=ap_mode)))
    print("Ap after tracking with constant acceleration: {}".format(
        calculate_ap(det_bb_acc, gt_bb, 0, video_n_frames, mode=ap_mode)))
    #Obtain animation
    max_id_vel = max(det_bb_vel, key=lambda x: x[2])[2]
    max_id_acc = max(det_bb_acc, key=lambda x: x[2])[2]
    ini_frame = 550
    end_frame = 650
Example #5
0
def task21(gt_annot_file, detections_file, frames_path):
    ap_mode = 'area'
    # Read and filter detections
    #    det_bb = read_detections_file(detections_file)

    #    det_bb = read_detections_file("datasets/AICity_data/train/S03/c010/det/det_mask_rcnn.txt")
    #    det_bb = read_detections_file("datasets/AICity_data/train/S03/c010/det/det_ssd512.txt")
    #    det_bb = read_detections_file("datasets/AICity_data/train/S03/c010/det/det_yolo3.txt")
    #    det_bb = read_detections_file("datasets/AICity_data/train/S03/c010/det/our_results_coco_faster.txt")

    det_bb = read_detections_file(
        "datasets/AICity_data/train/S03/c010/det/our_results_finetune_faster.txt"
    )
    det_bb = filter_det_confidence(det_bb, threshold=0.5)
    #Read and filter gt
    gt_bb = read_xml_gt_options(gt_annot_file, False, False)
    gt_bb = filter_gt(gt_bb, ["car"])

    video_n_frames = number_of_images_jpg(frames_path)

    original_ap = calculate_ap(det_bb, gt_bb, 0, video_n_frames, mode=ap_mode)

    det_bb_max_iou, idd = tracking_iou(copy.deepcopy(det_bb), video_n_frames)

    ap_max_iou = calculate_ap(det_bb_max_iou,
                              gt_bb,
                              0,
                              video_n_frames,
                              mode=ap_mode)

    print("Original ap: {}".format(original_ap))
    print("Ap after tracking with maximum IoU: {}".format(ap_max_iou))
    ini_frame = 550
    end_frame = 650
    animation_tracks(det_bb_max_iou, idd, ini_frame, end_frame, frames_path)
Example #6
0
def task2(gt_file, ini_frame):
    gt_bb = read_xml_gt(gt_file)
    classes_to_keep = ['car']
    gt_bb = filter_gt(gt_bb, classes_to_keep)

#    det_bb = generate_noisy_annotations(gt_bb)
#    det_bb = read_detections_file("datasets/AICity_data/train/S03/c010/det/det_mask_rcnn.txt")
#    det_bb = read_detections_file("datasets/AICity_data/train/S03/c010/det/det_ssd512.txt")
#    det_bb = read_detections_file("datasets/AICity_data/train/S03/c010/det/det_yolo3.txt")
    det_bb = read_detections_file("datasets/AICity_data/train/S03/c010/det/our_results_finetune_faster.txt")
    fps = 10
    seconds = 30
    animation_2bb("bb_frames_our_faster", ".gif", gt_bb, det_bb, "datasets/AICity_data/train/S03/c010/data/", fps, seconds,
              ini_frame, int(1920 / 4), int(1080 / 4))

    mious = []
    for f_val in range(ini_frame, ini_frame+int(seconds*fps)):
        frame_gt_bb = [gt_bb[i] for i, num in enumerate(gt_bb) if num[0] == f_val]

        frame_det_bb = [det_bb[i] for i, num in enumerate(det_bb) if num[0] == f_val]
        mious.append(frame_miou(frame_det_bb, frame_gt_bb, confidence=False))

    xx = np.arange(ini_frame, ini_frame+int(seconds*fps))
    plt.figure()
    plt.ylim((0,1))
    plt.xlabel('Frame')
    plt.ylabel('mIoU')
##    plt.plot(xx, yolo, label = 'YOLOv3')
##    plt.plot(xx, mask, label = 'Mask R-CNN')
##    plt.plot(xx, ssd, label = 'SSD')
    plt.plot(xx, retina, label = 'RetinaNet')      
    plt.plot(xx, faster, label = 'Faster R-CNN')    
    plt.plot(xx, our_faster, label = 'Faster R-CNN (Fine tuned)')
    plt.legend()
        
    frames = list(range(0, len(mious)))
    ani = plot_animation(frames, mious, 'Frame', 'IoU', [0,1], fps)
    ani.save('iou_our_faster.gif')
Example #7
0
def task12(images_path, config_file, dataset_annot_file, gt_annot_file):

    gt_bb = read_xml_gt(gt_annot_file)
    classes_to_keep = ['car']
    gt_bb = filter_gt(gt_bb, classes_to_keep)

    filename = "our_results_finetune_{}.txt".format("faster" if "faster" in
                                                    config_file else "retina")
    det_bb = train(config_file,
                   images_path,
                   dataset_annot_file,
                   out_filename=filename)
    ap50 = calculate_ap(det_bb, gt_bb, 0, 2140, mode='area')
    print(ap50)

    filename_gif = "faster_finetune" if "faster" in config_file else "retina_finetune"
    animation_2bb(
        filename_gif,
        '.gif',
        gt_bb,
        det_bb,
        images_path,
        ini=800,
    )
Example #8
0
def task2(frames_path, gt_path, color_space=cv2.COLOR_BGR2GRAY, channels=(0)):

    grid_search = False
    save_videos = False
    fine_tune_search = False
    videos_rgb_bb = True

    gt_bb = read_xml_gt_options(gt_path, True, True)

    classes_to_keep = ['car', 'bike']
    gt_bb = filter_gt(gt_bb, classes_to_keep)

    video_n_frames = number_of_images_jpg(frames_path)
    mu, sigma = bg_model(frames_path, color_space)

    if grid_search:
        mAPs = []
        alphas = [2, 2.5, 3, 3.5, 4, 4.5]
        rhos = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
        for alpha in alphas:
            mAP_same_alfa = []
            for rho in rhos:
                det_bb = remove_bg(mu,
                                   sigma,
                                   alpha,
                                   frames_path,
                                   int(video_n_frames * 0.25),
                                   video_n_frames,
                                   color_space=color_space,
                                   adaptive=True,
                                   rho=rho,
                                   channels=channels)
                mAP = calculate_ap(det_bb,
                                   gt_bb,
                                   int(video_n_frames * 0.25),
                                   video_n_frames,
                                   mode='area')
                mAP_same_alfa.append(mAP)
                print("Alpha: {:2f} | Rho: {:2f} | mAP: {:2f} |".format(
                    alpha, rho, mAP))
            mAPs.append(mAP_same_alfa)

        # Plot the surface
        X, Y = np.meshgrid(alphas, rhos)
        fig = plt.figure()
        ax = fig.gca(projection='3d')
        surf = ax.plot_surface(X,
                               Y,
                               mAPs,
                               rstride=0.1,
                               cstride=0.1,
                               cmap='viridis',
                               edgecolor='none')
        ax.set_xlabel('Alpha')
        ax.set_ylabel('Rho')
        ax.set_zlabel('mAP')
        # Add a color bar which maps values to colors.
        fig.colorbar(surf, shrink=0.5, aspect=5)
        plt.savefig("_grid_search.png")
        plt.show()

    if save_videos:
        alpha = 3.2
        rho = 0.02
        det_bb = remove_bg(mu,
                           sigma,
                           alpha,
                           frames_path,
                           800,
                           900,
                           rho=rho,
                           denoise=True,
                           animation=True,
                           color_space=color_space,
                           adaptive=True,
                           channels=channels)

    if fine_tune_search:
        mAPs = []
        alphas = [
            3, 3, 3, 3.5, 3.5, 3.5, 4, 4, 4, 3.5, 3.5, 3.5, 3.5, 3.7, 3.2, 3.1,
            3.3
        ]
        rhos = [
            0.0, 0.2, 0.4, 0.0, 0.2, 0.4, 0.0, 0.2, 0.4, 0.1, 0.3, 0.15, 0.05,
            0.03, 0.02, 0.01, 0.01
        ]
        for i in range(0, len(alphas)):
            det_bb = remove_bg(mu,
                               sigma,
                               alphas[i],
                               frames_path,
                               int(video_n_frames * 0.25),
                               video_n_frames,
                               color_space=color_space,
                               adaptive=True,
                               rho=rhos[i],
                               denoise=True)
            mAP = calculate_ap(det_bb,
                               gt_bb,
                               int(video_n_frames * 0.25),
                               video_n_frames,
                               mode='area')
            print("Alpha: {:2f} | Rho: {:2f} | mAP: {:2f} |".format(
                alphas[i], rhos[i], mAP))
            mAPs.append(mAP)

        fig, ax = plt.subplots()
        plt.xlim(2.9, 4.1)
        plt.ylim(-0.1, 0.5)
        ax.set_aspect(1)
        ax.set_xlabel('Alpha')
        ax.set_ylabel('Rho')

        for i in range(0, len(mAPs)):
            circle = plt.Circle((alphas[i], rhos[i]), mAPs[i] / 25)
            ax.add_artist(circle)

        plt.savefig("fine_tune_search.png", bbox_inches='tight')
        plt.show()
    if videos_rgb_bb:
        alpha = 2.5
        rho = 0.02
        det_bb = remove_bg(mu,
                           sigma,
                           alpha,
                           frames_path,
                           int(video_n_frames * 0.25),
                           video_n_frames,
                           color_space=color_space,
                           adaptive=True,
                           rho=rho,
                           denoise=True,
                           channels=channels)
        print("Calculating AP")
        mAP = calculate_ap(det_bb,
                           gt_bb,
                           int(video_n_frames * 0.25),
                           video_n_frames,
                           mode='area')
        animation_2bb('rgb_bb_adaptive', '.gif', gt_bb, det_bb, frames_path,
                      10, 10, 800, int(1920 / 4), int(1080 / 4))
        print(f"Channels: {channels}, Colorspace: {color_space}, mAP: {mAP}")