{"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\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","execution":{"iopub.status.busy":"2023-10-08T21:58:49.211831Z","iopub.execute_input":"2023-10-08T21:58:49.212807Z","iopub.status.idle":"2023-10-08T21:58:49.22217Z","shell.execute_reply.started":"2023-10-08T21:58:49.212769Z","shell.execute_reply":"2023-10-08T21:58:49.221176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch \nimport zipfile\nfrom pathlib import Path\n\nimport zipfile\n\nimport random\nfrom PIL import Image\n\nfrom torchvision import transforms","metadata":{"execution":{"iopub.status.busy":"2023-10-08T21:58:49.257113Z","iopub.execute_input":"2023-10-08T21:58:49.257686Z","iopub.status.idle":"2023-10-08T21:58:49.261274Z","shell.execute_reply.started":"2023-10-08T21:58:49.257656Z","shell.execute_reply":"2023-10-08T21:58:49.260545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get data","metadata":{}},{"cell_type":"code","source":"train_dir='/kaggle/input/dogs-vs-cats/train.zip'\ntest_dir='/kaggle/input/dogs-vs-cats/test1.zip'","metadata":{"execution":{"iopub.status.busy":"2023-10-08T21:58:49.292271Z","iopub.execute_input":"2023-10-08T21:58:49.292626Z","iopub.status.idle":"2023-10-08T21:58:49.298016Z","shell.execute_reply.started":"2023-10-08T21:58:49.292599Z","shell.execute_reply":"2023-10-08T21:58:49.296633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir,test_dir","metadata":{"execution":{"iopub.status.busy":"2023-10-08T21:58:49.346826Z","iopub.execute_input":"2023-10-08T21:58:49.347884Z","iopub.status.idle":"2023-10-08T21:58:49.355411Z","shell.execute_reply.started":"2023-10-08T21:58:49.34784Z","shell.execute_reply":"2023-10-08T21:58:49.354368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = Path(\"kaggle\")\ndata=Path(files/'dos_vs_cat')\nif data.is_dir():\n    print('already exist')\nelse:\n    data.mkdir()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-08T21:58:49.388209Z","iopub.execute_input":"2023-10-08T21:58:49.388551Z","iopub.status.idle":"2023-10-08T21:58:49.394485Z","shell.execute_reply.started":"2023-10-08T21:58:49.388526Z","shell.execute_reply":"2023-10-08T21:58:49.393587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(train_dir,'r') as z:\n    z.extractall(data)\n    \nwith zipfile.ZipFile(test_dir,'r') as z:\n    z.extractall(data)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-08T21:58:49.422514Z","iopub.execute_input":"2023-10-08T21:58:49.423053Z","iopub.status.idle":"2023-10-08T21:59:01.760335Z","shell.execute_reply.started":"2023-10-08T21:58:49.423024Z","shell.execute_reply":"2023-10-08T21:59:01.759327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setup train and testing paths\ntrain_data = data / \"train\"\ntest_data = data / \"test1\"\n\ntrain_data,test_data","metadata":{"execution":{"iopub.status.busy":"2023-10-08T21:59:27.835751Z","iopub.execute_input":"2023-10-08T21:59:27.836096Z","iopub.status.idle":"2023-10-08T21:59:27.842401Z","shell.execute_reply.started":"2023-10-08T21:59:27.836072Z","shell.execute_reply":"2023-10-08T21:59:27.84157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import random\n#from PIL import Image\n\n#random.seed(42)\n\nimage_path_list=list(files.glob('*/*/*.jpg'))\n\n# 2. Get random image path\nrandom_image_path = random.choice(image_path_list)\n\nimage_class=random_image_path.stem\n\nimg=Image.open(random_image_path)\nprint(f'class name {image_class}')\nimg\n","metadata":{"execution":{"iopub.status.busy":"2023-10-08T21:59:01.761896Z","iopub.execute_input":"2023-10-08T21:59:01.762163Z","iopub.status.idle":"2023-10-08T21:59:01.947659Z","shell.execute_reply.started":"2023-10-08T21:59:01.762141Z","shell.execute_reply":"2023-10-08T21:59:01.946675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## create folders\ncat_dir=train_data/'cat'\ndog_dir=train_data/'dog'\n\nif cat_dir.is_dir():\n    print('exist')\nelse:\n    cat_dir.mkdir()\n    \nif dog_dir.is_dir():\n    print('exist')\nelse:\n    dog_dir.mkdir()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T22:00:33.068154Z","iopub.execute_input":"2023-10-08T22:00:33.068562Z","iopub.status.idle":"2023-10-08T22:00:33.075134Z","shell.execute_reply.started":"2023-10-08T22:00:33.068533Z","shell.execute_reply":"2023-10-08T22:00:33.073968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\n\n# Define source and destination directories\nsource_dir = \"/kaggle/working/kaggle/dos_vs_cat/train\"\ndog_dir = \"/kaggle/working/kaggle/dos_vs_cat/train/dogs\"\ncat_dir = \"/kaggle/working/kaggle/dos_vs_cat/train/cats\"\n\n# Create destination directories if they don't exist\nos.makedirs(dog_dir, exist_ok=True)\nos.makedirs(cat_dir, exist_ok=True)\n\n# List all files in the source directory\nfilenames = os.listdir(source_dir)\n\nfor filename in filenames:\n    # Split the filename using '.' and get the last part (the file extension)\n    category = filename.split('.')[0]\n\n    # Construct the source file path\n    source_file = os.path.join(source_dir, filename)\n\n    if category == 'dog':\n        destination_folder = dog_dir\n    else:\n        destination_folder = cat_dir\n\n    # Construct the destination file path\n    destination_file = os.path.join(destination_folder, filename)\n\n    # Move the file to the destination folder\n    shutil.move(source_file, destination_file)\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-10-08T22:00:04.297442Z","iopub.execute_input":"2023-10-08T22:00:04.29786Z","iopub.status.idle":"2023-10-08T22:00:04.625689Z","shell.execute_reply.started":"2023-10-08T22:00:04.297828Z","shell.execute_reply":"2023-10-08T22:00:04.624027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ntrain_data,test_data","metadata":{"execution":{"iopub.status.busy":"2023-10-08T22:00:45.83663Z","iopub.execute_input":"2023-10-08T22:00:45.837122Z","iopub.status.idle":"2023-10-08T22:00:45.845479Z","shell.execute_reply.started":"2023-10-08T22:00:45.837081Z","shell.execute_reply":"2023-10-08T22:00:45.844268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path_l=list(files.glob('*/*/*/*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2023-10-08T22:00:53.267364Z","iopub.execute_input":"2023-10-08T22:00:53.26778Z","iopub.status.idle":"2023-10-08T22:00:53.329819Z","shell.execute_reply.started":"2023-10-08T22:00:53.267749Z","shell.execute_reply":"2023-10-08T22:00:53.328827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## transform data","metadata":{}},{"cell_type":"code","source":"#from torchvision import transforms\n\ndata_transformer=transforms.Compose([\n    transforms.Resize(size=(224,224)),\n    \n    transforms.ToTensor(),\n    # Flip the images randomly on the horizontal\n    transforms.RandomHorizontalFlip(p=0.5)\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-08T22:00:56.214908Z","iopub.execute_input":"2023-10-08T22:00:56.215571Z","iopub.status.idle":"2023-10-08T22:00:56.22072Z","shell.execute_reply.started":"2023-10-08T22:00:56.215516Z","shell.execute_reply":"2023-10-08T22:00:56.219534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path_list[234]","metadata":{"execution":{"iopub.status.busy":"2023-10-08T22:02:04.215313Z","iopub.execute_input":"2023-10-08T22:02:04.215662Z","iopub.status.idle":"2023-10-08T22:02:04.222477Z","shell.execute_reply.started":"2023-10-08T22:02:04.215636Z","shell.execute_reply":"2023-10-08T22:02:04.221286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" ## Loading Image Data Using ImageFolder\n","metadata":{}},{"cell_type":"code","source":"from torchvision import datasets\ntrain_da=datasets.ImageFolder(root=train_data)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-08T22:01:08.615055Z","iopub.execute_input":"2023-10-08T22:01:08.615494Z","iopub.status.idle":"2023-10-08T22:01:08.736933Z","shell.execute_reply.started":"2023-10-08T22:01:08.61546Z","shell.execute_reply":"2023-10-08T22:01:08.73523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}