Beispiel #1
0
    def test_five_crop(self):
        to_pil_image = transforms.ToPILImage()
        h = random.randint(5, 25)
        w = random.randint(5, 25)
        for single_dim in [True, False]:
            crop_h = random.randint(1, h)
            crop_w = random.randint(1, w)
            if single_dim:
                crop_h = min(crop_h, crop_w)
                crop_w = crop_h
                transform = transforms.FiveCrop(crop_h)
            else:
                transform = transforms.FiveCrop((crop_h, crop_w))

            img = torch.FloatTensor(3, h, w).uniform_()
            results = transform(to_pil_image(img))

            assert len(results) == 5
            for crop in results:
                assert crop.size == (crop_w, crop_h)

            to_pil_image = transforms.ToPILImage()
            tl = to_pil_image(img[:, 0:crop_h, 0:crop_w])
            tr = to_pil_image(img[:, 0:crop_h, w - crop_w:])
            bl = to_pil_image(img[:, h - crop_h:, 0:crop_w])
            br = to_pil_image(img[:, h - crop_h:, w - crop_w:])
            center = transforms.CenterCrop((crop_h, crop_w))(to_pil_image(img))
            expected_output = (tl, tr, bl, br, center)
            assert results == expected_output
Beispiel #2
0
    def test_ten_crop(self):
        to_pil_image = transforms.ToPILImage()
        h = random.randint(5, 25)
        w = random.randint(5, 25)
        for should_vflip in [True, False]:
            for single_dim in [True, False]:
                crop_h = random.randint(1, h)
                crop_w = random.randint(1, w)
                if single_dim:
                    crop_h = min(crop_h, crop_w)
                    crop_w = crop_h
                    transform = transforms.TenCrop(crop_h,
                                                   vertical_flip=should_vflip)
                    five_crop = transforms.FiveCrop(crop_h)
                else:
                    transform = transforms.TenCrop((crop_h, crop_w),
                                                   vertical_flip=should_vflip)
                    five_crop = transforms.FiveCrop((crop_h, crop_w))

                img = to_pil_image(torch.FloatTensor(3, h, w).uniform_())
                results = transform(img)
                expected_output = five_crop(img)
                if should_vflip:
                    vflipped_img = img.transpose(Image.FLIP_TOP_BOTTOM)
                    expected_output += five_crop(vflipped_img)
                else:
                    hflipped_img = img.transpose(Image.FLIP_LEFT_RIGHT)
                    expected_output += five_crop(hflipped_img)

                assert len(results) == 10
                assert expected_output == results
Beispiel #3
0
def get_transform(train=True):
    transform_list = []
    if cfg.DATA_TRANSFORM.RESIZE_OR_CROP == 'resize_and_crop':
        osize = [cfg.DATA_TRANSFORM.LOADSIZE, cfg.DATA_TRANSFORM.LOADSIZE]
        transform_list.append(transforms.Resize(osize, Image.BICUBIC))
        if train:
            transform_list.append(
                transforms.RandomCrop(cfg.DATA_TRANSFORM.FINESIZE))
        else:
            if cfg.DATA_TRANSFORM.WITH_FIVE_CROP:
                transform_list.append(
                    transforms.FiveCrop(cfg.DATA_TRANSFORM.FINESIZE))
            else:
                transform_list.append(
                    transforms.CenterCrop(cfg.DATA_TRANSFORM.FINESIZE))

    elif cfg.DATA_TRANSFORM.RESIZE_OR_CROP == 'crop':
        if train:
            transform_list.append(
                transforms.RandomCrop(cfg.DATA_TRANSFORM.FINESIZE))
        else:
            if cfg.DATA_TRANSFORM.WITH_FIVE_CROP:
                transform_list.append(
                    transforms.FiveCrop(cfg.DATA_TRANSFORM.FINESIZE))
            else:
                transform_list.append(
                    transforms.CenterCrop(cfg.DATA_TRANSFORM.FINESIZE))

    if train and cfg.DATA_TRANSFORM.FLIP:
        transform_list.append(transforms.RandomHorizontalFlip())

    to_normalized_tensor = [
        transforms.ToTensor(),
        transforms.Normalize(mean=cfg.DATA_TRANSFORM.NORMALIZE_MEAN,
                             std=cfg.DATA_TRANSFORM.NORMALIZE_STD)
    ]

    if not train and cfg.DATA_TRANSFORM.WITH_FIVE_CROP:
        transform_list += [
            transforms.Lambda(lambda crops: torch.stack([
                transforms.Compose(to_normalized_tensor)(crop)
                for crop in crops
            ]))
        ]
    else:
        transform_list += to_normalized_tensor

    return transforms.Compose(transform_list)
    def __call__(self, sample):
        image, label = sample["image"], sample["label"]
        if self.data_aug:
            image = sk.transform.resize(image,(120,140))
            # image = self.crop_center(image, (120,140))
            image = self.randon_crop(image)
            image = self.random_horizontal_flip(image)
            image = self.random_rotation(image)
            image = self.random_noise(image)
            # image = self.random_jitter(image)
            image = torch.from_numpy(image.astype(np.float32))
            image = transforms.RandomErasing()(image)
            image = image.permute(2,0,1)/255.0
            # return {'image': image, 'label': torch.from_numpy(np.array(label)).long()}

        else:
            image = sk.transform.resize(image,(120,140))
 #           image = self.crop_center(image, self.output_size)
            #image = sk.transform.resize(image,list(self.output_size))
            # image = transforms.Resize((self.output_size[0], self.output_size[1]))(image)
            image = Image.fromarray(np.uint8(image))
