예제 #1
0
def transforms():
    for i in tqdm(glob.glob(os.path.join(opt.train_data_root, '*.bmp'))):
        uid = uuid.uuid1()
        profix = "NEG"
        img = Image.open(i)
        if 'POS' in i.split('/')[-1]:
            profix = "POS"
        p = profix + "-" + str(uid) + "1.bmp"
        data = T.RandomHorizontalFlip(p=1)(img)
        data.save(os.path.join("./data/train", p))
        p = profix + "-" + str(uid) + "2.bmp"
        data = T.RandomVerticalFlip(p=1)(img)
        data.save(os.path.join("./data/train", p))
        p = profix + "-" + str(uid) + "3.bmp"
        data = img.rotate(90)
        data.save(os.path.join("./data/train", p))
        p = profix + "-" + str(uid) + "4.bmp"
        data = img.rotate(270)
        data.save(os.path.join("./data/train", p))
        p = profix + "-" + str(uid) + "5.bmp"
        data = T.RandomRotation(90)(img)
        data.save(os.path.join("./data/train", p))
        p = profix + "-" + str(uid) + "6.bmp"
        data = T.RandomRotation(270)(img)
        data.save(os.path.join("./data/train", p))
예제 #2
0
def lfw(stage, configs=None, augment=False, tta=False, tta_size=48):
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    folder_path = os.path.join(configs["data_path"], f"{stage}")

    dataset = datasets.ImageFolder(
        folder_path,
        transforms.Compose([
            #transforms.RandomResizedCrop(224),
            #transforms.RandomHorizontalFlip(),
            transforms.Resize(224),
            transforms.ToTensor(),
            #normalize,
        ]),
    )
    if augment:
        dataset2 = datasets.ImageFolder(
            folder_path,
            transforms.Compose([
                transforms.Resize(224),
                transforms.RandomHorizontalFlip(),
                transforms.RandomRotation((1, 20)),
                transforms.ColorJitter(),
                transforms.ToTensor(),
            ]),
        )
        dataset3 = datasets.ImageFolder(
            folder_path,
            transforms.Compose([
                transforms.Resize(224),
                transforms.RandomHorizontalFlip(),
                transforms.RandomRotation((-20, -1)),
                transforms.ColorJitter(),
                transforms.ToTensor(),
            ]),
        )

        dataset4 = datasets.ImageFolder(
            folder_path,
            transforms.Compose([
                transforms.Resize(224),
                transforms.RandomHorizontalFlip(),
                transforms.RandomAffine(degrees=0, translate=(0.1, 0.1)),
                transforms.ColorJitter(),
                transforms.ToTensor(),
                AddGaussianNoise(0., 0.05),
            ]),
        )
        return torch.utils.data.ConcatDataset(
            [dataset4, dataset2, dataset3, dataset])
    else:
        return dataset
예제 #3
0
def get_mnist_loader(data_dir='dataset', batch_size=128, num_workers=4):
    transform = transforms.Compose([
        transforms.RandomRotation((-30, 30)),
        transforms.ToTensor(),
    ])
    return DataLoader(dataset=MNIST(root=data_dir, train=True, transform=transform, download=True), batch_size=batch_size,
                      shuffle=True, num_workers=num_workers)
예제 #4
0
 def get_simclr_pipeline_transform(size, s=1, num_aug=5):
     """Return a set of data augmentation transformations as described in the SimCLR paper."""
     color_jitter = transforms.ColorJitter(0.8 * s, 0.8 * s, 0.8 * s,
                                           0.2 * s)
     if num_aug == 5:
         data_transforms = transforms.Compose([
             transforms.RandomResizedCrop(size=size),
             transforms.RandomHorizontalFlip(),
             transforms.RandomApply([color_jitter], p=0.8),
             transforms.RandomGrayscale(p=0.2),
             GaussianBlur(kernel_size=int(0.1 * size)),
             transforms.ToTensor()
         ])
     elif num_aug == 7:
         data_transforms = transforms.Compose([
             transforms.RandomResizedCrop(size=size),
             transforms.RandomHorizontalFlip(),
             transforms.RandomApply([color_jitter], p=0.8),
             transforms.RandomGrayscale(p=0.2),
             GaussianBlur(kernel_size=int(0.1 * size)),
             transforms.RandomRotation(degrees=45),
             transforms.RandomAffine(degrees=45),
             transforms.ToTensor()
         ])
     return data_transforms
