示例#1
0
    def __getitem__(self, index):
        A_path = self.A_paths[index % self.A_size]
        B_path = self.B_paths[index % self.B_size]
        # print('(A, B) = (%d, %d)' % (index_A, index_B))
        A_img = image.imread(A_path)
        B_img = image.imread(B_path)

        A = self.transform(A_img)
        B = self.transform(B_img)
        return A, B
示例#2
0
def read_voc_images(root='../data/VOCdevkit/VOC2012', train=True):
    """Read VOC images."""
    txt_fname = '%s/ImageSets/Segmentation/%s' % (root, 'train.txt'
                                                  if train else 'val.txt')
    with open(txt_fname, 'r') as f:
        images = f.read().split()
    data, label = [None] * len(images), [None] * len(images)
    for i, fname in enumerate(images):
        data[i] = image.imread('%s/JPEGImages/%s.jpg' % (root, fname))
        label[i] = image.imread('%s/SegmentationClass/%s.png' % (root, fname))
    return data, label
示例#3
0
def read_images(root=voc_root, train=True):
    txt_fname = root + '/ImageSets/Segmentation/' + ('train.txt'
                                                     if train else 'val.txt')
    with open(txt_fname, 'r') as f:
        images = f.read().split()
    n = len(images)
    data, label = [None] * n, [None] * n
    for i, fname in enumerate(images):
        data[i] = image.imread('%s/JPEGImages/%s.jpg' % (root, fname))
        label[i] = image.imread('%s/SegmentationClass/%s.png' % (root, fname))
    return data, label
def read_voc_images(root='../data/VOCdevkit/VOC2012', train=True):
    """Read VOC images."""
    txt_fname = '%s/ImageSets/Segmentation/%s' % (
        root, 'train.txt' if train else 'val.txt')
    with open(txt_fname, 'r') as f:
        images = f.read().split()
    data, label = [None] * len(images), [None] * len(images)
    for i, fname in enumerate(images):
        data[i] = image.imread('%s/JPEGImages/%s.jpg' % (root, fname))
        label[i] = image.imread('%s/SegmentationClass/%s.png' % (root, fname))
    return data, label
