Esempio n. 1
0
def load_mask(mask_path, yaml_path):
    img = Image.open(mask_path)
    width, height = img.size
    num_obj = np.max(img)
    mask = np.zeros([height, width, num_obj], dtype=np.uint8)
    mask = draw_mask(num_obj, mask, height, width, img)
    labels = yaml.load(open(yaml_path).read())['label_names']
    labels_form = []
    for i in range(len(labels)):
        for name in label_name_dict.values():
            if labels[i].find(name) != -1:
                labels_form.append(name)
    class_ids = np.array(
        [list(label_name_dict.values()).index(s) + 1 for s in labels_form])
    return mask, class_ids.astype(np.int32)
Esempio n. 2
0
 def load_mask(self, image_id):
     info = self.image_info[image_id]
     img = Image.open(info['mask_path'])
     num_obj = self.get_obj_index(img)
     mask = np.zeros([info['height'], info['width'], num_obj],
                     dtype=np.uint8)
     mask = self.draw_mask(num_obj, mask, img, image_id)
     labels = self.from_yaml_get_class(image_id)
     labels_form = []
     for i in range(len(labels)):
         for name in label_name_dict.values():
             if labels[i].find(name) != -1:
                 labels_form.append(name)
     class_ids = np.array([self.class_names.index(s) for s in labels_form])
     return mask, class_ids.astype(np.int32)
Esempio n. 3
0
    def load_mask(self, image_id):
        info = self.image_info[image_id]
        img = Image.open(info['mask_path'])
        num_obj = self.get_obj_index(img)
        mask = np.zeros([info['height'], info['width'], num_obj],
                        dtype=np.uint8)
        mask = self.draw_mask(num_obj, mask, img, image_id)
        labels = self.from_yaml_get_class(image_id)
        labels_form = []
        for i in range(len(labels)):
            for name in label_name_dict.values():
                pattern = re.compile(r'{}[0-9]'.format(name))
                if re.match(pattern, labels[i]) is not None:
                    labels_form.append(name)
                    break

        class_ids = np.array([self.class_names.index(s) for s in labels_form])
        return mask, class_ids.astype(np.int32)
Esempio n. 4
0
def inference():
    # summary and trained weights saved path
    MODEL_DIR = cfgs.log_dir
    class_names = list(label_name_dict.values())
    # basic config
    rgb_val_dir = cfgs.rgb_val_dir
    mask_dir = cfgs.mask_dir
    yaml_dir = cfgs.yaml_dir

    val_dataset = Dataset()
    val_dataset.load_data(rgb_val_dir, mask_dir, yaml_dir)
    val_dataset.prepare()

    inference_config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              config=inference_config,
                              model_dir=MODEL_DIR)
    model_path = os.path.join(
        cfgs.log_dir,
        'multibox_roi20180808T0911/mask_rcnn_multibox_roi_0016.h5')
    model.load_weights(model_path, by_name=True)
    save_dir = cfgs.val_result_dir
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    save_PR_dir = cfgs.val_result_pr_dir
    if not os.path.exists(save_PR_dir):
        os.mkdir(save_PR_dir)
    # test
    image_ids = val_dataset.image_ids
    APs = []
    for image_id in image_ids:
        t0 = time.time()
        image_info = val_dataset.image_info[image_id]
        image_path = image_info["path"]
        print("---+" * 30)
        print('inference {}...'.format(image_path))
        image_name = os.path.split(image_path)[-1]
        try:
            original_image, image_meta, gt_class_id, gt_bbox, gt_mask = \
                modellib.load_image_gt(val_dataset, inference_config, image_id, use_mini_mask=False)
        except Exception:
            print("ERROR!!!!!!!!!!")
            continue
        t1 = time.time()  # load gt
        results = model.detect([original_image], verbose=1)
        t2 = time.time()  # detect
        r = results[0]
        # Draw precision-recall curve
        AP, precisions, recalls, overlaps = utils.compute_ap(
            gt_bbox, gt_class_id, gt_mask, r['rois'], r['class_ids'],
            r['scores'], r['masks'])
        APs.append(AP)
        plot_precision_recall(save_PR_dir, image_name, precisions, recalls, AP)
        t3 = time.time()  # plot PR
        # rois = r['rois']
        # if len(rois) == 0:
        #     print('no any objects.')
        #     continue
        # class_ids = r['class_ids']
        # masks = r['masks']
        # colors = random_colors(len(class_ids))
        # img_mask = original_image
        # for i in range(len(class_ids)):
        #     mask = masks[:, :, i]
        #     img_mask = apply_mask(img_mask, mask, colors[i])
        # cv2.imwrite(os.path.join(save_dir, image_name.replace(".png", "_mask.png")), img_mask)
        t4 = time.time()  # plot mask
        print(
            '>>>> load gt time:%0.2f, detect time:%0.2f, plot PR time:%0.2f' %
            (t1 - t0, t2 - t1, t3 - t2))
        print('>>>> plot mask time:%0.2f, total time:%0.2f' %
              (t4 - t3, time.time() - t0))
    print("mAP:".format(np.mean(APs)))