예제 #5
0
def get_transforms():
    tsfm = []
    if rcp.transforms.topilimage: tsfm += [transforms.ToPILImage()]
    if rcp.transforms.randomrotation:
        tsfm += [transforms.RandomRotation(rcp.transforms.randomrotation)]
    if rcp.transforms.randomverticalflip:
        tsfm += [
            transforms.RandomVerticalFlip(rcp.transforms.randomverticalflip)
        ]
    if rcp.transforms.randomhorizontalflip:
        tsfm += [
            transforms.RandomHorizontalFlip(
                rcp.transforms.randomhorizontalflip)
        ]
    if rcp.transforms.colorjitter:
        tsfm += [transforms.ColorJitter(**rcp.transforms.colorjitter)]

    # if rcp.transforms.randomcrop: tsfm += [transforms.RandomCrop(rcp.transforms.randomcrop)]
    if rcp.transforms.resize:
        tsfm += [
            transforms.Resize((rcp.transforms.resize, rcp.transforms.resize))
        ]
    if rcp.transforms.totensor: tsfm += [transforms.ToTensor()]
    if rcp.transforms.normalize:
        tsfm += [transforms.Normalize(**rcp.transforms.normalize)]

    return transforms.Compose(tsfm)
예제 #6
0
def get_dataset_loaders(
        dataset_directory,
        minibatch_size) -> Tuple[DataLoader, DataLoader, DataLoader]:
    data_transform = transforms.Compose([
        transforms.RandomRotation(10),
        transforms.Resize((224, 224)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor()
    ])

    number_of_workers = 8
    training_dataset = ImageFolder(root=os.path.join(dataset_directory,
                                                     "training"),
                                   transform=data_transform)
    training_dataset_loader = DataLoader(training_dataset,
                                         batch_size=minibatch_size,
                                         shuffle=True,
                                         num_workers=number_of_workers)
    validation_dataset = ImageFolder(root=os.path.join(dataset_directory,
                                                       "validation"),
                                     transform=data_transform)
    validation_dataset_loader = DataLoader(validation_dataset,
                                           batch_size=minibatch_size,
                                           shuffle=False,
                                           num_workers=number_of_workers)
    testing_dataset = ImageFolder(root=os.path.join(dataset_directory, "test"),
                                  transform=data_transform)
    testing_dataset_loader = DataLoader(testing_dataset,
                                        batch_size=minibatch_size,
                                        shuffle=False,
                                        num_workers=number_of_workers)

    return training_dataset_loader, validation_dataset_loader, testing_dataset_loader
예제 #7
0
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
예제 #8
0
def build_dataset(config):
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])

    size = config['image_size']
    train_transforms = transforms.Compose([
          transforms.RandomResizedCrop(size=size, scale=(0.8, 1.0)),
          transforms.RandomRotation(degrees=5),
          transforms.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.1),
          transforms.RandomHorizontalFlip(),
          transforms.ToTensor(),
          normalize
      ])

    val_transforms = transforms.Compose([
        transforms.Resize(size=(size, size)),
        transforms.ToTensor(),
        normalize
    ])

    dsTrain = ImageFolder('data/train/', train_transforms, target_transform=target_transform)
    dsVal = ImageFolder('data/test/', val_transforms, target_transform=target_transform)
    #dsTest = ImageFolder('data/WildFire/test/', val_transforms, target_transform=target_transform)
   
    train_loader = DataLoader(dsTrain, batch_size=config['batch_size'], shuffle=True)
    val_loader = DataLoader(dsVal, batch_size=config['batch_size'], shuffle=True)
    #test_loader = DataLoader(dsTest, batch_size=config['batch_size'], shuffle=True)

    return train_loader, val_loader
