Python torchvision.transforms.ToPILImage() Examples

The following are 30 code examples of torchvision.transforms.ToPILImage(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torchvision.transforms , or try the search function .
Example #1
Source File: mask_generators.py    From vaeac with MIT License 7 votes vote down vote up
def regenerate_cache(self):
        """
        Resamples the big matrix and resets the counter of the total
        number of elements in the returned masks.
        """
        low_size = int(self.resolution * self.max_size)
        low_pattern = self.rng.uniform(0, 1, size=(low_size, low_size)) * 255
        low_pattern = torch.from_numpy(low_pattern.astype('float32'))
        pattern = transforms.Compose([
                        transforms.ToPILImage(),
                        transforms.Resize(self.max_size, Image.BICUBIC),
                        transforms.ToTensor(),
        ])(low_pattern[None])[0]
        pattern = torch.lt(pattern, self.density).byte()
        self.pattern = pattern.byte()
        self.points_used = 0 
Example #2
Source File: datasets.py    From multiple-objects-gan with MIT License 6 votes vote down vote up
def get_imgs(img_path, imsize, bbox=None,
             transform=None, normalize=None):
    img = Image.open(img_path).convert('RGB')
    if transform is not None:
        img = transform(img)

    img, bbox_scaled = crop_imgs(img, bbox)

    ret = []
    if cfg.GAN.B_DCGAN:
        ret = [normalize(img)]
    else:
        for i in range(cfg.TREE.BRANCH_NUM):
            # print(imsize[i])
            if i < (cfg.TREE.BRANCH_NUM - 1):
                re_img = transforms.ToPILImage()(img)
                re_img = transforms.Resize((imsize[i], imsize[i]))(re_img)
            else:
                re_img = transforms.ToPILImage()(img)
            ret.append(normalize(re_img))

    return ret, bbox_scaled 
Example #3
Source File: datasets.py    From semantic-object-accuracy-for-generative-text-to-image-synthesis with MIT License 6 votes vote down vote up
def get_imgs(img_path, imsize, max_objects, bbox=None, transform=None, normalize=None):
    img = Image.open(img_path).convert('RGB')
    if transform is not None:
        img = transform(img)

    img, bbox_scaled = crop_imgs(img, bbox, max_objects=max_objects)

    ret = []
    if cfg.GAN.B_DCGAN:
        ret = [normalize(img)]
    else:
        for i in range(cfg.TREE.BRANCH_NUM):
            # print(imsize[i])
            if i < (cfg.TREE.BRANCH_NUM - 1):
                re_img = transforms.ToPILImage()(img)
                re_img = transforms.Resize((imsize[i], imsize[i]))(re_img)
            else:
                re_img = transforms.ToPILImage()(img)
            ret.append(normalize(re_img))

    return ret, bbox_scaled 
Example #4
Source File: train.py    From pytorch-zssr with Apache License 2.0 6 votes vote down vote up
def test(model, img, sr_factor):
    model.eval()

    img = img.resize((int(img.size[0]*sr_factor), \
        int(img.size[1]*sr_factor)), resample=PIL.Image.BICUBIC)
    img.save('low_res.png')

    img = transforms.ToTensor()(img)
    img = torch.unsqueeze(img, 0)
    input = Variable(img.cuda())
    residual = model(input)
    output = input + residual

    output = output.cpu().data[0, :, :, :]
    o = output.numpy()
    o[np.where(o < 0)] = 0.0
    o[np.where(o > 1)] = 1.0
    output = torch.from_numpy(o)
    output = transforms.ToPILImage()(output) 
    output.save('zssr.png') 
Example #5
Source File: super.py    From jdit with Apache License 2.0 6 votes vote down vote up
def image(self, img_tensors: torch.Tensor, global_step: int, tag: str = "Train/input",
              grid_size: Union[list, tuple] = (3, 1), shuffle=True, save_file=False):

        if len(img_tensors.size()) != 4:
            raise TypeError("img_tensors rank should be 4, got %d instead" % len(img_tensors.size()))
        self._build_dir(os.path.join(self.logdir, "plots", tag))
        rows, columns = grid_size[0], grid_size[1]
        batch_size = len(img_tensors)  # img_tensors =>(batchsize, 3, 256, 256)
        num_samples: int = min(batch_size, rows * columns)
        sampled_tensor = self._sample(img_tensors, num_samples, shuffle).detach().cpu()
        # (sample_num, 3, 32,32)  tensors
        # sampled_images = map(transforms.Normalize(mean, std), sampled_tensor)  # (sample_num, 3, 32,32) images
        sampled_images: torch.Tensor = make_grid(sampled_tensor, nrow=rows, normalize=True, scale_each=True)
        self.writer.add_image(tag, sampled_images, global_step)

        if save_file:
            img = transforms.ToPILImage()(sampled_images)
            filename = "%s/plots/%s/E%03d.png" % (self.logdir, tag, global_step)
            img.save(filename) 
Example #6
Source File: dataset.py    From PerceptualGAN with GNU General Public License v3.0 6 votes vote down vote up
def __getitem__(self, index):

        outputs = []

        for i, d in enumerate(self.datasets):

            outputs += d.get_item(self.indices[i][index], self.flips[i][index])

        self.counter += 1

        # Shuffle datasets after each epoch
        if self.counter == len(self):
            if self.phase == 'train': self.shuffle()
            self.counter = 0

        if len(outputs) == 1 and self.aligned:

            # Super resolution
            outputs[0] = ToPILImage()((outputs[0] + 1) / 2)
            outputs.insert(0, self.down(outputs[0]))
            for i, o in enumerate(outputs):
                outputs[i] = ToTensor()(o) * 2 - 1

        return outputs 
Example #7
Source File: dataset.py    From kaggle_carvana_segmentation with MIT License 6 votes vote down vote up
def im_show(img_list):
    """
    It receives a list of images and plots them together
    :param img_list:
    :return:
    """
    to_PIL = transforms.ToPILImage()
    if len(img_list) >= 10:
        raise Exception("len(img_list) must be smaller than 10")

    for idx, img in enumerate(img_list):
        img = np.array(to_PIL(img))
        plt.subplot(100 + 10 * len(img_list) + (idx + 1))
        fig = plt.imshow(img)
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)

    plt.show() 
