def compute_util_amplification_factor(combined_util_matrix,
                                      frame_dirs,
                                      bin_files,
                                      final_bin_size,
                                      filter_color,
                                      filter_pixel_fraction,
                                      training_ratio,
                                      max_weight=None):
    max_util = 0
    for idx in range(len(frame_dirs)):
        bin_file = bin_files[idx]
        frame_dir = frame_dirs[idx]
        video_name = basename(bin_file)[:-4]

        # Extracting the features from training data samples
        observations = python_server.mapping_features.read_training_file(
            bin_file, absolute_pixel_count=False)
        training_samples = python_server.mapping_features.read_samples(
            bin_file)
        num_frame_files = len([
            o for o in listdir(frame_dir) if isfile(join(frame_dir, o))
            and o.startswith("frame_") and o.endswith(".txt")
        ])
        num_training_frames = int(training_ratio * num_frame_files)

        # Iterating over the training part of each video
        for frame_id in range(num_training_frames):
            sample = training_samples[frame_id]
            label = sample.label

            # Determine if Pixel Fraction is higher than threshold
            high_pf = plot_sv_weight_heatmap.is_pf_high(
                filter_color, filter_pixel_fraction, observations[frame_id])
            sv_weights = plot_sv_weight_heatmap.read_frame_sv_weights(
                join(frame_dir, "frame_%d.txt" % frame_id))
            res = plot_sv_weight_heatmap.readjust_sv_weights(sv_weights)

            if res == False or high_pf == False:
                continue

            aggr = plot_sv_weight_heatmap.aggregate_sv_weights(
                sv_weights, final_bin_size)

            util = 0
            for row in range(len(combined_util_matrix)):
                for col in range(len(combined_util_matrix[row])):
                    util += combined_util_matrix[row][col] * aggr[row][col]
            max_util = max(util, max_util)

    util_amplification_factor = 1.0 / max_util
    return util_amplification_factor
