Example #1
0
def show_frame():
    global operators_val
    global display
    global window

    ret, frame = cap.read()
    #    frame = cv2.imread('ade20k_example.jpg')
    frame = cv2.resize(frame, (0, 0), fx=0.9, fy=0.9)

    frame = cv2.flip(frame, 1)
    if int(operators_val.get()) == 0:
        pass
    elif int(operators_val.get()) == 1:
        x, img = load_test(frame, short=200)
        output = seg_net.demo(x)
        predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()
        print(predict.shape)

        mask = get_color_pallete(predict, 'pascal_voc')
        img = np.asarray(mask)
        print(img.shape)

        mask = cv2.resize(img, (frame.shape[1], frame.shape[0]))
        frame = frame.astype(np.float32) + mask.astype(np.float32)
        frame[frame > 255] = 255
        frame = frame.astype(np.uint8)
    elif int(operators_val.get()) == 2:
        x, img = load_test(frame, short=200)
        class_IDs, scores, bounding_boxs = net(x)
        frame = plot_bbox(frame,
                          img,
                          bounding_boxs[0],
                          scores[0],
                          class_IDs[0],
                          class_names=net.classes)
    elif int(operators_val.get()) == 3:
        frame = 255 - frame
    elif int(operators_val.get()) == 4:

        model = gluoncv.model_zoo.get_model('psp_resnet50_ade',
                                            pretrained=True)
        img, _ = load_test(frame, 150)
        output = model.demo(img)
        predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()
        mask = get_color_pallete(predict, 'ade20k')
        img = np.asarray(mask.convert('RGB'))
        mask = cv2.resize(img, (frame.shape[1], frame.shape[0]))
        frame = frame.astype(np.float32) + mask.astype(np.float32)
        frame[frame > 255] = 255
        frame = frame.astype(np.uint8)

    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    display.imgtk = imgtk
    display.configure(image=imgtk)
    window.after(10, show_frame)
Example #2
0
    def process(self, filename):
        origimg = image.imread(filename + '.png')
        img = self.transform_fn(origimg)
        img = img.expand_dims(0).as_in_context(self.ctx)
        output = self.model.demo(img)
        predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()

        s = set([])
        for (x, y), value in np.ndenumerate(predict):
            s.add(value)
        print(s)  #0:background, 15:person

        marginmap = np.zeros((predict.shape[0], predict.shape[1]), np.uint8)
        for (h, w), value in np.ndenumerate(predict):
            if marginmap[h][w] == 0 and value == 15:
                for u in range(-20, 20):
                    for v in range(-20, 20):
                        try:
                            marginmap[h + u][w + v] = 1
                            predict[h + u][w + v] = 15.0
                        except:
                            pass

        mask = get_color_pallete(predict, self.palletename)
        mask.save(filename + '_marginmask.png')

        mask2 = np.array(mask.convert('RGB'))

        overlay = cv2.addWeighted(origimg.asnumpy(), 0.5, mask2, 0.5, 0)
        overlay = Image.fromarray(overlay)
        overlay.save(filename + '_marginoverlay.png')
Example #3
0
def my_color_palette(npimg, dataset: str):
    """
    Visualize image and return PIL.Image with color palette.

    :param npimg: Single channel numpy image with shape `H, W, 1`
    :param dataset: dataset name
    """

    if dataset in ('pascal_voc', 'ade20k', 'citys', 'mhpv1'):
        return get_color_pallete(npimg, dataset=dataset)

    elif dataset == 'camvid':
        npimg[npimg == -1] = 11
        out_img = Image.fromarray(npimg.astype('uint8'))
        out_img.putpalette(cam_palette)
        return out_img
    elif dataset == 'mapillary':
        npimg[npimg == -1] = 65
        color_img = _apply_mapillary_palette(npimg)
        out_img = Image.fromarray(color_img)
        return out_img
    elif dataset == 'aeroscapes':
        out_img = Image.fromarray(npimg.astype('uint8'))
        out_img.putpalette(aeroscapes_palette)
        return out_img
    else:
        raise RuntimeError("Un-defined palette for data {}".format(dataset))
Example #4
0
def imageSegment(model_name, input_pic):

    # Load Images
    img = mx.image.imread("images/" + input_pic)

    # Transform
    transform_fn = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([.485, .456, .406], [.229, .224, .225])
    ])
    img = transform_fn(img)

    # Add extra dimension to img for batch size
    img = img.expand_dims(0)

    # Assume pretrained
    net = gcv.model_zoo.get_model(model_name, pretrained=True)

    # Output has dimensions of: (# images, # classes, pixelY, pixelX) in logits
    out = net.predict(img)
    out = out[0]
    # pred will contain the class index with highest probability for each pixel
    pred = mx.nd.argmax(out, 0).asnumpy()
    pred_img = get_color_pallete(pred, 'ade20k')
    plt.figure()
    plt.imshow(pred_img)
Example #5
0
def test_quantization(model, args, test_data, size, num_class, pred_offset):
    # output folder
    outdir = 'outdir_int8'
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    print(model)
    metric = gluoncv.utils.metrics.SegmentationMetric(num_class)

    tbar = tqdm(test_data)
    metric.reset()
    tic = time.time()
    for i, (batch, dsts) in enumerate(tbar):
        if args.eval:
            targets = mx.gluon.utils.split_and_load(dsts, ctx_list=args.ctx, even_split=False)
            data = mx.gluon.utils.split_and_load(batch, ctx_list=args.ctx, batch_axis=0, even_split=False)
            outputs = None
            for x in data:
                output = model(x)
                outputs = output if outputs is None else nd.concat(outputs, output, axis=0)
            metric.update(targets, outputs)
            pixAcc, mIoU = metric.get()
            tbar.set_description('pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
        else:
            for data, impath in zip(batch, dsts):
                data = data.as_in_context(args.ctx[0])
                if len(data.shape) < 4:
                    data = nd.expand_dims(data, axis=0)
                predict = model(data)[0]
                predict = mx.nd.squeeze(mx.nd.argmax(predict, 1)).asnumpy() + pred_offset
                mask = get_color_pallete(predict, args.dataset)
                outname = os.path.splitext(impath)[0] + '.png'
                mask.save(os.path.join(outdir, outname))
    speed = size / (time.time() - tic)
    print('Inference speed with batchsize %d is %.2f img/sec' % (args.batch_size, speed))
Example #6
0
def test(args):
    # output folder
    outdir = 'outdir'
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    # image transform
    input_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([.485, .456, .406], [.229, .224, .225]),
    ])
    # dataset and dataloader
    if args.eval:
        testset = get_segmentation_dataset(
            args.dataset, split='val', mode='testval', transform=input_transform, root='/mnt/mdisk/xcq/VOCdevkit/')
        total_inter, total_union, total_correct, total_label = \
            np.int64(0), np.int64(0), np.int64(0), np.int64(0)
    else:
        testset = get_segmentation_dataset(
            args.dataset, split='test', mode='test', transform=input_transform, root='/mnt/mdisk/xcq/VOCdevkit/')
    test_data = gluon.data.DataLoader(
        testset, args.test_batch_size, shuffle=False, last_batch='keep',
        batchify_fn=ms_batchify_fn, num_workers=args.workers)
    # create network
    if args.model_zoo is not None:
        model = get_model(args.model_zoo, pretrained=True)
    else:
        model = get_segmentation_model(model=args.model, dataset=args.dataset, ctx=args.ctx,
                                       backbone=args.backbone, norm_layer=args.norm_layer,
                                       norm_kwargs=args.norm_kwargs, aux=args.aux,
                                       base_size=args.base_size, crop_size=args.crop_size)
        # load pretrained weight
        assert args.resume is not None, '=> Please provide the checkpoint using --resume'
        if os.path.isfile(args.resume):
            model.load_parameters(args.resume, ctx=args.ctx)
        else:
            raise RuntimeError("=> no checkpoint found at '{}'" \
                .format(args.resume))
    print(model)
    evaluator = MultiEvalModel(model, testset.num_class, ctx_list=args.ctx)
    metric = gluoncv.utils.metrics.SegmentationMetric(testset.num_class)

    tbar = tqdm(test_data)
    for i, (data, dsts) in enumerate(tbar):
        if args.eval:
            predicts = [pred[0] for pred in evaluator.parallel_forward(data)]
            targets = [target.as_in_context(predicts[0].context) \
                       for target in dsts]
            metric.update(targets, predicts)
            pixAcc, mIoU = metric.get()
            tbar.set_description( 'pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
        else:
            im_paths = dsts
            predicts = evaluator.parallel_forward(data)
            for predict, impath in zip(predicts, im_paths):
                predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 1)).asnumpy() + \
                    testset.pred_offset
                mask = get_color_pallete(predict, args.dataset)
                outname = os.path.splitext(impath)[0] + '.png'
                mask.save(os.path.join(outdir, outname))