#            image = np.transpose(image, (2,0,1)).astype(np.float32)
            #image = torch.from_numpy(image.astype(np.float32))/255.0
            crops = transforms.FiveCrop(list(self.output_size))(image)
            image = torch.stack([transforms.ToTensor()(crop)/255.0 for crop in crops], 0)

        # mean = image.view(image.size(0), -1).mean(1)
        # std = image.view(image.size(0), -1).std(1)+ 1e-18
        # image = (image - mean.view(-1,1,1))/std.view(-1,1,1)
        # # print(image.shape)

        return {'image': image, 'label': torch.from_numpy(np.array(label)).long()}
Beispiel #5
0
    def __init__(self, fivecrop=False, test_dir='./test/'):
        '''
        Args:
            fivecrop(bool): If true, fivecrop input data.
            test_dir(str): Root directory saving test images.
        '''

        super(TestDataset, self).__init__()
        self.fivecrop = fivecrop
        if not self.fivecrop:
            self.transform = transforms.Compose([
                transforms.Resize(256),
                transforms.CenterCrop(224),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
            ])
        else:
            self.transform = transforms.Compose([
                transforms.Resize(256),
                transforms.FiveCrop(224),
                transforms.Lambda(lambda crops: torch.stack([
                    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225])
                    (transforms.ToTensor()(crop)) for crop in crops
                ]))
            ])
        self.test_dir = test_dir
        self.image_names = os.listdir(self.test_dir)
        self.image_dirs = [
            os.path.join(self.test_dir, name) for name in self.image_names
        ]
Beispiel #6
0
def load_data(img_paths, train = False):
    #DroneCrowd
    imgset = []
    gts = []
    for i in range(len(img_paths)):
        img = Image.open(img_paths[i]).convert('RGB')
        imgset.append(img)
        mat_path = img_paths[i].replace('.jpg', '.mat').replace('images', 'ground_truth').replace('img', 'GT_img')
        mat = io.loadmat(mat_path)
        gt = mat["image_info"][0, 0][0, 0][0]
        gt = np.array(gt, dtype=np.float32)
        gt_ = np.zeros(shape=[512 - gt.shape[0], 2])
        all_gt = np.vstack((gt, gt_))
        if i == 0:
            gtnum = np.sum(gt.shape[0])
        else:
            gtnum = np.vstack((gtnum, np.sum(gt.shape[0])))
        gts.append(all_gt)

    # crop the images
    crop_factor = 0.5
    imgs1, imgs2, imgs3, imgs4 = [], [], [], []
    for i in range(len(imgset)):
        # crop the images
        crop_size = (int(imgset[0].size[1] * crop_factor), int(imgset[0].size[0] * crop_factor))
        imgs = transforms.FiveCrop(crop_size)(imgset[i])
        img1, img2, img3, img4 = imgs[0:4]
        imgs1.append(img1)
        imgs2.append(img2)
        imgs3.append(img3)
        imgs4.append(img4)

    return imgs1, imgs2, imgs3, imgs4, gtnum, gts
