{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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\n# import 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":"2023-03-09T16:04:36.363505Z","iopub.execute_input":"2023-03-09T16:04:36.364151Z","iopub.status.idle":"2023-03-09T16:04:36.390666Z","shell.execute_reply.started":"2023-03-09T16:04:36.364115Z","shell.execute_reply":"2023-03-09T16:04:36.389854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-09T16:05:04.719613Z","iopub.execute_input":"2023-03-09T16:05:04.720855Z","iopub.status.idle":"2023-03-09T16:05:04.767756Z","shell.execute_reply.started":"2023-03-09T16:05:04.720806Z","shell.execute_reply":"2023-03-09T16:05:04.766541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample =  pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-09T16:05:36.660956Z","iopub.execute_input":"2023-03-09T16:05:36.661325Z","iopub.status.idle":"2023-03-09T16:05:37.183638Z","shell.execute_reply.started":"2023-03-09T16:05:36.661293Z","shell.execute_reply":"2023-03-09T16:05:37.182301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:12:21.204091Z","iopub.execute_input":"2023-03-09T18:12:21.205323Z","iopub.status.idle":"2023-03-09T18:12:21.219208Z","shell.execute_reply.started":"2023-03-09T18:12:21.205281Z","shell.execute_reply":"2023-03-09T18:12:21.217639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_dict = labels.set_index('id')['breed'].to_dict()","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:25:44.379235Z","iopub.execute_input":"2023-03-09T18:25:44.379698Z","iopub.status.idle":"2023-03-09T18:25:44.398708Z","shell.execute_reply.started":"2023-03-09T18:25:44.379661Z","shell.execute_reply":"2023-03-09T18:25:44.397304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_values = labels['breed'].unique()\nlen(unique_values)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T16:15:13.066337Z","iopub.execute_input":"2023-03-09T16:15:13.066757Z","iopub.status.idle":"2023-03-09T16:15:13.076523Z","shell.execute_reply.started":"2023-03-09T16:15:13.066720Z","shell.execute_reply":"2023-03-09T16:15:13.075115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-09T16:16:43.636892Z","iopub.execute_input":"2023-03-09T16:16:43.637302Z","iopub.status.idle":"2023-03-09T16:16:43.644738Z","shell.execute_reply.started":"2023-03-09T16:16:43.637265Z","shell.execute_reply":"2023-03-09T16:16:43.643462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\npath = '/kaggle/input/dog-breed-identification/train'\nfiles = os.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T17:42:49.782292Z","iopub.execute_input":"2023-03-09T17:42:49.782954Z","iopub.status.idle":"2023-03-09T17:42:50.409567Z","shell.execute_reply.started":"2023-03-09T17:42:49.782916Z","shell.execute_reply":"2023-03-09T17:42:50.408463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files\nlen(files)\ns = []\nfor file in range(len(files)):\n    img_id  = files[file][:-4]\n    s.append(img_id)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:15:41.790638Z","iopub.execute_input":"2023-03-09T18:15:41.791365Z","iopub.status.idle":"2023-03-09T18:15:41.800636Z","shell.execute_reply.started":"2023-03-09T18:15:41.791326Z","shell.execute_reply":"2023-03-09T18:15:41.799225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_dict[s[5]]","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:27:24.266900Z","iopub.execute_input":"2023-03-09T18:27:24.267310Z","iopub.status.idle":"2023-03-09T18:27:24.275259Z","shell.execute_reply.started":"2023-03-09T18:27:24.267276Z","shell.execute_reply":"2023-03-09T18:27:24.273714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****CREATING A DATAFRAME**","metadata":{}},{"cell_type":"code","source":"breed_name = []\nfor i in range(len(s)):\n    breed_name.append(my_dict[s[i]])","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:35:35.212708Z","iopub.execute_input":"2023-03-09T18:35:35.213120Z","iopub.status.idle":"2023-03-09T18:35:35.224782Z","shell.execute_reply.started":"2023-03-09T18:35:35.213082Z","shell.execute_reply":"2023-03-09T18:35:35.223701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.DataFrame(list(zip(files,s,breed_name)), columns=['Image_path', 'id', 'breed'])","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:53:14.125083Z","iopub.execute_input":"2023-03-09T18:53:14.125540Z","iopub.status.idle":"2023-03-09T18:53:14.135806Z","shell.execute_reply.started":"2023-03-09T18:53:14.125507Z","shell.execute_reply":"2023-03-09T18:53:14.134752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:53:14.339596Z","iopub.execute_input":"2023-03-09T18:53:14.339941Z","iopub.status.idle":"2023-03-09T18:53:14.350086Z","shell.execute_reply.started":"2023-03-09T18:53:14.339909Z","shell.execute_reply":"2023-03-09T18:53:14.349306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Image_path'] =  path  + '/'+train_df['Image_path']","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:53:21.308556Z","iopub.execute_input":"2023-03-09T18:53:21.308893Z","iopub.status.idle":"2023-03-09T18:53:21.316578Z","shell.execute_reply.started":"2023-03-09T18:53:21.308863Z","shell.execute_reply":"2023-03-09T18:53:21.315416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:53:23.660325Z","iopub.execute_input":"2023-03-09T18:53:23.660869Z","iopub.status.idle":"2023-03-09T18:53:23.671169Z","shell.execute_reply.started":"2023-03-09T18:53:23.660836Z","shell.execute_reply":"2023-03-09T18:53:23.670103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**LOAD THE IMAGES**","metadata":{}},{"cell_type":"code","source":"img_arr = []\nimport cv2\nfor path in train_df['Image_path']:\n    image = cv2.imread(path)\n    img_arr.append(image)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:54:10.955546Z","iopub.execute_input":"2023-03-09T18:54:10.955960Z","iopub.status.idle":"2023-03-09T18:56:27.945039Z","shell.execute_reply.started":"2023-03-09T18:54:10.955914Z","shell.execute_reply":"2023-03-09T18:56:27.943865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_arr[1]","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:57:29.461648Z","iopub.execute_input":"2023-03-09T18:57:29.462626Z","iopub.status.idle":"2023-03-09T18:57:29.470592Z","shell.execute_reply.started":"2023-03-09T18:57:29.462573Z","shell.execute_reply":"2023-03-09T18:57:29.469313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as transforms","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:59:40.917019Z","iopub.execute_input":"2023-03-09T18:59:40.917706Z","iopub.status.idle":"2023-03-09T18:59:43.424668Z","shell.execute_reply.started":"2023-03-09T18:59:40.917669Z","shell.execute_reply":"2023-03-09T18:59:43.423852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Resize to 224x224\n    transforms.ToTensor(),  # Convert to a PyTorch tensor\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))  # Normalize pixel values to [-1, 1]\n])","metadata":{"execution":{"iopub.status.busy":"2023-03-09T18:59:43.427231Z","iopub.execute_input":"2023-03-09T18:59:43.428305Z","iopub.status.idle":"2023-03-09T18:59:43.434294Z","shell.execute_reply.started":"2023-03-09T18:59:43.428264Z","shell.execute_reply":"2023-03-09T18:59:43.433227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2023-03-09T19:27:54.180863Z","iopub.execute_input":"2023-03-09T19:27:54.181309Z","iopub.status.idle":"2023-03-09T19:27:54.186743Z","shell.execute_reply.started":"2023-03-09T19:27:54.181269Z","shell.execute_reply":"2023-03-09T19:27:54.185410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**DATASET CLASS AND DATALOADERS**","metadata":{}},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader","metadata":{"execution":{"iopub.status.busy":"2023-03-09T19:28:00.870346Z","iopub.execute_input":"2023-03-09T19:28:00.871075Z","iopub.status.idle":"2023-03-09T19:28:00.876451Z","shell.execute_reply.started":"2023-03-09T19:28:00.871035Z","shell.execute_reply":"2023-03-09T19:28:00.875260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class  DogDataset(Dataset):\n    def __init__(self,df,transforms):\n        self.df = df\n        self.transform = transforms\n    def __len__(self):\n        return len(self.df)\n    def __getitem__(self,idx):\n        image = Image.open(self.df.loc[idx, 'Image_path'])\n        label = self.df.loc[idx, 'breed']\n        \n        if self.transform:\n            image = self.transform(image)\n\n        return image, label\n","metadata":{"execution":{"iopub.status.busy":"2023-03-09T19:28:01.228236Z","iopub.execute_input":"2023-03-09T19:28:01.229205Z","iopub.status.idle":"2023-03-09T19:28:01.236711Z","shell.execute_reply.started":"2023-03-09T19:28:01.229153Z","shell.execute_reply":"2023-03-09T19:28:01.235371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ndataset = DogDataset(train_df,transform)\ntrain_set, test_set = train_test_split(dataset, test_size=0.2, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-09T19:28:01.647468Z","iopub.execute_input":"2023-03-09T19:28:01.647892Z","iopub.status.idle":"2023-03-09T19:29:45.952416Z","shell.execute_reply.started":"2023-03-09T19:28:01.647855Z","shell.execute_reply":"2023-03-09T19:29:45.951130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\ntrain_dl = DataLoader(train_set, batch_size, shuffle=True, num_workers=4, pin_memory=True)\nval_dl = DataLoader(test_set, batch_size*2, num_workers=4, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T19:29:59.807656Z","iopub.execute_input":"2023-03-09T19:29:59.808061Z","iopub.status.idle":"2023-03-09T19:29:59.814934Z","shell.execute_reply.started":"2023-03-09T19:29:59.808026Z","shell.execute_reply":"2023-03-09T19:29:59.813500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch","metadata":{"execution":{"iopub.status.busy":"2023-03-09T19:31:00.970140Z","iopub.execute_input":"2023-03-09T19:31:00.970574Z","iopub.status.idle":"2023-03-09T19:31:00.976296Z","shell.execute_reply.started":"2023-03-09T19:31:00.970536Z","shell.execute_reply":"2023-03-09T19:31:00.974910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_default_device():\n    \"\"\"Pick GPU if available, else CPU\"\"\"\n    if torch.cuda.is_available():\n        return torch.device('cuda')\n    else:\n        return torch.device('cpu')\n    \ndef to_device(data, device):\n    \"\"\"Move tensor(s) to chosen device\"\"\"\n    if isinstance(data, (list,tuple)):\n        return [to_device(x, device) for x in data]\n    return data.to(device, non_blocking=True)\n\nclass DeviceDataLoader():\n    \"\"\"Wrap a dataloader to move data to a device\"\"\"\n    def __init__(self, dl, device):\n        self.dl = dl\n        self.device = device\n        \n    def __iter__(self):\n        \"\"\"Yield a batch of data after moving it to device\"\"\"\n        for b in self.dl: \n            yield to_device(b, self.device)\n\n    def __len__(self):\n        \"\"\"Number of batches\"\"\"\n        return len(self.dl)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T19:31:01.151801Z","iopub.execute_input":"2023-03-09T19:31:01.152186Z","iopub.status.idle":"2023-03-09T19:31:01.161079Z","shell.execute_reply.started":"2023-03-09T19:31:01.152153Z","shell.execute_reply":"2023-03-09T19:31:01.159748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = get_default_device()\n#import torch\ntrain_dl = DeviceDataLoader(train_dl,device)\nval_dl = DeviceDataLoader(val_dl,device)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T19:31:01.640146Z","iopub.execute_input":"2023-03-09T19:31:01.640566Z","iopub.status.idle":"2023-03-09T19:31:01.645756Z","shell.execute_reply.started":"2023-03-09T19:31:01.640528Z","shell.execute_reply":"2023-03-09T19:31:01.644493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**MODEL BUILDING**","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}