Ejemplo n.º 1
0
def reconstruct_ann(dic):
    result = []
    for obj in dic:
        ratio = COCO.compute_ratio(obj)
        obj["ratio"] = ratio
        result.append(obj)

    return result
Ejemplo n.º 2
0
def PlotPiecewiseBars(dataset, num_subfigs, sorted=False):
    ratios = {
        "0-1/5": [0, 1 / 5],
        "1/5-1/3": [1 / 5, 1 / 3],
        "1/3-1": [1 / 3, 1]
    }
    dicts = list(DatasetCatalog.get(dataset))

    metadata = MetadataCatalog.get(dataset)
    labels = metadata.thing_classes
    num_thing_per_fig = len(labels) // num_subfigs
    bars = dict()
    for key in ratios.keys():
        bars[key] = [0 for _ in range(len(labels))]

    all_ratios = []
    for dic in tqdm(dicts):
        for obj in dic["annotations"]:
            ratio = COCO.compute_ratio(obj, oriented=True)["ratio"]
            for key, ratio_range in ratios.items():
                if between(ratio, ratio_range):
                    bars[key][obj["category_id"]] += 1
            all_ratios.append(ratio)
    slender_ratios = [0 for _ in range(len(labels))]
    for i in range(len(labels)):
        slender_ratios[i] = bars["0-1/5"][i] / \
            (bars["0-1/5"][i]+bars["1/5-1/3"][i]+bars["1/3-1"][i])
    slender_ratios = np.array(slender_ratios)
    if sorted == True:
        sorted_indexes = np.argsort(slender_ratios)
    fig, axes = plt.subplots(num_subfigs, 1)
    for fig_i in range(num_subfigs):
        if sorted == True:
            index_range = sorted_indexes[num_thing_per_fig *
                                         fig_i:num_thing_per_fig * (fig_i + 1)]
        else:
            index_range = np.arrange(num_thing_per_fig * fig_i,
                                     num_thing_per_fig * (fig_i + 1))
        label = []
        for ind_i in index_range:
            label.append(labels[ind_i])

        axes[fig_i].set_yscale("symlog")
        prev = np.zeros((len(label), ))
        for key, bar in bars.items():
            bar = np.array(bar)
            axes[fig_i].bar(label, bar[index_range], bottom=prev, label=key)
            prev = prev + bar[index_range]
        axes[fig_i].legend()
        fig.set_size_inches(15, 15)
        axes[fig_i].set_xticklabels(label, rotation="vertical")
    plt.savefig("barchart.jpg")
Ejemplo n.º 3
0
def repeat_factors_from_ratios(dataset_dicts):
    rep_factors = []
    for dataset_dict in dataset_dicts:
        rep_factor = 0.1
        for ann in dataset_dict["annotations"]:
            ratio = COCO.compute_ratio(ann)["ratio"]
            if ratio < 1 / 5:
                rep_factor = 1
                break
            if ratio < 1 / 3:
                rep_factor = 0.5
        rep_factors.append(rep_factor)
    return torch.tensor(rep_factors, dtype=torch.float32)
Ejemplo n.º 4
0
def PlotPie(dataset, ratios):
    dicts = list(DatasetCatalog.get(dataset))

    metadata = MetadataCatalog.get(dataset)
    labels = metadata.thing_classes
    ratios_num = dict()
    for key in ratios.keys():
        ratios_num[key] = 0

    for dic in tqdm(dicts):
        for obj in dic["annotations"]:
            ratio = COCO.compute_ratio(obj, oriented=True)["ratio"]
            for key, ratio_range in ratios.items():
                if between(ratio, ratio_range):
                    ratios_num[key] += 1

    plt.pie(ratios_num.values(), labels=ratios_num.keys(), autopct='%1.2f%%')
    plt.savefig("PieChart.jpg")
    return ratios_num
Ejemplo n.º 5
0
def main(input_path, coco_json_path, output_path):
    origin_dict = json.load(smart_path(input_path).open("rt"))
    coco_dict = json.load(smart_path(coco_json_path).open("rt"))
    origin_coco = COCO(input_path)
    coco = COCO(coco_json_path)

    anns = []
    images = list()
    image_ids = set()
    for ann in origin_dict["annotations"]:
        ratio = COCO.compute_ratio(ann)["ratio"]
        if ratio > 1 / 3:
            continue
        ann["ratio"] = ratio
        ann["id"] = ann["id"] + 1 << 31

        image_id = ann["image_id"] + 1 << 31
        image = origin_coco.imgs[ann["image_id"]]

        image["id"] = image_id
        ann["image_id"] = image_id

        anns.append(ann)

        if not image_id in image_ids:
            images.append(image)
            image_ids.add(image_id)

    info = dict(date_created=str(datetime.datetime.now()),
                description="Merged from {} and {}.".format(
                    input_path, coco_json_path))
    new_coco_dict = dict(info=copy.deepcopy(info),
                         categories=copy.deepcopy(coco_dict["categories"]),
                         licenses=None,
                         annotations=copy.deepcopy(coco_dict["annotations"]) +
                         copy.deepcopy(anns),
                         images=copy.deepcopy(coco_dict["images"]) +
                         copy.deepcopy(images))

    with open(output_path, "wt") as writer:
        json.dump(new_coco_dict, writer)