Example #8
Source File: file_dataset_test.py    From baal with Apache License 2.0 6 votes vote down vote up
def test_segmentation_pipeline(self):
        class DrawSquare:
            def __init__(self, side):
                self.side = side

            def __call__(self, x, **kwargs):
                x, canvas = x  # x is a [int, ndarray]
                canvas[:self.side, :self.side] = x
                return canvas

        target_trans = BaaLCompose(
            [GetCanvas(), DrawSquare(3), ToPILImage(mode=None), Resize(60, interpolation=0),
             RandomRotation(10, resample=NEAREST, fill=0.0), PILToLongTensor()])
        file_dataset = FileDataset(self.paths, [1] * len(self.paths), self.transform, target_trans)

        x, y = file_dataset[0]
        assert np.allclose(np.unique(y), [0, 1])
        assert y.shape[1:] == x.shape[1:] 
Example #9
Source File: trainer.py    From tiny-faces-pytorch with MIT License 6 votes vote down vote up
def visualize_output(img, output, templates, proc, prob_thresh=0.55, nms_thresh=0.1):
    tensor_to_image = transforms.ToPILImage()

    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    for t, m, s in zip(img[0], mean, std):
        t.mul_(s).add_(m)

    image = tensor_to_image(img[0])  # Index into the batch

    cls_map = nnfunc.sigmoid(output[:, 0:templates.shape[0], :, :]).data.cpu(
    ).numpy().transpose((0, 2, 3, 1))[0, :, :, :]
    reg_map = output[:, templates.shape[0]:, :, :].data.cpu(
    ).numpy().transpose((0, 2, 3, 1))[0, :, :, :]

    print(np.sort(np.unique(cls_map))[::-1])
    proc.visualize_heatmaps(image, cls_map, reg_map, templates,
                            prob_thresh=prob_thresh, nms_thresh=nms_thresh)

    p = input("Continue? [Yn]")
    if p.lower().strip() == 'n':
        exit(0) 
Example #10
Source File: cifar10.py    From SE_DenseNet with MIT License 6 votes vote down vote up
def get_dataloader(batch_size, root="data/cifar10"):
    root = Path(root).expanduser()
    if not root.exists():
        root.mkdir()
    root = str(root)

    to_normalized_tensor = [transforms.ToTensor(),
                            transforms.ToPILImage(),
                            transforms.Resize((224, 224)),
                            transforms.ToTensor(),
                            transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))]
    data_augmentation = [transforms.RandomHorizontalFlip(),]

    train_loader = DataLoader(
        datasets.CIFAR10(root, train=True, download=True,
                         transform=transforms.Compose(data_augmentation + to_normalized_tensor)),
        batch_size=batch_size, shuffle=True)
    test_loader = DataLoader(
        datasets.CIFAR10(root, train=False, transform=transforms.Compose(to_normalized_tensor)),
        batch_size=batch_size, shuffle=True)
    return train_loader, test_loader 
