예제 #1
0
    def prepare_nori(self, cfg):
        self.use_nori = cfg.USE_NORI
        if not self.use_nori:
            return
        split_name = "train2017" if self.is_train else "val2017"

        self.image_fetcher = NoriRedis(
            cfg,
            smart_path(cfg.NORI_PATH).joinpath(split_name + ".nori").as_uri())
        if self.need_masks:
            self.sizes_fecher = NoriRedis(
                cfg,
                smart_path(cfg.NORI_PATH).joinpath(split_name +
                                                   "_sizes.nori").as_uri())
예제 #2
0
def test_rle_to_polygon(res_file):
    obj365_anno_file = "datasets/obj365/annotations/objects365_val_20190423.json"
    obj_api = COCO(annotation_file=obj365_anno_file)

    with open(file=res_file, mode='rb') as f:
        predictions = torch.load(f)

    anns = list(itertools.chain(*[x["instances"] for x in predictions]))

    oss_root = "s3://detection/objects365_raw_data/objects365/val/"
    img_format = "BGR"

    for ann in anns:
        segm = ann['segmentation']
        image_dict = obj_api.imgs[ann['image_id']]
        file_path = os.path.join(oss_root, image_dict['file_name'])
        image = load_image_from_oss(smart_path(file_path), format=img_format)
        image = convert_image_to_rgb(image, format=img_format)
        v_rle = Visualizer(image, None)
        v_rle = v_rle.overlay_instances(masks=[segm], )
        v_rle.save('/data/tmp/{}_rle.png'.format(ann['image_id']))

        v_polygon = Visualizer(copy.deepcopy(image), None)
        v_polygon = v_polygon.overlay_instances(
            masks=[_convert_rle_to_polygon(segm)], )
        v_polygon.save('/data/tmp/{}_polygon.png'.format(ann['image_id']))

        ipdb.set_trace()
예제 #3
0
    def masks_for_image(self, dataset_dict, transforms):
        image_name = smart_path(dataset_dict["file_name"]).name
        masks = dict()
        for key in self.mask_keys:
            data = self.sizes_fecher.fetch(image_name)
            image = np.fromstring(data, dtype=np.float32)

            if key == "sizes":
                image = image.reshape(
                    (dataset_dict["height"], dataset_dict["width"], 2))
                image = image.transpose(2, 0, 1)
                resize_tfm = transforms.transforms[0]
                assert isinstance(resize_tfm, T.ResizeTransform)
                ratio_h = resize_tfm.new_h / resize_tfm.h
                ratio_w = resize_tfm.new_w / resize_tfm.w
                image = np.stack(
                    [
                        transforms.apply_image(image[0] * ratio_w),
                        transforms.apply_image(image[1] * ratio_h),
                    ],
                    axis=0,
                )
            else:
                image = image.reshape(
                    (dataset_dict["height"], dataset_dict["width"]))
                image = transforms.apply_image(image)

            masks[key] = torch.as_tensor(np.ascontiguousarray(image.copy()))
            del image
        return masks
예제 #4
0
def main():
    parser = default_argument_parser()
    parser.add_argument("--prediction", help="predictions_file_path")
    args = parser.parse_args()
    cfg = setup(args)
    with smart_path(args.prediction).open("rb") as fp:
        buf = io.BytesIO(fp.read())
        predictions = torch.load(buf)
    pred_by_image = defaultdict(list)
    for p in predictions:
        pred_by_image[p["image_id"]].append(p)

    dataset = cfg.DATASETS.TEST[0]

    metadata = MetadataCatalog.get(dataset)

    if hasattr(metadata, "thing_dataset_id_to_contiguous_id"):

        def dataset_id_map(ds_id):
            return metadata.thing_dataset_id_to_contiguous_id[ds_id]

    output_folder = os.path.join(cfg.OUTPUT_DIR, "inference")
    evaluator = COCOEvaluator(dataset, cfg, False, output_folder)
    evaluator.reset()

    dicts = list(DatasetCatalog.get(dataset))

    count = 0
    
    for dic in tqdm.tqdm(dicts):
        #assert len(pred_by_image[dic["image_id"]]) == 1, str(dic["image_id"])
        if len(pred_by_image[dic["image_id"]]) == 0:
            continue
        prediction = pred_by_image[dic["image_id"]][0]
        prediction = create_instances(prediction, (dic["height"], dic["width"]))
        # Push an image
        dic["annotations"] = reconstruct_ann(dic["annotations"])
        evaluator.process([dic], [{"instances": prediction}])
        count += 1

    result = evaluator.evaluate()
    prediction_path = smart_path(args.prediction)
    save_path = prediction_path.parent.joinpath(prediction_path.stem + ".pkl")
    with save_path.open("wb") as writer:
        pickle.dump(result, writer)
    print_csv_format(result)