Example #7
0
def test(args):
    # output folder
    means = nd.array([123, 117, 104])
    std = nd.array([58.395, 57.12, 57.375])
    outdir = 'outdir'
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    # dataset and dataloader
    if args.eval:
        img = image.imread('./0000000152.png')
        img = img.astype('float32')
        img = img - means
        img = img / std
        img = nd.transpose(img, (2, 0, 1))
        img = nd.expand_dims(img, axis=0)
        testset = get_segmentation_dataset(
            args.dataset, split='val', mode='testval', transform=input_transform, root='/mnt/mdisk/xcq/VOCdevkit/')
        total_inter, total_union, total_correct, total_label = \
            np.int64(0), np.int64(0), np.int64(0), np.int64(0)
    else:
        testset = get_segmentation_dataset(
            args.dataset, split='test', mode='test', transform=input_transform, root='/mnt/mdisk/xcq/VOCdevkit/')
    if args.model_zoo is not None:
        model = get_model(args.model_zoo, pretrained=True)
    else:
        model = get_segmentation_model(model=args.model, dataset=args.dataset, ctx=args.ctx,
                                       backbone=args.backbone, norm_layer=args.norm_layer,
                                       norm_kwargs=args.norm_kwargs, aux=args.aux,
                                       base_size=args.base_size, crop_size=args.crop_size)
        # load pretrained weight
        assert args.resume is not None, '=> Please provide the checkpoint using --resume'
        if os.path.isfile(args.resume):
            model.load_parameters(args.resume, ctx=args.ctx)
        else:
            raise RuntimeError("=> no checkpoint found at '{}'" \
                .format(args.resume))
    print(model)
    predicts = model(img)
    evaluator = MultiEvalModel(model, testset.num_class, ctx_list=args.ctx)
    metric = gluoncv.utils.metrics.SegmentationMetric(testset.num_class)

    tbar = tqdm(test_data)
    for i, (data, dsts) in enumerate(tbar):
        if args.eval:
            predicts = [pred[0] for pred in evaluator.parallel_forward(data)]
            targets = [target.as_in_context(predicts[0].context) \
                       for target in dsts]
            metric.update(targets, predicts)
            pixAcc, mIoU = metric.get()
            tbar.set_description( 'pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
        else:
            im_paths = dsts
            predicts = evaluator.parallel_forward(data)
            for predict, impath in zip(predicts, im_paths):
                predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 1)).asnumpy() + \
                    testset.pred_offset
                mask = get_color_pallete(predict, args.dataset)
                outname = os.path.splitext(impath)[0] + '.png'
                mask.save(os.path.join(outdir, outname))
Example #8
0
 def getColor(self, predict, dataset='ade20k', saved_name=''):
     colored = get_color_pallete(predict, dataset)
     if saved_name != '':
         try:
             colored.save(saved_name)
             # io.imsave(saved_name, colored, check_contrast=False)
         except Exception as e:
             print("Error in saving file in getSeg():", e, saved_name)
     return colored
def show_color_pallete(predict):
    from gluoncv.utils.viz import get_color_pallete
    import matplotlib.image as mpimg
    mask = get_color_pallete(predict, 'pascal_voc')
    mask.save('output.png')

    # show the predicted mask
    mmask = mpimg.imread('output.png')
    plt.imshow(mmask)
    plt.show()
Example #10
0
def test_quantization(model, args, input_transform):
    # output folder
    outdir = 'outdir_int8'
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    # hybridize
    model.hybridize(static_alloc=True, static_shape=True)

    # get dataset
    if args.eval:
        testset = get_segmentation_dataset(
            args.dataset, split='val', mode=args.mode, transform=input_transform)
    else:
        testset = get_segmentation_dataset(
            args.dataset, split='test', mode=args.mode, transform=input_transform)
    size = len(testset)
    batchify_fn = ms_batchify_fn if testset.mode == 'test' else None
    test_data = gluon.data.DataLoader(
            testset, args.batch_size, batchify_fn=batchify_fn, last_batch='keep',
            shuffle=False, num_workers=args.workers)
    print(model)
    metric = gluoncv.utils.metrics.SegmentationMetric(testset.num_class)

    tbar = tqdm(test_data)
    metric.reset()
    tic = time.time()
    for i, (batch, dsts) in enumerate(tbar):
        if args.eval:
            targets = mx.gluon.utils.split_and_load(dsts, ctx_list=args.ctx, even_split=False)
            data = mx.gluon.utils.split_and_load(batch, ctx_list=args.ctx, batch_axis=0, even_split=False)
            outputs = None
            for x in data:
                output = model(x)
                outputs = output if outputs is None else nd.concat(outputs, output, axis=0)
            metric.update(targets, outputs)
            pixAcc, mIoU = metric.get()
            tbar.set_description('pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
        else:
            for data, impath in zip(batch, dsts):
                data = data.as_in_context(args.ctx[0])
                if len(data.shape) < 4:
                    data = nd.expand_dims(data, axis=0)
                predict = model(data)[0]
                predict = mx.nd.squeeze(mx.nd.argmax(predict, 1)).asnumpy() + \
                    testset.pred_offset
                mask = get_color_pallete(predict, args.dataset)
                outname = os.path.splitext(impath)[0] + '.png'
                mask.save(os.path.join(outdir, outname))
    speed = size / (time.time() - tic)
    print('Inference speed with batchsize %d is %.2f img/sec' % (args.batch_size, speed))
Example #11
0
def plot_image(model, img):
    st.warning("Inferencing from Model..")
    output = model.predict(img)
    predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()
    mask = get_color_pallete(predict, 'mhpv1')
    mask.save('output.png')
    mmask = mpimg.imread('output.png')
    image = mpimg.imread('saved_image.jpg')

    st.set_option('deprecation.showPyplotGlobalUse', False)
    st.success("Pose Estimation Successful!! Plotting Image..")

    st.image(image, width=500, height=500)
    st.image(mmask, width=500, height=500)
Example #12
0
def test(model, args, input_transform):
    # DO NOT modify!!! Only support batch_size=ngus
    batch_size = args.ngpus
    # output folder
    outdir = 'outdir'
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    # get dataset
    if args.eval:
        testset = get_segmentation_dataset(args.dataset,
                                           split='val',
                                           mode='testval',
                                           transform=input_transform)
        total_inter, total_union, total_correct, total_label = \
            np.int64(0), np.int64(0), np.int64(0), np.int64(0)
    else:
        testset = get_segmentation_dataset(args.dataset,
                                           split='test',
                                           mode='test',
                                           transform=input_transform)
    test_data = gluon.data.DataLoader(testset,
                                      batch_size,
                                      shuffle=False,
                                      last_batch='keep',
                                      batchify_fn=ms_batchify_fn,
                                      num_workers=args.workers)
    print(model)
    evaluator = MultiEvalModel(model, testset.num_class, ctx_list=args.ctx)
    metric = gluoncv.utils.metrics.SegmentationMetric(testset.num_class)

    tbar = tqdm(test_data)
    for i, (data, dsts) in enumerate(tbar):
        if args.eval:
            predicts = [pred[0] for pred in evaluator.parallel_forward(data)]
            targets = [target.as_in_context(predicts[0].context) \
                       for target in dsts]
            metric.update(targets, predicts)
            pixAcc, mIoU = metric.get()
            tbar.set_description('pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
        else:
            im_paths = dsts
            predicts = evaluator.parallel_forward(data)
            for predict, impath in zip(predicts, im_paths):
                predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 1)).asnumpy() + \
                    testset.pred_offset
                mask = get_color_pallete(predict, args.dataset)
                outname = os.path.splitext(impath)[0] + '.png'
                mask.save(os.path.join(outdir, outname))
