{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":31153,"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-10-14T14:43:33.346798Z","iopub.execute_input":"2025-10-14T14:43:33.347117Z","iopub.status.idle":"2025-10-14T14:43:35.803626Z","shell.execute_reply.started":"2025-10-14T14:43:33.347086Z","shell.execute_reply":"2025-10-14T14:43:35.802443Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!unzip -q \"/kaggle/input/dogs-vs-cats/train.zip\"\n!unzip -q \"/kaggle/input/dogs-vs-cats/test1.zip\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T14:54:27.900177Z","iopub.execute_input":"2025-10-14T14:54:27.900517Z","iopub.status.idle":"2025-10-14T14:54:46.297628Z","shell.execute_reply.started":"2025-10-14T14:54:27.900493Z","shell.execute_reply":"2025-10-14T14:54:46.296305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T16:36:38.361428Z","iopub.execute_input":"2025-10-14T16:36:38.361723Z","iopub.status.idle":"2025-10-14T16:36:38.367041Z","shell.execute_reply.started":"2025-10-14T16:36:38.361704Z","shell.execute_reply":"2025-10-14T16:36:38.365407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ndef prepare_data(train_path, val_size=0.2, random_state=42):\n    train_filenames = os.listdir(train_path)\n    train_categories = ['dog' if filename.split(\".\")[0] == 'dog' else 'cat' for filename in train_filenames]\n\n    df = pd.DataFrame({\n        'filename': train_filenames,\n        'category': train_categories\n    })\n\n    train_df, val_df = train_test_split(df, test_size=val_size, stratify=df[\"category\"], random_state=random_state)\n    return train_df, val_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T16:52:34.124136Z","iopub.execute_input":"2025-10-14T16:52:34.124481Z","iopub.status.idle":"2025-10-14T16:52:34.131830Z","shell.execute_reply.started":"2025-10-14T16:52:34.124460Z","shell.execute_reply":"2025-10-14T16:52:34.130478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_path = \"/kaggle/working/train\"\ntrain_df, val_df = prepare_data(train_path)\nprint(f\"Total Training Images: {len(train_df)}\")\nprint(f\"Total Validation Images: {len(val_df)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T16:52:36.332826Z","iopub.execute_input":"2025-10-14T16:52:36.333198Z","iopub.status.idle":"2025-10-14T16:52:36.393006Z","shell.execute_reply.started":"2025-10-14T16:52:36.333172Z","shell.execute_reply":"2025-10-14T16:52:36.391795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T16:53:09.403711Z","iopub.execute_input":"2025-10-14T16:53:09.404042Z","iopub.status.idle":"2025-10-14T16:53:09.414389Z","shell.execute_reply.started":"2025-10-14T16:53:09.404014Z","shell.execute_reply":"2025-10-14T16:53:09.413257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.15)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=train_path,\n    x_col='filename',\n    y_col='category',\n    target_size=(128, 128),\n    class_mode='binary',\n    batch_size=32,\n    subset='training'\n)\n\nval_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=train_path,\n    x_col='filename',\n    y_col='category',\n    target_size=(128, 128),\n    class_mode='binary',\n    batch_size=32,\n    subset='validation'\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T17:36:05.966649Z","iopub.execute_input":"2025-10-14T17:36:05.967308Z","iopub.status.idle":"2025-10-14T17:36:06.351620Z","shell.execute_reply.started":"2025-10-14T17:36:05.967263Z","shell.execute_reply":"2025-10-14T17:36:06.350435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, GlobalAveragePooling2D, Dropout\n\nIMAGE_SIZE = (128, 128, 3)\n\nmodel = Sequential([\n    Conv2D(32, (3,3), activation='relu', input_shape=IMAGE_SIZE),\n    MaxPooling2D(2,2),\n    Conv2D(64, (3,3), activation='relu'),\n    MaxPooling2D(2,2),\n    Conv2D(128, (3,3), activation='relu'),\n    MaxPooling2D(2,2),\n    GlobalAveragePooling2D(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')\n])\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T17:36:10.678339Z","iopub.execute_input":"2025-10-14T17:36:10.678644Z","iopub.status.idle":"2025-10-14T17:36:10.754807Z","shell.execute_reply.started":"2025-10-14T17:36:10.678624Z","shell.execute_reply":"2025-10-14T17:36:10.753918Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    loss='binary_crossentropy',\n    optimizer='adam',\n    metrics=['accuracy']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T17:36:15.207216Z","iopub.execute_input":"2025-10-14T17:36:15.207529Z","iopub.status.idle":"2025-10-14T17:36:15.218337Z","shell.execute_reply.started":"2025-10-14T17:36:15.207508Z","shell.execute_reply":"2025-10-14T17:36:15.217401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs=5,\n    validation_data=val_generator,\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T17:36:17.445160Z","iopub.execute_input":"2025-10-14T17:36:17.446127Z","iopub.status.idle":"2025-10-14T18:01:54.172366Z","shell.execute_reply.started":"2025-10-14T17:36:17.446083Z","shell.execute_reply":"2025-10-14T18:01:54.169200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.utils import load_img, img_to_array\n\ntest_dir = \"/kaggle/working/test1\"\n\ndef load_test_images(test_dir, image_size=(128, 128)):\n    image_ids = []\n    images = []\n    for filename in sorted(os.listdir(test_dir)):\n        if filename.endswith(\".jpg\"):\n            img_path = os.path.join(test_dir, filename)\n            img = load_img(img_path, target_size=image_size)\n            img_array = img_to_array(img) / 255.0\n            images.append(img_array)\n            image_ids.append(int(filename.split(\".\")[0]))\n    return np.array(images), image_ids\n\ntest_images, test_ids = load_test_images(test_dir)\npredictions = model.predict(test_images, batch_size=32, verbose=1)\npredictions = (predictions > 0.5).astype(int).flatten()\n\nsubmission_df = pd.DataFrame({\"id\": test_ids, \"label\": predictions})\nsubmission_df = submission_df.sort_values(by=\"id\")\nsubmission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T18:06:22.398734Z","iopub.execute_input":"2025-10-14T18:06:22.400979Z","iopub.status.idle":"2025-10-14T18:08:01.408642Z","shell.execute_reply.started":"2025-10-14T18:06:22.400935Z","shell.execute_reply":"2025-10-14T18:08:01.407377Z"}},"outputs":[],"execution_count":null}]}