예제 #9
0
    def __init__(self, args):
        self.args = args
        self.rvs = args.rvs
        self.insp = args.insp
        self.inch = args.inch
        self.outf = args.outf

        self.transformer = transforms.RandomChoice([
            transforms.Compose([transforms.ToPILImage(), transforms.ColorJitter(brightness=0.1), transforms.ToTensor()]),
            transforms.Compose([transforms.ToPILImage(), transforms.ColorJitter(contrast=0.1), transforms.ToTensor()]),
            transforms.Compose([transforms.ToPILImage(), transforms.ColorJitter(saturation=0.1), transforms.ToTensor()]),
            transforms.Compose([transforms.ToPILImage(), transforms.RandomRotation(degrees=10, resample=False, expand=False, center=None), transforms.ToTensor()]),
            transforms.Compose([transforms.ToPILImage(), transforms.RandomHorizontalFlip(p=1), transforms.ToTensor()]),
        ])

        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        self.D = D(insp=args.insp, inch=args.inch, outf=args.outf).to(self.device)
        self.G = G(rvs=args.rvs).to(self.device)
        self.criterion = nn.BCELoss()
        self.optimizerD = torch.optim.Adam(self.D.parameters(), lr=args.lr, betas=(args.beta, 0.999))
        self.optimizerG = torch.optim.Adam(self.G.parameters(), lr=args.lr, betas=(args.beta, 0.999))

        self.test_noise = torch.randn(args.grid_w * args.grid_h, args.rvs, 1, 1).to(device=self.device)
        self.real_label = 1
        self.fake_label = 0
예제 #10
0
 def __getitem__(self, idx):
     img = self.load_image(idx)
     img = transforms.RandomRotation(self.degree)(img)
     root = self.label.loc[idx]['grapheme_root']
     consonant = self.label.loc[idx]['consonant_diacritic']
     vowel = self.label.loc[idx]['vowel_diacritic']
     return transforms.ToTensor()(img), root, consonant, vowel
예제 #11
0
    def __init__(self, hparams):
        super().__init__()

        self.data_dir = hparams["data_dir"] + "/snake_dataset/train"
        self.batch_size = hparams.get("batch_size") or 64
        self.train_val_split_ratio = hparams.get("train_val_split_ratio") or 0.9
        self.num_workers = hparams.get("num_workers") or 1
        self.pin_memory = hparams.get("pin_memory") or False

        img_augmentation_transformations = [
            # transforms.RandomAffine((-15, 15), translate=(0.2, 0.2)),
            transforms.RandomHorizontalFlip(p=1.0),
            transforms.RandomRotation((-15, 15)),
            transforms.ColorJitter(hue=.05, saturation=.05),
        ]

        self.transforms = transforms.Compose([
            transforms.Resize((112, 112)),
            transforms.ToTensor(),
            transforms.RandomApply(img_augmentation_transformations, 0.5),
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
         ])

        self.data_train = None
        self.data_val = None
def build_data_loader():
    transform = transforms.Compose([
        transforms.ColorJitter(brightness=0.1,
                               contrast=0.1,
                               saturation=0.1,
                               hue=0.1),
        transforms.RandomRotation(90),
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
    ])
    dataset = PlantSeedlingsDataset(os.environ['DATA_ROOT'],
                                    transforms=transform)
    train_set_size = len(dataset) * 7 // 10
    val_set_size = len(dataset) - train_set_size
    train_set, val_set = random_split(dataset, [train_set_size, val_set_size])
    data_loader = {
        'train':
        DataLoader(train_set,
                   batch_size=BATCH_SIZE,
                   shuffle=True,
                   num_workers=NUM_WORKERS),
        'val':
        DataLoader(val_set,
                   batch_size=BATCH_SIZE,
                   shuffle=False,
                   num_workers=NUM_WORKERS),
    }
    return data_loader
예제 #13
0
def get_train_trans(SIZE):
    return transforms.Compose([
        transforms.CenterCrop(SIZE),
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(5),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0, 0, 0], std=[0.3, 0.2, 0.1])
    ])
예제 #14
0
def hard_augment():
    hard_augment_ = transforms.Compose([
        transforms.RandomHorizontalFlip(p=0.5),
        transforms.RandomRotation(degrees=(-90, 90)),
        transforms.RandomVerticalFlip(p=0.5),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
    ])
    return hard_augment_