Esempio n. 5
0
def inference():
    # summary and trained weights saved path
    MODEL_DIR = cfgs.log_dir
    class_names = list(label_name_dict.values())
    # basic config
    rgb_val_dir = cfgs.rgb_val_dir
    inference_config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              config=inference_config,
                              model_dir=MODEL_DIR)
    model_path = os.path.join(
        cfgs.log_dir,
        'multibox_roi_border20180809T0232/mask_rcnn_multibox_roi_border_0100.h5'
    )
    model.load_weights(model_path, by_name=True)
    save_dir = cfgs.val_result_dir
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    # test
    files = get_files(rgb_val_dir)
    for file in files:
        print("---+" * 30)
        print('inference {}...'.format(file))
        image_path = os.path.join(rgb_val_dir, file)
        img = cv2.imread(image_path)
        h, w = img.shape[0], img.shape[1]
        t0 = time.time()
        original_image = skimage.io.imread(image_path)

        results = model.detect([original_image], verbose=1)

        # Run RPN sub-graph
        # pillar = model.keras_model.get_layer("ROI").output  # node to start searching from
        rpn = model.run_graph(
            [original_image],
            [
                ("rpn_class", model.keras_model.get_layer("rpn_class").output),
                # ("pre_nms_anchors", model.ancestor(pillar, "ROI/pre_nms_anchors:0")),
                # ("refined_anchors", model.ancestor(pillar, "ROI/refined_anchors:0")),
                # ("refined_anchors_clipped", model.ancestor(pillar, "ROI/refined_anchors_clipped:0")),
                # ("post_nms_anchor_ix", model.ancestor(pillar, "ROI/rpn_non_max_suppression:0")),
                ("proposals", model.keras_model.get_layer("ROI").output)
            ])
        proposals = rpn['proposals']
        limits = min(100, len(proposals[0]))
        # limits = len(proposals[0])
        proposals = rpn['proposals'][0, :limits, :] * np.array([h, w, h, w])
        print('proposals shape:', proposals.shape)
        img_rpn = copy.deepcopy(img)
        colors_rpn = random_colors(limits)
        for i in range(len(proposals)):
            proposal = proposals[i]
            color = colors_rpn[i]
            color = tuple([v * 255 for v in color])
            x1, y1, x2, y2 = tuple([int(val) for val in proposal])
            cv2.rectangle(img_rpn, (x1, y1), (x2, y2), color, 1)
        rpn_img_path = os.path.join(save_dir, file.replace(".png", "_rpn.png"))
        cv2.imwrite(rpn_img_path, img_rpn)
        t1 = time.time()
        r = results[0]
        rois = r['rois']
        if len(rois) == 0:
            print('no any objects.')
            continue
        class_ids = r['class_ids']
        masks = r['masks']
        #         index = np.where((class_ids==1.)|(class_ids==2.))[0]
        index = np.where((class_ids == 1.) | (class_ids == 2.)
                         | (class_ids == 6.))[0]
        if len(index) == 0:
            continue

        rois = rois[index, :]
        class_ids = class_ids[index]
        masks = masks[:, :, index]
        colors = random_colors(len(class_ids))
        img_mask = copy.deepcopy(img)
        for i in range(len(class_ids)):
            mask = masks[:, :, i]
            img_mask = apply_mask(img_mask, mask, colors[i])
            mask_val_index = np.where(mask == True)
            coords = list(
                map(lambda y, x: [x, y], mask_val_index[0], mask_val_index[1]))
            try:
                box_list = get_min_area_rectangle(coords, mode=1)
            except Exception:
                print(np.max(mask))
                continue
            left_bottom_coord = (int(box_list[0][0] + box_list[2][0]) // 2,
                                 int(box_list[0][1] + box_list[2][1]) // 2)
            cv2.putText(img,
                        label_name_dict[int(class_ids[i])],
                        left_bottom_coord,
                        fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,
                        fontScale=1,
                        color=(0, 0, 255),
                        thickness=1)
            cv2.putText(img_mask,
                        label_name_dict[int(class_ids[i])],
                        left_bottom_coord,
                        fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,
                        fontScale=1,
                        color=(0, 0, 255),
                        thickness=1)

            for ii in range(len(box_list)):
                x1 = int(box_list[ii][0])
                y1 = int(box_list[ii][1])
                x2 = int(box_list[(ii + 1) % 4][0])
                y2 = int(box_list[(ii + 1) % 4][1])
                cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 1)
        cv2.imwrite(os.path.join(save_dir, file.replace(".png", "_mask.png")),
                    img_mask)
        cv2.imwrite(os.path.join(save_dir, file.replace(".png", ".png")), img)
        print('>>>> detect time:{}, post processing time:{}, total time:{}'.
              format(t1 - t0,
                     time.time() - t1,
                     time.time() - t0))