Beispiel #7
0
    def __init__(self):
        seed = 999
        random.seed(seed)
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.backends.cudnn.deterministic = True

        self.device = torch.device('cpu')

        self.model = ResidualAttentionModel_56()
        self.model_path = "./Res_56_new.pth"
        if torch.cuda.is_available():
            self.model.load_state_dict(torch.load(self.model_path))
        else:
            self.model.load_state_dict(
                torch.load(self.model_path, map_location=torch.device('cpu')))
        self.model.to(self.device)
        self.model.eval()

        self.testing_transforms = transforms.Compose([
            transforms.ToPILImage(),
            transforms.FiveCrop(224),
            transforms.Lambda(lambda crops: torch.stack(
                [transforms.ToTensor()(crop) for crop in crops])),
        ])
        self.prediction = None
        self.gc_img = None
Beispiel #8
0
    def _init_multi_crop(self):
        tfs = []
        if self.preserve_aspect_ratio:
            tfs.append(
                transforms.Resize(int(
                    math.floor(max(self.input_size) / self.scale)),
                                  interpolation=_pil_interp("bilinear")))
        else:
            height = int(self.input_size[1] / self.scale)
            width = int(self.input_size[2] / self.scale)
            tfs.append(
                transforms.Resize((height, width),
                                  interpolation=_pil_interp("bilinear")))
        if self.ten_crop is True:
            tfs.append(transforms.TenCrop(max(self.input_size)))
        else:
            tfs.append(transforms.FiveCrop(max(self.input_size)))
        local_tfs = []
        local_tfs.append(transforms.ToTensor())
        local_tfs.append(ToSpaceBGR(self.input_space == 'BGR'))
        local_tfs.append(ToRange255(max(self.input_range) == 255))
        local_tfs.append(transforms.Normalize(mean=self.mean, std=self.std))
        local_tfs = transforms.Compose(local_tfs)

        tfs.append(
            transforms.Lambda(lambda crops: torch.stack(
                [local_tfs(crop) for crop in crops])))

        self.tf = transforms.Compose(tfs)
def test_five_crop():
    pil = pil_transforms.FiveCrop((224, 224))(pil_image)
    cv = transforms.FiveCrop((224, 224))(image)
    pil_stacked = np.hstack([np.asarray(i) for i in pil])
    cv_stacked = np.hstack(cv)
    l1 = L1(pil_stacked, cv_stacked)
    assert l1 - 22.0444 < TOL
Beispiel #10
0
def get_crops_transform(args) -> transforms:
    if args.n_crops == 10:
        return transforms.TenCrop(224)
    elif args.n_crops == 5:
        return transforms.FiveCrop(224)
    else:
        return transforms.Lambda(lambda x: x)
Beispiel #11
0
def get_densenet_transforms(args):
    """
    densenet needs RGB images and normalization.
    """
    custom_transforms = CustomTransforms()
    normalize = transforms.Normalize([0.485, 0.456, 0.406],
                                     [0.229, 0.224, 0.225])
    crops_transform = get_crops_transform(args)
    transformation_list = [
        transforms.ToPILImage(),
        transforms.Lambda(custom_transforms.to_RGB),
        # transforms.Resize(args.img_size)
    ]
    if args.n_crops not in [10, 5]:
        transformation_list.extend([transforms.ToTensor(), normalize])
    else:
        # image is split in n_crops number of crops, stacked and every crop is then normalized
        if args.n_crops == 10:
            transformation_list.append(transforms.TenCrop(224))
        elif args.n_crops == 5:
            crops_transform = transforms.FiveCrop(224)

        transformation_list.extend([
            crops_transform,
            transforms.Lambda(custom_transforms.crops_to_tensor),
            transforms.Lambda(custom_transforms.normalize_crops)
        ])

    return transforms.Compose(transformation_list)