Example #13
0
def test(args):
    # output folder
    outdir = 'outdir'
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    # image transform
    input_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([.485, .456, .406], [.229, .224, .225]),
    ])
    # dataset and dataloader
    testset = get_segmentation_dataset(args.dataset,
                                       split='test',
                                       transform=input_transform)
    test_data = gluon.data.DataLoader(testset,
                                      args.test_batch_size,
                                      last_batch='keep',
                                      batchify_fn=test_batchify_fn,
                                      num_workers=args.workers)
    # create network
    model = get_segmentation_model(model=args.model,
                                   dataset=args.dataset,
                                   backbone=args.backbone,
                                   norm_layer=args.norm_layer)
    print(model)
    evaluator = MultiEvalModel(model, testset.num_class, ctx_list=args.ctx)
    # load pretrained weight
    assert (args.resume is not None)
    if os.path.isfile(args.resume):
        model.load_params(args.resume, ctx=args.ctx)
    else:
        raise RuntimeError("=> no checkpoint found at '{}'" \
            .format(args.resume))

    tbar = tqdm(test_data)
    for i, (data, im_paths) in enumerate(tbar):
        predicts = evaluator.parallel_forward(data)
        for predict, impath in zip(predicts, im_paths):
            predict = mx.nd.squeeze(mx.nd.argmax(predict, 1)).asnumpy()
            mask = get_color_pallete(predict, args.dataset)
            outname = os.path.splitext(impath)[0] + '.png'
            mask.save(os.path.join(outdir, outname))
Example #14
0
def get_color_palette(npimg, dataset: str = 'pascal_voc'):
    """
    Visualize image and return PIL.Image with color palette.

    :param npimg: Single channel numpy image with shape `H, W, 1`
    :param dataset: dataset name
    """

    if dataset in ('pascal_voc', 'ade20k', 'citys', 'mhpv1'):
        return get_color_pallete(npimg, dataset=dataset)

    elif dataset == 'camvid':
        npimg[npimg == -1] = 11
        out_img = Image.fromarray(npimg.astype('uint8'))
        out_img.putpalette(cam_palette)
        return out_img
    elif dataset == 'kittizhang':
        npimg = npimg + 1
        out_img = Image.fromarray(npimg.astype('uint8'))
        out_img.putpalette(kittizhang_palette)
        return out_img
    elif dataset == 'kittixu':
        npimg = npimg + 1
        out_img = Image.fromarray(npimg.astype('uint8'))
        out_img.putpalette(kittixu_palette)
        return out_img
    elif dataset == 'kittiros':
        npimg[npimg == -1] = 11
        out_img = Image.fromarray(npimg.astype('uint8'))
        out_img.putpalette(kittiros_palette)
        return out_img
    elif dataset == 'gatech':
        npimg = npimg + 1
        out_img = Image.fromarray(npimg.astype('uint8'))
        out_img.putpalette(gatech_palette)
        return out_img
    else:
        raise RuntimeError("Un-defined palette for data {}".format(dataset))
Example #15
0
def test(model, args, input_transform):
    # DO NOT modify!!! Only support batch_size=ngus
    batch_size = args.ngpus

    # output folder
    outdir = 'outdir'
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    # get dataset
    if args.eval:
        testset = get_segmentation_dataset(
            args.dataset, split='val', mode='testval', transform=input_transform)
    else:
        testset = get_segmentation_dataset(
            args.dataset, split='test', mode='test', transform=input_transform)

    if 'icnet' in args.model:
        test_data = gluon.data.DataLoader(
            testset, batch_size, shuffle=False, last_batch='rollover',
            num_workers=args.workers)
    else:
        test_data = gluon.data.DataLoader(
            testset, batch_size, shuffle=False, last_batch='rollover',
            batchify_fn=ms_batchify_fn, num_workers=args.workers)
    print(model)

    if 'icnet' in args.model:
        evaluator = DataParallelModel(SegEvalModel(model), ctx_list=args.ctx)
    else:
        evaluator = MultiEvalModel(model, testset.num_class, ctx_list=args.ctx)

    metric = gluoncv.utils.metrics.SegmentationMetric(testset.num_class)

    if 'icnet' in args.model:
        tbar = tqdm(test_data)
        t_gpu = 0
        num = 0
        for i, (data, dsts) in enumerate(tbar):
            tic = time.time()
            outputs = evaluator(data.astype('float32', copy=False))
            t_gpu += time.time() - tic
            num += 1

            outputs = [x[0] for x in outputs]
            targets = mx.gluon.utils.split_and_load(dsts, ctx_list=args.ctx, even_split=False)
            metric.update(targets, outputs)

            pixAcc, mIoU = metric.get()
            gpu_time = t_gpu / num
            tbar.set_description('pixAcc: %.4f, mIoU: %.4f, t_gpu: %.2fms' % (pixAcc, mIoU, gpu_time*1000))
    else:
        tbar = tqdm(test_data)
        for i, (data, dsts) in enumerate(tbar):
            if args.eval:
                predicts = [pred[0] for pred in evaluator.parallel_forward(data)]
                targets = [target.as_in_context(predicts[0].context) \
                           for target in dsts]
                metric.update(targets, predicts)
                pixAcc, mIoU = metric.get()
                tbar.set_description('pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
            else:
                im_paths = dsts
                predicts = evaluator.parallel_forward(data)
                for predict, impath in zip(predicts, im_paths):
                    predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 1)).asnumpy() + \
                        testset.pred_offset
                    mask = get_color_pallete(predict, args.dataset)
                    outname = os.path.splitext(impath)[0] + '.png'
                    mask.save(os.path.join(outdir, outname))
