{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\nfor 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","trusted":true,"execution":{"iopub.status.busy":"2025-07-04T04:25:12.473211Z","iopub.execute_input":"2025-07-04T04:25:12.473650Z","iopub.status.idle":"2025-07-04T04:25:12.497456Z","shell.execute_reply.started":"2025-07-04T04:25:12.473608Z","shell.execute_reply":"2025-07-04T04:25:12.496290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from zipfile import ZipFile\nimport torch\n\nwith ZipFile(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\", 'r') as zip:\n    zip.extractall()\n\nwith ZipFile(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip\", 'r') as zip:\n    zip.extractall()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T04:25:15.005919Z","iopub.execute_input":"2025-07-04T04:25:15.006213Z","iopub.status.idle":"2025-07-04T04:25:28.033899Z","shell.execute_reply.started":"2025-07-04T04:25:15.006194Z","shell.execute_reply":"2025-07-04T04:25:28.032933Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\ndevice","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T04:25:50.869906Z","iopub.execute_input":"2025-07-04T04:25:50.870222Z","iopub.status.idle":"2025-07-04T04:25:50.882788Z","shell.execute_reply.started":"2025-07-04T04:25:50.870198Z","shell.execute_reply":"2025-07-04T04:25:50.881679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\nimport os, glob\n\ntrain_folder_path = \"/kaggle/working/train\"\ntest_folder_path = \"/kaggle/working/test\"\n\ntrain_files = [f for f in os.listdir(train_folder_path) if f.endswith('.jpg')]\ntest_files = [f for f in os.listdir(test_folder_path) if f.endswith('.jpg')]\n\ntrain_images = []\n\nfor image_path in train_files:\n    image = Image.open(os.path.join(train_folder_path, image_path))\n    image = image.resize((100, 100))\n    train_images.append(torch.tensor(np.array(image)).float().to(device).permute(2, 0, 1) / 255)\n\nall_images_train = glob.glob(os.path.join(train_folder_path, \"*.jpg\"))\ntrain_labels = [1.0 if \"dog\" in os.path.basename(p) else 0.0 for p in all_images_train]\n\ntest_images = []\n\nfor image_path in test_files:\n    image = Image.open(os.path.join(test_folder_path, image_path))\n    image = image.resize((100, 100))\n    test_images.append(torch.tensor(np.array(image)).float().to(device).permute(2, 0, 1) / 255)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T04:44:16.172572Z","iopub.execute_input":"2025-07-04T04:44:16.176139Z","iopub.status.idle":"2025-07-04T04:46:16.535622Z","shell.execute_reply.started":"2025-07-04T04:44:16.176066Z","shell.execute_reply":"2025-07-04T04:46:16.534444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX, y = train_images, torch.tensor(train_labels).long().to(device)\n\nX_train, X_test, y_train, y_test = train_test_split(X, y)\n\nprint(\"Train class counts:\", torch.bincount(y_train.long()))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T04:47:36.784968Z","iopub.execute_input":"2025-07-04T04:47:36.785316Z","iopub.status.idle":"2025-07-04T04:47:37.582075Z","shell.execute_reply.started":"2025-07-04T04:47:36.785294Z","shell.execute_reply":"2025-07-04T04:47:37.581134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom torch import nn\nfrom torch.utils.data import Dataset\nfrom torch.utils.data import DataLoader\nimport torchvision\n\nclass CatsAndDogs(Dataset):\n    def __init__(self, x, y):\n        self.imgs = x\n        self.labels = y\n        \n    def __len__(self):\n        return len(self.imgs)\n\n    def __getitem__(self, idx):\n        # transform = torchvision.transforms.Compose([\n        #     torchvision.transforms.ToTensor()\n        # ])\n        img = self.imgs[idx]\n        label = self.labels[idx]\n        # label = torch.tensor(label).float()\n        # img = transform(img)\n        return img, label\n\ntraining_data = CatsAndDogs(X_train, y_train)\ntest_data = CatsAndDogs(X_test, y_test)\n\nbatch_size = 16\ntrain_dataloader = DataLoader(training_data, batch_size=batch_size, shuffle=True)\ntest_dataloader = DataLoader(test_data, batch_size=batch_size, shuffle=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T04:49:54.727863Z","iopub.execute_input":"2025-07-04T04:49:54.728445Z","iopub.status.idle":"2025-07-04T04:49:59.710997Z","shell.execute_reply.started":"2025-07-04T04:49:54.728418Z","shell.execute_reply":"2025-07-04T04:49:59.709614Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cnt = 0\n\nfor idx, (inputs, labels) in enumerate(train_dataloader):\n    if cnt < 1:\n        print(labels.view(-1, 1) )\n    cnt += 1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T04:50:11.845371Z","iopub.execute_input":"2025-07-04T04:50:11.845875Z","iopub.status.idle":"2025-07-04T04:50:13.108002Z","shell.execute_reply.started":"2025-07-04T04:50:11.845823Z","shell.execute_reply":"2025-07-04T04:50:13.106658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}