Beispiel #12
0
def build_transform(methods, im_size):
    norm = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                std=[0.229, 0.224, 0.225])
    transform_default = transforms.Compose([transforms.ToTensor(), norm])
    if methods == "resize_crop":
        transform = transforms.Compose([
            transforms.Resize(int(im_size[0] * 8 / 7.)),
            transforms.CenterCrop(im_size[0]),
            transforms.ToTensor(), norm
        ])
    elif methods == "FiveCrop":
        transform = transforms.Compose([
            transforms.Resize(int(im_size[0] * 8 / 7.)),
            transforms.FiveCrop(im_size[0]),
            transforms.Lambda(lambda crops: torch.stack(
                [norm(transforms.ToTensor()(crop)) for crop in crops]))
        ])
    elif methods == "TenCrop":
        transform = transforms.Compose([
            transforms.Resize(int(im_size[0] * 8 / 7.)),
            transforms.TenCrop(im_size[0]),
            transforms.Lambda(lambda crops: torch.stack(
                [norm(transforms.ToTensor()(crop)) for crop in crops]))
        ])
    elif methods == "TenCrop+Resize":
        transform = [
            transforms.Compose([
                transforms.Resize(int(im_size[0] * 8 / 7.)),
                transforms.TenCrop(im_size[0]),
                transforms.Lambda(lambda crops: torch.stack(
                    [norm(transforms.ToTensor()(crop)) for crop in crops]))
            ]), transform_default
        ]
    return transform
Beispiel #13
0
def make_test_loader():
    # TTA
    rng = RNG(args.random_seed)
    test_transforms_list = make_aug_transforms(rng, propagate_manip=False)
    if args.crop_size == 512:
        test_transforms_list += [
            transforms.Lambda(
                lambda img: [img, img.transpose(Image.ROTATE_90)]),
            transforms.Lambda(lambda crops: torch.stack([
                transforms.Normalize(args.means, args.stds)
                (transforms.ToTensor()(crop)) for crop in crops
            ]))
        ]
    else:
        test_transforms_list += [
            transforms.FiveCrop(args.crop_size),
            transforms.Lambda(lambda imgs: list(imgs) +\
                                           [img.transpose(Image.ROTATE_90) for img in imgs]),
            transforms.Lambda(lambda crops: torch.stack([transforms.Normalize(args.means, args.stds)(transforms.ToTensor()(crop)) for crop in crops]))
        ]
    test_transform = transforms.Compose(test_transforms_list)
    test_dataset = KaggleCameraDataset(args.data_path,
                                       train=False,
                                       transform=test_transform)
    test_loader = DataLoader(dataset=test_dataset,
                             batch_size=args.batch_size,
                             shuffle=False,
                             num_workers=args.n_workers)
    return test_dataset, test_loader
Beispiel #14
0
def model_predict(img_path, model, device):
    '''
    Use pre-trained VGG-16 model to obtain index corresponding to
    predicted ImageNet class for image at specified path

    Args:
        img_path: path to an image

    Returns:
        Index corresponding to VGG-16 model's prediction
    '''
    ## TODO: Complete the function.
    ## Load and pre-process an image from the given img_path
    ## Return the *index* of the predicted class for that image
    image = Image.open(img_path).convert('RGB')
    # To avoid changing the aspect ratio of the image we can use the FiveCrop transform, which returns the center and the
    # corner crops. Five outputs are calculated and then averaged.
    in_transform = transforms.Compose([
        transforms.Resize(img_short_side_resize),
        transforms.FiveCrop(img_input_size),
        transforms.Lambda(lambda crops: torch.stack([
            transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize(mean=norm_mean, std=norm_std)
            ])(crop) for crop in crops
        ]))
    ])

    output = torch.argmax(model(in_transform(image).to(device)).mean(0))

    return output.to("cpu").item()  # predicted class index