Example #11
Source File: trainer.py    From pggan-pytorch with MIT License 6 votes vote down vote up
def feed_interpolated_input(self, x):
        if self.phase == 'gtrns' and floor(self.resl)>2 and floor(self.resl)<=self.max_resl:
            alpha = self.complete['gen']/100.0
            transform = transforms.Compose( [   transforms.ToPILImage(),
                                                transforms.Scale(size=int(pow(2,floor(self.resl)-1)), interpolation=0),      # 0: nearest
                                                transforms.Scale(size=int(pow(2,floor(self.resl))), interpolation=0),      # 0: nearest
                                                transforms.ToTensor(),
                                            ] )
            x_low = x.clone().add(1).mul(0.5)
            for i in range(x_low.size(0)):
                x_low[i] = transform(x_low[i]).mul(2).add(-1)
            x = torch.add(x.mul(alpha), x_low.mul(1-alpha)) # interpolated_x

        if self.use_cuda:
            return x.cuda()
        else:
            return x 
Example #12
Source File: LiveCamera.py    From simple-HRNet with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, camera_id=0, epoch_length=1, resolution=(384, 288), interpolation=cv2.INTER_CUBIC,
                 multiperson=False, device=torch.device('cpu')):
        super(LiveCameraDataset, self).__init__()
        self.camera_id = camera_id
        self.epoch_length = epoch_length
        self.resolution = resolution
        self.interpolation = interpolation
        self.multiperson = multiperson
        self.device = device

        self.camera = cv2.VideoCapture(self.camera_id)
        assert self.camera.isOpened()

        if not self.multiperson:
            self.transform = transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
            ])

        else:
            self.detector = YOLOv3(model_def="./models/detectors/yolo/config/yolov3.cfg",
                                   class_path="./models/detectors/yolo/data/coco.names",
                                   weights_path="./models/detectors/yolo/weights/yolov3.weights",
                                   classes=('person',), device=device)

            self.transform = transforms.Compose([
                transforms.ToPILImage(),
                transforms.Resize((self.resolution[1], self.resolution[0])),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
            ]) 
Example #13
Source File: predict.py    From Pytorch-UNet with GNU General Public License v3.0 5 votes vote down vote up
def predict_img(net,
                full_img,
                device,
                scale_factor=1,
                out_threshold=0.5):
    net.eval()

    img = torch.from_numpy(BasicDataset.preprocess(full_img, scale_factor))

    img = img.unsqueeze(0)
    img = img.to(device=device, dtype=torch.float32)

    with torch.no_grad():
        output = net(img)

        if net.n_classes > 1:
            probs = F.softmax(output, dim=1)
        else:
            probs = torch.sigmoid(output)

        probs = probs.squeeze(0)

        tf = transforms.Compose(
            [
                transforms.ToPILImage(),
                transforms.Resize(full_img.size[1]),
                transforms.ToTensor()
            ]
        )

        probs = tf(probs.cpu())
        full_mask = probs.squeeze().cpu().numpy()

    return full_mask > out_threshold 
Example #14
Source File: trainer.py    From PAST-ReID with MIT License 5 votes vote down vote up
def __init__(self, args, cls_params=None, tblogger=None):
        super(ReIDTrainer, self).__init__()

        self.args = args
        self.cls_params = cls_params
        self.tblogger = tblogger
        self.init_log()
        self.init_device()
        if not self.args.evaluate:
            self.init_trainer()
        self.init_eval()
        self.to_pil_image = transforms.ToPILImage() 
Example #15
Source File: DD.py    From DEEPSEC with MIT License 5 votes vote down vote up
def __getitem__(self, index):
        single_image, single_label = self.images[index], self.labels[index]
        if self.transform:
            img = ToPILImage(mode=self.color_mode)(single_image)
            single_image = self.transform(img)
        return single_image, single_label 