예제 #15
0
    def rotate_img(img: torch.Tensor) -> (torch.Tensor, float):
        angle = torch.rand(1) * 360
        rotate_transform = transforms.Compose([
            transforms.ToPILImage(),
            transforms.RandomRotation((angle, angle)),
            transforms.ToTensor()
        ])

        return rotate_transform(img), angle.item()
예제 #16
0
 def __init__(self, resize_to=(224, 224)):
     list_of_transforms = [
         transforms.RandomVerticalFlip(),
         transforms.RandomRotation(degrees=(-180, 180)),
         transforms.Resize(size=resize_to),
         transforms.ToTensor(),
         transforms.Normalize(mean=[0.485, 0.456, 0.406],
                              std=[0.229, 0.224, 0.225])
     ]
     self.transform = transforms.Compose(list_of_transforms)
예제 #17
0
 def __getitem__(self, idx):
     img = self.load_image(idx)
     img = transforms.RandomRotation(15)(img)
     root = self.label.loc[idx]['grapheme_root']
     consonant = self.label.loc[idx]['consonant_diacritic']
     vowel = self.label.loc[idx]['vowel_diacritic']
     timer = time.time()
     unique = np.where(self.uniques == self.label.grapheme[idx])[0][0]
     #print('unique: ', time.time() - timer)
     return transforms.ToTensor()(img), root, consonant, vowel, unique
예제 #18
0
def get_train_trans(SIZE):
    return transforms.Compose([
        transforms.Resize((SIZE, SIZE)),
        transforms.RandomHorizontalFlip(),
        transforms.RandomVerticalFlip(),
        transforms.RandomRotation(5),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])
예제 #19
0
class Config:
    data_root = args.path
    training_size = 12500
    test_size = 12500
    lexpert = [[.55, .45], [.45, .55]]
    if args.expertise == 0:
        if args.case == 1:
            expert_num = 10
        elif args.case == 2:
            expert_num = 25
        elif args.case == 3:
            expert_num = 12
        as_expertise = np.array([
            lexpert, lexpert, lexpert, lexpert, lexpert, lexpert, lexpert,
            lexpert, lexpert, lexpert, lexpert, lexpert, lexpert, lexpert,
            lexpert
        ])
    elif args.expertise == 1:
        if args.case == 1:
            expert_num = 5
        elif args.case == 2:
            expert_num = 10
        elif args.case == 3:
            expert_num = 10
        as_expertise = np.array([[[0.6, 0.4], [0.1, 0.9]],
                                 [[0.8, 0.2], [0.4, 0.6]],
                                 [[0.6, 0.4], [0.4, 0.6]],
                                 [[0.7, 0.3], [0.3, 0.7]],
                                 [[0.7, 0.3], [0.4, 0.6]]])

    missing_label = np.array([0, 0, 0, 0, 0])
    missing = False
    num_classes = 2
    left_input_size = 28 * 28
    batch_size = 16
    left_learning_rate = 1e-4
    right_learning_rate = 1e-4
    epoch_num = 20
    device_id = args.device
    experiment_case = args.case

    train_transform = transforms.Compose([
        transforms.Resize((150, 150), interpolation=2),
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(45),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
    ])
    test_transform = transforms.Compose([
        transforms.Resize((150, 150), interpolation=2),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
    ])