示例#5
0
def read_images(
        img_path="/data/dataset/SiZe/hard_e_datasets/train_patch/",
        label_path="/data/dataset/SiZe/hard_e_datasets/train_label_patch/"):
    img_name_list, label_name_list = tools.generate_list(img_path, label_path)
    n = len(img_name_list)
    n1 = len(label_path)
    data, label = [None] * (n // 4), [None] * (n // 4)
    for i in range(n // 4):
        print(i)
        data[i] = image.imread(img_name_list[i])
        label[i] = image.imread(label_name_list[i])
    return data, label
示例#6
0
def read_voc_images(root, is_train):
    txt_fname = '%s/ImageSets/Segmentation/%s' % (root, 'train.txt'
                                                  if is_train else 'test.txt')
    with open(txt_fname, 'r') as f:
        # images = f.read().split() # 该凡是 丢失 文本行中的空白符号
        images = f.readlines()
        images = [ele.replace('\n', '') for ele in images]
    features, labels = [None] * len(images), [None] * len(images)
    for i, fname in enumerate(images):
        features[i] = image.imread('%s/JPEGImages/%s.jpg' % (root, fname))
        labels[i] = image.imread('%s/SegmentationClass/%s.png' % (root, fname))
    return features, labels
示例#7
0
def creat_h5_file(img_name_list, label_name_list):
    for times in range(101):
        print(times)
        if times == 0:
            h5f = h5py.File("train_data.h5", 'w')
            x = h5f.create_dataset("data", (1000, 3, 256, 256),
                                   maxshape=(None, 3, 256, 256),
                                   dtype=np.uint8)
            y = h5f.create_dataset("label", (1000, 256, 256),
                                   maxshape=(None, 256, 256),
                                   dtype=np.float32)
        h5f = h5py.File("train_data.h5", "a")  #add mode
        x = h5f["data"]
        y = h5f["label"]
        if times != 100:
            x.resize([times * 1000 + 1000, 3, 256, 256])
            y.resize([times * 1000 + 1000, 256, 256])

            index = times * 1000
            data_zeros = np.zeros(shape=(1000, 3, 256, 256))
            label_zeros = np.zeros(shape=(1000, 256, 256))
            for k, i in enumerate(range(index, index + 1000)):
                img_t = image.imread(img_name_list[i])
                img_t = img_t.asnumpy().transpose((2, 0, 1))
                data_zeros[k, :, :, :] = img_t

                label_t = image.imread(label_name_list[i])
                label_t = image2label(label_t)
                label_zeros[k, :, :] = label_t.asnumpy()

            x[times * 1000:times * 1000 + 1000, :, :, :] = data_zeros
            y[times * 1000:times * 1000 + 1000, :, :] = label_zeros

        else:
            print('the last one')
            x.resize([times * 1000 + 566, 3, 256, 256])
            y.resize([times * 1000 + 566, 256, 256])
            index = times * 1000

            data_zeros = np.zeros(shape=(566, 3, 256, 256))
            label_zeros = np.zeros(shape=(566, 256, 256))
            for k, i in enumerate(range(index, index + 566)):
                img_t = image.imread(img_name_list[i])
                img_t = img_t.asnumpy().transpose((2, 0, 1))
                data_zeros[k, :, :, :] = img_t

                label_t = image.imread(label_name_list[i])
                label_t = image2label(label_t)
                label_zeros[k, :, :] = label_t.asnumpy()
            x[times * 1000:times * 1000 + 566, :, :, :] = data_zeros
            y[times * 1000:times * 1000 + 566, :, :] = label_zeros
    h5f.close()
    print("the end")
示例#8
0
def read_voc_images(root, is_train=True, num=17125):
    txt_fname = '%s/ImageSets/Segmentation/%s' % (root, 'train.txt'
                                                  if is_train else 'val.txt')
    with open(txt_fname, 'r') as f:
        images = f.read().split()
    features, labels = [None] * len(images), [None] * len(images)
    for i, fname in enumerate(images):
        if i >= num:
            break
        features[i] = image.imread('%s/JPEGImages/%s.jpg' % (root, fname))
        labels[i] = image.imread('%s/SegmentationClass/%s.png' % (root, fname))
    return features, labels
示例#9
0
def process(content_path, style_path, output_shape, style_save_path):
    content_img, style_img = image.imread(content_path), image.imread(style_path)
    content_X, contents_Y = get_contents(content_img, output_shape, ctx)
    if style_save_path:
        styles_npy = os.path.join(style_save_path, "styles.npy")
        if os.path.exists(styles_npy):
            styles_Y = nd.load(styles_npy)
        else:
            _, styles_Y = get_styles(style_img, output_shape, ctx)
            nd.save(styles_npy, styles_Y)
    else:
        _, styles_Y = get_styles(style_img, output_shape, ctx)
    return train(content_X, contents_Y, styles_Y, ctx, lr, max_epochs, lr_decay_epoch)
    def read_images(self, root):

        dataroot = root + 'left_frames/'  # left_frames   #data
        labelroot = root + 'labels/'  # labels   #label

        DataNamelist = sorted(self.DataNameList)

        data, label = [None] * len(self.DataNameList), [None] * len(
            self.DataNameList)

        for i, name in enumerate(DataNamelist):
            data[i] = image.imread(dataroot + name)
            label[i] = image.imread(labelroot + name)

        return data, label
示例#11
0
def read_person_data(root='data', is_train=True):
    txt_fname = '%s/%s' % (root, 'trainval.txt' if is_train else 'test.txt')
    with open(txt_fname, 'r') as f:
        images = f.read().split()

    # images = images[:10]

    features, labels = [None] * len(images), [None] * len(images)

    for i, fname in enumerate(images):
        # print(fname)  # 输出读取的所有图片名
        features[i] = image.imread('%s/image/%s.jpg' % (root, fname))
        labels[i] = image.imread('%s/label/%s.png' % (root, fname))

    return features, labels
示例#12
0
def hard_sum(img_path1,img_path2):
    whole_data=[]
    img_name_list1,label_name_list1=tool.generate_list(img_path1,img_path1)
    img_name_list2,label_name_list2=tool.generate_list(img_path2,img_path2)
    
    number=len(img_name_list1)
    name_list=[]
    sum_hard=[]
    sum_mic=[]
    sum_heam=[]
    
    for i in range(number):
        img=image.imread(img_name_list1[i])
        img1=image.imread(img_name_list2[i])

        x,y,z=img.shape
        imgr=tool.imresize(img1)  
        stamp,prob_map=tool.optic_disc_seg(imgr,net_vessel,crop_size=256,stride=128,ctx=ctx1,threshold=0.5)
        stamp=cv2.resize(stamp, (y, x), interpolation=cv2.INTER_NEAREST)
        prob_map=cv2.resize(prob_map.asnumpy(), (y, x), interpolation=cv2.INTER_NEAREST)
        
        hardpr=prob_map_get(img,net_hard,ctx=ctx2,crop_size=256,stride=128,threshold=0.5)
        heampr=prob_map_get(img,net_heam,ctx=ctx4,crop_size=256,stride=128,threshold=0.5)
        micpr=prob_map_get(img,net_mic,ctx=ctx3,crop_size=256,stride=128,threshold=0.5)

        hard_mask=(hardpr.asnumpy()*(1-stamp)*(1-prob_map))>0.5
        mic_mask=((1-stamp)*(1-heampr.asnumpy())*micpr.asnumpy())>0.5
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10))
        mic_mask = cv2.morphologyEx((mic_mask*np.array([1])).astype('uint8'), cv2.MORPH_OPEN, kernel)
        heam_mask=((1-stamp)*(1-prob_map)*heampr.asnumpy())>0.5
        
        hardpr1=hardpr.asnumpy()*hard_mask
        micpr1=micpr.asnumpy()*mic_mask
        heampr1=heampr.asnumpy()*heam_mask
        
        img_name=img_name_list1[i].split('/')[-1]
        print(img_name)
        name_list.append(img_name)
        sum_hard.append(int(np.sum(hardpr1)))    
        sum_mic.append(int(np.sum(micpr1)))
        sum_heam.append(int(np.sum(heampr1)))
        whole_data.append([img_name,int(np.sum(hardpr1)),int(np.sum(micpr1)),int(np.sum(heampr1))])
        
    return whole_data
        
        
        
        
