Data Augmentation on 1D Signal image

brunoj: <p>Hello. I have images dataset of ECG Signal which has 6 classes but the classes are imbalanced. Now I wanna use data augmentation on my dataset to balance the classes. You know ECG Signal needs to be augmented to have a benefit so I do not see it benefiting by croping, rotating etc so Im doing scaling, translation. My goal is these two techniques. I have read about this in pytorch and came to know that transforms.affine has attributes that can perform scaling and translation.<br>
Now my query is when I apply it like:<br>
from torchvision import transforms as T, datasets<br>
import os<br>
import random</p>
<h1><a name="define-the-data-augmentation-transformations-1" class="anchor" href="#define-the-data-augmentation-transformations-1"></a>Define the data augmentation transformations</h1>
<p>train_transform = T.Compose([<br>
T.RandomApply([T.RandomAffine(degrees=0, translate=(0.1, 0), scale=(0.9, 1.1))], p=0.5),<br>
T.Resize(size=(CFG.img_size,CFG.img_size)),<br>
T.ToTensor(), # convert the dimension from (height,weight,channel) to (channel,height,weight) convention of PyTorch<br>
T.Grayscale(1)<br>
])</p>
<h1><a name="define-the-validation-and-test-transformations-2" class="anchor" href="#define-the-validation-and-test-transformations-2"></a>Define the validation and test transformations</h1>
<p>validate_transform = T.Compose([<br>
T.RandomApply([T.RandomAffine(degrees=0, translate=(0.1, 0), scale=(0.9, 1.1))], p=0.5),<br>
T.Resize(size=(CFG.img_size,CFG.img_size)),<br>
T.ToTensor(),<br>
T.Grayscale(1)<br>
])</p>
<p>test_transform = T.Compose([<br>
T.RandomApply([T.RandomAffine(degrees=0, translate=(0.1, 0), scale=(0.9, 1.1))], p=0.5),<br>
T.Resize(size=(CFG.img_size,CFG.img_size)),<br>
T.ToTensor(),<br>
T.Grayscale(1)<br>
])</p>
<p>But my images looked like this :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/7/e70789e2c426bc0e0ba563a6a279aef49659c446.png" data-download-href="https://discuss.pytorch.org/uploads/default/e70789e2c426bc0e0ba563a6a279aef49659c446" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/7/e70789e2c426bc0e0ba563a6a279aef49659c446_2_690x388.png" alt="image" data-base62-sha1="wXMrIARgKlJJMOeVA8Z9QNq54P4" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/7/e70789e2c426bc0e0ba563a6a279aef49659c446_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/7/e70789e2c426bc0e0ba563a6a279aef49659c446_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/7/e70789e2c426bc0e0ba563a6a279aef49659c446_2_1380x776.png 2x" data-dominant-color="E5E5E5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1920×1080 147 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>by running the code: dataiter = iter(trainloader)<br>
images,labels = next(dataiter)</p>
<p>out = make_grid(images,nrow=4)</p>
<p>show_grid(out,title = [class_name<span class="chcklst-box checked fa fa-check-square-o fa-fw"></span> for x in labels])</p>
<p>Now my problem is how can I fix it? why it is happening ? besides as I said i need class balance but how will I ensure that classes got balanced. ? I have say 9000 images in train 1000 in validation and 4000 in test. i need to see my dataset getting large by balancing classes by introducing translation or scaling.<br>
I tried this code chunk but as PIL needs rgb image I get error I have to work with grayscale Images through out for my application.<br>
from PIL import Image</p>
<h1><a name="define-the-number-of-images-you-want-per-class-3" class="anchor" href="#define-the-number-of-images-you-want-per-class-3"></a>Define the number of images you want per class</h1>
<p>num_images_per_class = 2200</p>
<h1><a name="going-to-be-used-for-loading-dataset-4" class="anchor" href="#going-to-be-used-for-loading-dataset-4"></a>going to be used for loading dataset</h1>
<p>train_path=‘/content/train’<br>
validate_path= ‘/content/val’<br>
test_path=‘/content/ECG_Image_data/test’</p>
<h1><a name="loop-through-the-train-val-and-test-folders-5" class="anchor" href="#loop-through-the-train-val-and-test-folders-5"></a>Loop through the train, val, and test folders</h1>
<p>for folder_path in [train_path,validate_path,test_path]:<br>
# Loop through each sub-folder (class) in the current folder<br>
for class_folder in os.listdir(folder_path):<br>
class_path = os.path.join(folder_path, class_folder)<br>
# Count the number of images in the current class<br>
num_images = len(os.listdir(class_path))<br>
# Calculate the number of images to generate<br>
num_images_to_generate = num_images_per_class - num_images<br>
# Generate new images by applying the transform to existing images<br>
for i in range(num_images_to_generate):<br>
# Choose a random image from the class folder<br>
image_path = os.path.join(class_path, random.choice(os.listdir(class_path)))<br>
# Load the image<br>
image = Image.open(image_path)<br>
# Apply the transform<br>
transformed_image = train_transform(image)<br>
# Save the transformed image with a new name<br>
new_image_name = f"{class_folder}<em>transformed</em>{i}.png"<br>
transformed_image.save(os.path.join(class_path, new_image_name))</p>
<p>Plz help me solve this. I can’t fix it.</p>

