Exemplo n.º 1
0
def main():
    chainer.config.train = False

    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained_model', default=TRAINED_MODEL_DEFAULT)
    parser.add_argument('image')
    args = parser.parse_args()

    if args.pretrained_model == TRAINED_MODEL_DEFAULT and \
       not os.path.exists(TRAINED_MODEL_DEFAULT):
        download_model.download_model(MODEL_URL, TRAINED_MODEL_DEFAULT)

    model = FasterRCNNVGG16(n_fg_class=1,
                            pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        model.to_gpu(args.gpu)
        chainer.cuda.get_device(args.gpu).use()

    img = utils.read_image(args.image, color=True)
    bboxes, labels, scores = model.predict([img])
    bbox, label, score = bboxes[0], labels[0], scores[0]

    vis_bbox(img, bbox, label, score, label_names=('face', ))
    plot.show()
Exemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        choices=('faster_rcnn_fpn_resnet50',
                                 'faster_rcnn_fpn_resnet101'),
                        default='faster_rcnn_fpn_resnet50')
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained-model', default='coco')
    parser.add_argument('image')
    args = parser.parse_args()

    if args.model == 'faster_rcnn_fpn_resnet50':
        model = FasterRCNNFPNResNet50(n_fg_class=len(coco_bbox_label_names),
                                      pretrained_model=args.pretrained_model)
    elif args.model == 'faster_rcnn_fpn_resnet101':
        model = FasterRCNNFPNResNet101(n_fg_class=len(coco_bbox_label_names),
                                       pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image)
    bboxes, labels, scores = model.predict([img])
    bbox = bboxes[0]
    label = labels[0]
    score = scores[0]

    vis_bbox(img, bbox, label, score, label_names=coco_bbox_label_names)
    plt.show()
Exemplo n.º 3
0
def main():
    chainer.config.train = False

    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained_model')
    parser.add_argument('image')
    parser.add_argument('--label_names',
                        help='The path to the file with label names')
    args = parser.parse_args()

    with open(args.label_names, 'r') as f:
        label_names = f.read().splitlines()

    model = SSD300(n_fg_class=len(label_names),
                   pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)
    model.nms_thresh = 0.45
    model.score_thresh = 0.3
    bboxes, labels, scores = model.predict([img])
    print((bboxes, labels, scores))
    bbox, label, score = bboxes[0], labels[0], scores[0]

    vis_bbox(img, bbox, label, score, label_names=label_names)
    plot.show()
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--model', choices=('ssd300', 'ssd512'), default='ssd300')
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained_model', default='voc0712')
    parser.add_argument('image')
    args = parser.parse_args()

    if args.model == 'ssd300':
        model = SSD300(
            n_fg_class=len(voc_bbox_label_names),
            pretrained_model=args.pretrained_model)
    elif args.model == 'ssd512':
        model = SSD512(
            n_fg_class=len(voc_bbox_label_names),
            pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)
    bboxes, labels, scores = model.predict([img])
    bbox, label, score = bboxes[0], labels[0], scores[0]

    vis_bbox(
        img, bbox, label, score, label_names=voc_bbox_label_names)
    plot.show()
Exemplo n.º 5
0
def main():
    dataset = VOCBboxDataset(year='2007', split='test')
    models = [
        ('Faster R-CNN', FasterRCNNVGG16(pretrained_model='voc07')),
        ('SSD300', SSD300(pretrained_model='voc0712')),
        ('SSD512', SSD512(pretrained_model='voc0712')),
    ]
    indices = [29, 301, 189, 229]

    fig = plot.figure(figsize=(30, 30))
    for i, idx in enumerate(indices):
        for j, (name, model) in enumerate(models):
            img, _, _ = dataset[idx]
            bboxes, labels, scores = model.predict([img])
            bbox, label, score = bboxes[0], labels[0], scores[0]

            ax = fig.add_subplot(
                len(indices), len(models), i * len(models) + j + 1)
            vis_bbox(
                img, bbox, label, score,
                label_names=voc_bbox_label_names, ax=ax
            )

            # Set MatplotLib parameters
            ax.set_aspect('equal')
            if i == 0:
                font = FontProperties()
                font.set_family('serif')
                ax.set_title(name, fontsize=35, y=1.03, fontproperties=font)
            plot.axis('off')
            plot.tight_layout()

    plot.show()
Exemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained-model')
    parser.add_argument('--dataset', choices=('voc', ), default='voc')
    parser.add_argument('image')
    args = parser.parse_args()

    if args.dataset == 'voc':
        if args.pretrained_model is None:
            args.pretrained_model = 'voc07'
        label_names = voc_bbox_label_names

    model = FasterRCNNVGG16(n_fg_class=len(label_names),
                            pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)
    bboxes, labels, scores = model.predict([img])
    bbox, label, score = bboxes[0], labels[0], scores[0]

    vis_bbox(img, bbox, label, score, label_names=label_names)
    plt.show()
Exemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        choices=('ssd300', 'ssd512'),
                        default='ssd300')
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained-model', default='voc0712')
    parser.add_argument('image')
    args = parser.parse_args()

    if args.model == 'ssd300':
        model = SSD300(n_fg_class=len(voc_bbox_label_names),
                       pretrained_model=args.pretrained_model)
    elif args.model == 'ssd512':
        model = SSD512(n_fg_class=len(voc_bbox_label_names),
                       pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)
    bboxes, labels, scores = model.predict([img])
    bbox, label, score = bboxes[0], labels[0], scores[0]

    vis_bbox(img, bbox, label, score, label_names=voc_bbox_label_names)
    plt.show()
Exemplo n.º 8
0
def save_bbox(img,
              bbox,
              filename=None,
              label=None,
              score=None,
              label_names=None,
              instance_colors=None,
              alpha=1.0,
              linewidth=3.0,
              sort_by_score=True,
              ax=None):

    vis_bbox(img,
             bbox,
             label=label,
             score=score,
             label_names=label_names,
             instance_colors=instance_colors,
             alpha=alpha,
             linewidth=linewidth,
             sort_by_score=sort_by_score,
             ax=ax)

    plt.tick_params(labelbottom=False,
                    labelleft=False,
                    labelright=False,
                    labeltop=False)

    plt.tick_params(bottom=False, left=False, right=False, top=False)

    plt.savefig(filename, bbox_inches='tight', pad_inches=0)
    plt.close()
Exemplo n.º 9
0
def main():
    dataset = VOCBboxDataset(year='2007', split='test')
    models = [
        ('Faster R-CNN', FasterRCNNVGG16(pretrained_model='voc07')),
        ('SSD300', SSD300(pretrained_model='voc0712')),
        ('SSD512', SSD512(pretrained_model='voc0712')),
    ]
    indices = [29, 301, 189, 229]

    fig = plt.figure(figsize=(30, 30))
    for i, idx in enumerate(indices):
        for j, (name, model) in enumerate(models):
            img, _, _ = dataset[idx]
            bboxes, labels, scores = model.predict([img])
            bbox, label, score = bboxes[0], labels[0], scores[0]

            ax = fig.add_subplot(
                len(indices), len(models), i * len(models) + j + 1)
            vis_bbox(
                img, bbox, label, score,
                label_names=voc_bbox_label_names, ax=ax
            )

            # Set MatplotLib parameters
            ax.set_aspect('equal')
            if i == 0:
                font = FontProperties()
                font.set_family('serif')
                ax.set_title(name, fontsize=35, y=1.03, fontproperties=font)
            plt.axis('off')
            plt.tight_layout()

    plt.show()
Exemplo n.º 10
0
def runYOLO():
    '''parser = argparse.ArgumentParser()
      parser.add_argument(
          '--model', choices=('yolo_v2', 'yolo_v3'),
          default='yolo_v2')
      parser.add_argument('--gpu', type=int, default=-1)
      parser.add_argument('--pretrained-model', default='voc0712')
      parser.add_argument('image')
      args = parser.parse_args()'''

    aimage = 'sample7.jpg'
    amodel = 'yolo_v2'
    apretrained_model = 'voc0712'
    agpu = -1

    if amodel == 'yolo_v2':
        model = YOLOv2(n_fg_class=len(voc_bbox_label_names),
                       pretrained_model=apretrained_model)
    elif amodel == 'yolo_v3':
        model = YOLOv3(n_fg_class=len(voc_bbox_label_names),
                       pretrained_model=apretrained_model)

    if agpu >= 0:
        chainer.cuda.get_device_from_id(agpu).use()
        model.to_gpu()

    img = utils.read_image(aimage, color=True)
    bboxes, labels, scores = model.predict([img])
    bbox, label, score = bboxes[0], labels[0], scores[0]

    vis_bbox(img, bbox, label, score, label_names=voc_bbox_label_names)
