{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[],"gpuType":"T4","authorship_tag":"ABX9TyOK0gWCyzbiPRIV/Ef+pAFQ"},"accelerator":"GPU","kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":31011,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport os\nfrom PIL import Image\nfrom pathlib import Path\nimport pandas as pd\nimport torchvision\nfrom sklearn.model_selection import train_test_split","metadata":{"id":"7SI2AJxEuVQM","executionInfo":{"status":"ok","timestamp":1746958083609,"user_tz":-330,"elapsed":9846,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:52:30.623879Z","iopub.execute_input":"2025-05-11T10:52:30.624145Z","iopub.status.idle":"2025-05-11T10:52:34.318065Z","shell.execute_reply.started":"2025-05-11T10:52:30.624122Z","shell.execute_reply":"2025-05-11T10:52:34.317301Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels=pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ntrain,valid=train_test_split(labels,train_size=0.8,shuffle=True,stratify=labels['breed'],random_state=42)","metadata":{"id":"vSEDkkr6u26J","executionInfo":{"status":"ok","timestamp":1746958083628,"user_tz":-330,"elapsed":15,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:52:34.319302Z","iopub.execute_input":"2025-05-11T10:52:34.319698Z","iopub.status.idle":"2025-05-11T10:52:34.370296Z","shell.execute_reply.started":"2025-05-11T10:52:34.319658Z","shell.execute_reply":"2025-05-11T10:52:34.369813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train,train_labels=train['id'].reset_index(drop=True),train['breed'].reset_index(drop=True)\nval,val_labels=valid['id'].reset_index(drop=True),valid['breed'].reset_index(drop=True)","metadata":{"id":"2zRNdpYXu5kc","executionInfo":{"status":"ok","timestamp":1746958083640,"user_tz":-330,"elapsed":3,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:52:34.370900Z","iopub.execute_input":"2025-05-11T10:52:34.371076Z","iopub.status.idle":"2025-05-11T10:52:34.375932Z","shell.execute_reply.started":"2025-05-11T10:52:34.371061Z","shell.execute_reply":"2025-05-11T10:52:34.375324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''Encoding labels'''\n\nbreeds=dict()\nbreed_count=1\n\nfor breed in labels['breed'].value_counts().index:\n\n    breeds[breed]=breed_count-1\n    breed_count+=1\n\nval_labels_torch=torch.zeros(len(val_labels),1)\ntrain_labels_torch=torch.zeros(len(train_labels),1)\n\nfor index in val_labels.index:\n    val_labels_torch[index]=breeds[val_labels.iloc[index]]\n\nfor index in train_labels.index:\n    train_labels_torch[index]=breeds[train_labels.iloc[index]]\n\nval_labels_torch = val_labels_torch.long()\ntrain_labels_torch = train_labels_torch.long()\n\n\nval_labels_torch[91]\n# val_labels=torch.tensor(val_labels.values)\n# train_labels=torch.tensor(train_labels.values)","metadata":{"id":"WIHO4n_Ju8nM","executionInfo":{"status":"ok","timestamp":1746958083769,"user_tz":-330,"elapsed":125,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"outputId":"8cb81f81-2891-474c-82bb-ea487051d674","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:52:34.377427Z","iopub.execute_input":"2025-05-11T10:52:34.377849Z","iopub.status.idle":"2025-05-11T10:52:34.549200Z","shell.execute_reply.started":"2025-05-11T10:52:34.377831Z","shell.execute_reply":"2025-05-11T10:52:34.548664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform = torchvision.transforms.Compose([\n    torchvision.transforms.Resize((224, 224)),\n    torchvision.transforms.RandomHorizontalFlip(),\n    torchvision.transforms.RandomRotation(10),\n    torchvision.transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),\n    torchvision.transforms.ToTensor()\n])\n\n\ndef create_dataset(series, directory_path, transform):\n    tensors = []\n    for i in range(len(series)):\n        if(series[i].endswith('.jpg')):\n            img_file=series[i]\n        else:\n            img_file = series[i] + '.jpg'\n        img_path = os.path.join(directory_path, img_file)\n        img = Image.open(img_path).convert('RGB')  # Ensure RGB\n        img_t = transform(img)\n        tensors.append(img_t)\n    return torch.stack(tensors, dim=0)\n","metadata":{"id":"1cVs4iXju9d4","executionInfo":{"status":"ok","timestamp":1746958083775,"user_tz":-330,"elapsed":4,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:12:27.585057Z","iopub.execute_input":"2025-05-11T11:12:27.585533Z","iopub.status.idle":"2025-05-11T11:12:27.591033Z","shell.execute_reply.started":"2025-05-11T11:12:27.585509Z","shell.execute_reply":"2025-05-11T11:12:27.590383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"directory_path=Path('/kaggle/input/dog-breed-identification/train')\ntrain_dataset=create_dataset(train,directory_path,transform)","metadata":{"id":"GDdwcrUpvBPW","executionInfo":{"status":"ok","timestamp":1746958140954,"user_tz":-330,"elapsed":57177,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:52:34.555631Z","iopub.execute_input":"2025-05-11T10:52:34.555881Z","iopub.status.idle":"2025-05-11T10:54:15.821184Z","shell.execute_reply.started":"2025-05-11T10:52:34.555863Z","shell.execute_reply":"2025-05-11T10:54:15.820598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# For validation: no augmentation\nval_transform = torchvision.transforms.Compose([\n    torchvision.transforms.Resize((224, 224)),\n    torchvision.transforms.ToTensor()\n])\nval_dataset = create_dataset(val, directory_path, transform=val_transform)\n","metadata":{"id":"Hx9KbX_svEvA","executionInfo":{"status":"ok","timestamp":1746958149022,"user_tz":-330,"elapsed":8072,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:54:15.821952Z","iopub.execute_input":"2025-05-11T10:54:15.822221Z","iopub.status.idle":"2025-05-11T10:54:36.535314Z","shell.execute_reply.started":"2025-05-11T10:54:15.822197Z","shell.execute_reply":"2025-05-11T10:54:36.534706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torch.utils.data import TensorDataset\n\ntrain_dataset_torch=TensorDataset(train_dataset,train_labels_torch)\nval_dataset_torch=TensorDataset(val_dataset,val_labels_torch)","metadata":{"id":"VvTnPpwovFYX","executionInfo":{"status":"ok","timestamp":1746958149029,"user_tz":-330,"elapsed":3,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:54:36.536234Z","iopub.execute_input":"2025-05-11T10:54:36.536493Z","iopub.status.idle":"2025-05-11T10:54:36.540731Z","shell.execute_reply.started":"2025-05-11T10:54:36.536469Z","shell.execute_reply":"2025-05-11T10:54:36.540055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset.shape,train_labels_torch.shape","metadata":{"id":"Qpoq_g4CvHc8","executionInfo":{"status":"ok","timestamp":1746958149056,"user_tz":-330,"elapsed":6,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"outputId":"9bf8c22c-75ab-42de-fed8-3cb51ad11b8d","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:54:36.541295Z","iopub.execute_input":"2025-05-11T10:54:36.541466Z","iopub.status.idle":"2025-05-11T10:54:36.558299Z","shell.execute_reply.started":"2025-05-11T10:54:36.541451Z","shell.execute_reply":"2025-05-11T10:54:36.557730Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_dataset_torch[0]","metadata":{"id":"BbIhL7JEvJXn","executionInfo":{"status":"ok","timestamp":1746958149205,"user_tz":-330,"elapsed":148,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"outputId":"d5008de8-3971-4eba-826e-b56924d88b5a","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:54:36.560057Z","iopub.execute_input":"2025-05-11T10:54:36.560237Z","iopub.status.idle":"2025-05-11T10:54:36.589303Z","shell.execute_reply.started":"2025-05-11T10:54:36.560222Z","shell.execute_reply":"2025-05-11T10:54:36.588826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torchvision.models import resnet101, ResNet101_Weights\nimport torch.nn as nn\nimport torch.optim as optim\n\nresnet = resnet101(weights=ResNet101_Weights.IMAGENET1K_V1)\nfor param in resnet.parameters():\n    param.requires_grad = False\n\n\nresnet.fc = nn.Sequential(\n    nn.Linear(resnet.fc.in_features, 256),  # From ResNet's output to 256 units\n    nn.ReLU(),                              # Activation function\n    nn.BatchNorm1d(256),                    # Normalization for stability\n    nn.Linear(256, 256),                    # Additional dense layer\n    nn.ReLU(),                              # Activation again\n    nn.Dropout(0.3),                        # Dropout to reduce overfitting\n    nn.BatchNorm1d(256),                    # Another batch norm layer\n    nn.Linear(256, 120)                     # Final layer: 120 output classes\n)\nmodel_torch = resnet\n\noptimizier=optim.Adam(model_torch.parameters(),lr=1e-4)\nloss_fn=nn.CrossEntropyLoss()","metadata":{"id":"bd2sqqsZvLMS","executionInfo":{"status":"ok","timestamp":1746958152241,"user_tz":-330,"elapsed":3038,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"outputId":"3103c565-08ca-4618-9f68-8726641c4eb9","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:54:36.589871Z","iopub.execute_input":"2025-05-11T10:54:36.590174Z","iopub.status.idle":"2025-05-11T10:54:38.258034Z","shell.execute_reply.started":"2025-05-11T10:54:36.590157Z","shell.execute_reply":"2025-05-11T10:54:38.257245Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\ntrain_loader = DataLoader(train_dataset_torch, batch_size=64, shuffle=True)\nval_loader = DataLoader(val_dataset_torch, batch_size=64, shuffle=False)\n\nn_epochs=20\nmodel_torch=model_torch.to('cuda')\nfor i in range(1,n_epochs+1):\n    model_torch.train()\n    training_loss=0.0\n    for img,label in train_loader:\n        img=img.to('cuda')\n        label=label.to('cuda')\n        label=label.squeeze(1)\n        outputs=model_torch(img)\n        optimizier.zero_grad()\n        losses=loss_fn(outputs,label)\n        training_loss+=losses.item()\n        losses.backward()\n        optimizier.step()\n\n    model_torch.eval()\n    val_loss=0.0\n    correct = 0\n    total = 0\n\n    with torch.no_grad():\n        for img,label in val_loader:\n            img=img.to('cuda')\n            label=label.to('cuda')\n            label=label.squeeze(1)\n            outputs=model_torch(img)\n            loss=loss_fn(outputs,label)\n            val_loss+=loss.item()\n\n            _, predicted = torch.max(outputs, 1)\n            total += label.size(0)\n            correct += (predicted == label).sum().item()\n    print(f\"Epoch [{i}/{n_epochs}], Training Loss: {training_loss/len(train_loader):.4f}, Validation Loss: {val_loss/len(val_loader):.4f}, Accuracy: {100 * correct / total:.2f}%\")\n","metadata":{"id":"3uHyvWMyvN_Z","executionInfo":{"status":"ok","timestamp":1746958863365,"user_tz":-330,"elapsed":529574,"user":{"displayName":"Sai","userId":"00014113356133247508"}},"outputId":"c618767b-5afc-48f5-a988-9649c41958e9","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T10:56:43.411326Z","iopub.execute_input":"2025-05-11T10:56:43.411885Z","iopub.status.idle":"2025-05-11T11:06:01.572609Z","shell.execute_reply.started":"2025-05-11T10:56:43.411860Z","shell.execute_reply":"2025-05-11T11:06:01.572009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.save(resnet.state_dict(), 'resnet_custom_head.pth')\n","metadata":{"id":"UeMv4VYL4lXO","trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:07:14.878506Z","iopub.execute_input":"2025-05-11T11:07:14.879011Z","iopub.status.idle":"2025-05-11T11:07:15.132187Z","shell.execute_reply.started":"2025-05-11T11:07:14.878985Z","shell.execute_reply":"2025-05-11T11:07:15.131634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir('/kaggle/input/dog-breed-identification/test')[1:10]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:10:33.709149Z","iopub.execute_input":"2025-05-11T11:10:33.709415Z","iopub.status.idle":"2025-05-11T11:10:33.717613Z","shell.execute_reply.started":"2025-05-11T11:10:33.709394Z","shell.execute_reply":"2025-05-11T11:10:33.716823Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ntest=os.listdir('/kaggle/input/dog-breed-identification/test')\ntest_dataset=create_dataset(test,'/kaggle/input/dog-breed-identification/test',val_transform)\ntest_dataset=test_dataset.to(device='cuda')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:19:30.940493Z","iopub.execute_input":"2025-05-11T11:19:30.940986Z","iopub.status.idle":"2025-05-11T11:20:25.276545Z","shell.execute_reply.started":"2025-05-11T11:19:30.940956Z","shell.execute_reply":"2025-05-11T11:20:25.275972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\noutput=pd.DataFrame(columns=['file',*breeds.keys()])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:39:56.291446Z","iopub.execute_input":"2025-05-11T11:39:56.292140Z","iopub.status.idle":"2025-05-11T11:39:56.298940Z","shell.execute_reply.started":"2025-05-11T11:39:56.292113Z","shell.execute_reply":"2025-05-11T11:39:56.298100Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:40:09.559552Z","iopub.execute_input":"2025-05-11T11:40:09.559935Z","iopub.status.idle":"2025-05-11T11:40:09.568691Z","shell.execute_reply.started":"2025-05-11T11:40:09.559907Z","shell.execute_reply":"2025-05-11T11:40:09.567977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"breeds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:17:02.186993Z","iopub.execute_input":"2025-05-11T11:17:02.187277Z","iopub.status.idle":"2025-05-11T11:17:02.193252Z","shell.execute_reply.started":"2025-05-11T11:17:02.187257Z","shell.execute_reply":"2025-05-11T11:17:02.192720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.nn.functional as F\ni=0\nfor file,sample in zip(os.listdir('/kaggle/input/dog-breed-identification/test'),test_dataset):\n    sample=sample.cuda()\n    output_values=model_torch(sample.unsqueeze(dim=0))\n    output_values=output_values.cpu()\n    probabilities = F.softmax(output_values, dim=1)\n    output.loc[i]=[file,*probabilities[0].detach().numpy()]\n    i+=1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:40:15.680117Z","iopub.execute_input":"2025-05-11T11:40:15.680634Z","iopub.status.idle":"2025-05-11T11:43:17.761475Z","shell.execute_reply.started":"2025-05-11T11:40:15.680612Z","shell.execute_reply":"2025-05-11T11:43:17.760926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:43:17.762508Z","iopub.execute_input":"2025-05-11T11:43:17.762703Z","iopub.status.idle":"2025-05-11T11:43:17.783235Z","shell.execute_reply.started":"2025-05-11T11:43:17.762682Z","shell.execute_reply":"2025-05-11T11:43:17.782608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output.to_csv('test_submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:45:04.838503Z","iopub.execute_input":"2025-05-11T11:45:04.839138Z","iopub.status.idle":"2025-05-11T11:45:06.286662Z","shell.execute_reply.started":"2025-05-11T11:45:04.839115Z","shell.execute_reply":"2025-05-11T11:45:06.286131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}