{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30587,"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 dls.show_batch(max_n=6)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\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-11-20T17:28:56.515951Z","iopub.execute_input":"2023-11-20T17:28:56.516353Z","iopub.status.idle":"2023-11-20T17:28:56.872149Z","shell.execute_reply.started":"2023-11-20T17:28:56.516308Z","shell.execute_reply":"2023-11-20T17:28:56.871262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SPLITTING THE DATA","metadata":{}},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:09:19.066538Z","iopub.execute_input":"2023-11-20T16:09:19.067050Z","iopub.status.idle":"2023-11-20T16:09:31.321888Z","shell.execute_reply.started":"2023-11-20T16:09:19.067013Z","shell.execute_reply":"2023-11-20T16:09:31.320939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = \"/kaggle/working/train\"\ntest = \"/kaggle/working/test\"\ntransducer = \"/kaggle/working/transducer\"","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:09:31.323485Z","iopub.execute_input":"2023-11-20T16:09:31.323895Z","iopub.status.idle":"2023-11-20T16:09:31.328526Z","shell.execute_reply.started":"2023-11-20T16:09:31.323854Z","shell.execute_reply":"2023-11-20T16:09:31.327530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport random\n\ndef organize_train_folder(train_folder):\n    cat_folder = os.path.join(train_folder, 'cat')\n    dog_folder = os.path.join(train_folder, 'dog')\n\n    if not os.path.exists(cat_folder):\n        os.makedirs(cat_folder)\n    if not os.path.exists(dog_folder):\n        os.makedirs(dog_folder)\n\n    for filename in os.listdir(train_folder):\n        file_path = os.path.join(train_folder, filename)\n        if os.path.isfile(file_path):\n            if filename.startswith('cat'):\n                shutil.move(file_path, os.path.join(cat_folder, filename))\n            elif filename.startswith('dog'):\n                shutil.move(file_path, os.path.join(dog_folder, filename))\n\n\ndef count_pictures(folder):\n    dog_count = 0\n    cat_count = 0\n    for subfolder in ['dog', 'cat']:\n        path = os.path.join(folder, subfolder)\n        for filename in os.listdir(path):\n            if filename.startswith('dog'):\n                dog_count += 1\n            elif filename.startswith('cat'):\n                cat_count += 1\n    return dog_count, cat_count\n\ndef create_folder_with_subfolders(source_folder, target_folder, percentage=0.30):\n    if not os.path.exists(target_folder):\n        os.makedirs(target_folder)\n        os.makedirs(os.path.join(target_folder, 'dog'))\n        os.makedirs(os.path.join(target_folder, 'cat'))\n\n    dogs = [f for f in os.listdir(os.path.join(source_folder, 'dog'))]\n    cats = [f for f in os.listdir(os.path.join(source_folder, 'cat'))]\n\n    num_dogs_to_select = int(len(dogs) * percentage)\n    num_cats_to_select = int(len(cats) * percentage)\n\n    selected_dogs = random.sample(dogs, num_dogs_to_select)\n    selected_cats = random.sample(cats, num_cats_to_select)\n\n    for filename in selected_dogs:\n        shutil.move(os.path.join(source_folder, 'dog', filename), os.path.join(target_folder, 'dog', filename))\n\n    for filename in selected_cats:\n        shutil.move(os.path.join(source_folder, 'cat', filename), os.path.join(target_folder, 'cat', filename))\n\n# Assuming your train folder is already populated with cat and dog images\ntrain = \"/kaggle/working/train\"\n\n# Organize train folder\norganize_train_folder(train)\n\n# Create transducer and test folders\ncreate_folder_with_subfolders(train, 'transducer')\ncreate_folder_with_subfolders(train, 'test', 0.2)\n\n# Count pictures in train, test, and transducer folders\ntrain_dog_count, train_cat_count = count_pictures(train)\ntest_dog_count, test_cat_count = count_pictures('test')\ntransducer_dog_count, transducer_cat_count = count_pictures('transducer')\n\nprint(f\"Train - Dogs: {train_dog_count}, Cats: {train_cat_count}\")\nprint(f\"Test - Dogs: {test_dog_count}, Cats: {test_cat_count}\")\nprint(f\"Transducer - Dogs: {transducer_dog_count}, Cats: {transducer_cat_count}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:09:31.331654Z","iopub.execute_input":"2023-11-20T16:09:31.332574Z","iopub.status.idle":"2023-11-20T16:09:31.972497Z","shell.execute_reply.started":"2023-11-20T16:09:31.332538Z","shell.execute_reply":"2023-11-20T16:09:31.971357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TRAINING THE RESNET MODEL","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:09:31.974341Z","iopub.execute_input":"2023-11-20T16:09:31.975117Z","iopub.status.idle":"2023-11-20T16:09:42.134597Z","shell.execute_reply.started":"2023-11-20T16:09:31.975078Z","shell.execute_reply":"2023-11-20T16:09:42.133587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = \"/kaggle/working/train\"","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:09:42.135867Z","iopub.execute_input":"2023-11-20T16:09:42.136165Z","iopub.status.idle":"2023-11-20T16:09:42.140459Z","shell.execute_reply.started":"2023-11-20T16:09:42.136137Z","shell.execute_reply":"2023-11-20T16:09:42.139539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = DataBlock(\n    blocks=(ImageBlock, CategoryBlock), \n    get_items=get_image_files, \n    splitter=RandomSplitter(valid_pct=0.2, seed=42),\n    get_y=parent_label,\n    item_tfms=[Resize(200, method='squish')]\n).dataloaders(image_path, bs=32)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:09:42.141712Z","iopub.execute_input":"2023-11-20T16:09:42.141992Z","iopub.status.idle":"2023-11-20T16:09:47.771495Z","shell.execute_reply.started":"2023-11-20T16:09:42.141967Z","shell.execute_reply":"2023-11-20T16:09:47.770467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(max_n=6)","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:09:47.773018Z","iopub.execute_input":"2023-11-20T16:09:47.773617Z","iopub.status.idle":"2023-11-20T16:09:48.780491Z","shell.execute_reply.started":"2023-11-20T16:09:47.773569Z","shell.execute_reply":"2023-11-20T16:09:48.779418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nlearn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(3)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:09:48.782027Z","iopub.execute_input":"2023-11-20T16:09:48.782423Z","iopub.status.idle":"2023-11-20T16:11:24.319803Z","shell.execute_reply.started":"2023-11-20T16:09:48.782388Z","shell.execute_reply":"2023-11-20T16:11:24.318665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TRANSDUCER DATAFRAME","metadata":{}},{"cell_type":"code","source":"transducer_df = pd.DataFrame([], columns=['RNOutput', 'TrueLabel']) ","metadata":{"execution":{"iopub.status.busy":"2023-11-20T17:29:07.279395Z","iopub.execute_input":"2023-11-20T17:29:07.279977Z","iopub.status.idle":"2023-11-20T17:29:07.297858Z","shell.execute_reply.started":"2023-11-20T17:29:07.279931Z","shell.execute_reply":"2023-11-20T17:29:07.296852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom fastai.vision.core import load_image\n\ntransducer_folder = Path('/kaggle/working/transducer')\n\n# List all images in the test folder\ntransducer_images = os.listdir(transducer_folder)","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:11:24.339976Z","iopub.execute_input":"2023-11-20T16:11:24.340324Z","iopub.status.idle":"2023-11-20T16:11:24.353326Z","shell.execute_reply.started":"2023-11-20T16:11:24.340291Z","shell.execute_reply":"2023-11-20T16:11:24.352308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.core import load_image\nimport pandas as pd\nimport os\nfrom pathlib import Path\n\ntransducer_folder = Path('/kaggle/working/transducer')\n\n# Initialize DataFrame\ntransducer_df = pd.DataFrame(columns=['Predicted', 'Actual'])\n\n# Function to process each image\ndef process_image(image_path, label):\n    img_fastai = load_image(image_path)\n    pred_class, pred_idx, outputs = learn.predict(img_fastai)\n    return {'Predicted': pred_class, 'Actual': label}\n\n# Iterate over each subfolder ('cat' and 'dog') and process images\nresults = []\nfor label in ['cat', 'dog']:\n    subfolder_path = transducer_folder / label\n    if subfolder_path.exists() and subfolder_path.is_dir():\n        for image_file in subfolder_path.iterdir():\n            if image_file.is_file():\n                result = process_image(image_file, label)\n                results.append(result)\n\n                \n                \ntransducer_df = pd.DataFrame(results)","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:12:23.991207Z","iopub.execute_input":"2023-11-20T16:12:23.992179Z","iopub.status.idle":"2023-11-20T16:14:24.207649Z","shell.execute_reply.started":"2023-11-20T16:12:23.992130Z","shell.execute_reply":"2023-11-20T16:14:24.206834Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transducer_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:15:50.864301Z","iopub.execute_input":"2023-11-20T16:15:50.864774Z","iopub.status.idle":"2023-11-20T16:15:50.884059Z","shell.execute_reply.started":"2023-11-20T16:15:50.864734Z","shell.execute_reply":"2023-11-20T16:15:50.882854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EXPORTS","metadata":{}},{"cell_type":"code","source":"transducer_df.to_csv('transducer_dataframe.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-20T16:38:57.465126Z","iopub.execute_input":"2023-11-20T16:38:57.465537Z","iopub.status.idle":"2023-11-20T16:38:57.492311Z","shell.execute_reply.started":"2023-11-20T16:38:57.465507Z","shell.execute_reply":"2023-11-20T16:38:57.491362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export('resnet18.pkl')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nimport os\n\ndef zip_folder(folder_path, output_file):\n    with zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED) as zipf:\n        for root, dirs, files in os.walk(folder_path):\n            for file in files:\n                # Create a proper path relative to the folder being zipped\n                file_path = os.path.join(root, file)\n                file_path_in_zip = os.path.relpath(file_path, os.path.dirname(folder_path))\n                # Add file to the zip file\n                zipf.write(file_path, file_path_in_zip)\n\n# Path to your test folder\ntest_folder = \"/kaggle/working/test\"\n\n# Path where you want to save the zip file\nzip_file_path = \"/kaggle/working/test.zip\"\n\n# Zip the test folder\nzip_folder(test_folder, zip_file_path)\n\nprint(f\"Folder '{test_folder}' has been zipped to '{zip_file_path}'\")\n","metadata":{},"execution_count":null,"outputs":[]}]}