Exemplo n.º 11
0
    def detectChocoballImage(self, img):
        """
        ChocoBallの個数をカウントする

        Args:
            img : jpeg image (binary)
        Returns:
            dict{'img':image_binary, 'box':[[ymin,xmin,ymax,xmax]], 'objects':[object_id], 'scores':[score]}
        """
        #img_pil = Image.open(BytesIO(img))
        img_pil = Image.open(img)
        print(img_pil.size)
        if img_pil.size[0] > self.MAX_WIDTH:
            img_pil.thumbnail(size=(self.MAX_WIDTH, self.MAX_WIDTH))
            print(img_pil.size)
        img_arr = np.asarray(img_pil).transpose(2, 0, 1).astype(np.float32)
        bboxes, labels, scores = self.model_frcnn.predict([img_arr])

        fig = plt.figure(figsize=(5, 4))
        ax = fig.subplots(1, 1)
        ax.tick_params(labelbottom="off", bottom="off")
        ax.tick_params(labelleft="off", left="off")
        vis_bbox(img_arr, bboxes[0], labels[0], ax=ax)
        plt.savefig('tmp/tmp.png', bbox_inches="tight", pad_inches=0.0)
        with open('tmp/tmp.png', 'rb') as f:
            img_b = f.read()
        return {'img': img_b, 'box': bboxes[0], 'objects': labels[0], 'scores': scores[0]}
Exemplo n.º 12
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--base-network', choices=('vgg16', 'resnet101'),
                        default='vgg16', help='Base network')
    parser.add_argument('--pretrained-model', required=True)
    parser.add_argument('image')
    args = parser.parse_args()

    if args.base_network == 'vgg16':
        model = chainercv.links.SSD300(
           n_fg_class=len(roaddamage_label_names),
           pretrained_model=args.pretrained_model)
    elif args.base_network == 'resnet101':
        model = ssd_resnet101.SSD224(
           n_fg_class=len(roaddamage_label_names),
           pretrained_model=args.pretrained_model)
    else:
        raise ValueError('Invalid base network')

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)
    bboxes, labels, scores = model.predict([img])
    bbox, label, score = bboxes[0], labels[0], scores[0]

    vis_bbox(
        img, bbox, label, score, label_names=roaddamage_label_names)
    plt.axis('off')
    plt.show()
Exemplo n.º 13
0
    def image_cb(self, msg):
        img = self.bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
        H, W = img.shape[:2]
        objs = self.engine.DetectWithImage(PIL.Image.fromarray(img),
                                           threshold=self.score_thresh,
                                           keep_aspect_ratio=True,
                                           relative_coord=True,
                                           top_k=self.top_k)

        bboxes = []
        scores = []
        labels = []
        rect_msg = RectArray(header=msg.header)
        for obj in objs:
            x_min, y_min, x_max, y_max = obj.bounding_box.flatten().tolist()
            x_min = int(np.round(x_min * W))
            y_min = int(np.round(y_min * H))
            x_max = int(np.round(x_max * W))
            y_max = int(np.round(y_max * H))
            bboxes.append([y_min, x_min, y_max, x_max])
            scores.append(obj.score)
            labels.append(self.label_ids.index(int(obj.label_id)))
            rect = Rect(x=x_min,
                        y=y_min,
                        width=x_max - x_min,
                        height=y_max - y_min)
            rect_msg.rects.append(rect)
        bboxes = np.array(bboxes)
        scores = np.array(scores)
        labels = np.array(labels)

        cls_msg = ClassificationResult(
            header=msg.header,
            classifier=self.classifier_name,
            target_names=self.label_names,
            labels=labels,
            label_names=[self.label_names[l] for l in labels],
            label_proba=scores)

        self.pub_rects.publish(rect_msg)
        self.pub_class.publish(cls_msg)

        if self.visualize:
            vis_img = img[:, :, ::-1].transpose(2, 0, 1)
            vis_bbox(vis_img,
                     bboxes,
                     labels,
                     scores,
                     label_names=self.label_names)
            fig = plt.gcf()
            fig.canvas.draw()
            w, h = fig.canvas.get_width_height()
            vis_img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
            fig.clf()
            vis_img.shape = (h, w, 3)
            plt.close()
            vis_msg = self.bridge.cv2_to_imgmsg(vis_img, 'rgb8')
            vis_msg.header = msg.header
            self.pub_image.publish(vis_msg)