Example #16
0
def test(args):
    # output folder
    outdir = 'outdir'
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    # image transform
    input_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([.485, .456, .406], [.229, .224, .225]),
    ])
    # dataset and dataloader
    if args.eval:
        testset = get_segmentation_dataset(args.dataset,
                                           split='val',
                                           mode='testval',
                                           transform=input_transform)
        total_inter, total_union, total_correct, total_label = \
            np.int64(0), np.int64(0), np.int64(0), np.int64(0)
    else:
        testset = get_segmentation_dataset(args.dataset,
                                           split='test',
                                           mode='test',
                                           transform=input_transform)
    test_data = gluon.data.DataLoader(testset,
                                      args.test_batch_size,
                                      last_batch='keep',
                                      batchify_fn=ms_batchify_fn,
                                      num_workers=args.workers)
    # create network
    if args.model_zoo is not None:
        model = get_model(args.model_zoo, pretrained=True)
    else:
        model = get_segmentation_model(model=args.model,
                                       dataset=args.dataset,
                                       ctx=args.ctx,
                                       backbone=args.backbone,
                                       norm_layer=args.norm_layer)
        # load pretrained weight
        assert args.resume is not None, '=> Please provide the checkpoint using --resume'
        if os.path.isfile(args.resume):
            model.load_params(args.resume, ctx=args.ctx)
        else:
            raise RuntimeError("=> no checkpoint found at '{}'" \
                .format(args.resume))
    print(model)
    evaluator = MultiEvalModel(model, testset.num_class, ctx_list=args.ctx)

    tbar = tqdm(test_data)
    for i, (data, dsts) in enumerate(tbar):
        if args.eval:
            targets = dsts
            predicts = evaluator.parallel_forward(data)
            for predict, target in zip(predicts, targets):
                target = target.as_in_context(predict[0].context)
                correct, labeled = batch_pix_accuracy(predict[0], target)
                inter, union = batch_intersection_union(
                    predict[0], target, testset.num_class)
                total_correct += correct.astype('int64')
                total_label += labeled.astype('int64')
                total_inter += inter.astype('int64')
                total_union += union.astype('int64')
            pixAcc = np.float64(1.0) * total_correct / (
                np.spacing(1, dtype=np.float64) + total_label)
            IoU = np.float64(1.0) * total_inter / (
                np.spacing(1, dtype=np.float64) + total_union)
            mIoU = IoU.mean()
            tbar.set_description('pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
        else:
            im_paths = dsts
            predicts = evaluator.parallel_forward(data)
            for predict, impath in zip(predicts, im_paths):
                predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 1)).asnumpy()
                mask = get_color_pallete(predict, args.dataset)
                outname = os.path.splitext(impath)[0] + '.png'
                mask.save(os.path.join(outdir, outname))
Example #17
0
            print("frame_save_full_path is " + frame_save_full_path)

            file_dirs = os.listdir(frame_save_full_path)
            video_name = video[:-4].split("/")[-1]

            # inference by using pretrained model
            for file in file_dirs:
                if file.endswith(
                    (
                        ".bmp",
                        ".dib",
                        ".png",
                        ".jpg",
                        ".jpeg",
                        ".pbm",
                        ".pgm",
                        ".ppm",
                        ".tif",
                        ".tiff",
                    )
                ):
                    img = image.imread(os.path.join(frame_save_full_path, file))
                    img = test_transform(img, ctx)
                    output = model.predict(img)
                    predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()
                    mask = get_color_pallete(predict, "citys")
                    mask.save(os.path.join(output_data_path, video_name + "_" + file))

        else:
            continue
Example #18
0
class_index = mx.nd.argmax(px_logit, axis=0)
# now convert float to int
class_index = class_index[0].astype('int').asscalar()
print(class_index)
# 2

# what is this class index?
# check the lookup table from indecies to labels
from gluoncv.data.ade20k.segmentation import ADE20KSegmentation
class_label = ADE20KSegmentation.CLASSES[class_index]
print(class_label)
# sky

# now perform it for all pixels
# axis=0 corresponds to channel dimension which are the classes
# output is a probability distributed across classes for every pixel
output_proba = mx.nd.softmax(output, axis=0)

output_heatmap = output_proba[2]
# plt.imshow(output_heatmap.asnumpy())
# plt.show()

prediction = mx.nd.argmax(output, 0).asnumpy()
# print(prediction)

# visualize most likely class
from gluoncv.utils.viz import get_color_pallete
prediction_image = get_color_pallete(prediction, 'ade20k')
plt.imshow(prediction_image)
plt.show()
Example #19
0
##############################################################################
# normalize the image using dataset mean
from gluoncv.data.transforms.presets.segmentation import test_transform
img = test_transform(img, ctx)

##############################################################################
# Load the pre-trained model and make prediction
# ----------------------------------------------
#
# get pre-trained model
model = gluoncv.model_zoo.get_model('icnet_resnet50_mhpv1', pretrained=True)

##############################################################################
# make prediction using single scale
output = model.predict(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()

##############################################################################
# Add color pallete for visualization
from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg
mask = get_color_pallete(predict, 'mhpv1')
mask.save('output.png')

##############################################################################
# show the predicted mask
mmask = mpimg.imread('output.png')
plt.imshow(mmask)
plt.show()
Example #20
0
from gluoncv.data import CitySegmentation
train_dataset = CitySegmentation(split='train')
val_dataset = CitySegmentation(split='val')
print('Training images:', len(train_dataset))
print('Validation images:', len(val_dataset))

################################################################
# Get the first sample
# --------------------
#
import numpy as np
img, mask = val_dataset[0]
# get pallete for the mask
from gluoncv.utils.viz import get_color_pallete
mask = get_color_pallete(mask.asnumpy(), dataset='citys')
mask.save('mask.png')

################################################################
# Visualize data and label
# ------------------------
#
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
# subplot 1 for img
fig = plt.figure()
fig.add_subplot(1, 2, 1)
plt.imshow(img.asnumpy().astype('uint8'))
# subplot 2 for the mask
mmask = mpimg.imread('mask.png')
fig.add_subplot(1, 2, 2)
Example #21
0
from gluoncv.data import ADE20KSegmentation
train_dataset = ADE20KSegmentation(split='train')
val_dataset = ADE20KSegmentation(split='val')
print('Training images:', len(train_dataset))
print('Validation images:', len(val_dataset))


################################################################
# Get the first sample
# --------------------
#
import numpy as np
img, mask = val_dataset[0]
# get pallete for the mask
from gluoncv.utils.viz import get_color_pallete
mask = get_color_pallete(mask.asnumpy(), dataset='ade20k')
mask.save('mask.png')


################################################################
# Visualize data and label
# ------------------------
#
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
# subplot 1 for img
fig = plt.figure()
fig.add_subplot(1,2,1)
plt.imshow(img.asnumpy().astype('uint8'))
# subplot 2 for the mask
mmask = mpimg.imread('mask.png')
Example #22
0
##############################################################################
# For data augmentation,
# we follow the standard data augmentation routine to transform the input image
# and the ground truth label map synchronously. (*Note that "nearest"
# mode upsample are applied to the label maps to avoid messing up the boundaries.*)
# We first randomly scale the input image from 0.5 to 2.0 times, then rotate
# the image from -10 to 10 degrees, and crop the image with padding if needed.
# Finally a random Gaussian blurring is applied.
#
# Random pick one example for visualization:
from random import randint
idx = randint(0, len(trainset))
img, mask = trainset[idx]
from gluoncv.utils.viz import get_color_pallete, DeNormalize
# get color pallete for visualize mask
mask = get_color_pallete(mask.asnumpy(), dataset='pascal_voc')
mask.save('mask.png')
# denormalize the image
img = DeNormalize([.485, .456, .406], [.229, .224, .225])(img)
img = np.transpose((img.asnumpy() * 255).astype(np.uint8), (1, 2, 0))

##############################################################################
# Plot the image and mask
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
# subplot 1 for img
fig = plt.figure()
fig.add_subplot(1, 2, 1)

plt.imshow(img)
# subplot 2 for the mask
Example #23
0
# we follow the standard data augmentation routine to transform the input image
# and the ground truth label map synchronously. (*Note that "nearest"
# mode upsample are applied to the label maps to avoid messing up the boundaries.*)
# We first randomly scale the input image from 0.5 to 2.0 times, then rotate
# the image from -10 to 10 degrees, and crop the image with padding if needed.
# Finally a random Gaussian blurring is applied.
#
# Random pick one example for visualization:
import random
from datetime import datetime
random.seed(datetime.now())
idx = random.randint(0, len(trainset))
img, mask = trainset[idx]
from gluoncv.utils.viz import get_color_pallete, DeNormalize
# get color pallete for visualize mask
mask = get_color_pallete(mask.asnumpy(), dataset='pascal_voc')
mask.save('mask.png')
# denormalize the image
img = DeNormalize([.485, .456, .406], [.229, .224, .225])(img)
img = np.transpose((img.asnumpy()*255).astype(np.uint8), (1, 2, 0))

##############################################################################
# Plot the image and mask
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
# subplot 1 for img
fig = plt.figure()
fig.add_subplot(1,2,1)

plt.imshow(img)
# subplot 2 for the mask
Example #24
0
# Load the pre-trained model and make prediction
# ----------------------------------------------
#
# get pre-trained model
model = gluoncv.model_zoo.get_model('fcn_resnet101_voc', pretrained=True)

##############################################################################
# make prediction using single scale
output = model.demo(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()

##############################################################################
# Add color pallete for visualization
from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg
mask = get_color_pallete(predict, 'pascal_voc')
mask.save('output.png')

##############################################################################
# show the predicted mask
mmask = mpimg.imread('output.png')
plt.imshow(mmask)
plt.show()

##############################################################################
# More Examples
# -------------
#
#.. image:: https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/segmentation/voc_examples/4.jpg
#    :width: 45%
#
    transforms.Normalize([.485, .456, .406], [.229, .224, .225])
])
img = transform_fn(img)
img = img.expand_dims(0).as_in_context(ctx)