ptrblck: <aside class="quote no-group" data-username="brunoj" data-post="1" data-topic="176062">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brunoj/48/56049_2.png" class="avatar"> brunoj:</div>
<blockquote>
<p>Now my problem is how can I fix it? why it is happening ?</p>
</blockquote>
</aside>
<p>You didn’t explain the issue, so I don’t know what exactly is unexpected.</p>
<aside class="quote no-group" data-username="brunoj" data-post="1" data-topic="176062">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brunoj/48/56049_2.png" class="avatar"> brunoj:</div>
<blockquote>
<p>besides as I said i need class balance but how will I ensure that classes got balanced. ?</p>
</blockquote>
</aside>
<p>You could use a <code>WeightedRandomSampler</code> as explained e.g. <a href="https://discuss.pytorch.org/t/how-to-handle-imbalanced-classes/11264/2">here</a> assuming you are working on a multi-class classification.</p>

brunoj: <p>well the problem is my images seems to have black areas do you see tha? I think it zero pads by default. But i want to scale/translate but perserve the original image not to get black where it should be white like my original image.</p>

brunoj: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/7/77817de2b5bcfb0e8e7814496ce96396f29834af.png" data-download-href="https://discuss.pytorch.org/uploads/default/77817de2b5bcfb0e8e7814496ce96396f29834af" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/7/77817de2b5bcfb0e8e7814496ce96396f29834af_2_690x388.png" alt="image" data-base62-sha1="h3cfJrEO1JCBaVd66ZO7G3SYsxx" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/7/77817de2b5bcfb0e8e7814496ce96396f29834af_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/7/77817de2b5bcfb0e8e7814496ce96396f29834af_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/7/7/77817de2b5bcfb0e8e7814496ce96396f29834af_2_1380x776.png 2x" data-dominant-color="F1F1F1"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1920×1080 191 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>This is the original image right But if you look at scaled/translated with probability =0.5 in grid images they happen to have black areas which is what I suspect maybe it is zero padding by default. I need not to have it.</p>

ptrblck: <aside class="quote no-group" data-username="brunoj" data-post="3" data-topic="176062">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brunoj/48/56049_2.png" class="avatar"> brunoj:</div>
<blockquote>
<p>well the problem is my images seems to have black areas do you see tha?</p>
</blockquote>
</aside>
<p>Yes, I see the black areas which is expected when you <code>translate</code> the input.</p>
<aside class="quote no-group" data-username="brunoj" data-post="3" data-topic="176062">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brunoj/48/56049_2.png" class="avatar"> brunoj:</div>
<blockquote>
<p>I think it zero pads by default.</p>
</blockquote>
</aside>
<p>Yes, that’s also the case as described in the <a href="https://pytorch.org/vision/main/generated/torchvision.transforms.RandomAffine.html#torchvision.transforms.RandomAffine">docs</a>.</p>
<p>I still don’t know what your expectations are and how you would like to translate an image without adding new border pixels.</p>

brunoj: <p>Umm well. I want like if i have a signal I want to move it basically advance or delay and vary the amplitude-wise so that I can do data-augmentation on ecg signal images. Now wanna shift the pixels of an image.I dont care if say I move my signal right with some x value and when I move the x pixels moves out of image boundary and i lose info. That is what I want actually i dont know if I make sense now or not.</p>

brunoj: <p>A naive idea came and I decided to crop width wise and once cropped resized the image to original dimensions. But how can I vary amplitude wise? Because in that case the spikes/peaks will be clipped.</p>