Example #16
Source File: predictor.py    From FreeAnchor with MIT License 5 votes vote down vote up
def build_transform(self):
        """
        Creates a basic transformation that was used to train the models
        """
        cfg = self.cfg

        # we are loading images with OpenCV, so we don't need to convert them
        # to BGR, they are already! So all we need to do is to normalize
        # by 255 if we want to convert to BGR255 format, or flip the channels
        # if we want it to be in RGB in [0-1] range.
        if cfg.INPUT.TO_BGR255:
            to_bgr_transform = T.Lambda(lambda x: x * 255)
        else:
            to_bgr_transform = T.Lambda(lambda x: x[[2, 1, 0]])

        normalize_transform = T.Normalize(
            mean=cfg.INPUT.PIXEL_MEAN, std=cfg.INPUT.PIXEL_STD
        )

        transform = T.Compose(
            [
                T.ToPILImage(),
                T.Resize(self.min_image_size),
                T.ToTensor(),
                to_bgr_transform,
                normalize_transform,
            ]
        )
        return transform 
Example #17
Source File: utils.py    From SRN-DeblurNet with MIT License 5 votes vote down vote up
def resize(x, size):
    transform = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Scale(size),
        transforms.ToTensor(),
        ])
    return transform(x) 
Example #18
Source File: vis_tools.py    From Lighthead-RCNN-in-Pytorch0.4.1 with MIT License 5 votes vote down vote up
def show_util_with_conf(conf,idx,imgs, labels_group, bboxes_group, confidences_group, cls_conf_group, correct_id_2_class, class_2_color):
    if torch.sum(torch.abs(bboxes_group[idx])).item() == 0:
        return trans.ToPILImage()(de_preprocess(conf, imgs[idx].cpu()))
    return draw_bbox_class_with_conf(conf, trans.ToPILImage()(de_preprocess(conf, imgs[idx].cpu())),\
                                     labels_group[idx].cpu(), bboxes_group[idx].cpu(),\
                                     correct_id_2_class, class_2_color, \
                                     confidences_group[idx].cpu(), cls_conf_group[idx].cpu()) 
Example #19
Source File: InputTransformations.py    From DEEPSEC with MIT License 5 votes vote down vote up
def defend_jpeg(input_tensor, image_mode, quality):
    pil_image = ToPILImage(mode=image_mode)(input_tensor)
    fd = BytesIO()
    pil_image.save(fd, format='jpeg', quality=quality)  # quality level specified in paper
    jpeg_image = ToTensor()(PIL.Image.open(fd))
    return jpeg_image


# based on https://github.com/scikit-image/scikit-image/blob/master/skimage/restoration/_denoise_cy.pyx

# super slow since this is implemented in pure python :'( 
Example #20
Source File: EIT.py    From DEEPSEC with MIT License 5 votes vote down vote up
def __getitem__(self, index):
        single_image, single_label = self.images[index], self.labels[index]
        if self.transform:
            img = ToPILImage(mode=self.color_mode)(single_image)
            single_image = self.transform(img)
        return single_image, single_label 
Example #21
Source File: EIT.py    From DEEPSEC with MIT License 5 votes vote down vote up
def image_crop_rescale(sample, crop_size, color_mode):
    image = ToPILImage(mode=color_mode)(sample)
    cropped_image = RandomCrop(crop_size)(image)
    rescaled_image = Resize((sample.shape[1], sample.shape[2]), interpolation=0)(cropped_image)
    cropped_rescaled_sample = ToTensor()(rescaled_image)
    return cropped_rescaled_sample 
Example #22
Source File: test_recipes.py    From Torchelie with MIT License 5 votes vote down vote up
def test_deepdream():
    model = nn.Sequential(nn.Conv2d(3, 6, 3))
    dd = DeepDream(model, '0')
    dd.fit(ToPILImage()(torch.randn(3, 128, 128)), 1) 
