Thank you. Unfortunately it’s not working. If i change my batch size anything greater than 12 it keeps saying cuda out of memory error.
import numpy as np
import pandas as pd
from PIL import Image
import torch
import torchvision
from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader
from torchvision import transforms
from torch import nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from sklearn.preprocessing import MultiLabelBinarizer
import torch.backends.cudnn as cudnn
import visdom
vis = visdom.Visdom()
vis.delete_env('hckrerth_cpu') #If you want to clear all the old plots for this python Experiments.Resets the Environment
vis = visdom.Visdom(env='hckrerth_cpu')

class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, in_planes, planes, stride=1):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(self.expansion*planes)

        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion*planes:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*planes)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        out = self.bn3(self.conv3(out))
        out += self.shortcut(x)
        out = F.relu(out)
        return out

class KaggleAmazonDataset(Dataset):
    """Dataset wrapping images and target labels for Kaggle - Planet Amazon from Space competition.

    Arguments:
        A CSV file path
        Path to image folder
        Extension of images
        PIL transforms
    """

    def __init__(self, csv_path, img_path, img_ext, transform=None,train=True):
    
        tmp_df = pd.read_csv(csv_path)
        limit=len(tmp_df)
        if train:
            print("Loading Train Data")
            tmp_df=tmp_df.head(int(0.8*limit))
        else:
            print("Loadifn Test Data")
            tmp_df=tmp_df.tail(int(0.2*limit))
            tmp_df=tmp_df.reset_index(drop=True)

#         tmp_df['image_name']= tmp_df['image_name'].apply(lambda x: os.path.isfile(img_path + x)).all() 
#         print(tmp_df.head())
        #\"Some images referenced in the CSV file were not found"
        self.mlb = MultiLabelBinarizer()
        self.img_path = img_path
        self.img_ext = img_ext
        self.transform = transform

        self.X_train = tmp_df['image_name']
        self.y_train = tmp_df['tags']#self.mlb.fit_transform(tmp_df['tags'].str.split()).astype(np.float32)

    def __getitem__(self, index):
        img = Image.open(self.img_path + self.X_train[index])
        img = img.convert('RGB')
        if self.transform is not None:
            img = self.transform(img)
        
        label = (self.y_train[index])
        return img, label

    def __len__(self):
        return len(self.X_train.index)

task_test_accuracy=[]
def train(model, device, train_loader, optimizer, epochs,test_loader):
    options = dict(fillarea=True,width=400,height=400,xlabel='Batch_ID(Iterations)',ylabel='Loss',title='Train Loss')
    acc_options = dict(fillarea=True,width=400,height=400,xlabel='Epoch',ylabel='Accuracy',title='Test Acc')
    win = vis.line(X=np.array([0]),Y=np.array([0.7]),win='1',name='1',opts=options)
    model.train()
    total_batchid=0
    for epoch in range(0,epochs):
        running_loss = 0.0
        for batch_idx, (data, target) in enumerate(train_loader):
            data, target = data.to(device), target.to(device)
            optimizer.zero_grad()
            output = model(data)
            loss = nn.CrossEntropyLoss()(output, target)
            loss.backward()
            optimizer.step()
            running_loss += loss.item()
            if (batch_idx+1) % 10 == 0:
                vis.line(X=np.array([total_batchid+batch_idx]),Y=np.array([loss.item()]),win=win,update='append')
                print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                    epoch, batch_idx * len(data), len(train_loader.dataset),
                    100. * batch_idx / len(train_loader), loss.item()))
       # if (batch_idx+1) % 1000 == 0:
        total_batchid=total_batchid+batch_idx
        torch.save(model.state_dict(),'saved_models/model_'+str(epoch)+'_'+str(loss)+'.pt')
        accuracy=test(model,device,test_loader)
        task_test_accuracy.append(accuracy)
        vis.bar(X=np.array(task_test_accuracy),win='ACC',opts=acc_options)

def test(model, device, test_loader):
    model.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            #print(output,target)
            test_loss += F.nll_loss(output, target, reduction='sum').item()
            pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(test_loader.dataset)
    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))
    return 100. * correct / len(test_loader.dataset)


#transformations = transforms.Compose([transforms.ToTensor()])
transform = transforms.Compose([
    transforms.Resize(256),
   transforms.RandomSizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
    transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))
])

dset = KaggleAmazonDataset('training-data/train.csv','training-data/train-images/','jpg',transform,train=True)
dset_test = KaggleAmazonDataset('training-data/train.csv','training-data/train-images/','jpg',transform,train=False)
train_loader = DataLoader(dset,batch_size=13,shuffle=True,num_workers=4)# pin_memory=True # CUDA only
test_loader = DataLoader(dset_test,batch_size=32,shuffle=False,num_workers=4)# pin_memory=True # CUDA only
use_cuda = torch.cuda.is_available()
model =torchvision.models.resnet18(pretrained='imagenet')
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 43)
device =torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Device is",device)
if use_cuda:
    #model.to(device)
    print("cahmp")
    model = torch.nn.DataParallel(model, device_ids=range(torch.cuda.device_count())).cuda()
    #cudnn.benchmark = True

#model = models.SimpleNet(num_classes=43).to(device)
#print(model)nvi
optimizer = optim.SGD(model.parameters(), lr=0.01,momentum=0.9)


# for epoch in range(1, 3):
    #test(model,device,test_loader)
train(model, device, train_loader, optimizer, 50,test_loader)