Exemplo n.º 14
0
def detect(image, model="ssd300", gpu=-1, pretrained_model="voc0712"):
    detector = Detector(model, gpu, pretrained_model)
    img = utils.read_image(image, color=True)

    bbox, label, score = detector(image)

    vis_bbox(img, bbox, label, score, label_names=voc_bbox_label_names)
    plt.show()
Exemplo n.º 15
0
    def test_vis_bbox_invalid_inputs(self):
        if not optional_modules:
            return

        with self.assertRaises(ValueError):
            vis_bbox(
                self.img, self.bbox, self.label, self.score,
                label_names=self.label_names)
Exemplo n.º 16
0
 def test_vis_bbox_invalid_inputs(self):
     with self.assertRaises(ValueError):
         vis_bbox(self.img,
                  self.bbox,
                  self.label,
                  self.score,
                  label_names=self.label_names,
                  instance_colors=self.instance_colors,
                  sort_by_score=self.sort_by_score)
 def save_result_images(self, image, save_as, bboxes, labels, scores):
 
     if (self.model == 'fasterrcnnfpnresnet50') or (self.model == 'fasterrcnnfpnresnet101'):
         bbox, label, score = bboxes[0], labels[0], scores[0]
         vis_bbox(image, bbox, label, score, label_names=coco_bbox_label_names)
     else:
         vis_bbox(image, bboxes[0], labels[0], scores[0], label_names=voc_bbox_label_names)
     
     plt.savefig(save_as)        
Exemplo n.º 18
0
    def test_vis_bbox_invalid_inputs(self):
        if not optional_modules:
            return

        with self.assertRaises(ValueError):
            vis_bbox(self.img,
                     self.bbox,
                     self.label,
                     self.score,
                     label_names=self.label_names)
Exemplo n.º 19
0
    def __call__(self, img, bbox, label=None, score=None, save=False, path=None):
        vis_bbox(
            img, bbox, label, score, label_names=self.names
        )
        plt.xticks([])
        plt.yticks([])

        if save:
            if path is None:
                raise ValueError('Path should be set')
            plt.savefig(path)
        else:
            plt.show()
Exemplo n.º 20
0
def save_data(image, bbox, mask):
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt

    from chainercv import visualizations

    img = np.concatenate((image, image, mask * 255), axis=0)
    visualizations.vis_bbox(img, bbox)
    global N
    plt.savefig('data/{}.png'.format(N))
    N += 1
    plt.close()
Exemplo n.º 21
0
def runRCNN():
    startTime = time.time()
    #print(startTime)
    # Read an RGB image and return it in CHW format.
    img = read_image('sample7.jpg')
    model = SSD300(pretrained_model='voc0712')
    model1 = SSD512(pretrained_model='voc0712')
    bboxes, labels, scores = model1.predict([img])
    vis_bbox(img,
             bboxes[0],
             labels[0],
             scores[0],
             label_names=voc_bbox_label_names)
    print(time.time() - startTime)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=0)
    parser.add_argument('--engine',
                        required=False,
                        help='TensorRT engine file. If None, chainer-based '
                        'inference will run')
    parser.add_argument('--n-try', '-n', type=int, default=1000)
    parser.add_argument('--chainer', action='store_true')
    parser.add_argument('image')
    args = parser.parse_args()

    model = YOLOv2(n_fg_class=len(voc_bbox_label_names),
                   pretrained_model='voc0712')
    img = utils.read_image(args.image, color=True)

    chainer.cuda.get_device_from_id(args.gpu).use()

    if args.engine is not None:
        # Key idea:
        # `predict` method applies some pre-processings and preparations,
        # runs forward-pass and some post-processings.
        # We want to re-use all of them without copy-and-pasting the code,
        # while only forward-pass runs with TensorRT.
        # solution: replace __call__ and let `predict` call it,
        # as if it is the original python based forward pass
        def run_infer(self, x):
            return tuple(chainer.Variable(t) for t in infer(x))

        infer = chainer_trt.Infer(args.engine)  # NOQA
        type(model).__call__ = run_infer
        print("Loaded TensorRT inference engine {}".format(args.engine))
    else:
        model.to_gpu()
        print("Run Chainer based inference")

    # Run inference once and get detections
    # (1st execution tends to be slow in CUDA)
    bboxes, labels, scores = model.predict([img])

    before = time.time()
    for _ in range(args.n_try):
        model.predict([img])
    after = time.time()
    print('{:.3f}ms/img'.format(1000 * (after - before) / args.n_try))

    bbox, label, score = bboxes[0], labels[0], scores[0]
    vis_bbox(img, bbox, label, score, label_names=voc_bbox_label_names)
    plt.show()