예제 #20
0
    def __init__(self, image_file, autoaugment=False):
        super(ImageNetTrainingDataset, self).__init__()
        self.image_file = image_file
        # self.data = None
        with open(self.image_file, "r") as file:
            self.data = file.readlines()
        # shuffle the dataset
        for i in range(10):
            random.shuffle(self.data)
        self.imagenet_normalization_paramters = transforms.Normalize(
            mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        # 先resize到512 再crop到448
        # 用config来指定模型的size
        self.model_size = ModelSize("resnet50_448")
        model_size = self.model_size.imagesize_choice()
        self.BASE_RESIZE_SIZE = model_size["resize"]
        self.INPUT_SIZE = model_size["input"]
        self.BRIGHTNESS = 0.4
        self.HUE = 0.1
        self.CONTRAST = 0.4
        self.SATURATION = 0.4

        # autoaugment
        self.Autoaugment = autoaugment

        self.index_sampler = [i for i in range(len(self.data))]

        # 当前的数据增强【随机crop, 随机水平翻转,颜色变换,随机灰度,】
        if self.Autoaugment:
            self.image_transforms = transforms.Compose([
                transforms.Resize(
                    (self.BASE_RESIZE_SIZE, self.BASE_RESIZE_SIZE),
                    Image.BILINEAR),
                transforms.RandomCrop(self.INPUT_SIZE),
                transforms.RandomHorizontalFlip(),
                ImageNetPolicy(),
                transforms.ToTensor(), self.imagenet_normalization_paramters
            ])
        else:
            self.image_transforms = transforms.Compose([
                # transforms.RandomResizedCrop(self.INPUT_SIZE, scale=(0.2, 1.)),
                transforms.Resize(
                    (self.BASE_RESIZE_SIZE, self.BASE_RESIZE_SIZE),
                    Image.BILINEAR),
                transforms.RandomCrop(self.INPUT_SIZE),
                transforms.RandomHorizontalFlip(),
                transforms.RandomRotation(degrees=15),
                transforms.ColorJitter(brightness=self.BRIGHTNESS,
                                       contrast=self.CONTRAST,
                                       hue=self.HUE,
                                       saturation=self.SATURATION),
                transforms.ToTensor(),
                self.imagenet_normalization_paramters
            ])
예제 #21
0
def medium_augment():
    medium_augment_ = transforms.Compose([
        transforms.CenterCrop((100, 100)),
        transforms.RandomCrop((80, 80)),
        transforms.RandomHorizontalFlip(p=0.5),
        transforms.RandomRotation(degrees=(-90, 90)),
        transforms.RandomVerticalFlip(p=0.5),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
    ])
    return medium_augment_
예제 #22
0
def preprocess_with_augmentation_without_norm(image_size):
    return transforms.Compose([
        transforms.Resize((image_size + 20, image_size + 20)),
        transforms.RandomRotation(15, expand=True),
        transforms.RandomCrop((image_size, image_size)),
        transforms.RandomHorizontalFlip(),
        transforms.ColorJitter(brightness=0.4,
                               contrast=0.4,
                               saturation=0.4,
                               hue=0.2),
        transforms.ToTensor()
    ])
예제 #23
0
def gen_dataloaders(indir,
                    val_split=0.05,
                    shuffle=True,
                    batch_size=4,
                    seed=42,
                    img_size=224,
                    cuda=True):
    data_transforms = {
        'train':
        transforms.Compose([
            # transforms.ColorJitter(0.3, 0.3, 0.3, 0.3),
            transforms.RandomResizedCrop(img_size, scale=(0.1, 1.0)),
            transforms.RandomAffine(10.),
            transforms.RandomRotation(13.),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
        ]),
        'valid':
        transforms.Compose([
            transforms.Resize((img_size, img_size)),
            transforms.ToTensor(),
        ])
    }

    mask_train_dataset = MaskDataset(path=indir,
                                     transform=data_transforms['train'])
    mask_valid_dataset = MaskDataset(path=indir,
                                     transform=data_transforms['valid'])

    # Creating data indices for training and validation splits:
    dataset_size = len(mask_train_dataset)
    indices = list(range(dataset_size))
    split = int(np.floor(val_split * dataset_size))
    if shuffle:
        np.random.seed(seed)
        np.random.shuffle(indices)
    train_indices, val_indices = indices[split:], indices[:split]

    # Creating data samplers and loaders
    train_sampler = SubsetRandomSampler(train_indices)
    valid_sampler = SubsetRandomSampler(val_indices)

    kwargs = {'num_workers': 4, 'pin_memory': True} if cuda else {}

    train_loader = torch.utils.data.DataLoader(mask_train_dataset,
                                               batch_size=batch_size,
                                               sampler=train_sampler,
                                               **kwargs)
    valid_loader = torch.utils.data.DataLoader(mask_valid_dataset,
                                               batch_size=batch_size,
                                               sampler=valid_sampler,
                                               **kwargs)
    return train_loader, valid_loader
예제 #24
0
def eyetransfrom(imsize=224):

    eyetrans = transforms.Compose([
        transforms.RandomHorizontalFlip(),
        transforms.RandomVerticalFlip(),
        transforms.RandomRotation(degrees=(0, 360)),
        transforms.Resize(imsize),
        transforms.CenterCrop(imsize),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
    return eyetrans
예제 #25
0
def main():
    args = args_parser()
    utils.delete_path(args.log_dir)
    utils.ensure_path(args.save_dir)
    utils.ensure_path(args.log_dir)

    utils.write_dict(vars(args), os.path.join(args.save_dir, 'arguments.csv'))

    torch.manual_seed(args.seed)
    cudnn.benchmark = True
    torch.cuda.manual_seed_all(args.seed)
    os.environ['CUDA_VISIBLE_DEVICES'] = args.device_ids

    train_transformer = transforms.Compose([
        transforms.Resize(size=(256, 256)),
        transforms.RandomCrop(size=image_size),
        transforms.RandomRotation(10),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
    ])
    train_dataset = ZSLDataset(args.image_dir, args.train_file,
                               args.embed_file, args.attribute_file,
                               args.lable_name_file)
    train_dataloader = train_dataset.get_dataloader(batch_size=args.batch_size,
                                                    num_workers=args.nw,
                                                    shuffle=True)

    val_transformer = transforms.Compose([
        transforms.Resize(size=image_size),
        # transforms.CenterCrop(size=image_size),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
    ])
    val_dataset = ZSLDataset(args.image_dir, args.val_file, args.embed_file,
                             args.attribute_file, args.lable_name_file)
    val_dataloader = val_dataloader.get_dataloader(batch_size=args.batch_size,
                                                   num_workers=args.nw,
                                                   shuffle=False)

    print(vars(args))

    num_classes = train_dataset.get_class_size()

    train_logger = SummaryLogger(args.log_dir)

    trainer = FeatTrainer(num_classes,
                          backbone=args.backbone,
                          input_size=image_size,
                          train_logger=train_logger,
                          opt=args)
    trainer.train(train_dataloader, val_dataloader)
def get_ood_dataloader(data_manager, batch_size: int = 16):
    """get_ood_dataloader [Returns OOD dataloader for Outlier exposure experiment]
    Args:
        data_manager ([object]): [data_manager]
        batch_size (int, optional): [batch size for dataloader]. Defaults to 16.

    Returns:
        [torch.Dataloader]: [train loader]
        [torch.Dataloader]: [ood lloader]
    """
    train_X, train_y = data_manager.get_train_data()
    ood_X, ood_y = data_manager.get_ood_data()

    train_y = np.ones_like(train_y)
    ood_y = np.zeros_like(ood_y)

    train_X, train_y = train_X.astype(np.float32), train_y.astype(np.float32)
    ood_X, ood_y = ood_X.astype(np.float32), ood_y.astype(np.float32)

    train_X, train_y = torch.from_numpy(train_X), torch.from_numpy(train_y)
    ood_X, ood_y = torch.from_numpy(ood_X), torch.from_numpy(ood_y)

    train_dataset = DataHandler_For_Arrays(train_X, train_y)

    transform_ood = transforms.Compose([
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
        transforms.RandomCrop(size=32),
        transforms.RandomRotation(degrees=(0, 180)),
        transforms.RandomHorizontalFlip(),
    ])
    outlier_data = DataHandler_For_Arrays(train_X,
                                          train_y,
                                          transform=transform_ood,
                                          num_classes=1)

    train_loader = DataLoader(
        train_dataset,
        sampler=RandomSampler(train_dataset),
        batch_size=batch_size * 4,
        num_workers=2,
        pin_memory=pin_memory,
    )

    outlier_loader = DataLoader(
        outlier_data,
        sampler=RandomSampler(outlier_data),
        batch_size=batch_size,
        num_workers=2,
        pin_memory=pin_memory,
    )
    return train_loader, outlier_loader
예제 #27
0
def generate_transform(transform_dict):
    transform_list = []
    for k, v in transform_dict.items():
        if k.lower() == 'resize':
            transform_list.append(transforms.Resize(v))
        elif k.lower() == 'resize_nearest':
            transform_list.append(
                transforms.Resize(v, interpolation=Image.NEAREST))
        elif k.lower() == 'crop':
            transform_list.append(transforms.RandomCrop(v))
        elif k.lower() == 'resized_crop':
            assert len(v) == 4
            h, w, min_scale = v
            transform_list.append(
                transforms.RandomResizedCrop((h, w), (min_scale, 1.)))
        elif k.lower() == 'center_crop':
            transform_list.append(transforms.CenterCrop(v))
        elif k.lower() == 'center_resize':
            transform_list.append(custom_transforms.CenterResize(v))
        elif k.lower() == 'resize_center':
            transform_list.append(
                custom_transforms.CenterResize(v, resize_first=True))
        elif k.lower() == 'h_flip':
            transform_list.append(transforms.RandomHorizontalFlip())
        elif k.lower() == 'rotate':
            transform_list.append(transforms.RandomRotation(v))
        elif k.lower() == 'color_jitter':
            assert len(v) == 4
            brightness, contrast, saturation, hue = v
            transform_list.append(
                transforms.ColorJitter(brightness, contrast, saturation, hue))
        elif k.lower() == 'to_tensor':
            transform_list.append(transforms.ToTensor())
        elif k.lower() == 'normalize':
            if v is None:
                norm_value = ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
            elif len(v) == 6:
                assert len(v) == 6
                norm_value = ([v[0], v[1], v[2]], [v[3], v[4], v[5]])
            elif len(v) == 2:
                norm_value = ([v[0]], [v[1]])
            else:
                raise RuntimeError('Invalid arguments for normalize transform')

            transform_list.append(
                transforms.Normalize(mean=norm_value[0], std=norm_value[1]))
        else:
            print('Unsupported data transform type: {}'.format(k))

    return transforms.Compose(transform_list)
예제 #28
0
	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('%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.RandomResizedCrop(self.resize, scale=(0.8, 1.0)),
			                                     transforms.RandomHorizontalFlip(),
			                                     # transforms.RandomVerticalFlip(),
			                                     transforms.RandomRotation(15),
			                                     transforms.ColorJitter(0.1, 0.1, 0.2, 0),
			                                     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)
예제 #29
0
def get_transformations(mean, std) -> Tuple[any, any]:
    # normalization values for pretrained resnet on Imagenet
    transform_train = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(degrees=60),
        transforms.ToTensor(),
        transforms.Normalize(mean, std),
    ])
    transform_test = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean, std),
    ])
    return transform_train, transform_test
