<jupyter_start><jupyter_text>Data Preparation<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import copy


import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader,Dataset,random_split
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision.models as models
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
import os
df=pd.read_csv("./dataset/train.csv",)
print(df.head())
df.shape
train={}
for image,label in zip(df["Image"],df["target"]):
  train[image]=label
print(len(train))
print(train)<jupyter_output>364
{'96.jpg': 'manipuri', '163.jpg': 'bharatanatyam', '450.jpg': 'odissi', '219.jpg': 'kathakali', '455.jpg': 'odissi', '46.jpg': 'kathak', '326.jpg': 'sattriya', '408.jpg': 'kuchipudi', '365.jpg': 'sattriya', '422.jpg': 'kuchipudi', '397.jpg': 'kuchipudi', '400.jpg': 'kuchipudi', '32.jpg': 'kathak', '350.jpg': 'sattriya', '234.jpg': 'kathakali', '492.jpg': 'odissi', '333.jpg': 'sattriya', '376.jpg': 'kuchipudi', '103.jpg': 'manipuri', '39.jpg': 'kathak', '87.jpg': 'manipuri', '155.jpg': 'bharatanatyam', '388.jpg': 'kuchipudi', '384.jpg': 'kuchipudi', '248.jpg': 'mohiniyattam', '494.jpg': 'odissi', '47.jpg': 'kathak', '488.jpg': 'odissi', '462.jpg': 'odissi', '347.jpg': 'sattriya', '242.jpg': 'kathakali', '16.jpg': 'kathak', '178.jpg': 'kathakali', '348.jpg': 'sattriya', '164.jpg': 'bharatanatyam', '2.jpg': 'kathak', '141.jpg': 'bharatanatyam', '187.jpg': 'kathakali', '464.jpg': 'odissi', '472.jpg': 'odissi', '489.jpg': 'odissi', '387.jpg': 'kuchipudi', '109.jpg': 'manipuri', '255.jpg[...]<jupyter_text>Data Augumentation<jupyter_code>from skimage.transform import warp,rotate
from skimage.util import random_noise
from skimage.filters import gaussian
import skimage.io as io
# this will make the final csv final
train_final=[]
for image_name,label in zip(train.keys(),train.values()):
    train_final.append((image_name,label))
len(train_final)
for image_name in train.keys():
    #img=io.imread("./dataset/train/"+str(image_name))
    #rotated_image=rotate(img,angle=45,mode="wrap")
    name=str(1000+int(image_name[:-4]))+".jpg"
    train_final.append((name,train[image_name]))
    #io.imsave(name,rotated_image)
len(train_final)
for image_name in train.keys():
    #img=io.imread("./dataset/train/"+str(image_name))
    #rotated_image=rotate(img,angle=30,mode="wrap")
    name=str(2000+int(image_name[:-4]))+".jpg"
    train_final.append((name,train[image_name]))
    #io.imsave(name,rotated_image)
for image_name in train.keys():
    #img=io.imread("./dataset/train/"+str(image_name))
    #rotated_image=rotate(img,angle=60,mode="wrap")
    name=str(3000+int(image_name[:-4]))+".jpg"
    train_final.append((name,train[image_name]))
    #io.imsave(name,rotated_image)
train_final[0]
# now lets shift the image for 1456 images
from skimage.transform import AffineTransform
spam=[]
transform=AffineTransform(translation=(25,25))
for item in train_final:
    name=str(int(str(item[0])[:-4])+4000)+".jpg"
    label=item[1]
    spam.append((name,label))
    img=io.imread("./dataset/aug_train/"+item[0])
    wrapshift=warp(img,transform,mode="wrap")
    io.imsave(name,wrapshift)
train_final=train_final+spam
len(train_final)
spam=[]
for item in train_final:
    name=str(int(str(item[0])[:-4])+8000)+".jpg"
    label=item[1]
    spam.append((name,label))
    img=io.imread("./dataset/aug_train/"+item[0])
    flipped=np.fliplr(img)
    io.imsave(name,flipped)
train_final=train_final+spam
len(train_final)
spam=[]
for item in train_final:
    name=str(int(str(item[0])[:-4])+16000)+".jpg"
    label=item[1]
    spam.append((name,label))
    img=io.imread("./dataset/aug_train/"+item[0])
    flipped=np.flipud(img)
    io.imsave(name,flipped)
train_final=train_final+spam
len(train_final)
spam=[]
for item in train_final:
    name=str(int(str(item[0])[:-4])+32000)+".jpg"
    label=item[1]
    spam.append((name,label))
    img=io.imread("./dataset/aug_train/"+item[0])
    noisy=random_noise(img,var=0.2**2)
    io.imsave(name,noisy)
train_final=train_final+spam
len(train_final)
spam=[]
for item in train_final:
    name=str(int(str(item[0])[:-4])+128000)+".jpg"
    label=item[1]
    spam.append((name,label))
    img=io.imread("./dataset/aug_train/"+item[0])
    noisy=gaussian(img,sigma=1,multichannel=True)
    io.imsave(name,noisy)
train_final=train_final+spam
len(train_final)
train_list_final=[]
for item in train_final:
    train_list_final.append([item[0],item[1]])
df=pd.DataFrame(train_final,columns=["Image","target"])
df.to_csv("new_train.csv",index=False)
df=pd.read_csv("./new_train.csv",)
print(df.head())
df.shape
train={}
for image,label in zip(df["Image"],df["target"]):
  train[image]=label<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>class DancePredictionDataset(Dataset):

  def __init__(self,file_path,train=True,transform=None):
    self.transform=transform
    self.file_path=file_path
    self.train=train
    self.file_names=[file for _,_,files in os.walk(self.file_path) for file in files]
    self.len=len(self.file_names)
    if self.train:
      self.classes_mapping=self.get_classes()

  def __len__(self):
    return len(self.file_names)

    
  def pil_loader(self,path):
    with open(path,"rb") as f:
      img=Image.open(f)
      return img.convert("RGB")

  def __getitem__(self,index):
    file_name=self.file_names[index]
    image_data=self.pil_loader(self.file_path+"/"+file_name)
    if self.transform:
      image_data=self.transform(image_data)

    if self.train:
      class_=train[file_name]
      label=self.classes_mapping[class_]
      return image_data,label
    else:
      return image_data,file_name

  def get_classes(self):
    class_mapping={}
    class_mapping["manipuri"]=torch.tensor([1,0,0,0,0,0,0,0])
    class_mapping["bharatanatyam"]=torch.tensor([0,1,0,0,0,0,0,0])
    class_mapping["odissi"]=torch.tensor([0,0,1,0,0,0,0,0])
    class_mapping["kathakali"]=torch.tensor([0,0,0,1,0,0,0,0])
    class_mapping["kathak"]=torch.tensor([0,0,0,0,1,0,0,0])
    class_mapping["sattriya"]=torch.tensor([0,0,0,0,0,1,0,0])
    class_mapping["kuchipudi"]=torch.tensor([0,0,0,0,0,0,1,0])
    class_mapping["mohiniyattam"]=torch.tensor([0,0,0,0,0,0,0,1])
    return class_mapping
transform=transforms.Compose([transforms.Resize(256),transforms.CenterCrop(224),transforms.ToTensor()])
full_data = DancePredictionDataset("./dataset/aug_train/",train=True,transform=transform)
train_size = int(0.95 * len(full_data))
test_size = len(full_data) - train_size

train_data, validation_data = random_split(full_data, [train_size, test_size])
batch_size=32
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
validation_loader = DataLoader(validation_data, batch_size=batch_size, shuffle=True)
data_iter=iter(train_loader)
images,label=data_iter.next()
print(images.shape)
print(label.shape)
mapping={}
mapping[1]="manipuri"
mapping[2]="bharatanatyam"
mapping[3]="odissi"
mapping[4]="kathakali"
mapping[5]="kathak"
mapping[6]="sattriya"
mapping[7]="kuchipudi"
mapping[8]="mohiniyattam"
class Net(nn.Module):
    
    
    def __init__(self):
        super(Net,self).__init__()
        self.feature=resnet         
        
    def forward(self,input_):
        input_=input_.to(device)
        out=self.feature(input_)        
        return out
def eval(net,dataloader,device="cpu"):
    total=0
    correct=0
    for data in dataloader:
        net.load_state_dict(best_model)
        net.eval()
        img,label=data
        img,label=img.to(device),label.to(device)
        pred=net(img)
        for i in range(pred.shape[0]):
            total+=1
            if (pred[i].argmax()==label[i].argmax()):
                correct+=1
                
        del img,label,pred
        torch.cuda.empty_cache
    
    accuracy=(correct/total)*100
    return accuracy
resnet=models.resnet50(pretrained=True)
resnet.to(device)
net=Net()
print(resnet)
for param in net.parameters():
    param.requires_grad=False<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup