Albumentation error

I am using albumentations for data augmentation but I am getting the error in the title.

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets, transforms, models # add models to the list
import os
import time
from PIL import Image
from IPython.display import display
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sn # for heatmaps
from sklearn.metrics import confusion_matrix
import math
from albumentations import (
HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose
)

MobileNet = models.mobilenet_v2(pretrained = True)

class_names = [‘covid’, ’ normal’, ’ pnuemonia’]

torch.manual_seed(50)

MobileNet.classifier = nn.Sequential(nn.Linear(1280,3), nn.LogSoftmax(dim=1))
#MobileNet.classifier = nn.Sequential(nn.Linear(1280, 1000),nn.ReLU(), nn.Linear(1000,3), nn.LogSoftmax(dim=1))

if torch.cuda.is_available():
MobileNet.cuda()

criterion = nn.CrossEntropyLoss()
#optimizer = torch.optim.Adam(MobileNet.classifier.parameters(), lr = 0.001)
optimizer = torch.optim.SGD(MobileNet.classifier.parameters(), lr = 0.001, momentum=0.9, weight_decay=0.00004)

def strong_aug(p=0.5):
return Compose([
RandomRotate90(),
Flip(),
Transpose(),
OneOf([
IAAAdditiveGaussianNoise(),
GaussNoise(),
], p=0.2),
OneOf([
MotionBlur(p=0.2),
MedianBlur(blur_limit=3, p=0.1),
Blur(blur_limit=3, p=0.1),
], p=0.2),
ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.2),
OneOf([
OpticalDistortion(p=0.3),
GridDistortion(p=0.1),
IAAPiecewiseAffine(p=0.3),
], p=0.2),
OneOf([
CLAHE(clip_limit=2),
IAASharpen(),
IAAEmboss(),
RandomBrightnessContrast(),
], p=0.3),
HueSaturationValue(p=0.3),
], p=p)

augmentation = strong_aug(p=0.9)

train_data = datasets.ImageFolder(‘C:/Users/deonh/Pictures/Summer Work/scaledata/train’, augmentation)
test_data = datasets.ImageFolder(‘C:/Users/deonh/Pictures/Summer Work/scaledata/test’, augmentation)
test_batch = len(test_data)
train_loader = DataLoader(train_data, batch_size=batch, shuffle=True)
test_loader = DataLoader(test_data, batch_size=batch, shuffle=True)
if torch.cuda.is_available():
train_loader = DataLoader(train_data, batch_size=batch, shuffle=True, pin_memory = True)
test_loader = DataLoader(test_data, batch_size=test_batch, shuffle=True, pin_memory = True)

Q = math.floor(len(train_data)/batch)

lrs = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max = Q)
for i in range(epochs):
trn_corr = 0
tst_corr = 0

# 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}  \

accuracy: {accuracy:3.3f}%’)

        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")

Can someone help me fix this?

Hi,

What is the error you see exactly?
Is it related to pytorch?

I am getting this error.

This seems to be related to albumentations not pytorch right, you might not find someone that can help your here :confused:

As @albanD said, this issue came up a couple of times and seems to be related to a wrong usage of albumentations.
Here is a potential solution.

1 Like