Hello
I am using a dataloader and I am creating a transform list to do all the transformations on the tensors once I read them before passing to the network.
Currently, I was using random cropping by providing
transform_list = [transforms.RandomCrop((height, width))] + transform_list if crop else transform_list
I want to change the random cropping to a defined normal cropping for all images starting from one x,y
coordinate to a certain width and height (basically I want to take the bottom half of the image and remove the upper one)
I searched through the functions provided by the transforms in torch and I found this transforms.functional.crop()
, the problem that this function has to take the image as an argument, however, I still don’t have the image since I am still creating my transform list, is there a way to do so ?
PS: My image size is 720x1280
Here is how I do it:
def get_all_data_loaders(conf):
batch_size = conf['batch_size']
num_workers = conf['num_workers']
if 'new_size' in conf:
new_size_a = new_size_b = conf['new_size']
else:
new_size_a = conf['new_size_a']
new_size_b = conf['new_size_b']
height = conf['crop_image_height']
width = conf['crop_image_width']
train_loader_a = get_data_loader_folder(os.path.join(conf['data_root'], 'trainA'), batch_size, True,
new_size_a, height, width, num_workers, True)
test_loader_a = get_data_loader_folder(os.path.join(conf['data_root'], 'testA'), batch_size, False,
new_size_a, new_size_a, new_size_a, num_workers, True)
train_loader_b = get_data_loader_folder(os.path.join(conf['data_root'], 'trainB'), batch_size, True,
new_size_b, height, width, num_workers, True)
test_loader_b = get_data_loader_folder(os.path.join(conf['data_root'], 'testB'), batch_size, False,
new_size_b, new_size_b, new_size_b, num_workers, True)
def get_data_loader_folder(input_folder, batch_size, train, new_size=None,
height=256, width=256, num_workers=4, crop=True):
transform_list = [transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))]
# Only for R2NR implementation - crop half of the image
transform_list = [transforms.functional.crop(360, 640, 360, 640)] + transform_list if crop else transform_list
#transform_list = [transforms.RandomCrop((height, width))] + transform_list if crop else transform_list
transform_list = [transforms.Resize(new_size)] + transform_list if new_size is not None else transform_list
transform_list = [transforms.RandomHorizontalFlip()] + transform_list if train else transform_list
transform = transforms.Compose(transform_list)
dataset = ImageFolder(input_folder, transform=transform)
loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=train, drop_last=True, num_workers=num_workers)
return loader