示例#13
0
def targetClassify(model_name,input_pic,target_class):
    # The purpose of this is to simply output the percent probability that
    # the image is specified target_class
    # Load specified Model
    # Assume pretrained
    net = get_model(model_name, pretrained=True)
    
    classes = net.classes
    
    classInd = -1;
    # Find index of target class
    for i,j in enumerate(classes):
        if target_class == j.lower():
            classInd = i
            break
        
    # Exit if target class not found
    if classInd == -1:
        print("ERROR: Target class not found in this model : %s" % target_class)
        return            
    
    # Load Images, assume all data is in "images/" directory
    img = image.imread("images/" + input_pic)
    
    # Transform and predict
    img = transform_eval(img)
    pred = net(img)
    # use softmax and print probability
    #prob = nd.softmax(pred)
    print("Probability of class [%s] for [%s]: %.3f" % (classes[classInd],input_pic,nd.softmax(pred)[0][classInd].asscalar()))   
    
示例#14
0
 def __getitem__(self, idx):
     fpath = os.path.join(self._root, self.items[idx][0])
     img = image.imread(fpath, self._flag)
     label = self.items[idx][1]
     if self._transform is not None:
         img = self._transform(img)
     return img, label
示例#15
0
def mxnet_cifar10(im):

    img = image.imread(im)

    # plt.imshow(img.asnumpy())
    # plt.show()

    # transform image
    transform_fn = transforms.Compose([
        transforms.Resize(32),
        transforms.CenterCrop(32),
        transforms.ToTensor(),
        transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
    ])

    img = transform_fn(img)
    # plt.imshow(nd.transpose(img, (1,2,0)).asnumpy())
    # plt.show()

    # load pre-trained model
    net = get_model('cifar_resnet110_v1', classes=10, pretrained=True)

    # predict class
    pred = net(img.expand_dims(axis=0))

    class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
                'dog', 'frog', 'horse', 'ship', 'truck']
    ind = nd.argmax(pred, axis=1).astype('int')
    return [str(class_names[ind.asscalar()]), str(round(nd.softmax(pred)[0][ind].asscalar(), 2))]    
