JunlinHan / YOCO

Code for You Only Cut Once: Boosting Data Augmentation with a Single Cut, ICML 2022.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

YOCO generates a single image when we have single image

khawar-islam opened this issue · comments

Hi @JunlinHan

I have a dataset contains single image and I am simply applying your YOCO technique to visualize image generated by YOCO. I just get a single output sometimes the output is same image as input and sometimes flip+cut. Is that correct?

Code

import torch
from torchvision.utils import save_image
import torchvision.transforms as transforms
from torchvision import datasets
from torchvision.transforms import ToTensor
from torch.utils.data import DataLoader

training_data = datasets.ImageFolder(root="/media/cvpr/CM_22/dataset/train", transform=ToTensor())
train_loader = DataLoader(training_data, batch_size=64, shuffle=True)


def YOCO(images, aug, h, w):
    images = torch.cat((aug(images[:, :, :, 0:int(w / 2)]), aug(images[:, :, :, int(w / 2):w])), dim=3) if \
        torch.rand(1) > 0.5 else torch.cat((aug(images[:, :, 0:int(h / 2), :]), aug(images[:, :, int(h / 2):h, :])),
                                           dim=2)
    return images


for i, (images, target) in enumerate(train_loader):
    aug = torch.nn.Sequential(
        transforms.RandomHorizontalFlip(), )
    _, _, h, w = images.shape
    # perform augmentations with YOCO
    images = YOCO(images, aug, h, w)
    save_image(images, 'img' + str(i) + '.png')

Input image:
aeroplane_+_tiger_google_0017

Output Image
img0

Hello Khawar,
Thanks for your questions! Yes, it is correct. It depends on the probability of applying augmentation operations. The demo code you are using is horizontal flip, which is applied with a probabily of 0.5 by default. You may modify the following part, an example (p=1) is attached below:

    aug = torch.nn.Sequential(
        transforms.RandomHorizontalFlip(p=1), )

@JunlinHan thank you for your answer. If i apply two techniques at the same time, it must generate two augmented images.
Am i right?

for i, (images, target) in enumerate(train_loader):
    aug = torch.nn.Sequential(
        transforms.RandomHorizontalFlip(p=1),
        transforms.RandomVerticalFlip(p=1))
    _, _, h, w = images.shape
    # perform augmentations with YOCO
    images = YOCO(images, aug, h, w)
    save_image(images, 'img' + str(i) + '.png')

Hello Khawar,
No, this is a sequence operation, so you will get one augmented image only (but this image has been augmented with 2 operations).