def _train_image_transform(self): transform = transforms.Compose([ transforms.Resize(256), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.RandomRotation(0.2), transforms.ColorJitter(0.1, 0, 0, 0), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) return transform
args = parse_args() data_dir = args.data_dir save_dir = args.save_dir num_classes = args.num_classes channel = args.channel clip_min_value = args.clip_min_value clip_max_value = args.clip_max_value mean = args.mean std = args.std num_epochs = args.num_epochs train_batch_size = args.train_batch_size lr = args.lr # 定义训练和验证时的transforms train_transforms = T.Compose([ T.RandomVerticalFlip(0.5), T.RandomHorizontalFlip(0.5), T.ResizeStepScaling(0.5, 2.0, 0.25), T.RandomPaddingCrop(1000), T.Clip(min_val=clip_min_value, max_val=clip_max_value), T.Normalize( min_val=clip_min_value, max_val=clip_max_value, mean=mean, std=std), ]) eval_transforms = T.Compose([ T.Clip(min_val=clip_min_value, max_val=clip_max_value), T.Normalize( min_val=clip_min_value, max_val=clip_max_value, mean=mean, std=std), ]) train_list = osp.join(data_dir, 'train.txt')
def load_data_transformers(resize_reso=512, crop_reso=448, swap_num=[7, 7]): center_resize = 600 Normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) data_transforms = { 'swap': transforms.Compose([ transforms.Resize((resize_reso, resize_reso)), transforms.RandomRotation(degrees=15), transforms.RandomCrop((crop_reso, crop_reso)), transforms.RandomHorizontalFlip(), transforms.Randomswap((swap_num[0], swap_num[1])), ]), 'food_swap': transforms.Compose([ transforms.Resize((resize_reso, resize_reso)), transforms.RandomRotation(degrees=90), #transforms.RandomCrop((crop_reso, crop_reso)), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.RandomResizedCrop(size=crop_reso, scale=(0.75, 1)), transforms.Randomswap((swap_num[0], swap_num[1])), ]), 'food_unswap': transforms.Compose([ transforms.Resize((resize_reso, resize_reso)), transforms.RandomRotation(degrees=90), #transforms.RandomCrop((crop_reso, crop_reso)), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.RandomResizedCrop(size=crop_reso, scale=(0.75, 1)), ]), 'unswap': transforms.Compose([ transforms.Resize((resize_reso, resize_reso)), transforms.RandomRotation(degrees=15), transforms.RandomCrop((crop_reso, crop_reso)), transforms.RandomHorizontalFlip(), ]), 'train_totensor': transforms.Compose([ transforms.Resize((crop_reso, crop_reso)), #ImageNetPolicy(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]), 'val_totensor': transforms.Compose([ transforms.Resize((crop_reso, crop_reso)), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]), 'test_totensor': transforms.Compose([ transforms.Resize((resize_reso, resize_reso)), transforms.CenterCrop((crop_reso, crop_reso)), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]), 'None': None, 'Centered_swap': transforms.Compose([ transforms.CenterCrop((center_resize, center_resize)), transforms.Resize((resize_reso, resize_reso)), transforms.RandomRotation(degrees=15), transforms.RandomCrop((crop_reso, crop_reso)), transforms.RandomHorizontalFlip(), transforms.Randomswap((swap_num[0], swap_num[1])), ]), 'Centered_unswap': transforms.Compose([ transforms.CenterCrop((center_resize, center_resize)), transforms.Resize((resize_reso, resize_reso)), transforms.RandomRotation(degrees=15), transforms.RandomCrop((crop_reso, crop_reso)), transforms.RandomHorizontalFlip(), ]), 'Tencrop': transforms.Compose([ transforms.Resize((resize_reso, resize_reso)), transforms.TenCrop((crop_reso, crop_reso)), transforms.Lambda(lambda crops: torch.stack( [transforms.ToTensor()(crop) for crop in crops])), ]) } return data_transforms
# Load image dataset = SLAM('data/SLAM') data, target = dataset[1] # Add grid lines (https://stackoverflow.com/a/20473192/5828163) dx, dy = 80, 80 data[:, ::dy, :] = [0, 0, 0, 0] data[::dx, :, :] = [0, 0, 0, 0] target[:, ::dy] = 1 target[::dx, :] = 1 # Plot transformations plot_data_target(data, target, 'Original') transform_list = [ transforms.RandomHorizontalFlip(p=1), transforms.RandomVerticalFlip(p=1), transforms.MinMaxScaling(), transforms.RandomElasticDeformation(alpha=200, sigma=10, alpha_affine=40), transforms.Pad(92, padding_mode='reflect'), transforms.RandomRotation(180), transforms.RandomCrop(572, 388), transforms.ToTensor(), ] for transform in transform_list: data, target = transform(data, target) plot_data_target(data, target, transform.__class__.__name__)