예제 #5
0
 def target(self, queue):
     path = smart_path(self.path)
     with nr.open(path.as_uri(), "w") as writer:
         while True:
             content = queue.get()
             if content is None:
                 break
             writer.put(content[0], filename=[content[1]])
             if self.after is not None:
                 self.after()
예제 #6
0
    def read_image(self, file_name):
        #uods = _whether_use_oss_data_source(file_name)  # use_oss_data_source
        if self.uods:
            # set oss bucket name like: s3://detections/
            file_path = smart_path(os.path.join(self.oss_root, file_name))
            image = load_image_from_oss(file_path, format=self.image_format)
        else:
            image = utils.read_image(file_name, format=self.image_format)

        return image
예제 #7
0
    def fetch(self, file_name, fetcher=None):
        import nori2 as nr
        data_id = self.get(smart_path(file_name).name)
        if fetcher is None:
            if self.fechcer is None:
                self.fechcer = nr.Fetcher()

            fetcher = self.fechcer

        return fetcher.get(data_id)
예제 #8
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)
예제 #9
0
def save(rgb: np.ndarray, mask: np.ndarray, centers: np.ndarray, *file_names):
    assert len(file_names) > 0
    rgb_save_path = smart_path(os.path.join(file_names[0], file_names[1]))
    mask_save_path = smart_path(os.path.join(file_names[0], file_names[2]))
    if not exists(rgb_save_path):
        mknod(rgb_save_path)
    if not exists(mask_save_path):
        mknod(mask_save_path)

    im_rgb = Image.fromarray(rgb)
    im_rgb.save(rgb_save_path)

    mask = cv2.applyColorMap(mask, 0)

    im_mask = Image.fromarray(mask)
    draw = ImageDraw.Draw(im_mask)
    radis = 2
    for i in range(len(centers)):
        draw.pieslice((centers[i, 0] - radis, centers[i, 1] - radis, centers[i, 0] + radis, centers[i, 1] + radis),
                      start=0, end=360, fill=128)
    im_mask.save(mask_save_path)
예제 #10
0
def main(dest_dir="s3://detection/benchmarking/inference/"):
    dest_dir = smart_path(dest_dir)

    while True:
        for prediction_path in dest_dir.glob("*.pth"):
            command = "rlaunch --cpu 2 --memory $((10*1024)) -- python3 -m " \
                      "tools.result_statistic --config-file configs/retina/base_retina_R_50_FPN_1x.yaml " \
                      "--prediction {}".format(prediction_path.as_uri())
            print(command)
            code = os.system(command)
            print(code)
            if code == 0:
                prediction_path.rename(
                    dest_dir.joinpath("archived", prediction_path.name))
            else:
                print("Error for {}, will retry in 10 secs".format(prediction_path.as_uri()))

        print("Dir {} is clean, sleep for 10 secs.".format(dest_dir.as_uri()))
        time.sleep(10)
예제 #11
0
def test_dataset_dicts():
    dataset_dicts = get_detection_dataset_dicts(
        ['coco_objects365_val_with_masks'],
        filter_empty=False,
    )
    oss_root = "s3://detection/"
    img_format = "BGR"
    for dataset_dict in dataset_dicts:
        file_path = os.path.join(oss_root, dataset_dict['file_name'])
        image = load_image_from_oss(smart_path(file_path), format=img_format)
        image = convert_image_to_rgb(image, format=img_format)
        anns = dataset_dict['annotations']

        masks = [ann['segmentation'] for ann in anns]

        v_gt = Visualizer(image, None)
        v_gt = v_gt.overlay_instances(masks=masks, )
        v_gt.save('/data/tmp/test_dd_{}.png'.format(dataset_dict['image_id']))

        ipdb.set_trace()