Exemplo n.º 23
0
 def publish_bbox_image(self, img, bbox, label, score):
     vis_bbox(img, bbox, label, score, label_names=self.label_names)
     fig = plt.gcf()
     fig.canvas.draw()
     w, h = fig.canvas.get_width_height()
     img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
     fig.clf()
     img.shape = (h, w, 3)
     plt.close()
     try:
         msg = self.cv_bridge.cv2_to_imgmsg(img, "rgb8")
     except Exception as e:
         rospy.logerr("Failed to convert bbox image: %s" % str(e))
         return
     self.pub_image.publish(msg)
Exemplo n.º 24
0
def main():
    chainer.config.train = False

    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained_model')
    parser.add_argument('image')
    parser.add_argument(
        '--label_names', help='The path to the yaml file with label names')
    args = parser.parse_args()

    with open(args.label_names, 'r') as f:
        label_names = tuple(yaml.load(f))
    model = SSD300(
        n_fg_class=len(label_names),
        pretrained_model=args.pretrained_model)
    # Change the threshold for showing labels
    #model.score_thresh=0.55

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)
    bboxes, labels, scores = model.predict([img])
    bbox, label, score = bboxes[0], labels[0], scores[0]
    nb_containers = sum([1 for l in label if label_names[int(l)] == 'container'])

    a = vis_bbox(
        img, bbox, label, score, label_names=label_names)
    a.annotate("Number of containers = {}".format(nb_containers), xytext=(0,0), xy=(0,0))
    plot.show()
Exemplo n.º 25
0
def annotate(args, path_in, path_out):
    chainer.config.train = False

    with open("model/label_names_coco_container.yml", 'r') as f:
        label_names = tuple(yaml.load(f))
    model = SSD300(
        n_fg_class=len(label_names),
        pretrained_model=args.model)
    # Change the threshold for showing labels
    # model.score_thresh=0.4
    # model.nms_thres = args.threshold

    count = 1
    numfiles = len([f for f in os.listdir(path_in) if ".jpg" in f])

    for file in [f for f in os.listdir(path_in) if ".jpg" in f]:
        img = utils.read_image(path_in+file, color=True)
        bboxes, labels, scores = model.predict([img])
        bbox, label, score = bboxes[0], labels[0], scores[0]
        nb_containers = sum([1 for l in label if label_names[int(l)] == 'container'])

        a = vis_bbox(
            img, bbox, label, score, label_names=label_names)
        a.annotate("Number of containers = {}".format(nb_containers), xytext=(0, 0), xy=(0, 0))
        plot.axis('off')
        plot.savefig(path_out + "frame{}.png".format(str(count).zfill(5)), bbox_inches='tight')
        plot.close()
        if count % 10 == 0:
            print("Progress: %0.2f%%" % (count / numfiles * 100,), flush=True)
        count += 1