def main(frame_dirs,
         bin_files,
         outdir,
         final_bin_size,
         filter_color,
         filter_pixel_fraction,
         training_ratio,
         max_weight=None):
    if not validate_inputs(frame_dirs, bin_files):
        print("Invalid input. Exiting")
        exit(1)

    (util_matrix,
     total_matrix) = plot_sv_weight_heatmap_cross_video.build_util_matrix(
         frame_dirs, bin_files, final_bin_size, filter_color,
         filter_pixel_fraction, training_ratio, max_weight)

    # Plotting the SV heatmap
    plot_sv_weight_heatmap_cross_video.plot_sv_heatmap(
        util_matrix, total_matrix, max_weight, filter_color,
        filter_pixel_fraction, outdir, final_bin_size)

    # The second part of the script builds a utility model from the (s,v) 2D array
    combined_util_matrix = plot_sv_weight_heatmap_cross_video.compute_combined_util_matrix(
        util_matrix)

    # Now need to normalize the util matrix over training data
    plot_sv_weight_heatmap.normalize_combined_util_matrix(combined_util_matrix)
    util_amplification_factor = plot_sv_weight_heatmap_cross_video.compute_util_amplification_factor(
        combined_util_matrix, frame_dirs, bin_files, final_bin_size,
        filter_color, filter_pixel_fraction, training_ratio, max_weight)
    print(util_amplification_factor)
    for row in range(len(combined_util_matrix)):
        for col in range(len(combined_util_matrix[row])):
            combined_util_matrix[row][col] *= util_amplification_factor

    raw_data = []

    for idx in range(len(frame_dirs)):
        bin_file = bin_files[idx]
        frame_dir = frame_dirs[idx]
        video_name = basename(bin_file)[:-4]

        # Extracting the features from training data samples
        observations = python_server.mapping_features.read_training_file(
            bin_file, absolute_pixel_count=False)
        training_samples = python_server.mapping_features.read_samples(
            bin_file)

        num_frame_files = len([
            o for o in listdir(frame_dir) if isfile(join(frame_dir, o))
            and o.startswith("frame_") and o.endswith(".txt")
        ])

        num_training_frames = training_ratio * num_frame_files

        frame_id = 0
        for sample in training_samples:
            label = sample.label

            # Determine if Pixel Fraction is higher than threshold
            high_pf = plot_sv_weight_heatmap.is_pf_high(
                filter_color, filter_pixel_fraction, observations[frame_id])
            sv_weights = plot_sv_weight_heatmap.read_frame_sv_weights(
                join(frame_dir, "frame_%d.txt" % frame_id))
            res = plot_sv_weight_heatmap.readjust_sv_weights(sv_weights)

            if res == False or high_pf == False or frame_id < num_training_frames:
                frame_id += 1
                continue

            aggr = plot_sv_weight_heatmap.aggregate_sv_weights(
                sv_weights, final_bin_size)

            util = 0
            for row in range(len(combined_util_matrix)):
                for col in range(len(combined_util_matrix[row])):
                    util += combined_util_matrix[row][col] * aggr[row][col]

            count = 0
            if label:
                label = 1
                count = sample.detections.totalDetections
            else:
                label = 0
            raw_data.append([frame_id, label, util, video_name, count])
            frame_id += 1

    df = pd.DataFrame(
        raw_data, columns=["frame_id", "label", "util", "video_name", "count"])
    df.to_csv(join(outdir, "sv_utils_cross_video.csv"))
    max_util = df["util"].max()
    df["util"] = df["util"] / float(max_util)

    plt.close()
    fig, ax = plt.subplots()
    sns.ecdfplot(data=df, x="util")
    ax.set_xlabel("Utility of frame")
    fig.savefig(join(outdir, "util_cdf_CROSSVIDEO.png"), bbox_inches="tight")

    plt.close()
    vid_grouping = df.groupby("video_name")
    num_vids = len(df["video_name"].unique())
    cols = 3
    rows = math.ceil(num_vids / float(cols))
    fig, axs = plt.subplots(nrows=rows, ncols=cols, figsize=(24, 8))
    vid_idx = 0
    for vid in vid_grouping.groups.keys():
        vid_df = vid_grouping.get_group(vid)
        row = int(vid_idx / cols)
        col = vid_idx - row * cols
        ax = axs[row][col]

        sns.lineplot(data=vid_df, x="frame_id", y="util", ax=ax, color="blue")
        ax2 = ax.twinx()
        sns.lineplot(data=vid_df, x="frame_id", y="count", ax=ax2, color="red")
        ax.set_ylim([0, 1])
        ax.set_title(vid, fontsize=10)
        ax.set_ylabel("Frame utility")
        ax.yaxis.label.set_color("blue")
        ax2.set_ylabel("#target objects")
        ax2.yaxis.label.set_color("red")

        vid_idx += 1
    fig.savefig(join(outdir, "util_count.png"), bbox_inches="tight")

    plt.close()
    fig, axs = plt.subplots(nrows=rows, ncols=cols, figsize=(24, 8))
    vid_idx = 0
    for vid in vid_grouping.groups.keys():
        vid_df = vid_grouping.get_group(vid)
        row = int(vid_idx / cols)
        col = vid_idx - row * cols
        ax = axs[row][col]

        neg_frames = [[], []]
        pos_frames = [[], []]
        for i, row in vid_df.iterrows():
            if row["count"] == 0:
                neg_frames[0].append(row["frame_id"])
                neg_frames[1].append(row["util"])
            else:
                pos_frames[0].append(row["frame_id"])
                pos_frames[1].append(row["util"])

        ax.scatter(pos_frames[0], pos_frames[1], color="red", marker=".")
        ax.scatter(neg_frames[0], neg_frames[1], color="blue", marker=".")
        ax.set_ylim([0, 1])
        ax.set_title(vid, fontsize=10)
        ax.set_ylabel("Frame utility")

        vid_idx += 1
    fig.savefig(join(outdir, "util_segregated.png"), bbox_inches="tight")

    plt.close()
    fig, ax = plt.subplots(figsize=(6, 2))
    sns.boxplot(data=df, x="video_name", y="util", hue="label", ax=ax)
    ax.set_xlabel("Label of the frame")
    ax.set_ylabel("Utility")
    ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
    fig.savefig(join(outdir,
                     "combined_util_BIN_%d_CROSSVIDEO.png" % (final_bin_size)),
                bbox_inches="tight")
