{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-06T12:32:50.308810Z","iopub.execute_input":"2024-10-06T12:32:50.309331Z","iopub.status.idle":"2024-10-06T12:32:51.271391Z","shell.execute_reply.started":"2024-10-06T12:32:50.309295Z","shell.execute_reply":"2024-10-06T12:32:51.270615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import DataLoader, Dataset\nimport torchvision\nimport timm\nimport torchvision.transforms as transforms\nfrom PIL import Image\nfrom sklearn.preprocessing import LabelEncoder\n","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:32:51.273138Z","iopub.execute_input":"2024-10-06T12:32:51.273950Z","iopub.status.idle":"2024-10-06T12:32:58.012589Z","shell.execute_reply.started":"2024-10-06T12:32:51.273904Z","shell.execute_reply":"2024-10-06T12:32:58.011747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder = LabelEncoder()\nlabels_df = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")\nlabels_df[\"breed_encoded\"] = label_encoder.fit_transform(labels_df[\"breed\"])","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:32:58.013624Z","iopub.execute_input":"2024-10-06T12:32:58.014057Z","iopub.status.idle":"2024-10-06T12:32:58.051098Z","shell.execute_reply.started":"2024-10-06T12:32:58.014024Z","shell.execute_reply":"2024-10-06T12:32:58.050226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:32:58.052748Z","iopub.execute_input":"2024-10-06T12:32:58.053032Z","iopub.status.idle":"2024-10-06T12:32:58.065619Z","shell.execute_reply.started":"2024-10-06T12:32:58.053000Z","shell.execute_reply":"2024-10-06T12:32:58.064768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DogBreedDataset(Dataset):\n    def __init__(self, df, image_dir, transform = None):\n        self.df = df\n        self.image_dir = image_dir\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        image_name = os.path.join(self.image_dir, self.df.iloc[idx, 0] + \".jpg\")\n        image = Image.open(image_name)\n        label = self.df.iloc[idx, 2]\n        if self.transform:\n            image = self.transform(image)\n        \n        return image, label","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:33:01.299973Z","iopub.execute_input":"2024-10-06T12:33:01.300370Z","iopub.status.idle":"2024-10-06T12:33:01.307545Z","shell.execute_reply.started":"2024-10-06T12:33:01.300334Z","shell.execute_reply":"2024-10-06T12:33:01.306576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = int(0.8 * len(labels_df))\nval_size = len(labels_df) - train_size","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:33:16.469663Z","iopub.execute_input":"2024-10-06T12:33:16.470485Z","iopub.status.idle":"2024-10-06T12:33:16.474625Z","shell.execute_reply.started":"2024-10-06T12:33:16.470442Z","shell.execute_reply":"2024-10-06T12:33:16.473708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((224,224)),\n    #transforms.RandomHorizontalFlip(),\n    #transforms.RandomRotation(10),\n    transforms.ToTensor(),\n    #transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])\n])","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:33:17.864998Z","iopub.execute_input":"2024-10-06T12:33:17.865901Z","iopub.status.idle":"2024-10-06T12:33:17.870611Z","shell.execute_reply.started":"2024-10-06T12:33:17.865859Z","shell.execute_reply":"2024-10-06T12:33:17.869555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dir = \"/kaggle/input/dog-breed-identification/train\"","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:33:19.374409Z","iopub.execute_input":"2024-10-06T12:33:19.374791Z","iopub.status.idle":"2024-10-06T12:33:19.379277Z","shell.execute_reply.started":"2024-10-06T12:33:19.374755Z","shell.execute_reply":"2024-10-06T12:33:19.378333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = DogBreedDataset(labels_df[:train_size],img_dir, transform = transform)\nval_dataset = DogBreedDataset(labels_df[train_size:], img_dir, transform = transform)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:33:20.299648Z","iopub.execute_input":"2024-10-06T12:33:20.300499Z","iopub.status.idle":"2024-10-06T12:33:20.305361Z","shell.execute_reply.started":"2024-10-06T12:33:20.300455Z","shell.execute_reply":"2024-10-06T12:33:20.304244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_dataset, batch_size = 32, shuffle = True)\nval_loader = DataLoader(val_dataset, batch_size = 32, shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:33:21.089361Z","iopub.execute_input":"2024-10-06T12:33:21.089709Z","iopub.status.idle":"2024-10-06T12:33:21.094514Z","shell.execute_reply.started":"2024-10-06T12:33:21.089676Z","shell.execute_reply":"2024-10-06T12:33:21.093539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.optim as optim\nimport torchvision.models as models\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:33:22.198727Z","iopub.execute_input":"2024-10-06T12:33:22.199486Z","iopub.status.idle":"2024-10-06T12:33:22.203794Z","shell.execute_reply.started":"2024-10-06T12:33:22.199447Z","shell.execute_reply":"2024-10-06T12:33:22.202830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Creating Our CNN model for classification ##\n\nnum_classes = len(label_encoder.classes_)\nmodel = models.resnet152(pretrained = True)\nfor params in model.parameters():\n    params.requires_grad = False\n\nmodel.fc = nn.Linear(model.fc.in_features, num_classes)\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nprint(device)\nmodel.to(device)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.fc.parameters(), lr = 0.02)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:33:23.070059Z","iopub.execute_input":"2024-10-06T12:33:23.070453Z","iopub.status.idle":"2024-10-06T12:33:25.938719Z","shell.execute_reply.started":"2024-10-06T12:33:23.070416Z","shell.execute_reply":"2024-10-06T12:33:25.937939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 10\ntraining_losses = []\ntraining_accuracies = []\nvalidation_losses = []\nvalidation_accuracies = []\n\nfor epoch in range(num_epochs):\n    ## Training Phase ##\n    model.train()\n    running_loss = 0.0\n    correct = 0\n    total = 0\n    for images, labels in tqdm(train_loader, desc = f\"Training Phase {epoch+1}\"):\n        images = images.to(device)\n        labels = labels.to(device)\n        optimizer.zero_grad()\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n        \n        loss.backward()\n        optimizer.step()\n        \n        _, predictions = torch.max(outputs, 1)\n        correct += (predictions == labels).sum().item()\n        total += labels.size(0)\n        running_loss += loss.item() * images.size(0)\n    train_loss = running_loss/ len(train_loader)\n    train_accuracy = correct/total *100\n    \n    training_losses.append(train_loss)\n    training_accuracies.append(train_accuracy)\n    \n    print(f\"Epoch :{epoch+1}  || Training Loss :{train_loss}\")\n    \n    ## Evalution Phase ##\n    model.eval()\n    with torch.no_grad():\n        running_loss = 0.0\n        correct = 0\n        total = 0\n        for images, labels in tqdm(val_loader, desc = \"Validation Phase\"):\n            images = images.to(device)\n            labels = labels.to(device)\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n            running_loss+= loss.item() * images.size(0)\n            _, predictions = torch.max(outputs, 1)\n            correct += (predictions == labels).sum().item()\n            total += labels.size(0)\n        \n        valid_loss = running_loss/len(val_loader)\n        valid_accuracy = correct/total *100\n        validation_losses.append(valid_loss)\n        validation_accuracies.append(valid_accuracy)\n        \n        print(f\"Epoch : {epoch+1}  || Loss :{valid_loss}  || Accuracy :{valid_accuracy}\")","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:33:33.096243Z","iopub.execute_input":"2024-10-06T12:33:33.097099Z","iopub.status.idle":"2024-10-06T12:49:35.497841Z","shell.execute_reply.started":"2024-10-06T12:33:33.097057Z","shell.execute_reply":"2024-10-06T12:49:35.496837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_loader)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:54:26.239316Z","iopub.execute_input":"2024-10-06T12:54:26.239727Z","iopub.status.idle":"2024-10-06T12:54:26.246441Z","shell.execute_reply.started":"2024-10-06T12:54:26.239690Z","shell.execute_reply":"2024-10-06T12:54:26.245503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = \"/kaggle/input/dog-breed-identification/test\"\nlost = os.listdir(image_dir)\nimg1 = os.path.join(image_dir + '/' + lost[5])\nimage = Image.open(img1)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:54:26.993828Z","iopub.execute_input":"2024-10-06T12:54:26.994232Z","iopub.status.idle":"2024-10-06T12:54:27.891650Z","shell.execute_reply.started":"2024-10-06T12:54:26.994168Z","shell.execute_reply":"2024-10-06T12:54:27.890835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-06T12:54:28.759133Z","iopub.execute_input":"2024-10-06T12:54:28.759540Z","iopub.status.idle":"2024-10-06T12:54:29.105578Z","shell.execute_reply.started":"2024-10-06T12:54:28.759501Z","shell.execute_reply":"2024-10-06T12:54:29.104763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self, image_dir, transform = None):\n        self.image_dir = image_dir\n        self.transform = transform\n    \n    def __len__(self):\n        return len(os.listdir(self.image_dir))\n    \n    def __getitem__(self, idx):\n        self.list = os.listdir(self.image_dir)\n        image = Image.open(os.path.join(self.image_dir +'/' + self.list[idx]))\n        if transform:\n            image = transform(image)\n        return image","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:08:20.723750Z","iopub.execute_input":"2024-10-06T13:08:20.724159Z","iopub.status.idle":"2024-10-06T13:08:20.731218Z","shell.execute_reply.started":"2024-10-06T13:08:20.724119Z","shell.execute_reply":"2024-10-06T13:08:20.730139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = TestDataset(\"/kaggle/input/dog-breed-identification/test\", transform = transform)\ntest_loader = DataLoader(test_dataset, shuffle = False, batch_size = 32)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:08:21.459770Z","iopub.execute_input":"2024-10-06T13:08:21.460533Z","iopub.status.idle":"2024-10-06T13:08:21.465271Z","shell.execute_reply.started":"2024-10-06T13:08:21.460491Z","shell.execute_reply":"2024-10-06T13:08:21.464240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = label_encoder.classes_\nclasses","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:08:22.024022Z","iopub.execute_input":"2024-10-06T13:08:22.024436Z","iopub.status.idle":"2024-10-06T13:08:22.031328Z","shell.execute_reply.started":"2024-10-06T13:08:22.024393Z","shell.execute_reply":"2024-10-06T13:08:22.030427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(np.random.random((1,120)), columns = classes)\ndf","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:08:22.518529Z","iopub.execute_input":"2024-10-06T13:08:22.518877Z","iopub.status.idle":"2024-10-06T13:08:22.539722Z","shell.execute_reply.started":"2024-10-06T13:08:22.518842Z","shell.execute_reply":"2024-10-06T13:08:22.538771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    for images in tqdm(test_loader, desc = \"Test Phase\"):\n        images = images.to(device)\n        outputs = model(images)\n        outputs = torch.nn.functional.softmax(outputs, 1)\n        outputs = outputs.cpu().numpy()\n        temp_df = pd.DataFrame(outputs, columns = classes)\n        df = pd.concat([df, temp_df],ignore_index = True)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:08:24.129230Z","iopub.execute_input":"2024-10-06T13:08:24.129675Z","iopub.status.idle":"2024-10-06T13:10:30.760300Z","shell.execute_reply.started":"2024-10-06T13:08:24.129634Z","shell.execute_reply":"2024-10-06T13:10:30.759316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:14:02.084096Z","iopub.execute_input":"2024-10-06T13:14:02.084525Z","iopub.status.idle":"2024-10-06T13:14:02.120705Z","shell.execute_reply.started":"2024-10-06T13:14:02.084486Z","shell.execute_reply":"2024-10-06T13:14:02.119695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop(0)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:14:13.673735Z","iopub.execute_input":"2024-10-06T13:14:13.674437Z","iopub.status.idle":"2024-10-06T13:14:13.681359Z","shell.execute_reply.started":"2024-10-06T13:14:13.674396Z","shell.execute_reply":"2024-10-06T13:14:13.680392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names = os.listdir(\"/kaggle/input/dog-breed-identification/test\")\nfor i in range(len(names)):\n    names[i] = names[i].split('.')[0]","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:14:23.623513Z","iopub.execute_input":"2024-10-06T13:14:23.624153Z","iopub.status.idle":"2024-10-06T13:14:23.639005Z","shell.execute_reply.started":"2024-10-06T13:14:23.624108Z","shell.execute_reply":"2024-10-06T13:14:23.638073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.insert(0,\"id\", names)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:14:24.354098Z","iopub.execute_input":"2024-10-06T13:14:24.354481Z","iopub.status.idle":"2024-10-06T13:14:24.359927Z","shell.execute_reply.started":"2024-10-06T13:14:24.354442Z","shell.execute_reply":"2024-10-06T13:14:24.359040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:14:32.473913Z","iopub.execute_input":"2024-10-06T13:14:32.474308Z","iopub.status.idle":"2024-10-06T13:14:32.512164Z","shell.execute_reply.started":"2024-10-06T13:14:32.474271Z","shell.execute_reply":"2024-10-06T13:14:32.511100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:14:54.169594Z","iopub.execute_input":"2024-10-06T13:14:54.169986Z","iopub.status.idle":"2024-10-06T13:14:55.210637Z","shell.execute_reply.started":"2024-10-06T13:14:54.169948Z","shell.execute_reply":"2024-10-06T13:14:55.209582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:16:13.823689Z","iopub.execute_input":"2024-10-06T13:16:13.824068Z","iopub.status.idle":"2024-10-06T13:16:13.830946Z","shell.execute_reply.started":"2024-10-06T13:16:13.824029Z","shell.execute_reply":"2024-10-06T13:16:13.829882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-06T13:16:43.573650Z","iopub.execute_input":"2024-10-06T13:16:43.574038Z","iopub.status.idle":"2024-10-06T13:16:43.580478Z","shell.execute_reply.started":"2024-10-06T13:16:43.574001Z","shell.execute_reply":"2024-10-06T13:16:43.579398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}