{"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":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30702,"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_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Assignment: fit a CNN\n\nIn this notebook we load an image classification task: classify images of cats and dogs correctly.\n\nYour task to adapt this code to this dataset to fit a working cat/dog prediction models. You are asked to shortly describe what part of the coee you changed into what, and why that was necessary. ","metadata":{}},{"cell_type":"code","source":"!unzip -qq /kaggle/input/dogs-vs-cats/train.zip \n!unzip -qq /kaggle/input/dogs-vs-cats/test1.zip","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport torch\nfrom torch import nn\n\n# Import torchvision\nimport torchvision\nfrom torchvision.transforms import ToTensor, Resize\nimport torchvision.transforms as transforms\nfrom torch.utils.data import Dataset, DataLoader\n\ntorch.manual_seed(42)\n\nprint(f\"torch version: {torch.__version__}\")\nprint(f\"torchvision version: {torchvision.__version__}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = \"/kaggle/working/\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\nclass CatDogDataset(Dataset):\n\n    def __init__(self, train_dir, transform = None):\n        \n        self.train_dir = train_dir\n        self.transform = transform\n        self.images = os.listdir(train_dir)\n        \n\n    def __len__(self):\n        return len(self.images)\n    \n    def __getitem__(self, index):\n        image_path = os.path.join(self.train_dir, self.images[index])\n        label = self.images[index].split(\".\")[0]\n\n        label = 0 if label == 'cat' else 1\n        \n        image = np.array(Image.open(image_path))\n        \n        if self.transform is not None:\n            image = self.transform(image)\n\n        return image, label","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    ToTensor(),\n    Resize((100,100))\n])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}