I’m beginner for Pytorch. I want to increase the number of datasets (data augmentation).
I have two images (cat_1 and cat_2) in folder and I want to use albumentation
to increase the number of images as follows:
import cv2
import torch
import albumentations as A
import numpy as np
import matplotlib.pyplot as plt
import os
from os import listdir
from os.path import join, isfile
from torchvision.utils import save_image
# Read files from Directory
Dir_img = '/content/drive/MyDrive/Cat_Dog'
files = os.listdir(Dir_img)
print(files) # ['cat_1.jpg', 'cat_2.jpg']
After that, I use OpenCV
to read images from Dir_img and keep array of each image into list as follows:
dataset = [] # <class 'list'> In list consist of array of each image
# len(dataset) = 2
for img in files:
img_ = cv2.imread(os.path.join(Dir_img,img), cv2.IMREAD_UNCHANGED) # default BGR
Org_ = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB) # Convert BGR -> RGB
dataset.append(Org_)
# cat_1: dataset[0].shape = (666, 1000, 3)
# cat_2: dataset[1].shape = (825, 1100, 3)
Next, I defined an augmentation pipeline (multiple files)
as follows:
TRANSFORM = A.Compose([
A.Blur(p = 0.5, blur_limit = (3, 7)),
A.RandomBrightnessContrast(p = 0.5, brightness_limit = 1, contrast_limit = 1)
])
After that, I will pass all images to the augmentation pipeline and receive augmented images.
for IMG in dataset: # IMG is <class 'numpy.ndarray'>
MY_TRANSFORMED = TRANSFORM(image=IMG) #<class 'dict'>
#Convert 'dict' to 'numpy.array'
MY_TRANSFORMED_IMAGE = MY_TRANSFORMED['image'] # <class 'numpy.ndarray'>
MY_TRANSFORMED_IMAGE_1 = TRANSFORM(image=IMG)['image']
MY_TRANSFORMED_IMAGE_2 = TRANSFORM(image=IMG)['image']
MY_TRANSFORMED_IMAGE_3 = TRANSFORM(image=IMG)['image']
From above code, I received a total of 3 images of different cat_2
. But I think the resulting image should be of a cat_1
.
I have questions as follows:
- Every time, I run
TRANSFORM
. I receive a different image every time. Is there a way to get me the same results every time? - Why haven’t I received an image of a
cat_1
? - If I want 1 original images (
cat_1
andcat_2
) to do 10 different data augmentations, I can use afor loop
?
I’ve tried to find document from various sources, but I’m not getting the answer I’m wondering.