def build_dataset(config): normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) img_size = config['image_size'] train_transforms = transforms.Compose([ transforms.RandomResizedCrop(size=img_size, scale=(0.24, 0.25)), transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.05), transforms.RandomHorizontalFlip() ]) val_transforms = transforms.Compose( [transforms.RandomResizedCrop(size=img_size, scale=(0.24, 0.25))]) dsTrain = LasinkSimulation('data/nvidia_v2/train/', train_transforms) dsVal = LasinkSimulation('data/nvidia_v2/test/', val_transforms) train_loader = DataLoader(dsTrain, batch_size=config['batch_size'], shuffle=True, drop_last=True) val_loader = DataLoader(dsVal, batch_size=config['batch_size'], shuffle=True) return train_loader, val_loader
def get_simclr_data_transforms_train(dataset_name): if dataset_name == "stl10": input_shape = (96, 96, 3) s = 1 # get 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) return transforms.Compose([ transforms.RandomResizedCrop(size=input_shape[0]), transforms.RandomHorizontalFlip(), transforms.RandomApply([color_jitter], p=0.8), transforms.RandomGrayscale(p=0.2), GaussianBlur(kernel_size=int(0.1 * input_shape[0])), transforms.ToTensor() ]) elif dataset_name == "cifar10": return transforms.Compose([ transforms.RandomResizedCrop(32), transforms.RandomHorizontalFlip(p=0.5), transforms.RandomApply( [transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)], p=0.8), transforms.RandomGrayscale(p=0.2), transforms.ToTensor(), transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) ]) else: raise RuntimeError(f"unknown dataset: {dataset_name}")
def __init__(self): self.transform = transforms.Compose([ transforms.RandomResizedCrop(224, interpolation=Image.BICUBIC), transforms.RandomHorizontalFlip(p=0.5), transforms.RandomApply([ transforms.ColorJitter( brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1) ], p=0.8), transforms.RandomGrayscale(p=0.2), GaussianBlur(p=1.0), Solarization(p=0.0), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) self.transform_prime = transforms.Compose([ transforms.RandomResizedCrop(224, interpolation=Image.BICUBIC), transforms.RandomHorizontalFlip(p=0.5), transforms.RandomApply([ transforms.ColorJitter( brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1) ], p=0.8), transforms.RandomGrayscale(p=0.2), GaussianBlur(p=0.1), Solarization(p=0.2), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])
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
def __load_the_data(train_dir, test_dir, batch_size, data_augmentation): # Assign transform values with respect to the data augmentation strategy if data_augmentation: # Set normalization metrics for the data loaders normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) train_transform_list = [ transforms.RandomHorizontalFlip(), transforms.RandomResizedCrop(224), transforms.ToTensor(), normalize, ] test_transform_list = [ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize, ] else: # Set normalization metrics for the data loaders normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) train_transform_list = [ transforms.RandomResizedCrop(224), transforms.ToTensor(), normalize, ] test_transform_list = [ transforms.CenterCrop(224), transforms.ToTensor(), normalize, ] # Load the train directory and shuffle the images train_loader = torch.utils.data.DataLoader( datasets.ImageFolder(train_dir, transforms.Compose(train_transform_list)), batch_size=batch_size, shuffle=True, ) # Load the test directory test_loader = torch.utils.data.DataLoader( datasets.ImageFolder(test_dir, transforms.Compose(test_transform_list)), batch_size=batch_size, ) return train_loader, test_loader
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
def prepare_data_loaders(data_path): normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) dataset = torchvision.datasets.ImageNet( data_path, split="train", transform=transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize ])) dataset_test = torchvision.datasets.ImageNet( data_path, split="val", transform=transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize ])) train_sampler = torch.utils.data.RandomSampler(dataset) test_sampler = torch.utils.data.SequentialSampler(dataset_test) data_loader = torch.utils.data.DataLoader(dataset, batch_size=train_batch_size, sampler=train_sampler) data_loader_test = torch.utils.data.DataLoader(dataset_test, batch_size=eval_batch_size, sampler=test_sampler) return data_loader, data_loader_test
def __init__( self, crop_size, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), interpolation=InterpolationMode.BILINEAR, hflip_prob=0.5, auto_augment_policy=None, random_erase_prob=0.0, ): trans = [transforms.RandomResizedCrop(crop_size, interpolation=interpolation)] if hflip_prob > 0: trans.append(transforms.RandomHorizontalFlip(hflip_prob)) if auto_augment_policy is not None: if auto_augment_policy == "ra": trans.append(autoaugment.RandAugment(interpolation=interpolation)) elif auto_augment_policy == "ta_wide": trans.append(autoaugment.TrivialAugmentWide(interpolation=interpolation)) else: aa_policy = autoaugment.AutoAugmentPolicy(auto_augment_policy) trans.append(autoaugment.AutoAugment(policy=aa_policy, interpolation=interpolation)) trans.extend( [ transforms.PILToTensor(), transforms.ConvertImageDtype(torch.float), transforms.Normalize(mean=mean, std=std), ] ) if random_erase_prob > 0: trans.append(transforms.RandomErasing(p=random_erase_prob)) self.transforms = transforms.Compose(trans)
def inception_preproccess(input_size, normalize=__imagenet_stats): return transforms.Compose([ transforms.RandomResizedCrop(input_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(**normalize) ])
def __init__(self, data_folder="./data", cudaready=False): self.cuda = cudaready self.cnn_model = FlowerClassifierCNNModel() if (self.cuda): self.cnn_model.cuda() self.optimizer = Adam(self.cnn_model.parameters()) if (self.cuda): self.loss_fn = nn.CrossEntropyLoss().cuda() else: self.loss_fn = nn.CrossEntropyLoss() # load data self.transformations = transforms.Compose([ transforms.RandomResizedCrop(64), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) total_dataset = datasets.ImageFolder(data_folder, transform=self.transformations) dataset_loader = DataLoader(dataset=total_dataset, batch_size=100) items = iter(dataset_loader) image, label = items.next() print(len(total_dataset)) train_size = int(0.8 * len(total_dataset)) test_size = len(total_dataset) - train_size train_dataset, test_dataset = random_split(total_dataset, [train_size, test_size]) self.train_dataset_loader = DataLoader(dataset=train_dataset, batch_size=100) self.test_dataset_loader = DataLoader(dataset=test_dataset, batch_size=100)
def load_hymenoptera(): data_dir = 'data/hymenoptera_data' train_transform = transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) data_transforms = { 'train': train_transform, 'val': transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]), } image_datasets = { x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'val'] } dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']} class_names = image_datasets['train'].classes dataloaders = { x: torch.utils.data.DataLoader(image_datasets[x], batch_size=4, shuffle=True, num_workers=4) for x in ['train', 'val'] } return dataloaders['train'], dataloaders['val'], class_names
def woof_preproccess(input_size, normalize=__imagenet_stats): return transforms.Compose([ transforms.RandomResizedCrop(input_size, scale=(0.35, 1.)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(**normalize) ])
def get_dataloader(data_root_dir): data_transforms = { 'train': transforms.Compose([ transforms.RandomResizedCrop(IMAGE_SIZE), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]), 'val': transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(IMAGE_SIZE), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]), } image_datasets = { x: datasets.ImageFolder(os.path.join(data_root_dir, x), data_transforms[x]) for x in ['train', 'val'] } dataloaders = { x: torch.utils.data.DataLoader(image_datasets[x], batch_size=BATCH_SIZE, shuffle=True, num_workers=4) for x in ['train', 'val'] } return dataloaders, image_datasets, image_datasets['train'].classes
def produce_benchmark_datatsets(root = '../data', num = 30): transform_b = transforms.Compose( [transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip()],) data_dir = '../data/hymenoptera_data/' dataset = ImageFolder(os.path.join(data_dir, 'val'), transform_b) benchmark_datasets_dir = os.path.join(root, 'ant_benchmark_data') label_dic = {} if os.path.exists(benchmark_datasets_dir) is False: os.mkdir(benchmark_datasets_dir) for i in range(num): print('saving image {}'.format(i)) img = transform_b(dataset[i+60][0]) label = dataset[i+60][1] print(label) label_dic[i] = label img.save(os.path.join(benchmark_datasets_dir, '{}.png'.format(i))) label_file = os.path.join(benchmark_datasets_dir, 'label.json') with open(label_file, 'a') as f: json.dump(label_dic, f) '''
def train_dataloader(self): _train_transforms = None if self.hparams.augment: _train_transforms = transforms.Compose([ transforms.RandomResizedCrop(size=(36, 60), scale=(0.5, 1.3)), transforms.RandomGrayscale(p=0.1), transforms.ColorJitter(brightness=0.5, hue=0.2, contrast=0.5, saturation=0.5), lambda x: x if np.random.random_sample() <= 0.1 else x.filter( ImageFilter.GaussianBlur(radius=3)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) _data_train = RTGENEH5Dataset(h5_file=h5py.File(self.hparams.hdf5_file, mode="r"), subject_list=self._train_subjects, transform=_train_transforms) return DataLoader(_data_train, batch_size=self.hparams.batch_size, shuffle=True, num_workers=self.hparams.num_io_workers, pin_memory=False)
def handle(self, source, copy_to_local=False, normalize=True, split=None, classification_mode=False, **transform_args): """ Args: source: copy_to_local: normalize: **transform_args: Returns: """ Dataset = self.make_indexing(CelebA) data_path = self.get_path(source) if copy_to_local: data_path = self.copy_to_local_path(data_path) if normalize and isinstance(normalize, bool): normalize = [(0.5, 0.5, 0.5), (0.5, 0.5, 0.5)] if classification_mode: train_transform = transforms.Compose([ transforms.RandomResizedCrop(64), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(*normalize), ]) test_transform = transforms.Compose([ transforms.Resize(64), transforms.CenterCrop(64), transforms.ToTensor(), transforms.Normalize(*normalize), ]) else: train_transform = build_transforms(normalize=normalize, **transform_args) test_transform = train_transform if split is None: train_set = Dataset(root=data_path, transform=train_transform, download=True) test_set = Dataset(root=data_path, transform=test_transform) else: train_set, test_set = self.make_split( data_path, split, Dataset, train_transform, test_transform) input_names = ['images', 'labels', 'attributes'] dim_c, dim_x, dim_y = train_set[0][0].size() dim_l = len(train_set.classes) dim_a = train_set.attributes[0].shape[0] dims = dict(x=dim_x, y=dim_y, c=dim_c, labels=dim_l, attributes=dim_a) self.add_dataset('train', train_set) self.add_dataset('test', test_set) self.set_input_names(input_names) self.set_dims(**dims) self.set_scale((-1, 1))
def train_dataloader(self): _train_transforms: None = None if self.hparams.augment: _train_transforms = transforms.Compose([ transforms.RandomResizedCrop(size=(224, 224), scale=(0.85, 1.0)), transforms.RandomGrayscale(p=0.08), # lambda x: x if np.random.random_sample() > 0.08 else x.filter(ImageFilter.GaussianBlur(radius=1)), # lambda x: x if np.random.random_sample() > 0.08 else x.filter(ImageFilter.GaussianBlur(radius=3)), self.gaussianBlur1, self.gaussianBlur3, transforms.Resize((224, 224), Image.BICUBIC), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) _data_train = RTGENEH5Dataset(h5_file=h5pickle.File( self.hparams.hdf5_file, mode="r"), subject_list=self._train_subjects, transform=_train_transforms) return DataLoader(_data_train, batch_size=self.hparams.batch_size, shuffle=True, num_workers=self.hparams.num_io_workers, pin_memory=False)
def __init__(self, factor, size): self.factor = factor rc_scale = (2 - factor[1], 1) self.size = (size, size) self.rc_scale = rc_scale self.ratio = (3. / 4., 4. / 3.) self.resize_crop = transforms.RandomResizedCrop(size, rc_scale)
def load_data(traindir, valdir, img_size=224, crop_pct=0.875): # Data loading code normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) scale_size = min(int(math.floor(img_size / crop_pct)), 320) print("Loading training data") st = time.time() train_set = ImageFolder( traindir, transforms.Compose([ transforms.RandomResizedCrop(img_size, scale=(0.3, 1.0)), transforms.RandomHorizontalFlip(), transforms.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.1, hue=0.02), transforms.ToTensor(), normalize, transforms.RandomErasing(p=0.9, value='random') ])) print("Took", time.time() - st) print("Loading validation data") eval_tf = [] if scale_size < 320: eval_tf.append(transforms.Resize(scale_size)) eval_tf.extend( [transforms.CenterCrop(img_size), transforms.ToTensor(), normalize]) val_set = ImageFolder(valdir, transforms.Compose(eval_tf)) return train_set, val_set
def __init__(self, split: str = "train", resize_s=256, size=10e9): """ :type resize_s: int or tuple(w,h) :param dataset_path: dataset directory :param split: train, valid, test""" t = [] if split == "train": t.extend([ transforms.RandomResizedCrop(resize_s), transforms.RandomHorizontalFlip(), ]) else: t.extend( [transforms.Resize(resize_s), transforms.CenterCrop(resize_s)]) t.extend([ transforms.ToTensor() # transforms.Normalize(self.mean, self.std) ]) self.trans = transforms.Compose(t) self.env = connect_dict() self.env_iter = iter(connect_dict()) self.size = size
def train_dataloader(self): train_transforms = transforms.Compose([ transforms.RandomResizedCrop(size=(36, 60), scale=(0.5, 1.3)), transforms.RandomPerspective(distortion_scale=0.2), transforms.RandomGrayscale(p=0.1), transforms.ColorJitter(brightness=0.5, hue=0.2, contrast=0.5, saturation=0.5), lambda x: x if np.random.random_sample() <= 0.1 else x.filter( ImageFilter.GaussianBlur(radius=2)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) data_train = RTBENEH5Dataset(h5_pth=self.hparams.dataset, subject_list=self._train_subjects, transform=train_transforms, loader_desc="train") return DataLoader(data_train, batch_size=self.hparams.batch_size, shuffle=True, num_workers=self.hparams.num_io_workers, pin_memory=True)
def get_transforms(self)->tuple: # MEAN, STD computed for sport8 MEAN = [0.4734, 0.4856, 0.4526] STD = [0.2478, 0.2444, 0.2667] # transformations match that in # https://github.com/antoyang/NAS-Benchmark/blob/master/DARTS/preproc.py train_transf = [ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ColorJitter( brightness=0.4, contrast=0.4, saturation=0.4, hue=0.2) ] test_transf = [transforms.Resize(256), transforms.CenterCrop(224)] normalize = [ transforms.ToTensor(), transforms.Normalize(MEAN, STD) ] train_transform = transforms.Compose(train_transf + normalize) test_transform = transforms.Compose(test_transf + normalize) return train_transform, test_transform
def __init__(self, crop_size, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), hflip_prob=0.5, auto_augment_policy=None, random_erase_prob=0.0): trans = [transforms.RandomResizedCrop(crop_size)] if hflip_prob > 0: trans.append(transforms.RandomHorizontalFlip(hflip_prob)) if auto_augment_policy is not None: if auto_augment_policy == "ra": trans.append(autoaugment.RandAugment()) elif auto_augment_policy == "ta_wide": trans.append(autoaugment.TrivialAugmentWide()) else: aa_policy = autoaugment.AutoAugmentPolicy(auto_augment_policy) trans.append(autoaugment.AutoAugment(policy=aa_policy)) trans.extend([ transforms.ToTensor(), transforms.Normalize(mean=mean, std=std), ]) if random_erase_prob > 0: trans.append(transforms.RandomErasing(p=random_erase_prob)) self.transforms = transforms.Compose(trans)
def __init__(self): self.transform = transforms.Compose([ transforms.RandomResizedCrop(64), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])
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
def build_file_list(self, csv_path_1, csv_path_2): if self.dataset_type == 'train': with open(csv_path_1) as csv_train: csv_reader = csv.reader(csv_train) for index, row_item in enumerate(csv_reader): if index == 0: continue self.image_name_list_base.append(row_item[0]) self.index_list_base.append(index - 1) last_index = self.index_list_base[-1] with open(csv_path_2) as csv_val: csv_reader = csv.reader(csv_val) for index, row_item in enumerate(csv_reader): if index == 0: continue self.image_name_list_base.append(row_item[0]) self.index_list_base.append(last_index + index - 1) else: with open(csv_path_1) as csv_test: csv_reader = csv.reader(csv_test) for index, row_item in enumerate(csv_reader): if index == 0: continue self.image_name_list_base.append(row_item[0]) self.index_list_base.append(index - 1) self.label_list_base = [ i for i in range(self.num_classes) for _ in range(600) ] random.shuffle(self.index_list_base) self.image_name_list = [ self.image_name_list_base[i] for i in self.index_list_base ] self.label_list = [ self.label_list_base[i] for i in self.index_list_base ] self.mean = np.array([x / 255.0 for x in [125.3, 123.0, 113.9]]) self.std = np.array([x / 255.0 for x in [63.0, 62.1, 66.7]]) if self.data_aug is True: self.transform = transforms.Compose([ transforms.Resize(92), transforms.RandomResizedCrop(88), transforms.CenterCrop(self.img_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(self.mean, self.std) ]) else: self.transform = transforms.Compose([ transforms.Resize(92), transforms.CenterCrop(self.img_size), transforms.ToTensor(), transforms.Normalize(self.mean, self.std) ])
def get_training_transform(): return transforms.Compose([ transforms.RandomResizedCrop(vgg_utils.image_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=vgg_utils.normalization_means, std=vgg_utils.normalization_stds) ])
def __init__(self): ##The top config #self.data_root = '/media/hhy/data/USdata/MergePhase1/test_0.3' #self.log_dir = '/media/hhy/data/code_results/MILs/MIL_H_Attention' self.root = '/remote-home/my/Ultrasound_CV/data/Ruijin/clean' self.log_dir = '/remote-home/my/hhy/Ultrasound_MIL/experiments/weighted_sampler/' if not os.path.exists(self.log_dir): os.makedirs(self.log_dir) ##training config self.lr = 1e-4 self.epoch = 50 self.resume = -1 self.batch_size = 1 self.net = Attention() self.net.cuda() self.optimizer = Adam(self.net.parameters(), lr=self.lr) self.lrsch = torch.optim.lr_scheduler.MultiStepLR(self.optimizer, milestones=[10, 30, 50, 70], gamma=0.5) self.logger = Logger(self.log_dir) self.train_transform = transforms.Compose([ transforms.Resize((224,224)), transforms.RandomResizedCrop((224,224)), transforms.RandomHorizontalFlip(0.5), transforms.RandomVerticalFlip(0.5), transforms.ColorJitter(0.25,0.25,0.25,0.25), transforms.ToTensor() ]) self.test_transform = transforms.Compose([ transforms.Resize((224,224)), transforms.ToTensor() ]) self.trainbag = RuijinBags(self.root, [0,1,2,3],self.train_transform) self.testbag = RuijinBags(self.root, [4], self.test_transform) train_label_list = list(map(lambda x: int(x['label']), self.trainbag.patient_info)) pos_ratio = sum(train_label_list) / len(train_label_list) print(pos_ratio) train_weight = [(1-pos_ratio) if x>0 else pos_ratio for x in train_label_list] self.train_sampler = WeightedRandomSampler(weights=train_weight, num_samples=len(self.trainbag)) self.train_loader = DataLoader(self.trainbag, batch_size=self.batch_size, num_workers=8, sampler=self.train_sampler) self.val_loader = DataLoader(self.testbag, batch_size=self.batch_size, shuffle=False, num_workers=8) if self.resume > 0: self.net, self.optimizer, self.lrsch, self.loss, self.global_step = self.logger.load(self.net, self.optimizer, self.lrsch, self.loss, self.resume) else: self.global_step = 0 # self.trainer = MTTrainer(self.net, self.optimizer, self.lrsch, self.loss, self.train_loader, self.val_loader, self.logger, self.global_step, mode=2) self.trainer = MILTrainer_batch1(self.net, self.optimizer, self.lrsch, None, self.train_loader, self.val_loader, self.logger, self.global_step)
def get_simclr_data_transforms2(input_shape, s=1., blur=0.1): # get 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) data_transforms = transforms.Compose([ transforms.RandomResizedCrop(size=eval(input_shape)[0]), transforms.RandomHorizontalFlip(), transforms.RandomApply([color_jitter], p=0.5), transforms.ToTensor() ]) return data_transforms
def Transforms(Input_Dim, S=1): Color_Jitter = transforms.ColorJitter(0.8 * S, 0.8 * S, 0.8 * S, 0.2 * S) Data_Transforms = transforms.Compose([ transforms.RandomResizedCrop(size=Input_Dim[0]), transforms.RandomHorizontalFlip(), transforms.RandomApply([Color_Jitter], p=0.75), transforms.RandomGrayscale(p=0.2), GaussianBlur(int(0.1 * Input_Dim[0])), transforms.ToTensor(), ]) return Data_Transforms