Beispiel #15
0
def predict_breed_transfer(img_path, model, device):
    # load the image and return the predicted breed
    image = Image.open(img_path).convert('RGB')

    # Resnet replaces the fully connected layer with global average pooling. Theoretically then,
    # it should work with any input size, since the result of the global pooling depends only on
    # the number of filters and not on their spatial dimensions. Unfortunately, the provided
    # torchvision model implements global pooling as a regular average pooling in which the kernel
    # size is equal to the spatial size of the filters (in this case 7). So we still need to apply the
    # FiveCrop transform if we want to keep the aspect ratio and not only crop the center.

    in_transform = transforms.Compose([
        transforms.Resize(img_short_side_resize),
        transforms.FiveCrop(img_input_size),
        transforms.Lambda(lambda crops: torch.stack([
            transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize(mean=norm_mean, std=norm_std)
            ])(crop) for crop in crops
        ]))
    ])

    scores = model(in_transform(image).to(device)).mean(0)
    output = torch.argmax(scores)

    return output.to("cpu").item(), F.softmax(
        scores, dim=0).to("cpu").data.numpy()  # predicted class index
Beispiel #16
0
def main():
    root = r"C:\Users\lukeasargen\projects\data\test512"
    batch_size = 4
    input_size = 256

    nrow = 5
    valid_transform = T.Compose([
        T.Resize(int(1.2 * input_size)),
        T.FiveCrop(input_size),
        # T.TenCrop(input_size),
        T.Lambda(
            lambda crops: torch.stack([T.ToTensor()(crop) for crop in crops])),
    ])

    valid_ds = ImageFolder(root=root, transform=valid_transform)
    val_loader = DataLoader(dataset=valid_ds,
                            batch_size=batch_size,
                            shuffle=True,
                            num_workers=0)
    print("{} Test Samples.".format(len(valid_ds)))

    data, target = next(iter(val_loader))

    bs, ncrops, c, h, w = data.size()
    data = data.view(-1, c, h, w)

    grid_img = make_grid(data, nrow=nrow)
    plt.imshow(grid_img.permute(1, 2, 0))
    plt.show()
Beispiel #17
0
    def __getitem__(self, idx):
        # idx~[0~len(images)]
        # self.images, self.labels
        # img: 'pokemon\\bulbasaur\\00000000.png'
        # label: 0
        img, label = self.images[idx], self.labels[idx]
        img_init = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
        img_init = list(cv2.resize(img_init,(320,240)))
        tf = transforms.Compose([
            lambda x:Image.open(x).convert('L'), # string path= > image data
            transforms.FiveCrop((240,320)),
            transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
        ])
        img = tf(img)
        img_left_up = img[0]
        img_right_down = img[3]
        img = [[],[],[]]
        img[0] = img_init
        img[1] = img_left_up[0]
        img[2] = img_right_down[0]
        img = torch.tensor(img)
        tf_1 = transforms.Compose([
            transforms.Resize((224,224)),
            transforms.RandomRotation(30),
            transforms.ColorJitter(brightness=0.5, contrast=0.5, hue=0.5),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])
        label = torch.tensor(label)

        return img, label
Beispiel #18
0
def create_dataset_actual_fivecrop(path, attribute, protected_attribute, params, augment, dataset, number=0, split='train'):

    img_path = path+'/img_align_celeba/'
    split_path = path+'/list_eval_partition_celeba.txt'
    attr_path = path+'/list_attr_celeba.txt'
    list_ids = []
    label = open(attr_path, 'r')
    label = label.readlines()
    train_beg = 0
    valid_beg = 162770
    test_beg = 182637

    if split=='train':
        if number==0:
            number = valid_beg - train_beg
        beg = train_beg
    elif split=='valid':
        if number==0:
            number = test_beg - valid_beg
        beg = valid_beg
    elif split=='test':
        if number==0:
            number = 202599 - test_beg
        beg = test_beg
    else:
        print('Error')
        return
    attr = {}
    for i in range(beg+2, beg+ number+2):
        temp = label[i].strip().split()
        list_ids.append(img_path+temp[0])
        attr[img_path+temp[0]]=torch.Tensor([int((int(temp[attribute+1])+1)/2),  int((int(temp[protected_attribute+1])+1)/2)])

    normalize = T.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])

    if augment:
        transform = T.Compose([
            T.Resize(64),
            T.Resize(256),
            T.RandomCrop(224),
            T.RandomHorizontalFlip(),
            T.ToTensor(),
            normalize
        ])
    else:
        transform = T.Compose([
            T.Resize(64),
            T.Resize(256),
            T.FiveCrop(224),
            #Lambda(lambda crops: torch.stack([ToTensor()(crop) for crop in crops])),
            #normalize
            transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
            transforms.Lambda(lambda tensors: torch.stack([transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(t) for t in tensors]))
        ])

    #print(transform)
    dset = dataset(list_ids, attr, transform)
    loader = DataLoader(dset, **params)

    return loader