##############################################################################
# Load the pre-trained model and make prediction
# ----------------------------------------------
#
# get pre-trained model
model = gluoncv.model_zoo.get_model('deeplab_resnet101_ade', pretrained=True)

##############################################################################
# make prediction using single scale
output = model.demo(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()

##############################################################################
# Add color pallete for visualization
from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg
mask = get_color_pallete(predict, 'ade20k')
mask.save('output.png')

##############################################################################
# show the predicted mask
mmask = mpimg.imread('output.png')
plt.imshow(mmask)
plt.show()

Example #26
0
def main():
    start = timer()
    print('Processing Start time: %.1f' % (start))
    print("current time", datetime.now())
    gauth = GoogleAuth()
    gauth.LocalWebserverAuth()

    drive = GoogleDrive(gauth)

    # Auto-iterate through all files that matches this query
    file_list = drive.ListFile({'q': "'root' in parents"}).GetList()
    for file in file_list:
        # print('title: {}, id: {}'.format(file1['title'], file1['id']))
        file_id = None

        if file['title'] == "semanticsegmentation":
            print('Folder Found')
            file_id = file['id']
            break
    if file_id is not None:
        classes = ["wall","building;edifice","sky","floor;flooring","tree","ceiling","road;route","bed","windowpane;window","grass","cabinet","sidewalk;pavement","person;individual;someone;somebody;mortal;soul","earth;ground","door;double;door","table","mountain;mount","plant;flora;plant;life","curtain;drape;drapery;mantle;pall","chair","car;auto;automobile;machine;motorcar","water","painting;picture","sofa;couch;lounge","shelf","house","sea","mirror","rug;carpet;carpeting","field","armchair","seat","fence;fencing","desk","rock;stone","wardrobe;closet;press","lamp","bathtub;bathing;tub;bath;tub","railing;rail","cushion","base;pedestal;stand","box","column;pillar","signboard;sign","chest;of;drawers;chest;bureau;dresser","counter","sand","sink","skyscraper","fireplace;hearth;open;fireplace","refrigerator;icebox","grandstand;covered;stand","path","stairs;steps","runway","case;display;case;showcase;vitrine","pool;table;billiard;table;snooker;table","pillow","screen;door;screen","stairway;staircase","river","bridge;span","bookcase","blind;screen","coffee;table;cocktail;table","toilet;can;commode;crapper;pot;potty;stool;throne","flower","book","hill","bench","countertop","stove;kitchen;stove;range;kitchen;range;cooking;stove","palm;palm;tree","kitchen;island","computer;computing;machine;computing;device;data;processor;electronic;computer;information;processing;system","swivel;chair","boat","bar","arcade;machine","hovel;hut;hutch;shack;shanty","bus;autobus;coach;charabanc;double-decker;jitney;motorbus;motorcoach;omnibus;passenger;vehicle","towel","light;light;source","truck;motortruck","tower","chandelier;pendant;pendent","awning;sunshade;sunblind","streetlight;street;lamp","booth;cubicle;stall;kiosk","television;television;receiver;television;set;tv;tv;set;idiot;box;boob;tube;telly;goggle;box","airplane;aeroplane;plane","dirt;track","apparel;wearing;apparel;dress;clothes","pole","land;ground;soil","bannister;banister;balustrade;balusters;handrail","escalator;moving;staircase;moving;stairway","ottoman;pouf;pouffe;puff;hassock","bottle","buffet;counter;sideboard","poster;posting;placard;notice;bill;card","stage","van","ship","fountain","conveyer;belt;conveyor;belt;conveyer;conveyor;transporter","canopy","washer;automatic;washer;washing;machine","plaything;toy","swimming;pool;swimming;bath;natatorium","stool","barrel;cask","basket;handbasket","waterfall;falls","tent;collapsible;shelter","bag","minibike;motorbike","cradle","oven","ball","food;solid;food","step;stair","tank;storage;tank","trade;name;brand;name;brand;marque","microwave;microwave;oven","pot;flowerpot","animal;animate;being;beast;brute;creature;fauna","bicycle;bike;wheel;cycle","lake","dishwasher;dish;washer;dishwashing;machine","screen;silver;screen;projection;screen","blanket;cover","sculpture","hood;exhaust;hood","sconce","vase","traffic;light;traffic;signal;stoplight","tray","ashcan;trash;can;garbage;can;wastebin;ash;bin;ash-bin;ashbin;dustbin;trash;barrel;trash;bin","fan","pier;wharf;wharfage;dock","crt;screen","plate","monitor;monitoring;device","bulletin;board;notice;board","shower","radiator","glass;drinking;glass","clock","flag"]
        files = glob.glob(r'/Users/divyachandana/Documents/NJIT/work/summertasks/jun1-jun5/atlanta/*.jpg')
        print("Total Files",len(files))
        columns = ['filename','class','total_pixel','individual_pixel','ratio','timestamp']

        # ---------- drive code -----
        with open('semantic_results_atlanta.csv','a') as csvfile:
            csvwriter = csv.writer(csvfile,lineterminator='\n')
            # csvwriter.writerow(columns)
            # i=0
            for f in files:
                file_check_query = "select count(*) from {} where filename like '%{}%'".format('semantic_results_atlanta', os.path.basename(f))
                # print(file_check_query)
                # i += 1
                # print(i)
                count = dbms.get_count_result(file_check_query)
                # print(count)
                if count > 0: continue
                # print('resuming',f)
                try:
                    img = image.imread(f)
                    img = image.resize_short(img, 1024)
                    #         img = image.resize_short(img, 100)
                    # print("filename: ", f)
                    #         ctx = mx.gpu(0)
                    img = test_transform(img, ctx)
                    #         print("img: ", img)
                    output = model.predict(img)
                    #         print("output: ", output)
                    predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()
                    #         print("predict: ", predict)
                    mask = get_color_pallete(predict, 'ade20k')
                    # predict.save('predict.png')
                    # mmask = mpimg.imread('output.png')
                    predict = predict.astype(numpy.uint8)
                    convert_single_array = numpy.array(predict)
                    unique_numbers = numpy.unique(convert_single_array)
                    # print(unique_numbers)
                    new_basename = os.path.basename(f).replace(".jpg", ".png")
                    new_name = os.path.join('output/', new_basename)
                    mask.save(new_name)
                    # color_img = image.imread(new_name)
                    # colors, counts = numpy.unique(color_img.reshape(-1, 3), return_counts=True, axis=0)
                    total_pixel = numpy.sum(predict)
                    d_file = drive.CreateFile({'parents': [{'id': file_id}], 'title': os.path.basename(new_name)})
                    d_file.SetContentFile(new_name)
                    d_file.Upload()
                    # print('Created file %s with mimeType %s' % (d_file['title'], d_file['mimeType']))
                    combile_all_csv_data = []
                    combine_sql_srting_format = []
                    for i in unique_numbers:
                        individual_count = numpy.sum(predict == i)
                        # print(individual_count)
                        csv_data = []
                        csv_data.append(os.path.basename(f))
                        csv_data.append(classes[i])
                        csv_data.append(total_pixel)
                        csv_data.append(individual_count)
                        csv_data.append(round((individual_count/total_pixel),6))
                        time_stamp = datetime.now()
                        csv_data.append(time_stamp)
                        # csv_data = [filename,predict,colors,counts,total_pixel]
                        # print(csv_data)
                        combile_all_csv_data.append(csv_data)
                        sql_srting  = ["NULL" if val == None else "'"+str(val)+"'" for val in csv_data]
                        sql_srting_format = ",".join([str(val) for val in sql_srting])
                        combine_sql_srting_format.append(sql_srting_format)
                    csvwriter.writerows(combile_all_csv_data)
                    dbms.insertmany_sqlite3('semantic_results_atlanta',','.join(columns),combine_sql_srting_format)

                    os.remove(new_name)
                    # if idx % 10 == 0:
                    #     print("Processed: ", idx)
                except Exception as e:
                    print("Error in :", '       ' + f, e)
                    continue
        print('Finished')
        end = timer()
        print('Processing time: %.1f' % (end - start))
def test(args):
    # output folder
    #outdir = 'outdir'
    outdir = 'outdir_01262021'
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    # image transform
    input_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([.485, .456, .406], [.229, .224, .225]),
    ])
    # dataset and dataloader
    if args.eval:
        #testset = get_segmentation_dataset(
        #    args.dataset, split='val', mode='testval', transform=input_transform)
        #testset = COCOSemantic(split='val', mode='testval', transform=input_transform)
        testset = COCOSemantic(split='val',
                               mode='testval',
                               transform=input_transform)

        total_inter, total_union, total_correct, total_label = \
            np.int64(0), np.int64(0), np.int64(0), np.int64(0)
    else:
        #testset = get_segmentation_dataset(
        #    args.dataset, split='test', mode='test', transform=input_transform)
        testset = COCOSemantic(split='test',
                               mode='testval',
                               transform=input_transform)

    print("testset.num_class: ")
    print(testset.num_class)

    #test_data = gluon.data.DataLoader(
    #    testset, args.test_batch_size, shuffle=False, last_batch='keep',
    #    batchify_fn=ms_batchify_fn, num_workers=args.workers)
    # what is this one used for? ms_batchify_fn     """Multi-size batchfy function"""

    test_data = gluon.data.DataLoader(testset,
                                      args.test_batch_size,
                                      last_batch='keep',
                                      num_workers=args.workers)
    """
    # only used for generating mask ground truth.
    print(len(testset.ids))
    for i in np.arange(len(testset.ids)):
        img_out, mask_out = testset[i]
    """

    # create network
    if args.model_zoo is not None:
        model = get_model(args.model_zoo, pretrained=True)
    else:
        #model = get_segmentation_model(model=args.model, dataset=args.dataset, ctx=args.ctx,
        #                               backbone=args.backbone, norm_layer=args.norm_layer,
        #                               norm_kwargs=args.norm_kwargs, aux=args.aux,
        #                               base_size=args.base_size, crop_size=args.crop_size)

        model = model_zoo.PSPNet(
            nclass=testset.num_class,
            backbone=args.backbone,
            aux=args.aux,
            pretrained_base=False,
            #norm_layer=args.norm_layer,
            #norm_kwargs=args.norm_kwargs,
            #crop_size=args.crop_size,
            ctx=args.ctx)
        model.cast(args.dtype)
        # load pretrained weight
        assert args.resume is not None, '=> Please provide the checkpoint using --resume'
        if os.path.isfile(args.resume):
            #model.load_parameters(args.resume, ctx=args.ctx)
            model.load_parameters(args.resume, ctx=args.ctx)
        else:
            raise RuntimeError("=> no checkpoint found at '{}'" \
                .format(args.resume))

    print(model)

    evaluator = MultiEvalModel(model, testset.num_class, ctx_list=args.ctx)
    # this is used in the val, not test
    #evaluator2 = DataParallelModel(SegEvalModel(model), args.ctx)

    metric = gluoncv.utils.metrics.SegmentationMetric(testset.num_class)

    tbar = tqdm(test_data)
    for i, (data, dsts) in enumerate(tbar):
        if args.eval:
            #predicts = [ pred[0] for pred in evaluator.parallel_forward(data)]
            #targets = [target.as_in_context(predicts[0].context) \
            #           for target in dsts]
            #predicts = evaluator(data.astype(args.dtype, copy=False))
            #predicts = [x[0] for x in predicts]

            # method 3
            predicts = [pred[0] for pred in evaluator.parallel_forward(data)]
            #predicts = [[x for x in predicts1]]

            print(len(predicts))
            print(len(predicts[0]))
            print(len(predicts[0][0]))
            #predicts = mx.nd.expand_dims(mx.nd.array(predicts1), axis=0)
            predicts2 = mx.nd.expand_dims(predicts[0], axis=0)
            predicts3 = [predicts2.as_in_context(predicts[0].context)]

            targets = mx.gluon.utils.split_and_load(dsts,
                                                    args.ctx,
                                                    even_split=False)

            print("i: ")
            print(i)
            #print(predicts2)
            #print(len(targets[0]))
            #print(targets)
            #bbb = np.array([[[1],[2],[3]],[[7],[6],[0]]])
            #bbb = np.array(predicts)
            targets2 = targets[0]
            print(predicts2.shape)
            #print(predicts3)
            print(targets2.shape)
            #print(targets2)
            metric.update(targets, predicts3)
            pixAcc, mIoU = metric.get()
            tbar.set_description('pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
        else:
            im_paths = dsts
            #print("im_paths: ")
            #print(im_paths)
            predicts = evaluator.parallel_forward(data)
            for predict, impath in zip(predicts, im_paths):
                print('i: ')
                print(i)
                #print(predict)
                #print(predict[0])
                #predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 1)).asnumpy() + \
                predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 0)).asnumpy() + \
                    testset.pred_offset
                #print('predict: ')
                #print(predict)
                mask = get_color_pallete(predict, args.dataset)
                #print('mask: ')
                #print(mask)
                #print("outname: ")
                #print(os.path.splitext(impath)[0])
                #outname = os.path.splitext(impath)[0] + '.png'
                #mask.save(os.path.join(outdir, outname))
                outname = 'predict_mask_' + str(i) + '.png'
                mask.save(os.path.join(outdir, outname))