Exemplo n.º 26
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained-model', default='sbd')
    parser.add_argument('image')
    args = parser.parse_args()

    model = FCISPSROIAlignResNet101(n_fg_class=20,
                                    pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = read_image(args.image, color=True)

    masks, labels, scores = model.predict([img])
    mask, label, score = masks[0], labels[0], scores[0]
    bbox = mask_to_bbox(mask)
    colors = voc_colormap(list(range(1, len(mask) + 1)))
    ax = vis_bbox(img, bbox, instance_colors=colors, alpha=0.5, linewidth=1.5)
    vis_instance_segmentation(
        None,
        mask,
        label,
        score,
        label_names=sbd_instance_segmentation_label_names,
        instance_colors=colors,
        alpha=0.7,
        ax=ax)
    plt.show()
Exemplo n.º 27
0
 def publish_bbox_image(self, img, bbox, label, score):
     vis_bbox(img, bbox, label, score,
              label_names=self.label_names)
     fig = plt.gcf()
     fig.canvas.draw()
     w, h = fig.canvas.get_width_height()
     img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
     fig.clf()
     img.shape = (h, w, 3)
     plt.close()
     try:
         msg = self.cv_bridge.cv2_to_imgmsg(img, "rgb8")
     except Exception as e:
         rospy.logerr("Failed to convert bbox image: %s" % str(e))
         return
     self.pub_image.publish(msg)
Exemplo n.º 28
0
def visualize_dataset(config):
    from matplotlib import pyplot as plt
    dataset = select_dataset(config, return_data=["train_set"])
    hand_class = config.get('model_param', 'hand_class').split(",")
    hand_class = [k.strip() for k in hand_class]
    class_converter, flip_converter = create_converter(hand_class)
    logger.info("hand_class = {}".format(hand_class))
    logger.info("done get dataset")

    idx = random.randint(0, len(dataset) - 1)
    logger.info("get example")
    rgb, rgb_bbox, rgb_class = dataset.get_example(idx)
    logger.info("Done get example")
    fig = plt.figure(figsize=(5, 10))
    ax1 = fig.add_subplot(211)
    ax2 = fig.add_subplot(212)

    label = rgb_class
    class_converter = {v: k for k, v in class_converter.items()}
    color = [COLOR_MAP[class_converter[c]] for c in label]
    print(label)
    vis_bbox(
        rgb,
        rgb_bbox,
        instance_colors=color,
        label=label,
        label_names=hand_class,
        ax=ax1,
    )

    model = create_ssd_model()
    transform_dataset = TransformDataset(
        dataset, Transform(model.coder, model.insize, model.mean, train=True))

    img, mb_loc, mb_label = transform_dataset.get_example(idx)
    mb_color = [COLOR_MAP[class_converter[c]] for c in mb_label]
    vis_bbox(
        img,
        mb_loc,
        instance_colors=mb_color,
        label=mb_label,
        label_names=hand_class,
        ax=ax2,
    )
    plt.savefig("vis.png")
    plt.show()
Exemplo n.º 29
0
    def box_alignment(self, img, old_boxes, new_boxes, Spixels,
                      contours, new_superset, save=True, path=None
                      ):
        """ Visualize the change in the bboxes after box alignment

        Args:
            img      : Input image
            old_boxes: Old bboxes
            new_boxes: Updated bboxes
            Spixels  : set of superpixels
        """
        # fig = plt.figure(figsize=(16, 7))
        fig = plt.figure()
        ax1 = fig.add_subplot(221)
        plt.xticks([])
        plt.yticks([])
        plt.title('Superpixels')
        plt.imshow(contours)

        ax1 = fig.add_subplot(222)
        plt.xticks([])
        plt.yticks([])
        plt.title('Original box')
        vis_bbox(img, old_boxes, ax=ax1, linewidth=1.0)

        ax1 = fig.add_subplot(223)
        plt.xticks([])
        plt.yticks([])
        plt.title('Superpixels added')
        plt.imshow(new_superset[0])

        ax2 = fig.add_subplot(224)
        plt.xticks([])
        plt.yticks([])
        plt.title('After box-align')
        vis_bbox(img, new_boxes, ax=ax2, linewidth=1.0)
        for spixel in Spixels:
            plt.imshow(spixel, cmap='gray', alpha=0.5, interpolation='none')
        if save:
            if path is None:
                raise ValueError('Path should be set')
            plt.savefig(path+'.png')
        else:
            plt.show()
        plt.close(fig)
Exemplo n.º 30
0
    def test_vis_bbox(self):
        if not optional_modules:
            return

        ax = vis_bbox(
            self.img, self.bbox, self.label, self.score,
            label_names=self.label_names)

        self.assertIsInstance(ax, matplotlib.axes.Axes)
def save_images(image_path, model, out_root, label_names):
    image_path_list = [x for x in glob.glob(os.path.join(image_path, '*.jpg'))]
    image_path_list += [
        x for x in glob.glob(os.path.join(image_path, '*.png'))
    ]
    if not os.path.exists(out_root):
        os.mkdir(out_root)
    fig, ax = plt.subplots()
    for _img in image_path_list:
        img = chainercv.utils.read_image(_img, color=True)
        with chainer.using_config('train', False):
            bboxes, labels, scores = model.predict([img])
        bbox, label, score = bboxes[0], labels[0], scores[0]
        logger.info("{} {} {}".format(bbox, label, score))
        vis_bbox(img, bbox, label, score, label_names=label_names, ax=ax)
        f_name = os.path.join(out_root, os.path.basename(_img))
        plt.savefig(f_name)
        ax.clear()
Exemplo n.º 32
0
    def visualize_cb(self, event):
        if (not self.visualize or self.img is None or self.header is None
                or self.bboxes is None or self.labels is None
                or self.scores is None):
            return

        with self.lock:
            img = self.img.copy()
            header = copy.deepcopy(self.header)
            bboxes = self.bboxes.copy()
            labels = self.labels.copy()
            scores = self.scores.copy()

        fig = plt.figure(tight_layout={'pad': 0})
        ax = plt.Axes(fig, [0., 0., 1., 1.])
        ax.axis('off')
        fig.add_axes(ax)
        vis_bbox(img.transpose((2, 0, 1)),
                 bboxes,
                 labels,
                 scores,
                 label_names=self.label_names,
                 ax=ax)
        fig.canvas.draw()
        w, h = fig.canvas.get_width_height()
        vis_img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
        vis_img.shape = (h, w, 3)
        fig.clf()
        plt.close()
        if self.pub_image.get_num_connections() > 0:
            vis_msg = self.bridge.cv2_to_imgmsg(vis_img, 'rgb8')
            # BUG: https://answers.ros.org/question/316362/sensor_msgsimage-generates-float-instead-of-int-with-python3/  # NOQA
            vis_msg.step = int(vis_msg.step)
            vis_msg.header = header
            self.pub_image.publish(vis_msg)
        if self.pub_image_compressed.get_num_connections() > 0:
            # publish compressed http://wiki.ros.org/rospy_tutorials/Tutorials/WritingImagePublisherSubscriber  # NOQA
            vis_compressed_msg = CompressedImage()
            vis_compressed_msg.header = header
            vis_compressed_msg.format = "jpeg"
            vis_img_rgb = cv2.cvtColor(vis_img, cv2.COLOR_BGR2RGB)
            vis_compressed_msg.data = np.array(
                cv2.imencode('.jpg', vis_img_rgb)[1]).tostring()
            self.pub_image_compressed.publish(vis_compressed_msg)
Exemplo n.º 33
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        choices=('ssd300', 'ssd512'),
                        default='ssd300')
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained',
                        default='./models/model_iter_12000.npz')
    args = parser.parse_args()

    label_names = pose_bbox_label_names
    BboxDataset = PoseBboxDataset

    if args.model == 'ssd300':
        model = SSD300(n_fg_class=len(label_names),
                       pretrained_model=args.pretrained)
    elif args.model == 'ssd512':
        model = SSD512(n_fg_class=len(label_names),
                       pretrained_model=args.pretrained)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    for i in range(0, 10):
        dataset = BboxDataset(split='test')
        ori_img, ori_bbox, ori_label = dataset[i]
        pred_bbox, pred_labels, pred_scores = model.predict([ori_img])

        fig = plt.figure(figsize=(20, 10), dpi=80)
        fig.suptitle("Original vs Prediction Annotations", fontsize=32)
        ax1 = fig.add_subplot(1, 2, 1)
        ax1.set_xlabel("Original", fontsize=24)
        ax2 = fig.add_subplot(1, 2, 2)
        ax2.set_xlabel("Prediction", fontsize=24)
        vis_bbox(ori_img, ori_bbox, ori_label, label_names=label_names, ax=ax1)
        vis_bbox(ori_img,
                 pred_bbox[0],
                 pred_labels[0],
                 pred_scores[0],
                 label_names=label_names,
                 ax=ax2)
        plt.tight_layout()
        plt.savefig('visualization/compare-{}.png'.format(i))