def preprocess_line_input(pil_im, scale, shift=None):
    """ produces five copies of the segment at slightly different offsets

    :param pil_im: tablet segment that is to be processed
    :param scale: scale which should be used for resizing
    :param shift: offset shift used to produce five-fold oversampling
    :return: 4D tensor with 5xCxWxH
    """
    if shift is None:
        shift = 0  # cfg.TEST.SHIFT
    # compute scaled size
    imw, imh = pil_im.size
    imw = int(imw * scale)
    imh = int(imh * scale)
    # determine crop size
    crop_sz = [int(imw - shift), int(imh - shift)]
    # tensor-space transforms
    ts_transform = trafos.Compose([
        trafos.ToTensor(),
        trafos.Normalize(mean=[0.5], std=[1]),  # normalize
    ])
    # compose transforms
    tablet_transform = trafos.Compose([
        trafos.Lambda(lambda x: x.convert('L')),  # convert to gray
        trafos.Resize((imh, imw)),  # resize according to scale
        trafos.FiveCrop((crop_sz[1], crop_sz[0])),  # oversample
        trafos.Lambda(lambda crops: torch.stack(
            [ts_transform(crop) for crop in crops])),  # returns a 4D tensor
    ])
    # apply transforms
    im_list = tablet_transform(pil_im)
    return im_list
Beispiel #20
0
 def __init__(self,
              folder,
              augment=False,
              normalization=True,
              is_crop=True):
     self.crop = transforms.FiveCrop(128)
     super().__init__(folder, augment, normalization, is_crop)
Beispiel #21
0
def get_loader(args, kwargs):
    warnings.filterwarnings('ignore')
    norm_mean = [0.485, 0.456, 0.406]
    norm_std = [0.229, 0.224, 0.225]
    loader_train = None

    if not args.test_only:
        transform_list = [
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize(norm_mean, norm_std)]

        if not args.no_flip:
            transform_list.remove(transform_list[1])
        
        transform_train = transforms.Compose(transform_list)

        loader_train = DataLoader(
            datasets.ImageFolder(
                root=os.path.join(args.dir_data, 'ILSVRC2012', 'train'),
                transform=transform_train),
            batch_size=args.batch_size, shuffle=True, **kwargs
        )


    transform_list = [transforms.Resize(256)]
    batch_test = 128

    if args.crop > 1:
        def _fused(pil):
            tensor = transforms.ToTensor()(pil)
            normalized = transforms.Normalize(norm_mean, norm_std)(tensor)

            return normalized

        if args.crop == 5:
            transform_list.append(transforms.FiveCrop(224))
            batch_test //= 5
        elif args.crop == 10:
            transform_list.append(transforms.TenCrop(224))
            batch_test //= 10

        transform_list.append(transforms.Lambda(
            lambda crops: torch.stack([_fused(crop) for crop in crops])))
    else:
        transform_list.append(transforms.CenterCrop(224))
        transform_list.append(transforms.ToTensor())
        transform_list.append(transforms.Normalize(norm_mean, norm_std))

    transform_test = transforms.Compose(transform_list)

    loader_test = DataLoader(
        datasets.ImageFolder(
            root=os.path.join(args.dir_data, 'ILSVRC2012', 'val'),
            transform=transform_test),
        batch_size=batch_test, shuffle=False, **kwargs
    )

    return loader_train, loader_test
def get_transformer(opt):
    transform_list = []

    # resize
    osize = [opt.load_size, opt.load_size]
    transform_list.append(transforms.Resize(osize, Image.BICUBIC))

    # grayscale
    if opt.input_channel == 1:
        transform_list.append(transforms.Grayscale())

    # crop
    if opt.crop == "RandomCrop":
        transform_list.append(transforms.RandomCrop(opt.fineSize))
    elif opt.crop == "CenterCrop":
        transform_list.append(transforms.CenterCrop(opt.input_size))
    elif opt.crop == "FiveCrop":
        transform_list.append(transforms.FiveCrop(opt.input_size))
    elif opt.crop == "TenCrop":
        transform_list.append(transforms.TenCrop(opt.input_size))

    # flip
    if opt.mode == "Train" and opt.flip:
        transform_list.append(transforms.RandomHorizontalFlip())

    # to tensor
    transform_list.append(transforms.ToTensor())

    # If you make changes here, you should also modified
    # function `tensor2im` in util/util.py accordingly
    transform_list.append(transforms.Normalize(opt.mean, opt.std))

    return transforms.Compose(transform_list)