Example #28
0
# we follow the standard data augmentation routine to transform the input image
# and the ground truth label map synchronously. (*Note that "nearest"
# mode upsample are applied to the label maps to avoid messing up the boundaries.*)
# We first randomly scale the input image from 0.5 to 2.0 times, then rotate
# the image from -10 to 10 degrees, and crop the image with padding if needed.
# Finally a random Gaussian blurring is applied.
#
# Random pick one example for visualization:
import random
from datetime import datetime
random.seed(datetime.now())
idx = random.randint(0, len(trainset))
img, mask = trainset[idx]
from gluoncv.utils.viz import get_color_pallete, DeNormalize
# get color pallete for visualize mask
mask = get_color_pallete(mask.asnumpy(), dataset='ade20k')
mask.save('mask.png')
# denormalize the image
img = DeNormalize([.485, .456, .406], [.229, .224, .225])(img)
img = np.transpose((img.asnumpy()*255).astype(np.uint8), (1, 2, 0))

##############################################################################
# Plot the image and mask
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
# subplot 1 for img
fig = plt.figure()
fig.add_subplot(1,2,1)

plt.imshow(img)
# subplot 2 for the mask
def test(args):
    # output folder
    outdir = 'train_logs/outdir'
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    # dataset and dataloader
    testset = get_custom_segm_dataset("test", args)
    test_data = gluon.data.DataLoader(
        testset,
        args.test_batch_size,
        shuffle=False,
        last_batch='keep',
        batchify_fn=ms_batchify_fn if args.tta else None,
        num_workers=args.workers)
    # create network
    if args.model_zoo is not None:
        model = get_pretrained_segmentation_model(args)
        if args.resume is not None:
            resume_checkpoint(model, args)
            print("loading checkpoint from %s for testing" % args.resume)
    else:
        model = get_segmentation_model(model=args.model,
                                       dataset=args.dataset,
                                       ctx=args.ctx,
                                       backbone=args.backbone,
                                       norm_layer=args.norm_layer,
                                       norm_kwargs=args.norm_kwargs,
                                       aux=args.aux,
                                       base_size=args.base_size,
                                       crop_size=args.crop_size)
        # load pretrained weight
        assert args.resume is not None, '=> Please provide the checkpoint using --resume'
        resume_checkpoint(model, args)
    # print(model)
    if args.tta:
        evaluator = MultiEvalModel(model,
                                   testset.num_class,
                                   ctx_list=args.ctx,
                                   scales=[0.75, 1.0, 1.25, 1.5, 1.75])
    else:
        evaluator = DataParallelModel(SegEvalModel(model), args.ctx)
    metric = gluoncv.utils.metrics.SegmentationMetric(testset.num_class)

    tbar = tqdm(test_data)
    for i, (data, dsts) in enumerate(tbar):
        if args.eval:
            if args.tta:
                predicts = [
                    pred[0].expand_dims(0)
                    for pred in evaluator.parallel_forward(data)
                ]
                targets = [target.as_in_context(predicts[0].context).expand_dims(0) \
                        for target in dsts]
            else:
                data = data.astype(args.dtype, copy=False)
                predicts = evaluator(data)
                predicts = [x[0] for x in predicts]
                if args.test_flip:
                    assert (data.ndim == 4)
                    fdata = data.flip(3)
                    fpredicts = evaluator(fdata)
                    predicts = [(x + y[0].flip(3)) / 2
                                for x, y in zip(predicts, fpredicts)]
                targets = mx.gluon.utils.split_and_load(dsts,
                                                        args.ctx,
                                                        even_split=False)
            metric.update(targets, predicts)
            pixAcc, mIoU = metric.get()
            tbar.set_description('pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
            mx.nd.waitall()
        else:
            im_paths = dsts
            predicts = evaluator.parallel_forward(data)
            for predict, impath in zip(predicts, im_paths):
                predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 1)).asnumpy() + \
                    testset.pred_offset
                mask = get_color_pallete(predict, args.dataset)
                outname = os.path.splitext(impath)[0] + '.png'
                mask.save(os.path.join(outdir, outname))