Exemplo n.º 34
0
    def test_vis_bbox(self):
        ax = vis_bbox(self.img,
                      self.bbox,
                      self.label,
                      self.score,
                      label_names=self.label_names,
                      instance_colors=self.instance_colors,
                      sort_by_score=self.sort_by_score)

        self.assertIsInstance(ax, matplotlib.axes.Axes)
Exemplo n.º 35
0
def vis_pred_gt_bboxes(image, pred_bbox, gt_bbox, ax=None, show_iou=True):
    """Visualize both prediction and GT bboxes.

    The colors of prediction and GT bboxes are red and green, respectively.

    Args:
        image (~np.ndarray): Greyscale image of shape (H, W).
        pred_bbox (~np.ndarray): Prediction bbox of shape (R, 4).
        gt_bbox (~np.ndarray): GT bbox of shape (R', 4).
        ax (matplotlib.axes.Axis): The visualization is displayed on this
            axis. If this is :obj:`None` (default), a new axis is created.
        show_iou (bool): If `True`, show maximum IoU for each prediction bbox.

    Returns:
        ~matploblib.axes.Axes:
        Returns the Axes object with the plot for further tweaking.

    """
    image = np.repeat([image], 3, axis=0)  # (3, H, W)

    if show_iou and len(pred_bbox) > 0 and len(gt_bbox) > 0:
        iou = chainercv.utils.bbox_iou(pred_bbox, gt_bbox)
        iou = iou.max(axis=1)

        label_names = ['%.3f' % x for x in iou]
        label = range(len(label_names))
    else:
        label_names, label = None, None

    ax = vis_bbox(image,
                  pred_bbox,
                  label=label,
                  linewidth=1,
                  ax=ax,
                  label_names=label_names)
    transp_image = np.zeros((4, *image.shape[1:]), dtype=np.float32)
    ax = vis_bbox(transp_image,
                  gt_bbox,
                  instance_colors=((0, 255, 0), ),
                  ax=ax,
                  linewidth=1)
    return ax