예제 #30
0
    def __init__(self, data_dir, batch_size, labeled_percents=20):
        self.mean = [0.485, 0.456, 0.406]
        self.std = [0.229, 0.224, 0.225]
        self.train_path = os.path.join(data_dir, "train")
        self.classes = []
        for d in os.listdir(self.train_path):
            if ".txt" not in d:
                self.classes.append(d)

        self.transform = T.Compose([
            T.Resize((128, 128), interpolation=2),
            T.RandomRotation(45),
            T.RandomVerticalFlip(),
            T.RandomGrayscale(),
            T.RandomSizedCrop((112, 112)),
            T.ToTensor(),
            T.Normalize(self.mean, self.std)
        ])

        self.paths = []
        self.true_labels = []
        self.true_idxs = []
        self.relabeled = []
        self.labeled_idxs = []
        self.unlabeled_idxs = []
        ck = 0
        last_cls = None
        for i, c in enumerate(self.classes):
            for file in list(
                    glob.glob(
                        os.path.join(self.train_path, os.path.join(c,
                                                                   "*.*")))):
                self.paths.append(file)
                self.true_labels.append(c)
                self.true_idxs.append(i)
                self.relabeled.append(i)
            unlabeled_limit = int(
                (len(self.true_idxs) - ck) * labeled_percents)
            for idx in range(ck, ck + unlabeled_limit):
                self.relabeled[idx] = NO_LABEL
        # collect indexes of labels both labeled and unlabeled
        for i, l in enumerate(self.relabeled):
            if l == -1:
                self.unlabeled_idxs.append(i)
            else:
                self.labeled_idxs.append(i)