def __init__(self, mode='train'): """ 初始化函数 """ assert mode in ['train', 'test', 'valid'], 'mode is one of train, test, valid.' self.data = [] with open('signs/{}.txt'.format(mode)) as f: for line in f.readlines(): info = line.strip().split('\t') if len(info) > 0: self.data.append([info[0].strip(), info[1].strip()]) if mode == 'train': self.transforms = T.Compose([ T.RandomResizedCrop(IMAGE_SIZE), # 随机裁剪大小 T.RandomHorizontalFlip(0.5), # 随机水平翻转 T.ToTensor(), # 数据的格式转换和标准化 HWC => CHW T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # 图像归一化 ]) else: self.transforms = T.Compose([ T.Resize(256), # 图像大小修改 T.RandomCrop(IMAGE_SIZE), # 随机裁剪 T.ToTensor(), # 数据的格式转换和标准化 HWC => CHW T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # 图像归一化 ])
def __init__(self, noise_path, size, keys=None): self.noise_path = noise_path self.noise_imgs = sorted(glob.glob(noise_path + '*.png')) self.size = size self.keys = keys self.transform = T.Compose([ T.RandomCrop(size), T.Transpose(), T.Normalize([0., 0., 0.], [255., 255., 255.]) ])
def load_train_test_datasets(dataset_root): mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225] resize = transforms.Resize(256) rcrop = transforms.RandomCrop((224, 224)) ccrop = transforms.CenterCrop((224, 224)) tot = transforms.ToTensor() normalize = transforms.Normalize(mean, std) train_transforms = transforms.Compose([resize, rcrop, tot, normalize]) test_transforms = transforms.Compose([resize, ccrop, tot, normalize]) train_set = DatasetFolder(osp.join(dataset_root, 'train'), transform=train_transforms) test_set = DatasetFolder(osp.join(dataset_root, 'test'), transform=test_transforms) return train_set, test_set
NUM_WORKERS = cfg['NUM_WORKERS'] USE_PRETRAINED = cfg['USE_PRETRAINED'] logger.info("=" * 60) logger.info("Overall Configurations:") for cfg_, value in cfg.items(): logger.info(cfg_ + ":" + str(value)) logger.info("=" * 60) fleet.init(is_collective=True) train_transform = transforms.Compose([ # refer to https://pytorch.org/docs/stable/torchvision/transforms.html for more build-in online data augmentation transforms.Resize( [int(128 * INPUT_SIZE[0] / 112), int(128 * INPUT_SIZE[0] / 112)]), # smaller side resized transforms.RandomCrop([INPUT_SIZE[0], INPUT_SIZE[1]]), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=RGB_MEAN, std=RGB_STD), ]) logger.info('loading data form file:{}'.format(DATA_ROOT)) train_dataset = LFWDataset(os.path.join(DATA_ROOT), train_transform) num_classes = train_dataset.num_classes train_loader = paddle.io.DataLoader(train_dataset, places=[paddle.CPUPlace()], batch_size=BATCH_SIZE, shuffle=True, drop_last=DROP_LAST, num_workers=NUM_WORKERS) NUM_CLASS = train_dataset.num_classes
CUDA = True if CUDA: paddle.set_device('gpu') place = paddle.CUDAPlace(0) if CUDA else paddle.CPUPlace() mean, std = ([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) mean = list(map(lambda x: x * 255, mean)) std = list(map(lambda x: x * 255, std)) train_loader = paddle.io.DataLoader( paddle.vision.datasets.Cifar10( mode='train', transform=transforms.Compose([ # transforms.Resize(size=64), transforms.RandomHorizontalFlip(), transforms.RandomCrop(32, 4), transforms.Transpose(order=(2, 0, 1)), transforms.Normalize(mean=mean, std=std), ]), download=True), places=place, batch_size=128, shuffle=True, num_workers=0, use_shared_memory=True) val_loader = paddle.io.DataLoader(paddle.vision.datasets.Cifar10( mode='test', transform=transforms.Compose([ transforms.Transpose(order=(2, 0, 1)), transforms.Normalize(mean=mean, std=std),
parser.add_argument('--bottleneck', default=False, type=bool, help='whether to use bottleneck') parser.add_argument('--weights', default='no', type=str, help='the path for pretrained model') parser.add_argument('--pretrained', default=False, type=bool, help='whether to load pretrained weights') parser.add_argument('--batch_size', default=config_parameters['batch_size'], type=int, help='batch_size') parser.add_argument('--lr', default=config_parameters['lr'], type=float) parser.add_argument('--weight_decay', type=float, default=0.0001) parser.add_argument('--epochs', type = int, default = config_parameters['epochs']) parser.add_argument('--warmup', type = int, default = 10) args = parser.parse_args() train_transforms = T.Compose([ T.RandomCrop(32, padding=4), T.RandomHorizontalFlip(), T.ToTensor(), T.Normalize(mean=[x/255.0 for x in [125.3, 123.0, 113.9]], std=[x/255.0 for x in [63.0, 62.1, 66.7]]) ]) eval_transforms = T.Compose([ T.ToTensor(), T.Normalize(mean=[x/255.0 for x in [125.3, 123.0, 113.9]], std=[x/255.0 for x in [63.0, 62.1, 66.7]]) ]) if args.dataset == 'cifar10': train_dataset = paddle.vision.datasets.Cifar10(mode='train', transform=train_transforms) eval_dataset = paddle.vision.datasets.Cifar10(mode='test', transform=eval_transforms)