Python torchvision.transforms.transforms.ToTensor() Examples

The following are 30 code examples of torchvision.transforms.transforms.ToTensor(). 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.transforms , or try the search function .
Example #1
Source File: base.py    From SlowFast-Network-pytorch with MIT License 6 votes vote down vote up
def preprocess(image: PIL.Image.Image, image_min_side: float, image_max_side: float) -> Tuple[Tensor, float]:
        # resize according to the rules:
        #   1. scale shorter side to IMAGE_MIN_SIDE
        #   2. after scaling, if longer side > IMAGE_MAX_SIDE, scale longer side to IMAGE_MAX_SIDE
        scale_for_shorter_side = image_min_side / min(image.width, image.height)
        longer_side_after_scaling = max(image.width, image.height) * scale_for_shorter_side
        scale_for_longer_side = (image_max_side / longer_side_after_scaling) if longer_side_after_scaling > image_max_side else 1
        scale = scale_for_shorter_side * scale_for_longer_side

        transform = transforms.Compose([
            transforms.Resize((round(image.height * scale), round(image.width * scale))),  # interpolation `BILINEAR` is applied by default
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        image = transform(image)

        return image, scale 
Example #2
Source File: datasets.py    From ICIAR2018 with MIT License 6 votes vote down vote up
def __getitem__(self, index):
        im, xpatch, ypatch, rotation, flip, enhance = np.unravel_index(index, self.shape)

        with Image.open(self.names[im]) as img:
            extractor = PatchExtractor(img=img, patch_size=PATCH_SIZE, stride=self.stride)
            patch = extractor.extract_patch((xpatch, ypatch))

            if rotation != 0:
                patch = patch.rotate(rotation * 90)

            if flip != 0:
                patch = patch.transpose(Image.FLIP_LEFT_RIGHT)

            if enhance != 0:
                factors = np.random.uniform(.5, 1.5, 3)
                patch = ImageEnhance.Color(patch).enhance(factors[0])
                patch = ImageEnhance.Contrast(patch).enhance(factors[1])
                patch = ImageEnhance.Brightness(patch).enhance(factors[2])

            label = self.labels[self.names[im]]
            return transforms.ToTensor()(patch), label 
Example #3
Source File: YOLOv3.py    From simple-HRNet with GNU General Public License v3.0 6 votes vote down vote up
def prepare_data(images, color_mode='BGR', new_shape=416, color=(127.5, 127.5, 127.5), mode='square'):
    images_ok = np.zeros((images.shape[0], new_shape, new_shape, 3), dtype=images[0].dtype)
    images_tensor = torch.zeros((images.shape[0], 3, new_shape, new_shape), dtype=torch.float32)
    for i in range(len(images)):
        if color_mode == 'BGR':
            images[i] = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
        elif color_mode == 'RGB':
            pass
        else:
            raise NotImplementedError
        images_ok[i], _, _, _ = letterbox(images[i], new_shape, color, mode)

        images_tensor[i] = transforms.Compose([
            transforms.ToPILImage(),
            transforms.ToTensor(),
        ])(images_ok[i])

    return images_tensor 
Example #4
Source File: datasets.py    From ICIAR2018 with MIT License 6 votes vote down vote up
def __getitem__(self, index):
        file = self.names[index]
        with Image.open(file) as img:

            bins = 8 if self.augment else 1
            extractor = PatchExtractor(img=img, patch_size=PATCH_SIZE, stride=self.stride)
            b = torch.zeros((bins, extractor.shape()[0] * extractor.shape()[1], 3, PATCH_SIZE, PATCH_SIZE))

            for k in range(bins):

                if k % 4 != 0:
                    img = img.rotate((k % 4) * 90)

                if k // 4 != 0:
                    img = img.transpose(Image.FLIP_LEFT_RIGHT)

                extractor = PatchExtractor(img=img, patch_size=PATCH_SIZE, stride=self.stride)
                patches = extractor.extract_patches()

                for i in range(len(patches)):
                    b[k, i] = transforms.ToTensor()(patches[i])

            return b, file 
Example #5
Source File: base.py    From easy-fpn.pytorch with MIT License 6 votes vote down vote up
def preprocess(image: PIL.Image.Image, image_min_side: float, image_max_side: float) -> Tuple[Tensor, float]:
        # resize according to the rules:
        #   1. scale shorter side to IMAGE_MIN_SIDE
        #   2. after scaling, if longer side > IMAGE_MAX_SIDE, scale longer side to IMAGE_MAX_SIDE
        scale_for_shorter_side = image_min_side / min(image.width, image.height)
        longer_side_after_scaling = max(image.width, image.height) * scale_for_shorter_side
        scale_for_longer_side = (image_max_side / longer_side_after_scaling) if longer_side_after_scaling > image_max_side else 1
        scale = scale_for_shorter_side * scale_for_longer_side

        transform = transforms.Compose([
            transforms.Resize((round(image.height * scale), round(image.width * scale))),  # interpolation `BILINEAR` is applied by default
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        image = transform(image)

        return image, scale 
Example #6
Source File: vgg_mcdropout_cifar10.py    From baal with Apache License 2.0 6 votes vote down vote up
def get_datasets(initial_pool):
    transform = transforms.Compose(
        [transforms.Resize((224, 224)),
         transforms.RandomHorizontalFlip(),
         transforms.RandomRotation(30),
         transforms.ToTensor(),
         transforms.Normalize(3 * [0.5], 3 * [0.5]), ])
    test_transform = transforms.Compose(
        [
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize(3 * [0.5], 3 * [0.5]),
        ]
    )
    # Note: We use the test set here as an example. You should make your own validation set.
    train_ds = datasets.CIFAR10('.', train=True,
                                transform=transform, target_transform=None, download=True)
    test_set = datasets.CIFAR10('.', train=False,
                                transform=test_transform, target_transform=None, download=True)

    active_set = ActiveLearningDataset(train_ds, pool_specifics={'transform': test_transform})

    # We start labeling randomly.
    active_set.label_randomly(initial_pool)
    return active_set, test_set 
Example #7
Source File: util.py    From pytorch-glow with MIT License 6 votes vote down vote up
def pil_to_tensor(img, shape=(64, 64, 3), transform=None):
    """
    Convert PIL image to float tensor

    :param img: PIL image
    :type img: Image.Image
    :param shape: image shape in (H, W, C)
    :type shape: tuple or list
    :param transform: image transform
    :return: tensor
    :rtype: torch.Tensor
    """
    if transform is None:
        transform = transforms.Compose((
            transforms.Resize(shape[0]),
            transforms.ToTensor()
        ))
    return transform(img) 
Example #8
Source File: main.py    From VisualizingCNN with MIT License 6 votes vote down vote up
def load_images(img_path):
    # imread from img_path
    img = cv2.imread(img_path)
    img = cv2.resize(img, (224, 224))

    # pytorch must normalize the pic by 
    # mean = [0.485, 0.456, 0.406]
    # std = [0.229, 0.224, 0.225]
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
        ])
    
    img = transform(img)
    img.unsqueeze_(0)
    #img_s = img.numpy()
    #img_s = np.transpose(img_s, (1, 2, 0))
    #cv2.imshow("test img", img_s)
    #cv2.waitKey()
    return img 
Example #9
Source File: get_dataloader.py    From Greedy_InfoMax with MIT License 6 votes vote down vote up
def get_transforms(eval=False, aug=None):
    trans = []

    if aug["randcrop"] and not eval:
        trans.append(transforms.RandomCrop(aug["randcrop"]))

    if aug["randcrop"] and eval:
        trans.append(transforms.CenterCrop(aug["randcrop"]))

    if aug["flip"] and not eval:
        trans.append(transforms.RandomHorizontalFlip())

    if aug["grayscale"]:
        trans.append(transforms.Grayscale())
        trans.append(transforms.ToTensor())
        trans.append(transforms.Normalize(mean=aug["bw_mean"], std=aug["bw_std"]))
    elif aug["mean"]:
        trans.append(transforms.ToTensor())
        trans.append(transforms.Normalize(mean=aug["mean"], std=aug["std"]))
    else:
        trans.append(transforms.ToTensor())

    trans = transforms.Compose(trans)
    return trans 
Example #10
Source File: base.py    From easy-faster-rcnn.pytorch with MIT License 6 votes vote down vote up
def preprocess(image: PIL.Image.Image, image_min_side: float, image_max_side: float) -> Tuple[Tensor, float]:
        # resize according to the rules:
        #   1. scale shorter side to IMAGE_MIN_SIDE
        #   2. after scaling, if longer side > IMAGE_MAX_SIDE, scale longer side to IMAGE_MAX_SIDE
        scale_for_shorter_side = image_min_side / min(image.width, image.height)
        longer_side_after_scaling = max(image.width, image.height) * scale_for_shorter_side
        scale_for_longer_side = (image_max_side / longer_side_after_scaling) if longer_side_after_scaling > image_max_side else 1
        scale = scale_for_shorter_side * scale_for_longer_side

        transform = transforms.Compose([
            transforms.Resize((round(image.height * scale), round(image.width * scale))),  # interpolation `BILINEAR` is applied by default
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        image = transform(image)

        return image, scale 
Example #11
Source File: AVA.py    From SlowFast-Network-pytorch with MIT License 6 votes vote down vote up
def preprocess(self,image: PIL.Image.Image, image_min_side: float, image_max_side: float) -> Tuple[Tensor, float]:
        # resize according to the rules:
        #   1. scale shorter side to IMAGE_MIN_SIDE
        #   2. after scaling, if longer side > IMAGE_MAX_SIDE, scale longer side to IMAGE_MAX_SIDE
        scale_for_shorter_side = image_min_side / min(image.width, image.height)
        longer_side_after_scaling = max(image.width, image.height) * scale_for_shorter_side
        scale_for_longer_side = (image_max_side / longer_side_after_scaling) if longer_side_after_scaling > image_max_side else 1
        scale = scale_for_shorter_side * scale_for_longer_side

        transform = transforms.Compose([
            transforms.Resize((round(image.height * scale), round(image.width * scale))),  # interpolation `BILINEAR` is applied by default
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        image = transform(image)

        return image, scale 
Example #12
Source File: transforms.py    From Holocron with MIT License 5 votes vote down vote up
def __call__(self, img, target):

        img = super(ToTensor, self).__call__(img)
        target = torch.as_tensor(np.array(target), dtype=torch.int64)

        return img, target 
Example #13
Source File: COCODataset.py    From FasterRCNN.pytorch with MIT License 5 votes vote down vote up
def preprocessImage(img, use_color_jitter, image_size_dict, img_norm_info, use_caffe_pretrained_model):
		# calculate target_size and scale_factor, target_size's format is (h, w)
		w_ori, h_ori = img.width, img.height
		if w_ori > h_ori:
			target_size = (image_size_dict.get('SHORT_SIDE'), image_size_dict.get('LONG_SIDE'))
		else:
			target_size = (image_size_dict.get('LONG_SIDE'), image_size_dict.get('SHORT_SIDE'))
		h_t, w_t = target_size
		scale_factor = min(w_t/w_ori, h_t/h_ori)
		target_size = (round(scale_factor*h_ori), round(scale_factor*w_ori))
		# define and do transform
		if use_caffe_pretrained_model:
			means_norm = img_norm_info['caffe'].get('mean_rgb')
			stds_norm = img_norm_info['caffe'].get('std_rgb')
			if use_color_jitter:
				transform = transforms.Compose([transforms.Resize(target_size),
												transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1),
												transforms.ToTensor(),
												transforms.Normalize(mean=means_norm, std=stds_norm)])
			else:
				transform = transforms.Compose([transforms.Resize(target_size),
												transforms.ToTensor(),
												transforms.Normalize(mean=means_norm, std=stds_norm)])
			img = transform(img) * 255
			img = img[(2, 1, 0), :, :]
		else:
			means_norm = img_norm_info['pytorch'].get('mean_rgb')
			stds_norm = img_norm_info['pytorch'].get('std_rgb')
			if use_color_jitter:
				transform = transforms.Compose([transforms.Resize(target_size),
												transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1),
												transforms.ToTensor(),
												transforms.Normalize(mean=means_norm, std=stds_norm)])
			else:
				transform = transforms.Compose([transforms.Resize(target_size),
												transforms.ToTensor(),
												transforms.Normalize(mean=means_norm, std=stds_norm)])
			img = transform(img)
		# return necessary data
		return img, scale_factor, target_size 
Example #14
Source File: datasets.py    From ICIAR2018 with MIT License 5 votes vote down vote up
def __getitem__(self, index):
        im, rotation, flip, enhance = np.unravel_index(index, self.shape)

        with Image.open(self.names[im]) as img:

            if flip != 0:
                img = img.transpose(Image.FLIP_LEFT_RIGHT)

            if rotation != 0:
                img = img.rotate(rotation * 90)

            if enhance != 0:
                factors = np.random.uniform(.5, 1.5, 3)
                img = ImageEnhance.Color(img).enhance(factors[0])
                img = ImageEnhance.Contrast(img).enhance(factors[1])
                img = ImageEnhance.Brightness(img).enhance(factors[2])

            extractor = PatchExtractor(img=img, patch_size=PATCH_SIZE, stride=self.stride)
            patches = extractor.extract_patches()

            label = self.labels[self.names[im]]

            b = torch.zeros((len(patches), 3, PATCH_SIZE, PATCH_SIZE))
            for i in range(len(patches)):
                b[i] = transforms.ToTensor()(patches[i])

            return b, label 
Example #15
Source File: tasks_celebA.py    From cavia with MIT License 5 votes vote down vote up
def __init__(self, mode, device):

        self.device = device

        if os.path.isdir('/home/scratch/luiraf/work/data/celeba/'):
            data_root = '/home/scratch/luiraf/work/data/celeba/'
        else:
            raise FileNotFoundError('Can\'t find celebrity faces.')

        self.code_root = os.path.dirname(os.path.realpath(__file__))
        self.imgs_root = os.path.join(data_root, 'Img/img_align_celeba/')
        self.imgs_root_preprocessed = os.path.join(data_root, 'Img/img_align_celeba_preprocessed/')
        if not os.path.isdir(self.imgs_root_preprocessed):
            os.mkdir(self.imgs_root_preprocessed)
        self.data_split_file = os.path.join(data_root, 'Eval/list_eval_partition.txt')

        # input: x-y coordinate
        self.num_inputs = 2
        # output: pixel values (RGB)
        self.num_outputs = 3

        # get the labels (train/valid/test)
        train_imgs, valid_imgs, test_imgs = self.get_labels()
        if mode == 'train':
            self.image_files = train_imgs
        elif mode == 'valid':
            self.image_files = valid_imgs
        elif mode == 'test':
            self.image_files = test_imgs
        else:
            raise ValueError

        self.img_size = (32, 32, 3)
        self.transform = transforms.Compose([lambda x: Image.open(x).convert('RGB'),
                                             transforms.Resize((self.img_size[0], self.img_size[1]), Image.LANCZOS),
                                             transforms.ToTensor(),
                                             ]) 
Example #16
Source File: datasets.py    From TorchFusion with MIT License 5 votes vote down vote up
def pathimages_loader(image_paths,size=None,recursive=True,allowed_exts=['jpg', 'jpeg', 'png', 'ppm', 'bmp', 'pgm', 'tif'],shuffle=False,batch_size=32,mean=0.5,std=0.5,transform="default",**loader_args):
    """

    :param image_paths:
    :param size:
    :param recursive:
    :param allowed_exts:
    :param shuffle:
    :param batch_size:
    :param mean:
    :param std:
    :param transform:
    :param loader_args:
    :return:
    """
    if size is not None:
        if not isinstance(size,tuple):
            size = (size,size)

    if transform == "default":
        t = []
        if size is not None:
            t.append(transformations.Resize(size))

        t.append(transformations.ToTensor())

        if mean is not None and std is not None:
            if not isinstance(mean, tuple):
                mean = (mean,)
            if not isinstance(std, tuple):
                std = (std,)
            t.append(transformations.Normalize(mean=mean, std=std))

        trans = transformations.Compose(t)
    else:
        trans = transform

    data = ImagesFromPaths(image_paths,trans,recursive=recursive,allowed_exts=allowed_exts)

    return DataLoader(data,batch_size=batch_size,shuffle=shuffle,**loader_args) 
Example #17
Source File: datasets.py    From TorchFusion with MIT License 5 votes vote down vote up
def fashionmnist_loader(size=None,root="./fashionmnist",train=True,batch_size=32,mean=0.5,std=0.5,transform="default",download=True,target_transform=None,**loader_args):
    """

    :param size:
    :param root:
    :param train:
    :param batch_size:
    :param mean:
    :param std:
    :param transform:
    :param download:
    :param target_transform:
    :param loader_args:
    :return:
    """

    if size is not None:
        if not isinstance(size,tuple):
            size = (size,size)

    if transform == "default":
        t = []
        if size is not None:
            t.append(transformations.Resize(size))

        t.append(transformations.ToTensor())
        if mean is not None and std is not None:
            if not isinstance(mean, tuple):
                mean = (mean,)
            if not isinstance(std, tuple):
                std = (std,)
            t.append(transformations.Normalize(mean=mean, std=std))

        trans = transformations.Compose(t)
    else:
        trans = transform

    data = FashionMNIST(root,train=train,transform=trans,download=download,target_transform=target_transform)

    return DataLoader(data,batch_size=batch_size,shuffle=train,**loader_args) 
Example #18
Source File: datasets.py    From TorchFusion with MIT License 5 votes vote down vote up
def cifar100_loader(size=None,root="./cifar100",train=True,batch_size=32,mean=0.5,std=0.5,transform="default",download=True,target_transform=None,**loader_args):
    """

    :param size:
    :param root:
    :param train:
    :param batch_size:
    :param mean:
    :param std:
    :param transform:
    :param download:
    :param target_transform:
    :param loader_args:
    :return:
    """
    if size is not None:
        if not isinstance(size,tuple):
            size = (size,size)

    if transform == "default":
        t = []
        if size is not None:
            t.append(transformations.Resize(size))

        t.append(transformations.ToTensor())
        if mean is not None and std is not None:
            if not isinstance(mean, tuple):
                mean = (mean,)
            if not isinstance(std, tuple):
                std = (std,)
            t.append(transformations.Normalize(mean=mean, std=std))

        trans = transformations.Compose(t)
    else:
        trans = transform

    data = MNIST(root,train=train,transform=trans,download=download,target_transform=target_transform)

    return DataLoader(data,batch_size=batch_size,shuffle=train,**loader_args) 
Example #19
Source File: heuristics_gpu_test.py    From baal with Apache License 2.0 5 votes vote down vote up
def segmentation_task(tmpdir):
    model = nn.Sequential(nn.Conv2d(3, 32, 3),
                          nn.ReLU(),
                          nn.Conv2d(32, 64, 3),
                          nn.MaxPool2d(2),
                          nn.Conv2d(64, 64, 3),
                          Dropout2d(),
                          nn.ConvTranspose2d(64, 10, 3, 1)
                          )
    model = ModelWrapper(model, nn.CrossEntropyLoss())
    test = datasets.CIFAR10(tmpdir, train=False, download=True, transform=transforms.ToTensor())
    return model, test 
Example #20
Source File: heuristics_gpu_test.py    From baal with Apache License 2.0 5 votes vote down vote up
def classification_task(tmpdir):
    model = nn.Sequential(nn.Conv2d(3, 32, 3),
                          nn.ReLU(),
                          nn.Conv2d(32, 64, 3),
                          nn.MaxPool2d(2),
                          nn.AdaptiveAvgPool2d((7, 7)),
                          Flatten(),
                          nn.Linear(7 * 7 * 64, 128),
                          Dropout(),
                          nn.Linear(128, 10)
                          )
    model = ModelWrapper(model, nn.CrossEntropyLoss())
    test = datasets.CIFAR10(tmpdir, train=False, download=True, transform=transforms.ToTensor())
    return model, test 
Example #21
Source File: data_provider.py    From FastDVDNet with MIT License 5 votes vote down vote up
def __init__(self, base_path, txt_file, im_size=96, frames=5):
        super(Video_Provider, self).__init__()
        self.base_path = base_path
        self.txt_file = open(txt_file, 'r').readlines()
        self.im_size = im_size
        self.trans = transforms.ToTensor()
        self.frames = frames 
Example #22
Source File: pytorch_abstract_udf.py    From eva with Apache License 2.0 5 votes vote down vote up
def transforms(self) -> Compose:
        return Compose([transforms.ToTensor()]) 
Example #23
Source File: datasets.py    From TorchFusion with MIT License 4 votes vote down vote up
def svhn_loader(size=None,root="./shvn",set="train",batch_size=32,mean=0.5,std=0.5,transform="default",download=True,target_transform=None,**loader_args):
    """

    :param size:
    :param root:
    :param set:
    :param batch_size:
    :param mean:
    :param std:
    :param transform:
    :param download:
    :param target_transform:
    :param loader_args:
    :return:
    """
    valid_sets = ('train', 'test', 'extra')

    if set not in valid_sets: raise ValueError("set {}  is invalid, valid sets include {}".format(set,valid_sets))

    if size is not None:
        if not isinstance(size,tuple):
            size = (size,size)

    if transform == "default":
        t = []
        if size is not None:
            t.append(transformations.Resize(size))

        t.append(transformations.ToTensor())

        if mean is not None and std is not None:
            if not isinstance(mean, tuple):
                mean = (mean,)
            if not isinstance(std, tuple):
                std = (std,)
            t.append(transformations.Normalize(mean=mean, std=std))

        trans = transformations.Compose(t)
    else:
        trans = transform
    data = SVHN(root,split=set,transform=trans,download=download,target_transform=target_transform)
    shuffle_mode = True if set == "train" else False
    return DataLoader(data,batch_size=batch_size,shuffle=shuffle_mode,**loader_args) 
Example #24
Source File: datasets.py    From TorchFusion with MIT License 4 votes vote down vote up
def cmpfacades_loader(size=None,root="./cmpfacades",set="train",batch_size=32,mean=0.5,std=0.5,transform="default",download=True,reverse_mode=False,**loader_args):
    """

    :param size:
    :param root:
    :param set:
    :param batch_size:
    :param mean:
    :param std:
    :param transform:
    :param download:
    :param reverse_mode:
    :param loader_args:
    :return:
    """
    valid_sets = ('train', 'test', 'val')

    if set not in valid_sets: raise ValueError("set {}  is invalid, valid sets include {}".format(set,valid_sets))

    if size is not None:
        if not isinstance(size,tuple):
            size = (size,size)

    if transform == "default":
        t = []
        if size is not None:
            t.append(transformations.Resize(size))

        t.append(transformations.ToTensor())

        if mean is not None and std is not None:
            if not isinstance(mean, tuple):
                mean = (mean,)
            if not isinstance(std, tuple):
                std = (std,)
            t.append(transformations.Normalize(mean=mean, std=std))

        trans = transformations.Compose(t)
    else:
        trans = transform
    data = CMPFacades(root,source_transforms=trans,target_transforms=trans,set=set,download=download,reverse_mode=reverse_mode)
    shuffle_mode = True if set == "train" else False
    return DataLoader(data,batch_size=batch_size,shuffle=shuffle_mode,**loader_args) 
Example #25
Source File: datasets.py    From TorchFusion with MIT License 4 votes vote down vote up
def cifar10_loader(size=None,root="./cifar10",train=True,batch_size=32,mean=0.5,std=0.5,transform="default",download=True,target_transform=None,**loader_args):

    """

    :param size:
    :param root:
    :param train:
    :param batch_size:
    :param mean:
    :param std:
    :param transform:
    :param download:
    :param target_transform:
    :param loader_args:
    :return:
    """

    if size is not None:
        if not isinstance(size,tuple):
            size = (size,size)

    if transform == "default":
        t = []
        if size is not None:
            t.append(transformations.Resize(size))

        t.append(transformations.ToTensor())

        if mean is not None and std is not None:
            if not isinstance(mean, tuple):
                mean = (mean,)
            if not isinstance(std, tuple):
                std = (std,)
            t.append(transformations.Normalize(mean=mean, std=std))

        trans = transformations.Compose(t)
    else:
        trans = transform

    data = CIFAR10(root,train=train,transform=trans,download=download,target_transform=target_transform)

    return DataLoader(data,batch_size=batch_size,shuffle=train,**loader_args) 
Example #26
Source File: datasets.py    From TorchFusion with MIT License 4 votes vote down vote up
def imagefolder_loader(size=None,root="./data",shuffle=False,class_map=None,batch_size=32,mean=0.5,std=0.5,transform="default",allowed_exts=['.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif'],source=None,target_transform=None,**loader_args):

    """

    :param size:
    :param root:
    :param shuffle:
    :param class_map:
    :param batch_size:
    :param mean:
    :param std:
    :param transform:
    :param allowed_exts:
    :param source:
    :param target_transform:
    :param loader_args:
    :return:
    """

    if source is not None:
        if os.path.exists(root) == False:
            print("Downloading {}".format(source[0]))
            download_file(source[0],source[1],extract_path=root)
    elif len(os.listdir(root)) == 0:
        print("Downloading {}".format(source[0]))
        download_file(source[0], source[1], extract_path=root)

    if size is not None:
        if not isinstance(size,tuple):
            size = (size,size)

    if transform == "default":
        t = []
        if size is not None:
            t.append(transformations.Resize(size))

        t.append(transformations.ToTensor())

        if mean is not None and std is not None:
            if not isinstance(mean, tuple):
                mean = (mean,)
            if not isinstance(std, tuple):
                std = (std,)
            t.append(transformations.Normalize(mean=mean, std=std))

        trans = transformations.Compose(t)
    else:
        trans = transform

    data = DataFolder(root=root,loader=default_loader,extensions=allowed_exts,transform=trans,target_transform=target_transform,class_map=class_map)

    return DataLoader(data,batch_size=batch_size,shuffle=shuffle,**loader_args) 
Example #27
Source File: datasets.py    From TorchFusion with MIT License 4 votes vote down vote up
def mnist_loader(size=None,root="./mnist",train=True,batch_size=32,mean=0.5,std=0.5,transform="default",download=True,target_transform=None,**loader_args):

    """

    :param size:
    :param root:
    :param train:
    :param batch_size:
    :param mean:
    :param std:
    :param transform:
    :param download:
    :param target_transform:
    :param loader_args:
    :return:
    """

    if size is not None:
        if not isinstance(size,tuple):
            size = (size,size)

    if transform == "default":
        t = []
        if size is not None:
            t.append(transformations.Resize(size))

        t.append(transformations.ToTensor())

        if mean is not None and std is not None:
            if not isinstance(mean, tuple):
                mean = (mean,)
            if not isinstance(std, tuple):
                std = (std,)
            t.append(transformations.Normalize(mean=mean, std=std))

        trans = transformations.Compose(t)


    else:
        trans = transform

    data = MNIST(root,train=train,transform=trans,download=download,target_transform=target_transform)

    return DataLoader(data,batch_size=batch_size,shuffle=train,**loader_args) 
Example #28
Source File: datasets.py    From TorchFusion with MIT License 4 votes vote down vote up
def idenprof_loader(size=None,root="./idenprof",train=True,batch_size=32,mean=0.5,std=0.5,transform="default",target_transform=None,**loader_args):

    """

    :param size:
    :param root:
    :param train:
    :param batch_size:
    :param mean:
    :param std:
    :param transform:
    :param target_transform:
    :param loader_args:
    :return:
    """

    if size is not None:
        if not isinstance(size,tuple):
            size = (size,size)

    if transform == "default":
        t = []
        if size is not None:
            t.append(transformations.Resize(size))

        t.append(transformations.ToTensor())

        if mean is not None and std is not None:
            if not isinstance(mean, tuple):
                mean = (mean,)
            if not isinstance(std, tuple):
                std = (std,)
            t.append(transformations.Normalize(mean=mean, std=std))

        trans = transformations.Compose(t)


    else:
        trans = transform

    data = IdenProf(root,train=train,transform=trans,target_transform=target_transform)

    return DataLoader(data,batch_size=batch_size,shuffle=train,**loader_args) 
Example #29
Source File: MiniImagenet.py    From MAML-Pytorch with MIT License 4 votes vote down vote up
def __init__(self, root, mode, batchsz, n_way, k_shot, k_query, resize, startidx=0):
        """

        :param root: root path of mini-imagenet
        :param mode: train, val or test
        :param batchsz: batch size of sets, not batch of imgs
        :param n_way:
        :param k_shot:
        :param k_query: num of qeruy imgs per class
        :param resize: resize to
        :param startidx: start to index label from startidx
        """

        self.batchsz = batchsz  # batch of set, not batch of imgs
        self.n_way = n_way  # n-way
        self.k_shot = k_shot  # k-shot
        self.k_query = k_query  # for evaluation
        self.setsz = self.n_way * self.k_shot  # num of samples per set
        self.querysz = self.n_way * self.k_query  # number of samples per set for evaluation
        self.resize = resize  # resize to
        self.startidx = startidx  # index label not from 0, but from startidx
        print('shuffle DB :%s, b:%d, %d-way, %d-shot, %d-query, resize:%d' % (
        mode, batchsz, n_way, k_shot, k_query, resize))

        if mode == 'train':
            self.transform = transforms.Compose([lambda x: Image.open(x).convert('RGB'),
                                                 transforms.Resize((self.resize, self.resize)),
                                                 # transforms.RandomHorizontalFlip(),
                                                 # transforms.RandomRotation(5),
                                                 transforms.ToTensor(),
                                                 transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
                                                 ])
        else:
            self.transform = transforms.Compose([lambda x: Image.open(x).convert('RGB'),
                                                 transforms.Resize((self.resize, self.resize)),
                                                 transforms.ToTensor(),
                                                 transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
                                                 ])

        self.path = os.path.join(root, 'images')  # image path
        csvdata = self.loadCSV(os.path.join(root, mode + '.csv'))  # csv path
        self.data = []
        self.img2label = {}
        for i, (k, v) in enumerate(csvdata.items()):
            self.data.append(v)  # [[img1, img2, ...], [img111, ...]]
            self.img2label[k] = i + self.startidx  # {"img_name[:9]":label}
        self.cls_num = len(self.data)

        self.create_batch(self.batchsz) 
Example #30
Source File: torchvision_datasets.py    From cortex with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _handle_STL(self, Dataset, data_path, transform=None,
                    labeled_only=False, stl_center_crop=False,
                    stl_resize_only=False, stl_no_resize=False):
        normalize = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))

        if stl_no_resize:
            train_transform = transforms.Compose([
                transforms.RandomHorizontalFlip(),
                transforms.ToTensor(),
                normalize,
            ])
            test_transform = transforms.Compose([
                transforms.ToTensor(),
                normalize,
            ])
        else:
            if stl_center_crop:
                tr_trans = transforms.CenterCrop(64)
                te_trans = transforms.CenterCrop(64)
            elif stl_resize_only:
                tr_trans = transforms.Resize(64)
                te_trans = transforms.Resize(64)
            elif stl_no_resize:
                pass
            else:
                tr_trans = transforms.RandomResizedCrop(64)
                te_trans = transforms.Resize(64)

            train_transform = transforms.Compose([
                tr_trans,
                transforms.RandomHorizontalFlip(),
                transforms.ToTensor(),
                normalize,
            ])
            test_transform = transforms.Compose([
                te_trans,
                transforms.ToTensor(),
                normalize,
            ])
        if labeled_only:
            split = 'train'
        else:
            split = 'train+unlabeled'
        train_set = Dataset(
            data_path, split=split, transform=train_transform, download=True)
        test_set = Dataset(
            data_path, split='test', transform=test_transform, download=True)
        return train_set, test_set