Albumentation error

username: gamebo5000
cooked: <p>I am using albumentations for data augmentation but I am getting the error in the title.</p>
<blockquote>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
from torch.utils.data import DataLoader<br>
from torchvision import datasets, transforms, models # add models to the list<br>
import os<br>
import time<br>
from PIL import Image<br>
from IPython.display import display<br>
import matplotlib.pyplot as plt<br>
import numpy as np<br>
import pandas as pd<br>
import seaborn as sn  # for heatmaps<br>
from sklearn.metrics import confusion_matrix<br>
import math<br>
from albumentations import (<br>
HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,<br>
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,<br>
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,<br>
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose<br>
)</p>
<p>MobileNet = models.mobilenet_v2(pretrained = True)</p>
<p>class_names = [‘covid’, ’  normal’, ’ pnuemonia’]</p>
<p>torch.manual_seed(50)</p>
<p>MobileNet.classifier = nn.Sequential(nn.Linear(1280,3), nn.LogSoftmax(dim=1))<br>
<span class="hashtag-raw">#MobileNet</span>.classifier = nn.Sequential(nn.Linear(1280, 1000),nn.ReLU(), nn.Linear(1000,3), nn.LogSoftmax(dim=1))</p>
<p>if torch.cuda.is_available():<br>
MobileNet.cuda()</p>
<p>criterion = nn.CrossEntropyLoss()<br>
<span class="hashtag-raw">#optimizer</span> = torch.optim.Adam(MobileNet.classifier.parameters(), lr = 0.001)<br>
optimizer = torch.optim.SGD(MobileNet.classifier.parameters(), lr = 0.001, momentum=0.9, weight_decay=0.00004)</p>
<p>def strong_aug(p=0.5):<br>
return Compose([<br>
RandomRotate90(),<br>
Flip(),<br>
Transpose(),<br>
OneOf([<br>
IAAAdditiveGaussianNoise(),<br>
GaussNoise(),<br>
], p=0.2),<br>
OneOf([<br>
MotionBlur(p=0.2),<br>
MedianBlur(blur_limit=3, p=0.1),<br>
Blur(blur_limit=3, p=0.1),<br>
], p=0.2),<br>
ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.2),<br>
OneOf([<br>
OpticalDistortion(p=0.3),<br>
GridDistortion(p=0.1),<br>
IAAPiecewiseAffine(p=0.3),<br>
], p=0.2),<br>
OneOf([<br>
CLAHE(clip_limit=2),<br>
IAASharpen(),<br>
IAAEmboss(),<br>
RandomBrightnessContrast(),<br>
], p=0.3),<br>
HueSaturationValue(p=0.3),<br>
], p=p)</p>
<p>augmentation = strong_aug(p=0.9)</p>
<p>train_data = datasets.ImageFolder(‘C:/Users/deonh/Pictures/Summer Work/scaledata/train’, augmentation)<br>
test_data = datasets.ImageFolder(‘C:/Users/deonh/Pictures/Summer Work/scaledata/test’, augmentation)<br>
test_batch = len(test_data)<br>
train_loader = DataLoader(train_data, batch_size=batch, shuffle=True)<br>
test_loader = DataLoader(test_data, batch_size=batch, shuffle=True)<br>
if torch.cuda.is_available():<br>
train_loader = DataLoader(train_data, batch_size=batch, shuffle=True, pin_memory = True)<br>
test_loader = DataLoader(test_data, batch_size=test_batch, shuffle=True, pin_memory = True)</p>
<p>Q = math.floor(len(train_data)/batch)</p>
<p>lrs = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max = Q)<br>
for i in range(epochs):<br>
trn_corr = 0<br>
tst_corr = 0</p>
<pre><code># Run the training batches

for b, (images, labels) in enumerate(train_loader):
    
    if torch.cuda.is_available():
        images = images.cuda()
        labels = labels.cuda()
    
    b+=1
    
    # Apply the model
    y_pred = MobileNet(images)
    loss = criterion(y_pred, labels)

    # Tally the number of correct predictions
    train_predicted = torch.max(y_pred.data, 1)[1]
    batch_corr = (train_predicted == labels).sum()
    trn_corr += batch_corr
    
    accuracy = trn_corr.item()*100/(b*batch)
    # Update parameters
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    lrs.step()

    # Print interim results
    if b%Q == 0:
        print(f'epoch: {i:2}  batch: {b:4} loss: {loss.item():10.8f}  \
</code></pre>
<p>accuracy: {accuracy:3.3f}%')</p>
<pre><code>        train_acc.append(accuracy)
        train_losses.append(loss)
        train_correct.append(trn_corr)
        
        print("\n")



# Run the testing batches
with torch.no_grad():
    for b, (pics, names) in enumerate(test_loader):
        
        if torch.cuda.is_available():
            pics = pics.cuda()
            names = names.cuda()
            
        b+=1

        # Apply the model
        y_val = MobileNet(pics)
        val_loss = criterion(y_val, names)
        # Tally the number of correct predictions
        test_predicted = torch.max(y_val.data, 1)[1] 
        tst_corr += (test_predicted == names).sum()
        
        test_correct.append(tst_corr)
        Val_accuracy = tst_corr.item()*100/(test_batch)       
        # Print Validation results
        if b%1 == 0:
            print(f'test_epoch: {i:2}  batch: {b:4}  Val_loss: {val_loss.item():10.8f}  \Val_accuracy: {Val_accuracy:7.3f}%')
            test_acc.append(Val_accuracy)
            test_losses.append(val_loss)
            print("\n")
</code></pre>
</blockquote>
<p>Can someone help me fix this?</p>

username: albanD
cooked: <p>Hi,</p>
<p>What is the error you see exactly?<br>
Is it related to pytorch?</p>

username: gamebo5000
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/f/3faa6273da8763aebf4f3a3abcd300e54279ecde.png" data-download-href="https://discuss.pytorch.org/uploads/default/3faa6273da8763aebf4f3a3abcd300e54279ecde" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/f/3faa6273da8763aebf4f3a3abcd300e54279ecde.png" alt="image" data-base62-sha1="95d7VIqX6Tmpf8RV9iXNSjcQx2u" width="545" height="500" data-dominant-color="F4F5F5"><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">856×784 50.7 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>I am getting this error.</p>

username: albanD
cooked: <p>This seems to be related to albumentations not pytorch right, you might not find someone that can help your here <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>

username: ptrblck
cooked: <p>As <a class="mention" href="/u/alband">@albanD</a> said, this issue came up a couple of times and seems to be related to a wrong usage of <code>albumentations</code>.<br>
<a href="https://discuss.pytorch.org/t/assertionerror-force-apply-must-have-bool-or-int-type/73345/3">Here</a> is a potential solution.</p>