示例#16
0
def predict_with_models_from_gluon_model_zoo_example():
    # Gluon model zoo provides multiple pre-trained powerful models.
    #	We can download and load a pre-trained ResNet-50 V2 model that was trained on the ImageNet dataset.
    net = models.resnet50_v2(pretrained=True)

    # Download and load the text labels for each class.
    url = 'http://data.mxnet.io/models/imagenet/synset.txt'
    fname = download(url)
    with open(fname, 'r') as f:
        text_labels = [' '.join(l.split()[1:]) for l in f]

    # Randomly pick a dog image from Wikipedia as a test image, download and read it.
    url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Golden_Retriever_medium-to-light-coat.jpg/365px-Golden_Retriever_medium-to-light-coat.jpg'
    fname = download(url)
    x = image.imread(fname)

    # Use the image processing functions provided in the MXNet image module.
    x = image.resize_short(x, 256)
    x, _ = image.center_crop(x, (224, 224))
    plt.imshow(x.asnumpy())
    plt.show()

    def transform(data):
        data = data.transpose((2, 0, 1)).expand_dims(axis=0)
        rgb_mean = nd.array([0.485, 0.456, 0.406]).reshape((1, 3, 1, 1))
        rgb_std = nd.array([0.229, 0.224, 0.225]).reshape((1, 3, 1, 1))
        return (data.astype('float32') / 255 - rgb_mean) / rgb_std

    prob = net(transform(x)).softmax()
    idx = prob.topk(k=5)[0]
    for i in idx:
        i = int(i.asscalar())
        print('With prob = %.5f, it contains %s' %
              (prob[0, i].asscalar(), text_labels[i]))
示例#17
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')
示例#18
0
 def __getitem__(self, idx):
     img = image.imread(self.items[idx][0], self._flag)
     label_df = pd.read_csv(self.items[idx][1], index_col=[0])
     label = label_df.values.reshape((1, 22)).tolist()[0]
     if self._transform is not None:
         return self._transform(img, label)
     return img, label
def main():
    net = models.resnet50_v2(pretrained=True)
    url = 'http://data.mxnet.io/models/imagenet/synset.txt'
    fname = download(url)
    with open(fname, 'r') as f:
        text_labels = [' '.join(l.split()[1:]) for l in f]

    url2 = 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/\
        Golden_Retriever_medium-to-light-coat.jpg/\
        365px-Golden_Retriever_medium-to-light-coat.jpg'

    fname2 = download(url2)
    x = image.imread(fname2)

    x = image.resize_short(x, 256)
    x, _ = image.center_crop(x, (224, 224))
    plt.imshow(x.asnumpy())
    plt.show()

    prob = net(transform(x)).softmax()
    idx = prob.topk(k=5)[0]
    for i in idx:
        i = int(i.asscalar())
        print('With prob = %.5f, it contains %s' %
              (prob[0, i].asscalar(), text_labels[i]))
示例#20
0
def main():
    st.title("Semantic Segmentation App for Images")
    st.text("Built with gluoncv and Streamlit")
    st.markdown(
        """### [Semantic Segmentation](https://towardsdatascience.com/semantic-segmentation-with-deep-learning-a-guide-and-code-e52fc8958823)\
     `            `[PSPNet](https://towardsdatascience.com/review-pspnet-winner-in-ilsvrc-2016-semantic-segmentation-scene-parsing-e089e5df177d) \
	 `			  `[[Paper]](https://arxiv.org/abs/1612.01105)\
	 `			  `[![Open Source Love svg1](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/Hardly-Human/Semantic-Segmentation-of-Images)\
	 `            `[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://lbesson.mit-license.org/)"""
    )

    image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg'])

    if image_file is None:
        st.warning(
            "Upload Image and Run Model  (Use Image size <300 KB for faster inference)"
        )

    if image_file is not None:
        image1 = Image.open(image_file)
        rgb_im = image1.convert('RGB')
        image2 = rgb_im.save("saved_image.jpg")
        image_path = "saved_image.jpg"
        st.image(image1, width=500, height=500)

    if st.button("Run Model"):
        st.warning("Loading Model..🤞")
        model = load_model('psp_resnet101_ade')
        img = image.imread(image_path)
        img = test_transform(img, ctx)
        st.success("Loaded Model Succesfully!!🤩👍")

        plot_image(model, img)
示例#21
0
    def test_model_for_ml(self):
        net_path = os.path.join(
            DATA_DIR, 'model',
            'epoch-3-0.48-20180920164709.params-symbol.json')
        params_path = os.path.join(
            DATA_DIR, 'model',
            'epoch-3-0.48-20180920164709.params-0003.params')

        net = gluon.nn.SymbolBlock.imports(net_path, ['data'], params_path)

        im_path = os.path.join(DATA_DIR, 'imgs_data',
                               'd4YE10xHdvbwKJV5yBYsoJJke6K9b.jpg')
        img = image.imread(im_path)

        # plt.imshow(img.asnumpy())
        # plt.show()

        transform_fn = transforms.Compose([
            transforms.Resize(224, keep_ratio=True),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
        ])

        img = transform_fn(img)

        img = nd.expand_dims(img, axis=0)
        res = net(img.as_in_context(self.ctx[0]))
        res = sigmoid(nd.squeeze(res)).asnumpy()
        res = np.where(res > 0.5, 1, 0)
        indexes, = np.where(res == 1)
        res_classes = [self.class_names[i] for i in indexes.tolist()]
        # print(indexes.tolist())
        print('测试类别: {}'.format(res_classes))