예제 #12
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)
예제 #13
0
def main(
        result_path="train_log/retinanet/R_50_FPN/baseline/inference/instances_predictions_coco.pkl",
        result_path_2="../train_log/retinanet/R_50_FPN/baseline/inference/coco_objects365_val_with_masks/instances_predictions.pkl"):
    result_path = smart_path(result_path)
    results = pickle.loads(result_path.read_bytes())

    result_path_2 = smart_path(result_path_2)
    results_2 = pickle.loads(result_path_2.read_bytes())
    # shape (T, K, R, A), where T, K, R, A are number of thresholds,
    # classes, ratios, areas, respectively.
    ar = results["ar"]
    stats = results["ar"]["ar-stats"]["recalls"] * 100
    ar_2 = results_2["ar"]
    stats_2 = results_2["ar"]["ar-stats"]["recalls"] * 100
    plt.style.use(['ieee', "no-latex"])
    fig, axs = plt.subplots(3, 3, sharey=True, figsize=(9, 9))
    markers = ['*', '_', '+', 'x']

    # plot overall AR
    xs = np.arange(4) + 1
    ys = np.array([ar["AR@100"],
         ar["AR- 0  - 1/5@100"],
         ar["AR-1/5 - 1/3@100"],
         ar["AR-1/3 - 3/1@100"]])
    scatter_with_markers(axs[0, 0], xs, ys, markers, c="r")
    axs[0, 0].plot(xs, ys, linestyle="dotted")

    axs[0, 0].set_xticks(xs)
    title(axs[0, 0], "all objects")
    axs[0, 0].set_ylabel("mAR-COCO")
    axs[0, 0].set_xlabel("slenderness")
    axs[0, 0].set_xticklabels(["all", "XS", "S", "R"])

    # merge
    [ax.remove() for ax in axs[0, 1:]]
    ax = fig.add_subplot(axs[0, 2].get_gridspec()[0, 1:])
    
    # plot thresholds
    T = stats.shape[0]
    xs = np.array([1])
    x_labels = []
    thresh  = 0.5
    stride = 0.05
    xticks = []
    for i in range(T):
        ys = stats[i, :-1, 0:4, 0].mean(0)
        xs = np.arange(4) + xs.max() + 1
        scatter_with_markers(ax, xs, ys, markers, c='r')
        ax.plot(xs, ys, c='black', linestyle="dotted")
        x_labels += ["", str(int(thresh * 100) / 100), "", ""]
        thresh += stride
        xticks.append(xs)

    ax.set_xticks(np.concatenate(xticks))
    ax.set_xticklabels(x_labels)
    title(ax, "all objects")
    ax.set_xlabel("threshold")
    ax.legend(
        [Line2D([0], [0], color="b", linewidth=1, linestyle="none", marker=m, c="r")
            for m in markers],
        ["all", "XS", "S", "R"], loc="upper right")

    # plot overall AR
    xs = np.arange(4) + 1
    ys = np.array([ar_2["AR@100"],
         ar_2["AR- 0  - 1/5@100"],
         ar_2["AR-1/5 - 1/3@100"],
         ar_2["AR-1/3 - 3/1@100"]])
    ax = axs[1, 0]
    scatter_with_markers(ax, xs, ys, markers, c="g")
    ax.plot(xs, ys, linestyle="dotted")

    ax.set_xticks(xs)
    title(ax, "all objects")
    ax.set_ylabel("mAR-COCO+")
    ax.set_xlabel("slenderness")
    ax.set_xticklabels(["all", "XS", "S", "R"])

    # merge
    [ax.remove() for ax in axs[1, 1:]]
    ax = fig.add_subplot(axs[1, 2].get_gridspec()[1, 1:])
    
    # plot thresholds
    T = stats.shape[0]
    xs = np.array([1])
    x_labels = []
    thresh  = 0.5
    stride = 0.05
    xticks = []
    for i in range(T):
        ys = stats_2[i, :-1, 0:4, 0].mean(0)
        xs = np.arange(4) + xs.max() + 1
        scatter_with_markers(ax, xs, ys, markers, c='g')
        ax.plot(xs, ys, c='black', linestyle="dotted")
        x_labels += ["", str(int(thresh * 100) / 100), "", ""]
        thresh += stride
        xticks.append(xs)

    ax.set_xticks(np.concatenate(xticks))
    ax.set_xticklabels(x_labels)
    title(ax, "all objects")
    ax.set_xlabel("threshold")
    ax.legend(
        [Line2D([0], [0], color="b", linewidth=1, linestyle="none", marker=m, c="g")
            for m in markers],
        ["all", "XS", "S", "R"], loc="upper right")

    # plot small objects
    ax = axs[2, 0]
    xs = np.arange(4)
    ys = np.array([stats[:, :-1, 0, 1].mean(),
        stats[:, :-1, 1, 1].mean(),
        stats[:, :-1, 2, 1].mean(),
        stats[:, :-1, 3, 1].mean()])
    ys_2 = np.array([stats_2[:, :-1, 0, 1].mean(),
        stats_2[:, :-1, 1, 1].mean(),
        stats_2[:, :-1, 2, 1].mean(),
        stats_2[:, :-1, 3, 1].mean()])
    # scatter_with_markers(ax, xs, ys, markers, c='g')
    scatter_with_markers(ax, xs, ys_2, markers, c='g')
    # ax.plot(xs, ys, c="g", linestyle="dotted")
    ax.plot(xs, ys_2, c="black", linestyle="dotted")
    ax.set_xticks(xs)
    title(ax, "small objects")
    ax.set_ylabel("mAR-COCO+")
    ax.set_xlabel("slenderness")
    ax.set_xticklabels(["all", "XS", "S", "R"])
    
    # plot medium objects
    ax = axs[2, 1]
    xs = np.arange(4)
    ys = np.array([stats[:, :-1, 0, 2].mean(),
        stats[:, :-1, 1, 2].mean(),
        stats[:, :-1, 2, 2].mean(),
        stats[:, :-1, 3, 2].mean()])
    ys_2 = np.array([stats_2[:, :-1, 0, 2].mean(),
        stats_2[:, :-1, 1, 2].mean(),
        stats_2[:, :-1, 2, 2].mean(),
        stats_2[:, :-1, 3, 2].mean()])
    # scatter_with_markers(ax, xs, ys, markers, c='g')
    scatter_with_markers(ax, xs, ys_2, markers, c='g')
    # ax.plot(xs, ys, c="g", linestyle="dotted")
    ax.plot(xs, ys_2, c="black", linestyle="dotted")
    ax.set_xticks(xs)
    title(ax, "medium objects")
    ax.set_xlabel("slenderness")
    ax.set_xticklabels(["all", "XS", "S", "R"])

    # plot large objects
    ax = axs[2, 2]
    xs = np.arange(4)
    ys = np.array([stats[:, :-1, 0, 3].mean(),
        stats[:, :-1, 1, 3].mean(),
        stats[:, :-1, 2, 3].mean(),
        stats[:, :-1, 3, 3].mean()])
    ys_2 = np.array([stats_2[:, :-1, 0, 3].mean(),
        stats_2[:, :-1, 1, 3].mean(),
        stats_2[:, :-1, 2, 3].mean(),
        stats_2[:, :-1, 3, 3].mean()])
    # scatter_with_markers(ax, xs, ys, markers, c='g')
    scatter_with_markers(ax, xs, ys_2, markers, c='g')
    # ax.plot(xs, ys, c="g", linestyle="dotted")
    ax.plot(xs, ys_2, c="black", linestyle="dotted")
    ax.set_xticks(xs)
    title(ax, "large objects")
    ax.set_xlabel("slenderness")
    ax.set_xticklabels(["all", "XS", "S", "R"])

    group = fig2image(fig)
    webcv2.imshow("group", group)
    webcv2.waitKey()
