Esempio n. 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)
Esempio n. 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')))
Esempio n. 3
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
Esempio n. 4
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)
Esempio n. 5
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,
    )
Esempio n. 6
0
def task1(frames_path,
          detections_file,
          gt_file,
          tracking_method,
          postprocessing=True,
          visualization=False):
    print("Start loading data")
    # Read gt
    gt_bb = read_gt_txt(gt_file)

    # Read and filter detections
    det_bb = read_detections_file(detections_file)
    det_bb = filter_det_confidence(det_bb, threshold=0.5)

    # Tracking
    print("Start tracking")
    video_n_frames = number_of_images_jpg(frames_path)
    if tracking_method == "MaxOverlap":
        det_bb_tracking, id_max = tracking_iou(frames_path,
                                               copy.deepcopy(det_bb),
                                               video_n_frames,
                                               mode='other')
    elif tracking_method == "Kalman":
        det_bb_tracking = kalman_filter_tracking(copy.deepcopy(det_bb),
                                                 video_n_frames, 0)
        id_max = max([v[2] for v in det_bb_tracking])
    else:
        raise NotImplemented

    # Postprocessing
    if postprocessing:
        print("Starting postprocessing tracking")
        det_bb_tracking = clean_tracks(copy.deepcopy(det_bb_tracking), id_max)
        det_bb_tracking = remove_parked(copy.deepcopy(det_bb_tracking),
                                        id_max,
                                        threshold=10.0)

    # Results
    print("Start obtaining metrics")
    ap = calculate_ap(det_bb_tracking,
                      copy.deepcopy(gt_bb),
                      0,
                      video_n_frames,
                      mode='sort')
    print("Average precision: {}".format(ap))

    # Create animation if required
    if visualization:
        print("Start storing animation")
        ini_frame = 700
        end_frame = 800
        frames_path = 'datasets/AICity_data/train/S03/c010/data'
        animation_tracks(det_bb_tracking, id_max, ini_frame, end_frame,
                         frames_path)

    return det_bb_tracking
Esempio n. 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,
    )
Esempio n. 8
0
path = 'C:/Users/Sara/Documents/GitHub/Visual-recognition/week6/testing_sara/KITTI-MOTS/instances_txt/0014.txt'
gt_bb = read_gt_txt(path, mode='car')
det_bb = read_det(cat='car')

frame_path = "C:/Users/Sara/Documents/GitHub/Visual-recognition/week6/testing_sara/KITTI-MOTS/training/image_02/0014"

video_n_frames = number_of_images(frame_path)
det_bb_max_iou, idd = tracking_iou(frame_path,
                                   copy.deepcopy(det_bb),
                                   video_n_frames,
                                   mode='normal')

ap_max_iou = calculate_ap(det_bb_max_iou,
                          copy.deepcopy(gt_bb),
                          0,
                          video_n_frames,
                          mode='area')

frame_with_2bb(gt_bb, det_bb, frame_path, 10)

animation_tracks(det_bb_max_iou, idd, 0, video_n_frames, frame_path)

f = open('0014.txt', 'w')
for ele in det_bb_max_iou:
    f.write(str(ele[0]) + ' ')
    f.write(str(ele[2]) + ' ')
    f.write(str(ele[1]) + ' ')
    f.write(str(ele[8]) + ' ')
    f.write(str(ele[9]) + ' ')
    f.write(str(ele[10]) + '\n')