def build_util_matrix(frame_dirs, bin_files, final_bin_size, filter_color,
                      filter_pixel_fraction, training_ratio, max_weight):
    # To store information across videos
    util_matrix = None
    total_matrix = None

    for idx in range(len(frame_dirs)):
        bin_file = bin_files[idx]
        frame_dir = frame_dirs[idx]

        video_name = basename(bin_file)[:-4]

        # Extracting the features from training data samples
        observations = python_server.mapping_features.read_training_file(
            bin_file, absolute_pixel_count=False)
        training_samples = python_server.mapping_features.read_samples(
            bin_file)

        num_frame_files = len([
            o for o in listdir(frame_dir) if isfile(join(frame_dir, o))
            and o.startswith("frame_") and o.endswith(".txt")
        ])

        if num_frame_files != len(training_samples):
            print(
                "Mismatch b/w num_frames(%d) and num_training_samples(%d) for video %s. Exiting."
                % (num_frame_files, len(training_samples), video_name))
            exit(1)

        num_training_frames = training_ratio * num_frame_files

        frame_id = 0
        for sample in training_samples:
            if frame_id > num_training_frames:
                break

            label = sample.label
            # Determine if Pixel Fraction is higher than threshold
            high_pf = plot_sv_weight_heatmap.is_pf_high(
                filter_color, filter_pixel_fraction, observations[frame_id])
            # Read the raw SV weights from frame directory
            sv_weights = plot_sv_weight_heatmap.read_frame_sv_weights(
                join(frame_dir, "frame_%d.txt" % frame_id))
            # Readjust the SV weights since we are not counting S=0 and V=0 pixels
            res = plot_sv_weight_heatmap.readjust_sv_weights(sv_weights)

            if res == False or high_pf == False:
                frame_id += 1
                continue

            # Initializing the utility matrix
            if util_matrix == None:
                util_matrix = initialize_util_matrix(final_bin_size)
                total_matrix = {True: 0, False: 0}

                # Check that the final_bin_size is a factor of original bin size
                orig_size = len(sv_weights)
                q = int(orig_size / final_bin_size)
                if final_bin_size * q != orig_size:
                    print(
                        "Final bin size %d is not a factor of original bin size %d"
                        % (final_bin_size, orig_size))

            # Aggregating the SV weights into the final SV bins
            aggr = plot_sv_weight_heatmap.aggregate_sv_weights(
                sv_weights, final_bin_size)

            # Now contributing to the average across frames
            for row in range(final_bin_size):
                for col in range(final_bin_size):
                    util_matrix[label][row][col].append(aggr[row][col])

            total_matrix[label] += 1

            frame_id += 1

    # Computing the average value for each bin
    for l in util_matrix:
        for row in range(len(util_matrix[l])):
            for col in range(len(util_matrix[l][row])):
                m = np.mean(util_matrix[l][row][col])
                util_matrix[l][row][col] = m

    return (util_matrix, total_matrix)