예제 #14
0
def save(mask: np.ndarray, *file_names):
    assert len(file_names) > 0
    save_path = smart_path(os.path.join(file_names[0], *file_names[1:]))
    with save_path.open('wb') as writer:
        writer.write(mask.tostring())
예제 #15
0
            queue.put(("sizes", sizes.tostring(), file_name))
        else:
            save(borders, cfg.MASK_DIRECTORY, split_name, "borders", file_name)
            save(centers, cfg.MASK_DIRECTORY, split_name, "centers", file_name)
            save(sizes, cfg.MASK_DIRECTORY, split_name, "sizes", file_name)


if __name__ == '__main__':
    args = parse_args()
    assert not args.show or args.processes < 1

    logger = setup_logger()
    logger.info("Arguments: " + str(args))
    cfg = setup(args)
    dirname = args.output_dir
    output_dir = smart_path(dirname)
    if not output_dir.exists():
        output_dir.mkdir(exist_ok=True)

    split_name = getattr(cfg.DATASETS, args.source)[0]
    metadata = MetadataCatalog.get(split_name)

    dicts = list(chain.from_iterable([DatasetCatalog.get(k)
                                      for k in getattr(cfg.DATASETS, args.source)]))
    if args.processes > 0:
        pool = Pool(args.processes)

    bar = tqdm.tqdm(unit_divisor=len(dicts), maxinterval=len(dicts))
    if args.processes > 0:
        from utils.async_writer import AsyncWriter
        split_name = "train2017" if "train" in split_name else "val2017"
예제 #16
0
    logger = setup_logger()
    logger.info("Arguments: " + str(args))
    cfg = setup(args)

    split_name = getattr(cfg.DATASETS, args.source)[0]
    metadata = MetadataCatalog.get(split_name)

    dicts = list(
        chain.from_iterable([
            DatasetCatalog.get(k) for k in getattr(cfg.DATASETS, args.source)
        ]))
    if args.processes > 0:
        pool = Pool(args.processes)

    output_path = smart_path(args.output_file)
    if output_path.exists():
        print(output_path.as_uri, "exists!")
        import sys
        sys.exit()

    with nori2.open(output_path.as_uri(), "w") as writer:
        for dic in tqdm.tqdm(dicts):
            if args.image_path is None:
                file_path = smart_path(dic["file_name"])
            else:
                file_path = smart_path(args.image_path).joinpath(
                    smart_path(dic["file_name"]).name)

            with file_path.open("rb") as reader:
                writer.put(reader.read(), filename=file_path.name)