Example #30
0
import mxnet as mx
from mxnet import image
from mxnet.gluon.data.vision import transforms
import gluoncv
import cv2
# using cpu
ctx = mx.cpu(0)

filename= '71.jpeg'
# load the image
img = image.imread(filename)

from matplotlib import pyplot as plt
#plt.imshow(img.asnumpy())
#plt.show()
from gluoncv.data.transforms.presets.segmentation import test_transform
img = test_transform(img, ctx)
# get pre-trained model
model = gluoncv.model_zoo.get_model('fcn_resnet101_voc', pretrained=True)
output = model.predict(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()
from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg
mask = get_color_pallete(predict, 'pascal_voc')
mask.save('output.png')
Example #31
0
def test(args):
    if not horse_change:
        # output folder
        # outdir = 'outdir'
        outdir = args.outdir
        if not os.path.exists(outdir):
            os.makedirs(outdir)
        # image transform
        input_transform = transforms.Compose([
            transforms.ToTensor(),
            # transforms.Normalize([.485, .456, .406], [.229, .224, .225]),
            # transforms.Normalize([0, 0, 0], [1, 1, 1]),
            # transforms.Normalize([0], [100]), # this is for 1 channel: ([0], [1]) ([556.703], [482.175])
        ])
        # dataset and dataloader
        if args.eval:
            testset = get_segmentation_dataset(args.dataset, 
                                               split='val', 
                                               mode='testval', 
                                               transform=input_transform)
            total_inter, total_union, total_correct, total_label = \
                np.int64(0), np.int64(0), np.int64(0), np.int64(0)
        else:
            testset = get_segmentation_dataset(args.dataset, 
                                               split='test', 
                                               mode='test', 
                                               transform=input_transform)
        test_data = gluon.data.DataLoader(testset, 
                                          args.test_batch_size, 
                                          shuffle=False, 
                                          last_batch='keep',
                                          batchify_fn=ms_batchify_fn, 
                                          num_workers=args.workers)
        # create network
        if args.model_zoo is not None:
            model = get_model(args.model_zoo, pretrained=True)
        else:
            model = get_segmentation_model(model=args.model, 
                                           dataset=args.dataset, 
                                           ctx=args.ctx,
                                           backbone=args.backbone, 
                                           norm_layer=args.norm_layer,
                                           norm_kwargs=args.norm_kwargs, 
                                           aux=args.aux,
                                           base_size=args.base_size, 
                                           crop_size=args.crop_size)
            # load pretrained weight
            assert args.resume is not None, '=> Please provide the checkpoint using --resume'
            if os.path.isfile(args.resume):
                model.load_parameters(args.resume, ctx=args.ctx)
            else:
                raise RuntimeError("=> no checkpoint found at '{}'" \
                    .format(args.resume))
        # print(model) # [horse]: do not print model
        evaluator = MultiEvalModel(model, testset.num_class, ctx_list=args.ctx)
        metric = gluoncv.utils.metrics.SegmentationMetric(testset.num_class)

        print('testset.pred_offset:', testset.pred_offset) # horse
        print('model.crop_size', model.crop_size) # horse

        tbar = tqdm(test_data)
        for i, (data, dsts) in enumerate(tbar):
            if args.eval:
                # print('data', data[0].shape) # horse
                predicts = [pred[0] for pred in evaluator.parallel_forward(data)]
                # print('predicts', predicts[0].shape)
                targets = [target.as_in_context(predicts[0].context) \
                           for target in dsts]
                # horse begin 
                '''
                predict = mx.nd.squeeze(mx.nd.argmax(predicts[0], 0)).asnumpy() + \
                        testset.pred_offset
                '''
                # horse end
                print('targets', targets[0].shape)
                metric.update(targets, predicts)
                pixAcc, mIoU = metric.get()
                tbar.set_description( 'pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
            else:
                output_score_map = True # [horse added]
                if output_score_map:
                    # score_map_dir = 'scoredir'
                    score_map_dir = args.scoredir
                    if not os.path.exists(score_map_dir):
                        os.makedirs(score_map_dir)

                im_paths = dsts
                # print('data', data[0].shape) # horse
                predicts = evaluator.parallel_forward(data)
                # print(predicts[0].shape)
                for predict, impath in zip(predicts, im_paths):
                    # change from 1 to 0 [horse]
                    # print('predict:', predict[0].shape) # predict: (3, 127, 207)
                    if output_score_map:
                        score_map_name = os.path.splitext(impath)[0] + '.pkl'
                        score_map_path = os.path.join(score_map_dir, score_map_name)
                        with open(score_map_path, 'wb') as fo:
                            pickle.dump(predict[0].asnumpy()[0:3,:,:], fo)
                    '''
                    if i == 50:
                        with open('have_a_look.pkl', 'wb') as fo:
                            pickle.dump(predict[0].asnumpy(),fo)
                    '''
                    predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 0)).asnumpy() + \
                        testset.pred_offset
                    mask = get_color_pallete(predict, args.dataset)
                    outname = os.path.splitext(impath)[0] + '.png'
                    # print('predict:', predict.shape) # predict: (127, 207)
                    # print('mask:', mask) # it is a PIL.Image.Image
                    mask.save(os.path.join(outdir, outname))
                # break

    if horse_change: 
        # >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>>
        # output folder
        outdir = 'outdir'
        if not os.path.exists(outdir):
            os.makedirs(outdir)
        # image transform
        input_transform = transforms.Compose([
            transforms.ToTensor(),
            # transforms.Normalize([.485, .456, .406], [.229, .224, .225]),
            # transforms.Normalize([0, 0, 0], [1, 1, 1]),
            # transforms.Normalize([0], [100]), # this is for 1 channel: ([0], [1]) ([556.703], [482.175])
        ])
        # dataset and dataloader
        if args.eval:
            testset = get_segmentation_dataset(args.dataset, 
                                               split='val', 
                                               mode='testval', 
                                               transform=input_transform)
            total_inter, total_union, total_correct, total_label = \
                np.int64(0), np.int64(0), np.int64(0), np.int64(0)
        else:
            testset = get_segmentation_dataset(args.dataset, 
                                               split='test', 
                                               mode='test', 
                                               transform=input_transform)

        test_data = gluon.data.DataLoader(testset, 
                                          args.batch_size, # args.test_batch_size, [horse changed this]
                                          shuffle=False, 
                                          last_batch='keep',
                                          batchify_fn=ms_batchify_fn, 
                                          num_workers=args.workers)
        # create network
        if args.model_zoo is not None:
            model = get_model(args.model_zoo, pretrained=True)
        else:
            model = get_segmentation_model(model=args.model, 
                                           dataset=args.dataset, 
                                           ctx=args.ctx,
                                           backbone=args.backbone, 
                                           norm_layer=args.norm_layer,
                                           norm_kwargs=args.norm_kwargs, 
                                           aux=args.aux,
                                           base_size=args.base_size, 
                                           crop_size=args.crop_size)
            # load pretrained weight
            assert args.resume is not None, '=> Please provide the checkpoint using --resume'
            if os.path.isfile(args.resume):
                model.load_parameters(args.resume, ctx=args.ctx)
            else:
                raise RuntimeError("=> no checkpoint found at '{}'" \
                    .format(args.resume))
        # print(model) # [horse]: do not print model
        evaluator = MultiEvalModel(model, testset.num_class, ctx_list=args.ctx)
        metric = gluoncv.utils.metrics.SegmentationMetric(testset.num_class)

        print('testset.pred_offset:', testset.pred_offset) # horse
        print('model.crop_size', model.crop_size) # horse

        tbar = tqdm(test_data)
        for i, (data, dsts) in enumerate(tbar):
            if args.eval:
                # print('data', data[0].shape) # horse
                predicts = [pred[0] for pred in evaluator.parallel_forward(data)]
                # print('predicts', predicts[0].shape)
                targets = [target.as_in_context(predicts[0].context) \
                           for target in dsts]
                # horse begin 
                '''
                predict = mx.nd.squeeze(mx.nd.argmax(predicts[0], 0)).asnumpy() + \
                        testset.pred_offset
                '''
                # horse end
                print('targets', targets[0].shape)
                metric.update(targets, predicts)
                pixAcc, mIoU = metric.get()
                tbar.set_description( 'pixAcc: %.4f, mIoU: %.4f' % (pixAcc, mIoU))
            else:
                output_score_map = True # [horse added]
                if output_score_map:
                    score_map_dir = 'scoredir'

                im_paths = dsts
                print('data', data[0].shape) # horse
                predicts = evaluator.parallel_forward(data)
                print(predicts[0].shape)
                for predict, impath in zip(predicts, im_paths):

                    predict = mx.nd.squeeze(mx.nd.argmax(predict[0], 0)).asnumpy() + \
                        testset.pred_offset
                    mask = get_color_pallete(predict, args.dataset)
                    outname = os.path.splitext(impath)[0] + '.png'

                    mask.save(os.path.join(outdir, outname))