示例#22
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))
示例#23
0
    def __filter(self, idx, pass_filter=True):
        while True:
            x = image.imread(self.pre_x_path + '//' + self.image_list[idx] +
                             '.jpg')
            y = image.imread(self.pre_y_path + '//' + self.image_list[idx] +
                             '.png')
            print(x.shape)
            #x = nd.array(x)
            #y = nd.array(y)
            if (x.shape[0] >= self.crop_size[0]
                    and x.shape[1] >= self.crop_size[1]) or pass_filter:
                if x.shape == y.shape:
                    break
            else:
                idx = random.randint(0, len(self.image_list))

        return self.__normalize_image(x), y.astype('float32')
示例#24
0
    def __getitem__(self, index):
        image_info = self.paths[index]

        img = image.imread(image_info['path'])
        if self.transform is not None:
            img = self.transform(img)

        return img, index  # image_info['id'], image_info['frame']
示例#25
0
 def read_img(self, img_path):
     img_path = self.path % img_path
     img = image.imread(img_path)
     self.img_size = img.shape
     img = image.imresize(img, img_width, img_height)
     if self.transform is not None:
         img = self.transform(img)
     return img
示例#26
0
def open_img():
    url = 'd://VOCdevkit//VOC2012//SegmentationClass//2007_006076.png'
    img = image.imread(url)
    plt.ioff()
    plt.imshow(img.asnumpy())
    plt.colorbar()
    plt.show()
    #plt.close()
    return img
示例#27
0
 def __getitem__(self, idx):
     img = image.imread(self.items[idx][0], self._flag)
     # resize成2次幂的整数倍方便处理
     img = image.imresize(img, w=128, h=64)
     label = self.items[idx][1]
     label = str2vec(label)
     if self._transform is not None:
         return self._transform(img, label)
     return img, label
示例#28
0
    def __getitem__(self, index):
        path = self.paths[index]
        img = image.imread(path)

        hr_img = self.crop_transform(img)
        lr_img = self.downsample_transform(hr_img)
        hr_img = self.last_transform(hr_img)
        lr_img = self.last_transform(lr_img)
        return hr_img, lr_img
示例#29
0
 def __getitem__(self, idx):
     if (os.path.splitext(self.items[idx][0])[1]).lower() == '.ndarray':
         data = nd.load(self.items[idx][0])[0]
     else:
         data = image.imread(self.items[idx][0], self._flag)
     label = self.items[idx][1]
     if self._transform is not None:
         return self._transform(data, label)
     return data, label
示例#30
0
def transform_image(img_path):
    img = image.imread(img_path)
    data = image.resize_short(img, 256)
    data, _ = image.center_crop(data, (224, 224))
    data = data.transpose((2, 0, 1)).expand_dims(axis=0)
    rgb_mean = nd.array([0.485, 0.456, 0.406]).reshape((1, 3, 1, 1))
    rgb_std = nd.array([0.229, 0.224, 0.225]).reshape((1, 3, 1, 1))
    data = (data.astype("float32") / 255 - rgb_mean) / rgb_std
    return data
示例#31
0
文件: IN_data.py 项目: daniaokuye/Dy
    def read_img(self, img_path):
        img_path = self.path + img_path
        img = image.imread(img_path)
        self.img_size = img.shape
        img = image.imresize(img, img_width, img_height)

        for trans in self.transform:
            img = trans(img)
        return img
示例#32
0
def read_images(root=None, is_train=True):
    if is_train:
        train_dir = 'train'
        label_dir = 'train_mask_jpg'
    else:
        train_dir = 'test'
        label_dir = 'test_mask_jpg'
    train_path = os.path.join(root, train_dir)
    label_path = os.path.join(root, label_dir)
    txt_fname = os.path.join(root, 'train.txt' if is_train else 'test.txt')
    with open(txt_fname, 'r') as f:
        images = f.read().split()
    features, labels = [None] * len(images), [None] * len(images)
    for i, fname in enumerate(images):
        features[i] = image.imread(os.path.join(train_path, '%s.jpg' % fname))
        labels[i] = image.imread(
            os.path.join(label_path, '%s_mask.jpg' % fname))
    return features, labels