def FiveCropFull(shape):
    # If the augmented folder does not exist
    try:
        os.mkdir(args.output_dir)
    except OSError as error:
        print(error)
    transform = transforms.Compose([
        transforms.FiveCrop(shape),
        transforms.Lambda(lambda crops: torch.stack(
            [transforms.ToTensor()(crop) for crop in crops]))
    ])
    toPILImage = transforms.Compose([transforms.ToPILImage()])
    for specie in os.listdir(args.input_dir):
        specie_path = os.path.join(args.input_dir, specie)
        out_specie_path = os.path.join(args.output_dir, specie)
        # If the augmented specie folder does not exist
        try:
            os.mkdir(out_specie_path)
        except OSError as error:
            print(error)
        for filename in os.listdir(specie_path):
            print('FiveCropping:', os.path.join(specie_path, filename))
            image = Image.open(os.path.join(specie_path, filename))
            crops_t = transform(image)  # 4 dimensions tensor
            crops_ts = torch.split(crops_t, 1,
                                   0)  # List of 3 dimension tensors
            for i, img_t in enumerate(crops_ts):
                new_img_t = torch.squeeze(img_t)
                img = toPILImage(new_img_t)
                r_name = filename + '5_cropped_' + str(i)
                path = os.path.join(out_specie_path, r_name)
                print('Saving:', path + '.tif')
                img.save(fp=path + '.tif')
Beispiel #24
0
 def __init__(self, resize, mean, std):
     self.data_transform = {
         'train': transforms.Compose([
             transforms.RandomResizedCrop(
                 resize, scale=(0.5, 1.0)), # r*rに画素値を変形後、scaleで指定した比率に無作為に切り抜く
             transforms.RandomHorizontalFlip(),  # ランダムに水平方向に反転
             transforms.Lambda(gamma),  # ガンマ処理
             transforms.ToTensor(),  # テンソルに変換
             transforms.Normalize(mean, std)  # 標準化
         ]),
         'val': transforms.Compose([
             transforms.Resize(resize),  # 画素値の変更(トリミングの要素は無い点に注意!)
             transforms.CenterCrop(resize),  # 画像中央をresize×resizeで切り取り
             transforms.Lambda(gamma),  # ガンマ処理
             transforms.FiveCrop(120),
             # 処理後->[5, 3,r,r]
             # stack : リストを渡すと結合する
             transforms.Lambda(lambda crops : torch.stack( 
             [transforms.Normalize(mean, std)(
                     transforms.ToTensor()(crop)
                         )for crop in crops
             ])
             )
             #transforms.ToTensor(),  # テンソルに変換
             #transforms.Normalize(mean, std)  # 標準化
         ])
     }
Beispiel #25
0
def process_image(image):
    """
    This function takes as argument an image uploaded through an html form,
    and return it ready for model submission and prediction.
    """

    img = Image.open(image)

    input_tr = transforms.Compose([
        transforms.Resize(256),
        transforms.FiveCrop(224),
        transforms.Lambda(lambda crops: torch.stack([
            transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
            ])(crop) for crop in crops
        ]))
    ])

    data = input_tr(img)

    print("Image processed for prediction...")

    return data
 def __init__(self, file_path, size):
     """
     file_path (string): 测试样本的路径
     size (tuple): 裁剪的大小,是一个tuple,其中包含(H,W)
     """
     self.file_path = file_path
     # H x W
     self.five_crop = transforms.FiveCrop(size)
    def __call__(self, images):
        if not isinstance(images, list):
            images = [images]
        crops = []
        for image in images:
            crops.extend(tsfm.FiveCrop(self.size)(image))

        return crops
 def __init__(self, crop_size):
     self.transformer = transforms.Compose([
         transforms.FiveCrop(crop_size),
         transforms.Lambda(lambda crops: torch.stack(
             [self.to_tensor(crop) for crop in crops])),
         transforms.Lambda(lambda crops: torch.stack(
             [self.normalize(crop) for crop in crops]))
     ])