Example #23
Source File: utils.py    From Deep-Expander-Networks with GNU General Public License v3.0 5 votes vote down vote up
def __call__(self, img):
        centerCrop = transforms.CenterCrop(self.size)
        toPILImage = transforms.ToPILImage()
        toTensor = transforms.ToTensor()
        if self.opt.dataset == 'tuberlin':
            normalize = transforms.Normalize(mean=[0.06,], std=[0.93])
        if self.opt.dataset == 'sketchyrecognition':
            normalize = transforms.Normalize(mean=[0.0465,], std=[0.9])

        w, h = img.size(2), img.size(1)
        temp_output = []
        output = torch.FloatTensor(10, img.size(0), self.size, self.size)
        img = toPILImage(img)
        for img_cur in [img, img.transpose(Image.FLIP_LEFT_RIGHT)]:
            temp_output.append(centerCrop(img_cur))
            temp_output.append(img_cur.crop([0, 0, self.size, self.size]))
            temp_output.append(img_cur.crop([w-self.size, 0, w, self.size]))
            temp_output.append(img_cur.crop([0, h-self.size, self.size, h]))
            temp_output.append(img_cur.crop([w-self.size, h-self.size, w, h]))

        for img_idx in range(10):
            img_cur = temp_output[img_idx]
            img_cur = toTensor(img_cur)
            img_cur = normalize(img_cur)
            output[img_idx] = img_cur.view(img_cur.size(0), img_cur.size(1), img_cur.size(2))

        return output 
Example #24
Source File: trainer.py    From pytorch_segmentation with MIT License 5 votes vote down vote up
def __init__(self, model, loss, resume, config, train_loader, val_loader=None, train_logger=None, prefetch=True):
        super(Trainer, self).__init__(model, loss, resume, config, train_loader, val_loader, train_logger)
        
        self.wrt_mode, self.wrt_step = 'train_', 0
        self.log_step = config['trainer'].get('log_per_iter', int(np.sqrt(self.train_loader.batch_size)))
        if config['trainer']['log_per_iter']: self.log_step = int(self.log_step / self.train_loader.batch_size) + 1

        self.num_classes = self.train_loader.dataset.num_classes

        # TRANSORMS FOR VISUALIZATION
        self.restore_transform = transforms.Compose([
            local_transforms.DeNormalize(self.train_loader.MEAN, self.train_loader.STD),
            transforms.ToPILImage()])
        self.viz_transform = transforms.Compose([
            transforms.Resize((400, 400)),
            transforms.ToTensor()])
        
        if self.device ==  torch.device('cpu'): prefetch = False
        if prefetch:
            self.train_loader = DataPrefetcher(train_loader, device=self.device)
            self.val_loader = DataPrefetcher(val_loader, device=self.device)

        torch.backends.cudnn.benchmark = True 
Example #25
Source File: img_loader.py    From torch-light with MIT License 5 votes vote down vote up
def tensor2img(self, tensor, epoch):
        decode = transforms.Compose([transforms.Lambda(lambda x: x.mul_(1./255)),
               transforms.Normalize(mean=[-0.40760392, -0.45795686, -0.48501961],
                                    std=[1,1,1]),
               transforms.Lambda(lambda x: x[torch.LongTensor([2,1,0])]),
               ])
        tensor = decode(tensor)

        loader = transforms.Compose([transforms.ToPILImage()])
        img = loader(tensor.clamp_(0, 1))

        img.save(self.img_path + "/result_{}.jpg".format(epoch)) 
Example #26
Source File: img_loader.py    From torch-light with MIT License 5 votes vote down vote up
def imshow(tensor, imsize=512, title=None):
        image = tensor.clone().cpu()
        image = image.view(*tensor.size())
        image = transforms.ToPILImage()(image)
        plt.imshow(image)
        if title is not None:
            plt.title(title)
        plt.pause(5) 
Example #27
Source File: utils.py    From tvnet_pytorch with MIT License 5 votes vote down vote up
def save_im_tensor(x, addr):
    x = x.cpu().float()
    transpose = transforms.ToPILImage()
    x = transpose(x[0])
    x.save(addr) 
Example #28
Source File: utils.py    From tvnet_pytorch with MIT License 5 votes vote down vote up
def im_tensor_to_numpy(x):
    transpose = transforms.ToPILImage()
    x = np.asarray(transpose(x))
    return x 
Example #29
Source File: test_recipes.py    From Torchelie with MIT License 5 votes vote down vote up
def test_neuralstyle():
    stylizer = NeuralStyle()

    content = ToPILImage()(torch.randn(3, 64, 64))
    style_img = ToPILImage()(torch.randn(3, 64, 64))

    result = stylizer.fit(1, content, style_img, 1, ['conv1_1']) 
Example #30
Source File: test_tranforms.py    From Torchelie with MIT License 5 votes vote down vote up
def test_resizedcrop():
    img = ToPILImage()(torch.clamp(torch.randn(3, 30, 16) + 1, min=0, max=1))
    tf = ResizedCrop(48)
    tf(img)