Example #32
0
# normalize the image using dataset mean
from gluoncv.data.transforms.presets.segmentation import test_transform

img = test_transform(img, ctx)

##############################################################################
# Load the pre-trained model and make prediction
# ----------------------------------------------
#
# get pre-trained model
model = gluoncv.model_zoo.get_model('deeplab_resnet101_ade', pretrained=True)

##############################################################################
# make prediction using single scale
output = model.predict(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()

##############################################################################
# Add color pallete for visualization
from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg

mask = get_color_pallete(predict, 'ade20k')
mask.save('output.png')

##############################################################################
# show the predicted mask
mmask = mpimg.imread('output.png')
plt.imshow(mmask)
plt.show()
Example #33
0
    num_workers=batch_size)


##############################################################################
# Plot an Example of generated images:
#

# Random pick one example for visualization:
import random
from datetime import datetime
random.seed(datetime.now())
idx = random.randint(0, len(trainset))
img, mask = trainset[idx]
from gluoncv.utils.viz import get_color_pallete, DeNormalize
# get color pallete for visualize mask
mask = get_color_pallete(mask.asnumpy(), dataset='coco')
mask.save('mask.png')
# denormalize the image
img = DeNormalize([.485, .456, .406], [.229, .224, .225])(img)
img = np.transpose((img.asnumpy()*255).astype(np.uint8), (1, 2, 0))

from matplotlib import pyplot as plt
import matplotlib.image as mpimg
# subplot 1 for img
fig = plt.figure()
fig.add_subplot(1,2,1)

plt.imshow(img)
# subplot 2 for the mask
mmask = mpimg.imread('mask.png')
fig.add_subplot(1,2,2)