Exemplo n.º 4
0
def main(frame_dirs, bin_files, outdir, final_bin_size, filter_color, filter_pixel_fraction, training_ratio, max_weight=None):
    if not validate_inputs(frame_dirs, bin_files):
        print ("Invalid input. Exiting")
        exit(1)

    (util_matrix, total_matrix) = build_util_matrix(frame_dirs, bin_files, final_bin_size, filter_color, filter_pixel_fraction, training_ratio, max_weight)

    # Plotting the SV heatmap
    plot_sv_heatmap(util_matrix, total_matrix, max_weight, filter_color, filter_pixel_fraction, outdir, final_bin_size)

    # The second part of the script builds a utility model from the (s,v) 2D array
    combined_util_matrix = compute_combined_util_matrix(util_matrix)

    plot_sv_weight_heatmap.normalize_combined_util_matrix(combined_util_matrix)

    # Now need to normalize the util matrix over training data
    max_util = 0
    for idx in range(len(frame_dirs)):
        bin_file = bin_files[idx]
        frame_dir = frame_dirs[idx]
        video_name = basename(bin_file)[:-4]

        # Extracting the features from training data samples
        observations = python_server.mapping_features.read_training_file(bin_file, absolute_pixel_count=False)
        training_samples = python_server.mapping_features.read_samples(bin_file)
        num_frame_files = len([o for o in listdir(frame_dir) if isfile(join(frame_dir, o)) and o.startswith("frame_") and o.endswith(".txt")])
        num_training_frames = int(training_ratio * num_frame_files)

        # Iterating over the training part of each video
        for frame_id in range(num_training_frames):
            sample = training_samples[frame_id]
            label = sample.label

            # Determine if Pixel Fraction is higher than threshold
            high_pf = plot_sv_weight_heatmap.is_pf_high(filter_color, filter_pixel_fraction, observations[frame_id])
            sv_weights = plot_sv_weight_heatmap.read_frame_sv_weights(join(frame_dir, "frame_%d.txt"%frame_id))
            res = plot_sv_weight_heatmap.readjust_sv_weights(sv_weights)

            if res == False or high_pf == False:
                continue

            aggr = plot_sv_weight_heatmap.aggregate_sv_weights(sv_weights, final_bin_size)

            util = 0
            for row in range(len(combined_util_matrix)):
                for col in range(len(combined_util_matrix[row])):
                    util += combined_util_matrix[row][col] * aggr[row][col]
            max_util = max(util, max_util)

    util_amplification_factor = 1.0/max_util
    print (util_amplification_factor)
    for row in range(len(combined_util_matrix)):
        for col in range(len(combined_util_matrix[row])):
            combined_util_matrix[row][col] *= util_amplification_factor

    raw_data = []

    for idx in range(len(frame_dirs)):
        bin_file = bin_files[idx]
        frame_dir = frame_dirs[idx]
        video_name = basename(bin_file)[:-4]

        # Extracting the features from training data samples
        observations = python_server.mapping_features.read_training_file(bin_file, absolute_pixel_count=False)
        training_samples = python_server.mapping_features.read_samples(bin_file)

        num_frame_files = len([o for o in listdir(frame_dir) if isfile(join(frame_dir, o)) and o.startswith("frame_") and o.endswith(".txt")])

        num_training_frames = training_ratio * num_frame_files

        frame_id = 0
        for sample in training_samples:
            label = sample.label

            # Determine if Pixel Fraction is higher than threshold
            high_pf = plot_sv_weight_heatmap.is_pf_high(filter_color, filter_pixel_fraction, observations[frame_id])
            sv_weights = plot_sv_weight_heatmap.read_frame_sv_weights(join(frame_dir, "frame_%d.txt"%frame_id))
            res = plot_sv_weight_heatmap.readjust_sv_weights(sv_weights)

            if res == False or high_pf == False or frame_id < num_training_frames:
                frame_id += 1
                continue

            aggr = plot_sv_weight_heatmap.aggregate_sv_weights(sv_weights, final_bin_size)

            util = 0
            for row in range(len(combined_util_matrix)):
                for col in range(len(combined_util_matrix[row])):
                    util += combined_util_matrix[row][col] * aggr[row][col]

            if label:
                label = 1
            else:
                label = 0
            raw_data.append([frame_id, label, util, video_name])
            frame_id += 1

    df = pd.DataFrame(raw_data, columns=["frame_id", "label", "util", "video_name"])
    max_util = df["util"].max()
    df["util"] = df["util"]/float(max_util)
    plt.close()
    fig, ax = plt.subplots(figsize=(6,2))
    sns.boxplot(data=df, x="video_name", y="util", hue="label", ax=ax)
    ax.set_xlabel("Label of the frame")
    ax.set_ylabel("Utility")
    ax.set_xticklabels(ax.get_xticklabels(),rotation = 90)
    fig.savefig(join(outdir, "combined_util_BIN_%d_CROSSVIDEO.png"%(final_bin_size)), bbox_inches="tight")