示例#33
0
import matplotlib.pyplot as plt

from mxnet import gluon, nd, image
from mxnet.gluon.data.vision import transforms
from gluoncv import utils
from gluoncv.model_zoo import get_model

################################################################
#
# Then, we download and show the example image:

url = 'https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/classification/plane-draw.jpeg'
im_fname = utils.download(url)

img = image.imread(im_fname)

plt.imshow(img.asnumpy())
plt.show()

################################################################
# In case you don't recognize it, the image is a poorly-drawn airplane :)
#
# Now we define transformations for the image.

transform_fn = transforms.Compose([
    transforms.Resize(32),
    transforms.CenterCrop(32),
    transforms.ToTensor(),
    transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
])
示例#34
0
ctx = mx.cpu(0)


##############################################################################
# Prepare the image
# -----------------
#
# download the example image
url = 'https://github.com/zhanghang1989/image-data/blob/master/encoding/' + \
    'segmentation/ade20k/ADE_val_00001755.jpg?raw=true'
filename = 'ade20k_example.jpg'
gluoncv.utils.download(url, filename, True)

##############################################################################
# load the image
img = image.imread(filename)

from matplotlib import pyplot as plt
plt.imshow(img.asnumpy())
plt.show()

##############################################################################
# normalize the image using dataset mean
transform_fn = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([.485, .456, .406], [.229, .224, .225])
])
img = transform_fn(img)
img = img.expand_dims(0).as_in_context(ctx)

##############################################################################
示例#35
0
                        help='Load weights from previously saved parameters.')
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    opt = parse_args()
    # context list
    if opt.gpu_id == '-1':
        ctx = mx.cpu()
    else:
        ctx = mx.gpu(int(opt.gpu_id.strip()))

    netG = SRGenerator()
    netG.load_parameters(opt.pretrained)
    netG.collect_params().reset_ctx(ctx)
    image_list = [x.strip() for x in opt.images.split(',') if x.strip()]
    transform_fn = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
    ])

    ax = None
    for image_path in image_list:
        img = image.imread(image_path)
        img = transform_fn(img)
        img = img.expand_dims(0).as_in_context(ctx)
        output = netG(img)
        predict = mx.nd.squeeze(output)
        predict = ((predict.transpose([1,2,0]).asnumpy() * 0.5 + 0.5) * 255).astype('uint8')
        plt.imshow(predict)
        plt.show()
示例#36
0
文件: demo.py 项目: z01nl1o02/tests
import matplotlib as mpl
mpl.rcParams['figure.dpi']= 150
from matplotlib import pyplot as plt

from mxnet import image
import pdb
import cv2
import numpy as np

style_img = image.imread('img/style.jpg')
content_img = image.imread('img/content.jpg')

'''
plt.figure()
plt.imshow(style_img.asnumpy())
plt.figure()
plt.imshow(content_img.asnumpy())
plt.show()
'''
from mxnet import nd

rgb_mean = nd.array([0.485, 0.456, 0.406])
rgb_std = nd.array([0.229, 0.224, 0.225])


def preprocess(img, image_shape):
    img = image.imresize(img, *image_shape)
    img = (img.astype('float32')/255 - rgb_mean) / rgb_std
    return img.transpose((2,0,1)).expand_dims(axis=0)
示例#37
0
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']

context = [mx.cpu()]

# Load Model
model_name = opt.model
pretrained = True if opt.saved_params == '' else False
kwargs = {'classes': classes, 'pretrained': pretrained}
net = get_model(model_name, **kwargs)

if not pretrained:
    net.load_parameters(opt.saved_params, ctx = context)

# Load Images
img = image.imread(opt.input_pic)

# Transform
transform_fn = transforms.Compose([
    transforms.Resize(32),
    transforms.CenterCrop(32),
    transforms.ToTensor(),
    transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
])

img = transform_fn(img)
pred = net(img.expand_dims(0))

ind = nd.argmax(pred, axis=1).astype('int')
print('The input picture is classified to be [%s], with probability %.3f.'%
      (class_names[ind.asscalar()], nd.softmax(pred)[0][ind].asscalar()))