Ejemplo n.º 6
0
        all_count = 0
        empty_count = 0
        problematic_count = 0
        for dic in tqdm.tqdm(dicts):
            for obj in dic["annotations"]:
                all_count += 1
                if not "segmentation" in obj or len(obj["segmentation"]) == 0:
                    empty_count += 1
                    continue
                try:
                    segmentations = PolygonMasks([obj["segmentation"]])
                    ratio = segmentations.get_ratios(oriented=True)[0]
                    print(ratio)
                except:
                    problematic_count += 1
                ratio = COCO.compute_ratio(obj)

            img = utils.convert_PIL_to_numpy(
                Image.open(
                    io.BytesIO(
                        smart_path("s3://detection/" +
                                   dic["file_name"]).read_bytes())), "RGB")
            visualizer = Visualizer(img, metadata=metadata, scale=scale)
            vis = visualizer.draw_dataset_dict(dic)
            output(vis, os.path.basename(dic["file_name"]))

        print("all", all_count)
        print("empty", empty_count)
        print("problematic", problematic_count)
Ejemplo n.º 7
0
def PlotAll(dataset):

    dicts = list(DatasetCatalog.get(dataset))

    metadata = MetadataCatalog.get(dataset)
    labels = metadata.thing_classes
    ratios = {
        "0-1/5": [0, 1 / 5],
        "1/5-1/3": [1 / 5, 1 / 3],
        "1/3-1": [1 / 3, 1]
    }

    bars = dict()
    for key in ratios.keys():
        bars[key] = [0 for _ in range(len(labels))]

    all_ratios = []
    ratio_counts = {k: 0 for k in ratios.keys()}
    for dic in tqdm(dicts):
        for obj in dic["annotations"]:
            ratio = COCO.compute_ratio(obj, oriented=True)["ratio"]
            for key, ratio_range in ratios.items():
                if between(ratio, ratio_range):
                    bars[key][obj["category_id"]] += 1
                    ratio_counts[key] += 1
            all_ratios.append(ratio)

    print("images", len(dicts))
    print("counts", ratio_counts)

    fig, ax = plt.subplots()
    ax.set_yscale("symlog")
    prev = np.zeros((len(labels), ))
    for key, bar in bars.items():
        bar = np.array(bar)
        ax.bar(labels, bar, bottom=prev, label=key)
        prev = prev + bar
    ax.legend()
    fig.set_size_inches(18.5, 10.5)
    ax.set_xticklabels(labels, rotation="vertical")
    group = fig2image(fig)
    # cv2.imwrite('./group.png', group)
    webcv2.imshow("group", group)

    fig, ax = plt.subplots()
    all_ratios = sorted(all_ratios)
    numbers = [0]
    tick = 0.01
    seperator = tick
    count = 0

    for r in all_ratios:
        count += 1
        while seperator < r:
            seperator += tick
            numbers.append(count)

    ax.plot(np.arange(0, 1, tick), np.array(numbers) / count)
    ax.set_xlabel("slenderness")
    ax.set_title("cumulative distribution function")
    number = fig2image(fig)
    # cv2.imwrite('./number.png', number)
    webcv2.imshow("number", number)
    webcv2.waitKey()