Exemplo n.º 36
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--model', choices=('yolo_v2', 'yolo_v2_tiny', 'yolo_v3'),
        default='yolo_v2')
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--pretrained-model', default='voc0712')
    parser.add_argument('--export', action='store_true')
    parser.add_argument('image')
    args = parser.parse_args()

    if args.model == 'yolo_v2':
        model = YOLOv2(
            n_fg_class=len(voc_bbox_label_names),
            pretrained_model=args.pretrained_model)
    elif args.model == 'yolo_v2_tiny':
        model = YOLOv2Tiny(
            n_fg_class=len(voc_bbox_label_names),
            pretrained_model=args.pretrained_model)
    elif args.model == 'yolo_v3':
        model = YOLOv3(
            n_fg_class=len(voc_bbox_label_names),
            pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    img = utils.read_image(args.image, color=True)

    if args.export:
        import onnx_chainer
        x = model.xp.stack([img])
        onnx_chainer.export_testcase(model, x, args.model)
        return

    bboxes, labels, scores = model.predict([img])
    bbox, label, score = bboxes[0], labels[0], scores[0]

    vis_bbox(
        img, bbox, label, score, label_names=voc_bbox_label_names)
    plt.show()
Exemplo n.º 37
0
if __name__ == '__main__':
    from chainercv.visualizations import vis_bbox
    from chainercv.visualizations import vis_semantic_segmentation
    from chainercv.chainer_experimental.datasets.sliceable import TransformDataset 
    import matplotlib.pyplot as plt
    import numpy as np

    voc_segm = VOCSemanticSegmentationWithBboxDataset(
        split='aug')
    dataset = VOCSemanticSegmentationWithBboxDataset(
        split='aug').slice[:, ['img', 'bbox', 'label']]
    # transformed = TransformDataset(
    #     dataset, ('img', 'label_map'), grabcut_transform)
    transformed = TransformDataset(
        dataset, ('img', 'label_map'), SimpleDoesItTransform())

    indices = np.random.choice(np.arange(len(voc_segm)), size=(10,))
    for index in indices:
        img, label_map, bbox, label = voc_segm[index]

        vis_bbox(img, bbox, label)
        plt.show()
        # see doc for better visualization
        vis_semantic_segmentation(img, label_map)
        plt.show()

        img, label_map = transformed[index]
        vis_semantic_segmentation(img, label_map, alpha=0.6, ignore_label_color=(255, 0, 0))
        plt.show()