1

I have a dataset containing a single image and I am simply applying the YOCO technique to visualize images generated by YOCO. I just get a single output sometimes the output is the same image as the input and sometimes flip+cut. I have no idea why it happens.

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
import numpy as np
import matplotlib.pyplot as plt

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

enter image description here

Output

enter image description here

Ismaili Mohamedi
  • 906
  • 7
  • 15
Khawar Islam
  • 2,556
  • 2
  • 34
  • 56

1 Answers1

0

Your code is not very readable, if you reformat it properly you will see that it does something like:

images = aug(images) # random horizontal flip
if torch.rand(1) > .5:
    images = horizontal_cutflip(images)
else:
    images = vertica_cutflip(images)

In other words, it applies a random flip operation (defined in aug), then either applies a horizontal or vertical cut-flip operation with a 50/50 chance.

Ivan
  • 34,531
  • 8
  • 55
  • 100
  • Please elaborate me what is vertica_cutflip and horizontal_cutflip? – Khawar Islam Nov 09 '22 at 04:17
  • `vertica_cutflip` and `horizontal_cutflip` are the transformations you defined in your code using `torch.cat`: you first sliced along the width dimension (height dimension, respectively), and concatenate back by swapping the two pieces. – Ivan Nov 09 '22 at 08:22