Esempio n. 6
0
def inference():
    # summary and trained weights saved path
    MODEL_DIR = cfgs.log_dir
    class_names = list(label_name_dict.values())
    # basic config
    rgb_val_dir = cfgs.rgb_val_dir
    mask_dir = cfgs.mask_dir
    yaml_dir = cfgs.yaml_dir

    inference_config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              config=inference_config,
                              model_dir=MODEL_DIR)
    model_path = os.path.join(
        cfgs.log_dir,
        'multibox_roi20180808T0911/mask_rcnn_multibox_roi_0016.h5')
    model.load_weights(model_path, by_name=True)
    save_dir = cfgs.val_result_dir
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    save_PR_dir = cfgs.val_result_pr_dir
    if not os.path.exists(save_PR_dir):
        os.mkdir(save_PR_dir)
    # test
    files = get_files(rgb_val_dir)
    APs = []
    num_gts = 0
    tps = 0
    fps = 0
    for file in files:
        image_path = os.path.join(rgb_val_dir, file)
        mask_path = os.path.join(mask_dir, file)
        yaml_path = os.path.join(yaml_dir, file.replace('png', 'yaml'))
        print("---+" * 30)
        print('inference {}...'.format(image_path))
        gt_mask, gt_class_id = load_mask(mask_path, yaml_path)
        num_gts += len(gt_class_id)
        gt_bbox = extract_bboxes(gt_mask)
        original_image = load_image(image_path)
        t1 = time.time()  # load gt
        results = model.detect([original_image], verbose=1)
        t2 = time.time()  # detect
        r = results[0]
        if len(r['class_ids']) == 0:
            continue
        # Draw precision-recall curve
        AP, precisions, recalls, overlaps, tp, fp = compute_ap(
            gt_bbox, gt_class_id, gt_mask, r['rois'], r['class_ids'],
            r['scores'], r['masks'])
        tps += tp
        fps += fp
        APs.append(AP)
        plot_precision_recall(save_PR_dir, file, precisions, recalls, AP)
        t3 = time.time()  # plot PR
        # rois = r['rois']
        # if len(rois) == 0:
        #     print('no any objects.')
        #     continue
        # class_ids = r['class_ids']
        # masks = r['masks']
        # colors = random_colors(len(class_ids))
        # img_mask = original_image
        # for i in range(len(class_ids)):
        #     mask = masks[:, :, i]
        #     img_mask = apply_mask(img_mask, mask, colors[i])
        # cv2.imwrite(os.path.join(save_dir, image_name.replace(".png", "_mask.png")), img_mask)
        t4 = time.time()  # plot mask
        print('>>>> detect time:%0.2f, plot PR time:%0.2f' %
              (t2 - t1, t3 - t2))
        print('>>>> plot mask time:%0.2f, total time:%0.2f' %
              (t4 - t3, time.time() - t1))
    print("mAP:{}".format(np.mean(APs)))
    print("recall:{}, mAP:{}".format(tps / num_gts, tps / (tps + fps)))