Esempio n. 9
0
def task1(gt_path, color_space=cv2.COLOR_BGR2GRAY):
    path = 'datasets/AICity_data/train/S03/c010'
    save_frames(path)
    print("Finished saving")

    frames_path = 'datasets/AICity_data/train/S03/c010/data'
    video_n_frames = number_of_images_jpg(frames_path)

    # this is very time consuming, we should avoid comuting it more than once.
    mu, sigma = bg_model(frames_path, color_space)

    # Gaussian plot in the slides
    xx = np.linspace(0, 255, 1000)
    exp = 1 / (sigma[561, 712] * np.sqrt(2 * np.pi)) * \
          np.exp(-(xx - mu[561, 712]) ** 2 / (2 * sigma[561, 712] ** 2))
    plt.figure()
    plt.plot(xx, exp, color='blue', label='Distribution')
    plt.axvline(x=mu[561, 712], color='red', label='Mean')
    plt.axvline(x=mu[561, 712] - (sigma[561, 712] + 2),
                linestyle='--',
                color='green',
                label='Tolerance ' + r'$\alpha = 1$')
    plt.axvline(x=mu[561, 712] + (sigma[561, 712] + 2),
                linestyle='--',
                color='green')
    plt.axvline(x=mu[561, 712] - 3 * (sigma[561, 712] + 2),
                linestyle='--',
                color='purple',
                label='Tolerance ' + r'$\alpha = 5$')
    plt.axvline(x=mu[561, 712] + 3 * (sigma[561, 712] + 2),
                linestyle='--',
                color='purple')
    plt.legend()

    alphas = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7]
    aps7 = []

    # Test for different alpha values
    for a in alphas:
        det_bb = remove_bg(mu,
                           sigma,
                           a,
                           frames_path,
                           int(video_n_frames * 0.25),
                           video_n_frames,
                           animation=False,
                           denoise=True,
                           adaptive=False,
                           color_space=color_space)

        gt_bb = read_xml_gt_options(gt_path, True, True)

        ap = calculate_ap(det_bb,
                          gt_bb,
                          int(video_n_frames * 0.25),
                          video_n_frames,
                          mode='area')
        animation_2bb('try_dnoise', '.gif', gt_bb, det_bb, frames_path, 10, 10,
                      int(video_n_frames * 0.25), int(1920 / 4), int(1080 / 4))

        print(a, ap)
        aps7.append(ap)

    plt.title('Median Filter')
    #    plt.plot(alphas, aps3, label = 'Window size 3')
    #    plt.plot(alphas, aps5, label = 'Window size 5')
    plt.plot(alphas, aps7, label='Window size 7')
    plt.xlabel(r'$\alpha$')
    plt.ylabel('mAP')
    plt.legend()
Esempio n. 10
0
def task3(frames_path,
          annots_file,
          estimation_percent=.25,
          save_to_disk=False,
          save_size=(480, 270)):
    ims = get_files_from_dir(frames_path, "jpg")
    gt_bb = read_xml_gt_options(annots_file, True, True)
    save_to_disk = False

    init_images = int(len(ims) * estimation_percent)
    first_eval_image = init_images + 1
    modes = ['mog', 'knn', 'gmg', 'LSBP']

    model_params = {
        'mog': {
            # 'varThreshold': 32
        },
        'knn': {
            # 'dist2Threshold': 350
        },
        'gmg': {
            # 'initializationFrames': init_images
        },
        'LSBP': {},
    }

    for mode in modes:
        bg_sub = bg_estimation(mode, **model_params[mode])
        det_bb = []
        if save_to_disk:
            frames = np.zeros(
                (len(ims) - first_eval_image, save_size[1], save_size[0]))

        for i, im in enumerate(ims):
            im = cv2.imread(im, 0)
            fg = bg_sub.apply(im)
            fg[fg != 255] = 0

            # fg = denoise_bg(fg)

            if i >= first_eval_image:
                det_bb += fg_segmentation_to_boxes(fg, i)

            if save_to_disk:
                fg = cv2.resize(fg, save_size)
                frames[i - first_eval_image, ...] = fg

        ap = calculate_ap(det_bb,
                          gt_bb,
                          init_images + 1,
                          len(ims),
                          mode='area')
        if save_to_disk:
            animation_2bb(
                '{}_bg'.format(mode),
                '.gif',
                gt_bb,
                det_bb,
                frames_path,
                ini=800,
            )
            frames_to_gif("fg_{}.gif".format(mode), frames[:100])

        print("{} - {}".format(mode, ap))
Esempio n. 11
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}")