{"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 # for reading csv file\n\n#train and test images are .7z archives, we need to unpack them\n!pip install py7zr\n\nfrom py7zr import unpack_7zarchive\nimport shutil\n\n#Before using, we need to register unpack format\nshutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)\n\n#unpack train images in /kaggle/working or /kaggle/temp\nshutil.unpack_archive('/kaggle/input/cifar-10/train.7z', '/kaggle/temp/')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-26T18:35:08.055836Z","iopub.execute_input":"2024-02-26T18:35:08.056194Z","iopub.status.idle":"2024-02-26T18:35:20.623354Z","shell.execute_reply.started":"2024-02-26T18:35:08.056165Z","shell.execute_reply":"2024-02-26T18:35:20.621570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nif torch.cuda.is_available():\n    device=torch.device(type=\"cuda\")\nelse:\n    device=torch.device(type=\"cpu\")  ","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:35:20.624240Z","iopub.status.idle":"2024-02-26T18:35:20.624564Z","shell.execute_reply.started":"2024-02-26T18:35:20.624403Z","shell.execute_reply":"2024-02-26T18:35:20.624419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv(\"/kaggle/input/cifar-10/trainLabels.csv\", header='infer')\nclasses = train_labels['label'].unique()\n\nword2num={}\nnum2word={}\n\ni=0\nfor cls in classes:\n    word2num[cls]=i\n    i+=1\n    \nfor i in range(len(classes)):\n    num2word[i]=classes[i]","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:35:20.626788Z","iopub.status.idle":"2024-02-26T18:35:20.627266Z","shell.execute_reply.started":"2024-02-26T18:35:20.627017Z","shell.execute_reply":"2024-02-26T18:35:20.627036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.transforms import ToTensor, Normalize, Resize, Compose\nimport os\nfrom torchvision.io import read_image","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:35:20.629064Z","iopub.status.idle":"2024-02-26T18:35:20.629924Z","shell.execute_reply.started":"2024-02-26T18:35:20.629682Z","shell.execute_reply":"2024-02-26T18:35:20.629702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class 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])])\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'\n        img=read_image(finalpath)/255\n        img=self.transform(img)\n        label=self.labels.iloc[idx,1]\n        label=word2num[label]\n        return img,label","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:38:03.370350Z","iopub.execute_input":"2024-02-26T18:38:03.370730Z","iopub.status.idle":"2024-02-26T18:38:03.379470Z","shell.execute_reply.started":"2024-02-26T18:38:03.370701Z","shell.execute_reply":"2024-02-26T18:38:03.378447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=TrainDataset('/kaggle/temp/train','/kaggle/input/cifar-10/trainLabels.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:38:04.249339Z","iopub.execute_input":"2024-02-26T18:38:04.249713Z","iopub.status.idle":"2024-02-26T18:38:04.272641Z","shell.execute_reply.started":"2024-02-26T18:38:04.249685Z","shell.execute_reply":"2024-02-26T18:38:04.271745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import mobilenet_v3_large, MobileNet_V3_Large_Weights","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:35:49.536387Z","iopub.execute_input":"2024-02-26T18:35:49.536753Z","iopub.status.idle":"2024-02-26T18:35:49.541100Z","shell.execute_reply.started":"2024-02-26T18:35:49.536720Z","shell.execute_reply":"2024-02-26T18:35:49.540105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(mobilenet_v3_large(weights=MobileNet_V3_Large_Weights.DEFAULT))","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:35:51.145699Z","iopub.execute_input":"2024-02-26T18:35:51.146030Z","iopub.status.idle":"2024-02-26T18:35:51.324537Z","shell.execute_reply.started":"2024-02-26T18:35:51.146005Z","shell.execute_reply":"2024-02-26T18:35:51.323495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MobileNetforCIFAR(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.pretrainednet=mobilenet_v3_large(weights=MobileNet_V3_Large_Weights.DEFAULT)\n        self.pretrainednet.classifier=nn.Sequential(\n            nn.Linear(in_features=960, out_features=1280, bias=True),\n            nn.Hardswish(), \n            nn.Dropout(p=0.2, inplace=True), \n            nn.Linear(in_features=1280, out_features=10, bias=True)\n        )\n        \n    def forward(self,x):\n        x=self.pretrainednet(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:38:17.367272Z","iopub.execute_input":"2024-02-26T18:38:17.367898Z","iopub.status.idle":"2024-02-26T18:38:17.374380Z","shell.execute_reply.started":"2024-02-26T18:38:17.367865Z","shell.execute_reply":"2024-02-26T18:38:17.373439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size=64    \ntrain_dl = DataLoader(dataset=train_data, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:38:17.726568Z","iopub.execute_input":"2024-02-26T18:38:17.727228Z","iopub.status.idle":"2024-02-26T18:38:17.732090Z","shell.execute_reply.started":"2024-02-26T18:38:17.727198Z","shell.execute_reply":"2024-02-26T18:38:17.731035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_one_epoch(data, model, loss_fn, optimizer):\n    model.train()\n    track_loss=0\n    num_correct=0\n    num_param=0\n    \n    for i,(img,label)in enumerate(data):\n        img=img.to(device)\n        label=label.to(device)\n        pred=model(img)\n        \n        loss = loss_fn(pred,label)\n        track_loss += loss.item()\n        num_correct += (torch.argmax(pred,dim=1)==label).type(torch.float).sum().item()\n        \n        running_loss=round(track_loss/(i+(img.shape[0]/batch_size)),2)\n        running_acc=round((num_correct/((i*batch_size+img.shape[0])))*100,2)\n        \n        loss.backward()\n        optimizer.step()\n        optimizer.zero_grad()\n\n    epoch_loss=running_loss\n    epoch_acc=running_acc\n    return epoch_loss, epoch_acc","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:38:21.978508Z","iopub.execute_input":"2024-02-26T18:38:21.979406Z","iopub.status.idle":"2024-02-26T18:38:21.987416Z","shell.execute_reply.started":"2024-02-26T18:38:21.979373Z","shell.execute_reply":"2024-02-26T18:38:21.986433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=MobileNetforCIFAR()\nmodel.to(device)\n\nfor param in model.pretrainednet.features.parameters():\n    param.requires_grad=False\n    \nloss_fn=nn.CrossEntropyLoss()\nlr=0.001\noptimizer=torch.optim.Adam(params=model.parameters(), lr=lr)\nn_epochs=5","metadata":{"execution":{"iopub.status.busy":"2024-02-26T18:51:18.358741Z","iopub.execute_input":"2024-02-26T18:51:18.359174Z","iopub.status.idle":"2024-02-26T18:51:18.565009Z","shell.execute_reply.started":"2024-02-26T18:51:18.359136Z","shell.execute_reply":"2024-02-26T18:51:18.564205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(n_epochs):\n    print(\"Epoch No:\",i+1)\n    train_epoch_loss, train_epoch_acc=train_one_epoch(train_dl,model,loss_fn,optimizer)\n    print(\"Training:\", \"Epoch Loss:\", train_epoch_loss, \"Epoch Accuracy:\", train_epoch_acc)\n    print(\"--------------------------------------------------\")\n    \n#first learning and optimising weights the newly added classifier layers\n#then optimise entire network by fine tuning earlier layers\n\nfor param in model.pretrainednet.features.parameters():\n    param.requires_grad=True\n\nfor i in range(n_epochs):\n    print(\"Epoch No:\",i+1)\n    train_epoch_loss, train_epoch_acc=train_one_epoch(train_dl,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-02-26T18:51:23.786610Z","iopub.execute_input":"2024-02-26T18:51:23.787597Z","iopub.status.idle":"2024-02-26T19:09:14.220865Z","shell.execute_reply.started":"2024-02-26T18:51:23.787566Z","shell.execute_reply":"2024-02-26T19:09:14.219966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#unpacking test images 3 lacs images\nshutil.unpack_archive('/kaggle/input/cifar-10/test.7z', '/kaggle/temp/')\n\n#unregister unpack format, we are done with it\nshutil.unregister_unpack_format('7zip')","metadata":{"execution":{"iopub.status.busy":"2024-02-26T19:11:56.189357Z","iopub.execute_input":"2024-02-26T19:11:56.189969Z","iopub.status.idle":"2024-02-26T19:29:12.465102Z","shell.execute_reply.started":"2024-02-26T19:11:56.189940Z","shell.execute_reply":"2024-02-26T19:29:12.464093Z"},"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","metadata":{"execution":{"iopub.status.busy":"2024-02-26T19:30:31.339400Z","iopub.execute_input":"2024-02-26T19:30:31.340103Z","iopub.status.idle":"2024-02-26T19:30:31.347562Z","shell.execute_reply.started":"2024-02-26T19:30:31.340043Z","shell.execute_reply":"2024-02-26T19:30:31.346533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = TestDataset('/kaggle/temp/test/')\ntest_dl = DataLoader(test_data, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-02-26T19:30:50.296878Z","iopub.execute_input":"2024-02-26T19:30:50.298040Z","iopub.status.idle":"2024-02-26T19:30:50.641813Z","shell.execute_reply.started":"2024-02-26T19:30:50.298004Z","shell.execute_reply":"2024-02-26T19:30:50.640965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate(data, model,loss_fn, path):\n    model.eval()\n    df=pd.read_csv(path)\n    with torch.no_grad():\n        for i, img in enumerate(data):\n            finalbatchpred=np.zeros(img.shape[0],dtype='object')\n            img=img.to(device)\n            pred=model(img)\n            \n            pred=torch.argmax(pred,dim=1).type(torch.int).cpu()\n            \n            for j,p in enumerate(pred):\n                finalbatchpred[j]=num2word[p.item()]\n            df.iloc[i*batch_size:i*batch_size+batch_size ,1]=finalbatchpred\n    \n    df.to_csv('submission.csv', index=False)\n    df.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-26T19:31:02.824406Z","iopub.execute_input":"2024-02-26T19:31:02.824738Z","iopub.status.idle":"2024-02-26T19:31:02.832220Z","shell.execute_reply.started":"2024-02-26T19:31:02.824712Z","shell.execute_reply":"2024-02-26T19:31:02.831213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluate(test_dl, model,loss_fn, '/kaggle/input/cifar-10/sampleSubmission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-26T19:31:20.189520Z","iopub.execute_input":"2024-02-26T19:31:20.189985Z","iopub.status.idle":"2024-02-26T19:39:33.364380Z","shell.execute_reply.started":"2024-02-26T19:31:20.189955Z","shell.execute_reply":"2024-02-26T19:39:33.363447Z"},"trusted":true},"execution_count":null,"outputs":[]}]}