Ejemplo n.º 8
0
def PlotCOCOAndCOCOPlus(datasets,
                        num_subfigs,
                        cmap,
                        sorted=False,
                        include_all=False,
                        show_part=True):
    ratios = {
        "0-1/5": [0, 1 / 5],
        "1/5-1/3": [1 / 5, 1 / 3],
        "1/3-1": [1 / 3, 1]
    }
    total_slender_ratios = []
    total_num_all_ratios = []
    total_sorted_indexes = []
    total_labels = []
    min_ratios = []
    max_ratios = []
    symbols = ["", "+"]
    for dataset_i, dataset in enumerate(datasets):

        dicts = list(DatasetCatalog.get(dataset))

        metadata = MetadataCatalog.get(dataset)
        labels = metadata.thing_classes
        for i in range(len(labels)):
            labels[i] = labels[i] + symbols[dataset_i]
        bars = dict()
        for key in ratios.keys():
            bars[key] = [0 for _ in range(len(labels))]

        for dic in tqdm(dicts):
            for obj in dic["annotations"]:
                ratio = COCO.compute_ratio(obj, oriented=True)["ratio"]
                for key, ratio_range in ratios.items():
                    if between(ratio, ratio_range):
                        bars[key][obj["category_id"]] += 1
        slender_ratios = np.zeros(len(labels))
        num_all_ratios = np.zeros(len(labels))

        num_slender_ratios = np.array(bars["0-1/5"])

        for i in range(len(labels)):
            slender_ratios[i] = bars["0-1/5"][i] / \
                (bars["0-1/5"][i]+bars["1/5-1/3"][i]+bars["1/3-1"][i])
        for key, bar in bars.items():
            num_all_ratios = num_all_ratios + np.array(bar)

        slender_ratio_all = num_slender_ratios.sum() / num_all_ratios.sum()
        if sorted == True:
            sorted_indexes = np.argsort(slender_ratios)
            min_ratio = slender_ratios[sorted_indexes[0]]
            max_ratio = slender_ratios[sorted_indexes[-1]]
        else:
            sorted_indexes = np.arange(len(labels))
            min_ratio = slender_ratios.min()
            max_ratio = slender_ratios.max()
        if include_all == True:
            slender_ratios = np.append(slender_ratios, slender_ratio_all)
            num_all_ratios = np.append(num_all_ratios, num_all_ratios.sum())
            sorted_indexes = np.insert(sorted_indexes, 0, len(labels))
            labels.append('all' + symbols[dataset_i])
        total_slender_ratios.append(slender_ratios)
        total_num_all_ratios.append(num_all_ratios)
        total_sorted_indexes.append(sorted_indexes)
        total_labels.append(labels)
        min_ratios.append(min_ratio)
        max_ratios.append(max_ratio)

    if show_part:
        show_num = 10
        show_indexes = np.append(np.arange(0, show_num + 1),
                                 np.arange(-show_num, 0))
    else:
        show_indexes = np.arrange(0, len(total_labels[0]))
    min_ratio = min(min_ratios)
    max_ratio = max(max_ratios)

    fig, axes = plt.subplots(num_subfigs, 1)

    norm = mpl.colors.Normalize(vmin=min_ratio, vmax=max_ratio)
    subplots_adjust(left=0.15,
                    bottom=0.1,
                    right=0.9,
                    top=0.9,
                    wspace=0,
                    hspace=0.6)
    num_thing_per_fig = len(show_indexes) // num_subfigs
    first_row_plus_num = len(show_indexes) % num_subfigs
    show_label = []
    show_slender_ratio = []
    show_num_all_ratio = []
    start = 0
    end = 0
    show_x = []
    for fig_i in range(num_subfigs):
        start = end
        end += num_thing_per_fig
        if fig_i == 0:
            end += first_row_plus_num

        index_range = total_sorted_indexes[0][show_indexes[start:end]]
        label = []
        slender_ratio = []
        num_all_ratio = []
        coor_x = []
        count = 0
        for ind_i in index_range:
            for dataset_i in range(len(datasets)):
                label.append(total_labels[dataset_i][ind_i])
                slender_ratio.append(total_slender_ratios[dataset_i][ind_i])
                num_all_ratio.append(total_num_all_ratios[dataset_i][ind_i])
                coor_x.append(count)
                c = 1.5 if dataset_i == len(datasets) - 1 else 1
                count += c
        show_label.append(label)
        show_slender_ratio.append(slender_ratio)
        show_num_all_ratio.append(num_all_ratio)
        show_x.append(coor_x)

    plt.tick_params(labelsize=16)
    bar_ec = [(0.73, 0.73, 0.73, 1.0), (0.5, 0.5, 0.5, 0.8)]
    for fig_i in range(num_subfigs):
        axes[fig_i].set_yscale("symlog")
        prev = np.zeros((len(show_label[fig_i]), ))

        bar_colors = [cmap(norm(x)) for x in show_slender_ratio[fig_i]]
        bars = axes[fig_i].bar(show_x[fig_i],
                               show_num_all_ratio[fig_i],
                               bottom=prev,
                               color=bar_colors,
                               edgecolor=bar_ec[fig_i],
                               width=1)
        for i, bar in enumerate(bars):
            if i % 2 != 0:
                bar.set_hatch('\\')
        xticks = show_x[fig_i][::2] + np.ones(len(show_x[fig_i][::2])) * 0.5
        axes[fig_i].set_xticks(xticks)
        if fig_i == 1:
            axes[fig_i].xaxis.tick_top()
            axes[fig_i].set_xticklabels(show_label[fig_i][::2],
                                        fontdict={'fontsize': 14},
                                        rotation=45)
        else:
            axes[fig_i].set_xticklabels(show_label[fig_i][::2],
                                        fontdict={'fontsize': 14},
                                        rotation=315)
        legend_labels = ['COCO', 'COCO+']
        hatches = ['x', '\\']
        patches = []
        patches.append(mpatches.Patch(fc='white', label="COCO", ec="grey"))
        patches.append(
            mpatches.Patch(fc='white', label="COCO+", ec="grey", hatch='\\'))
        if fig_i == 0:
            axes[fig_i].legend(loc="upper center",
                               handles=patches,
                               prop={'size': 24},
                               ncol=2)
    fig.set_size_inches(12, 12)
    position = fig.add_axes([0., 0.1, 0.03, 0.8])
    fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
                 cax=position,
                 orientation='vertical')
    plt.savefig("coco_coco+.png")
Ejemplo n.º 9
0
def PlotGradientColorBars(dataset,
                          num_subfigs,
                          cmap,
                          sorted=False,
                          include_all=False):
    ratios = {
        "0-1/5": [0, 1 / 5],
        "1/5-1/3": [1 / 5, 1 / 3],
        "1/3-1": [1 / 3, 1]
    }
    dicts = list(DatasetCatalog.get(dataset))

    metadata = MetadataCatalog.get(dataset)
    labels = metadata.thing_classes
    bars = dict()
    for key in ratios.keys():
        bars[key] = [0 for _ in range(len(labels))]

    for dic in tqdm(dicts):
        for obj in dic["annotations"]:
            ratio = COCO.compute_ratio(obj, oriented=True)["ratio"]
            for key, ratio_range in ratios.items():
                if between(ratio, ratio_range):
                    bars[key][obj["category_id"]] += 1
    slender_ratios = np.zeros(len(labels))
    num_all_ratios = np.zeros(len(labels))

    num_slender_ratios = np.array(bars["0-1/5"])

    for i in range(len(labels)):
        slender_ratios[i] = bars["0-1/5"][i] / \
            (bars["0-1/5"][i]+bars["1/5-1/3"][i]+bars["1/3-1"][i])
    for key, bar in bars.items():
        num_all_ratios = num_all_ratios + np.array(bar)

    slender_ratio_all = num_slender_ratios.sum() / num_all_ratios.sum()
    if sorted == True:
        sorted_indexes = np.argsort(slender_ratios)
        min_ratio = slender_ratios[sorted_indexes[0]]
        max_ratio = slender_ratios[sorted_indexes[-1]]
    else:
        sorted_indexes = np.arange(len(labels))
        min_ratio = slender_ratios.min()
        max_ratio = slender_ratios.max()
    if include_all == True:
        slender_ratios = np.append(slender_ratios, slender_ratio_all)
        num_all_ratios = np.append(num_all_ratios, num_all_ratios.sum())
        sorted_indexes = np.insert(sorted_indexes, 0, len(labels))
        labels.append('all')

    fig, axes = plt.subplots(num_subfigs, 1)

    norm = mpl.colors.Normalize(vmin=min_ratio, vmax=max_ratio)
    subplots_adjust(left=0.15,
                    bottom=0.1,
                    right=0.9,
                    top=0.9,
                    wspace=0,
                    hspace=0.6)
    num_thing_per_fig = len(labels) // num_subfigs
    for fig_i in range(num_subfigs):
        start = num_thing_per_fig * fig_i
        end = num_thing_per_fig * (fig_i + 1)

        if fig_i == num_subfigs - 1:
            end = len(labels)
        index_range = sorted_indexes[start:end]

        label = []
        for ind_i in index_range:
            label.append(labels[ind_i])

        axes[fig_i].set_yscale("symlog")
        prev = np.zeros((len(label), ))
        bar_colors = [cmap(norm(x)) for x in slender_ratios[index_range]]
        axes[fig_i].bar(label,
                        num_all_ratios[index_range],
                        bottom=prev,
                        color=bar_colors,
                        edgecolor=(0, 0, 0))
        axes[fig_i].set_xticklabels(label, rotation="vertical")
    fig.set_size_inches(12, 12)
    position = fig.add_axes([0., 0.1, 0.03, 0.8])
    fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
                 cax=position,
                 orientation='vertical')
    plt.savefig("barchart.png")