Beispiel #29
0
def run():
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size', type=int, default=32, metavar='N',
                        help='input batch size for training (default: 32)')

    parser.add_argument('--test-batch-size', type=int, default=32, metavar='N',
                        help='input batch size for testing (default: 1000)')

    parser.add_argument('--epochs', type=int, default=15, metavar='N',
                        help='number of epochs to train (default: 15)')
    
    parser.add_argument('--lr', type=float, default=0.001, metavar='LR',
                        help='learning rate (default: 0.001)')
    
    parser.add_argument('--momentum', type=float, default=0.5, metavar='M',
                        help='SGD momentum (default: 0.5)')
    
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='disables CUDA training')
    
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')
    
    parser.add_argument('--log-interval', type=int, default=10, metavar='N',
                        help='how many batches to wait before logging training status')

    parser.add_argument('--mode', type=str, default='A', metavar='M',
                        help='Mode of model')

    args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    torch.manual_seed(args.seed)
    device = torch.device("cuda" if use_cuda else "cpu")

    root = './'
    model_path = './results/pascalvoc_A.pt'

    test_transform = transforms.Compose([
                transforms.Resize(256),
                transforms.FiveCrop(224),
                transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])), 
                transforms.Lambda(lambda crops: torch.stack([transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(crop) for crop in crops])), 
                ])

    #Get dataset and input into Dataloader
    test_loader = torch.utils.data.DataLoader(
        VOCDataset(root, 'val', transform = test_transform),
        batch_size=args.test_batch_size, shuffle=False)

    test_loss_function = F.binary_cross_entropy_with_logits
    
    #Define Model
    model = load_model(model_path)
    model = model.to(device)

    val_loss, val_acc, output = test(args, model, device, test_loader, test_loss_function)

    torch.save(output, 'val_set_results.pt')
Beispiel #30
0
def get_test_data(data_path):
    with open(data_path, 'rb') as f:
        train_test_paths_labels = pickle.load(f)

    test_paths = train_test_paths_labels[2]
    test_labels = train_test_paths_labels[5]
    test_num_each = train_test_paths_labels[8]

    print('test_paths   : {:6d}'.format(len(test_paths)))
    print('test_labels  : {:6d}'.format(len(test_labels)))

    test_labels = np.asarray(test_labels, dtype=np.int64)

    test_transforms = None
    if crop_type == 0:
        test_transforms = transforms.Compose([
            transforms.RandomCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.4310, 0.2971, 0.3126], [0.2405, 0.1863, 0.1935])
        ])
    elif crop_type == 1:
        test_transforms = transforms.Compose([
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.4310, 0.2971, 0.3126], [0.2405, 0.1863, 0.1935])
        ])
    elif crop_type == 5:
        test_transforms = transforms.Compose([
            transforms.FiveCrop(224),
            Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
            Lambda(
                lambda crops: torch.stack(
                    [transforms.Normalize([0.4310, 0.2971, 0.3126], [0.2405, 0.1863, 0.1935])(crop) for crop in crops]))
        ])
    elif crop_type == 10:
        test_transforms = transforms.Compose([
            transforms.TenCrop(224),
            Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
            Lambda(
                lambda crops: torch.stack(
                    [transforms.Normalize([0.4310, 0.2971, 0.3126], [0.2405, 0.1863, 0.1935])(crop) for crop in crops]))
        ])
    elif crop_type == 2:
        test_transforms = transforms.Compose([
            transforms.Resize((250, 250)),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.4310, 0.2971, 0.3126], [0.2405, 0.1863, 0.1935])
        ])
    elif crop_type == 3:
        test_transforms = transforms.Compose([
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize([0.4310, 0.2971, 0.3126], [0.2405, 0.1863, 0.1935])
        ])

    test_dataset = CholecDataset(test_paths, test_labels, test_transforms)
    return test_dataset, test_num_each