{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd ","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:47:57.016900Z","iopub.execute_input":"2024-03-03T05:47:57.017241Z","iopub.status.idle":"2024-03-03T05:47:57.970028Z","shell.execute_reply.started":"2024-03-03T05:47:57.017211Z","shell.execute_reply":"2024-03-03T05:47:57.969183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:47:57.971720Z","iopub.execute_input":"2024-03-03T05:47:57.972127Z","iopub.status.idle":"2024-03-03T05:48:23.906329Z","shell.execute_reply.started":"2024-03-03T05:47:57.972089Z","shell.execute_reply":"2024-03-03T05:48:23.905411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from py7zr import unpack_7zarchive\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:48:23.907604Z","iopub.execute_input":"2024-03-03T05:48:23.907912Z","iopub.status.idle":"2024-03-03T05:48:24.067205Z","shell.execute_reply.started":"2024-03-03T05:48:23.907885Z","shell.execute_reply":"2024-03-03T05:48:24.066521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This registers a '7zip' unpacking format with shutil and unpacks 'train.7z' into '/kaggle/temp/'.\n\nshutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)\n\nshutil.unpack_archive('/kaggle/input/cifar-10/train.7z', '/kaggle/temp/')","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:48:24.068828Z","iopub.execute_input":"2024-03-03T05:48:24.069391Z","iopub.status.idle":"2024-03-03T05:48:50.383949Z","shell.execute_reply.started":"2024-03-03T05:48:24.069359Z","shell.execute_reply":"2024-03-03T05:48:50.383186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking for availability of GPU or else using CPU\n\nimport torch\nif torch.cuda.is_available():\n    device=torch.device(type=\"cuda\", index=0)\nelse:\n    device=torch.device(type=\"cpu\", index=0)            ","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:48:50.386900Z","iopub.execute_input":"2024-03-03T05:48:50.387267Z","iopub.status.idle":"2024-03-03T05:48:53.789418Z","shell.execute_reply.started":"2024-03-03T05:48:50.387234Z","shell.execute_reply":"2024-03-03T05:48:53.788165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creates dictionaries name2num and num2name for mapping label to number and vice-versa\n\ntrain_labels=pd.read_csv(\"/kaggle/input/cifar-10/trainLabels.csv\", header='infer')\n\nclasses=train_labels['label'].unique()\nprint(classes)\n\nname2num={}\ni=0\nfor name in classes:\n    name2num[name]=i\n    i=i+1\n\nnum2name={}\nfor i in range(len(classes)):\n    num2name[i]=classes[i]","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:48:53.792659Z","iopub.execute_input":"2024-03-03T05:48:53.793598Z","iopub.status.idle":"2024-03-03T05:48:53.990785Z","shell.execute_reply.started":"2024-03-03T05:48:53.793568Z","shell.execute_reply":"2024-03-03T05:48:53.989798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset\nfrom torch.utils.data import DataLoader\nimport os\nfrom torchvision.io import read_image\nfrom torchvision.transforms import ToTensor, Normalize, Resize, Compose\nfrom torch.optim.lr_scheduler import StepLR\n\nclass TrainDataset(Dataset):\n    def __init__(self, imgpath, labelpath):\n        super().__init__()\n        self.imgpath=imgpath\n        self.labelpath=labelpath\n        self.labels=pd.read_csv(labelpath, header='infer')\n        self.transform=Compose([Resize((224,224), antialias=True), Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])]) # standard transformations\n        \n    def __len__(self):\n        return self.labels.shape[0]\n    \n    def __getitem__(self,idx):\n        finalpath=os.path.join(self.imgpath,str(idx+1))+'.png' # Generating the complete path to the image\n        img=read_image(finalpath)/255 # normalizing image\n        img=self.transform(img)\n        label=self.labels.iloc[idx,1]\n        label=name2num[label]\n        return img,label\n\ntraindataset=TrainDataset('/kaggle/temp/train','/kaggle/input/cifar-10/trainLabels.csv')        \n        \nbatch_size=64    \ntraindataloader=DataLoader(dataset=traindataset, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:48:53.992137Z","iopub.execute_input":"2024-03-03T05:48:53.992496Z","iopub.status.idle":"2024-03-03T05:48:57.209399Z","shell.execute_reply.started":"2024-03-03T05:48:53.992464Z","shell.execute_reply":"2024-03-03T05:48:57.208452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nfrom torchvision.models import mobilenet_v3_large, MobileNet_V3_Large_Weights\nclass Cifar10Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.pretrainednet=mobilenet_v3_large(weights=MobileNet_V3_Large_Weights.DEFAULT)\n        \n        # Modifying the classifier of the pretrained MobileNetV3 model \n        # since the default classifer used to classify 1000 classes but we only requires 10\n        # you can use your own but it should match input according to your last layer of model and output to desire dataset\n        self.pretrainednet.classifier=nn.Sequential(\n            \n            nn.Linear(in_features=960, out_features=1024, bias=True),\n            nn.Hardswish(),\n            nn.BatchNorm1d(1024),\n            nn.Dropout(p=0.5),\n            nn.Linear(in_features=1024, out_features=512, bias=True),\n            nn.ReLU(inplace=True),\n            nn.BatchNorm1d(512),\n            nn.Dropout(p=0.3),\n            nn.Linear(in_features=512, out_features=10, bias=True)\n        )\n    def forward(self,x):\n        x=self.pretrainednet(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:48:57.210560Z","iopub.execute_input":"2024-03-03T05:48:57.210865Z","iopub.status.idle":"2024-03-03T05:48:57.218678Z","shell.execute_reply.started":"2024-03-03T05:48:57.210840Z","shell.execute_reply":"2024-03-03T05:48:57.217797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_one_epoch(dataloader, model,loss_fn, optimizer):\n    model.train()\n    track_loss=0\n    num_correct=0\n    num_param=0\n    \n    for i, (imgs, labels) in enumerate(dataloader):\n        imgs=imgs.to(device)\n        labels=labels.to(device)\n        pred=model(imgs)\n                    \n        loss=loss_fn(pred,labels)\n        track_loss+=loss.item()\n        num_correct+=(torch.argmax(pred,dim=1)==labels).type(torch.float).sum().item()\n        \n        running_loss=round(track_loss/(i+(imgs.shape[0]/batch_size)),2)\n        running_acc=round((num_correct/((i*batch_size+imgs.shape[0])))*100,2)\n        \n        loss.backward()\n        optimizer.step()\n        optimizer.zero_grad()\n\n#         if i%100==0:\n#             print(\"Batch:\", i+1, \"/\",len(dataloader), \"Running Loss:\",running_loss, \"Running Accuracy:\",running_acc)\n            \n    epoch_loss=running_loss\n    epoch_acc=running_acc\n    return epoch_loss, epoch_acc","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:48:57.219902Z","iopub.execute_input":"2024-03-03T05:48:57.220475Z","iopub.status.idle":"2024-03-03T05:48:57.232023Z","shell.execute_reply.started":"2024-03-03T05:48:57.220444Z","shell.execute_reply":"2024-03-03T05:48:57.231197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Cifar10Net()\nmodel=model.to(device)\n\nfor param in model.pretrainednet.features.parameters():\n    param.requires_grad=False\n\nloss_fn=nn.CrossEntropyLoss()\nlr=0.001\nlr_step_size = 5\nlr_gamma = 0.8 \n\n# If lr_gamma = 0.9 lr is reduced by 10% every 5 epochs.\n# lr_gamma = 0.5 lr is halved every 5 epochs.\n# If you want an even more aggressive decay, choose lr_gamma = 0.1 mean the lr is reduced to 10% of its previous value every 5 epochs.\n\n#optimizer=torch.optim.SGD(params=model.parameters(), lr=lr)\noptimizer=torch.optim.Adam(params=model.parameters(), lr=lr)\nscheduler = StepLR(optimizer, step_size=lr_step_size, gamma=lr_gamma)\n\nn_epochs=25\n\nfor i in range(n_epochs):\n    print(\"Epoch No:\",i+1)\n    train_epoch_loss, train_epoch_acc=train_one_epoch(traindataloader,model,loss_fn,optimizer)\n    print(\"Training:\", \"Epoch Loss:\", train_epoch_loss, \"Epoch Accuracy:\", train_epoch_acc)\n    print(\"--------------------------------------------------\")\n\nfor param in model.pretrainednet.features.parameters():\n    param.requires_grad=True\n\nn_epochs = 15\n\nfor i in range(n_epochs):\n    print(\"Epoch No:\",i+1)\n    train_epoch_loss, train_epoch_acc=train_one_epoch(traindataloader,model,loss_fn,optimizer)\n    print(\"Training:\", \"Epoch Loss:\", train_epoch_loss, \"Epoch Accuracy:\", train_epoch_acc)\n    print(\"--------------------------------------------------\")","metadata":{"execution":{"iopub.status.busy":"2024-03-03T05:48:57.233136Z","iopub.execute_input":"2024-03-03T05:48:57.233456Z","iopub.status.idle":"2024-03-03T06:51:35.306887Z","shell.execute_reply.started":"2024-03-03T05:48:57.233428Z","shell.execute_reply":"2024-03-03T06:51:35.305981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Unpack the archive file '/kaggle/input/cifar-10/test.7z' into the directory '/kaggle/temp/'\nshutil.unpack_archive('/kaggle/input/cifar-10/test.7z', '/kaggle/temp/')\n\n# Unregister the format used for unpacking 7zip files\nshutil.unregister_unpack_format('7zip')\n","metadata":{"execution":{"iopub.status.busy":"2024-03-03T06:51:35.308388Z","iopub.execute_input":"2024-03-03T06:51:35.308680Z","iopub.status.idle":"2024-03-03T06:54:42.828659Z","shell.execute_reply.started":"2024-03-03T06:51:35.308656Z","shell.execute_reply":"2024-03-03T06:54:42.827890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self, imgpath):\n        super().__init__()\n        self.imgpath=imgpath\n        _,_,self.files=next(os.walk(self.imgpath))\n        self.length=len(self.files)\n        self.transform=Compose([Resize((224,224), antialias=True), Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])])        \n    \n    def __len__(self):\n        return self.length\n    \n    def __getitem__(self,idx):\n        finalpath=os.path.join(self.imgpath,str(idx+1))+'.png'\n        img=read_image(finalpath)/255.0\n        img=self.transform(img)\n        return img\n\ntestdataset=TestDataset('/kaggle/temp/test/')\ntestdataloader=DataLoader(dataset=testdataset, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-03-03T06:54:42.829954Z","iopub.execute_input":"2024-03-03T06:54:42.830310Z","iopub.status.idle":"2024-03-03T06:54:43.193793Z","shell.execute_reply.started":"2024-03-03T06:54:42.830280Z","shell.execute_reply":"2024-03-03T06:54:43.193003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def eval(dataloader, model,loss_fn, path):\n    model.eval()\n    data=pd.read_csv(path)\n    with torch.no_grad():\n        for i, imgs in enumerate(dataloader):\n            \n            # Create an array to store final batch predictions\n            finalbatchpred=np.zeros(imgs.shape[0],dtype='object')\n            imgs=imgs.to(device)\n            pred=model(imgs)\n            \n            pred=torch.argmax(pred,dim=1).type(torch.int).cpu()\n            \n            # Map integer predictions to class names\n            for j,p in enumerate(pred):\n                finalbatchpred[j]=num2name[p.item()]\n                \n            # Update the corresponding rows in the DataFrame with predictions\n            data.iloc[i*batch_size:i*batch_size+batch_size ,1]=finalbatchpred\n    \n    data.to_csv('submission.csv', index=False)\n    data.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-03T06:54:43.194824Z","iopub.execute_input":"2024-03-03T06:54:43.195079Z","iopub.status.idle":"2024-03-03T06:54:43.294267Z","shell.execute_reply.started":"2024-03-03T06:54:43.195051Z","shell.execute_reply":"2024-03-03T06:54:43.293499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval(testdataloader, model,loss_fn, '/kaggle/input/cifar-10/sampleSubmission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-03-03T07:02:41.722149Z","iopub.execute_input":"2024-03-03T07:02:41.722460Z","iopub.status.idle":"2024-03-03T07:10:40.884416Z","shell.execute_reply.started":"2024-03-03T07:02:41.722435Z","shell.execute_reply":"2024-03-03T07:10:40.883547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Upvote if you find it useful!","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}