{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"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\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:50:06.774165Z","iopub.execute_input":"2026-09-11T02:50:06.774553Z","iopub.status.idle":"2026-09-11T02:50:08.730629Z","shell.execute_reply.started":"2026-09-11T02:50:06.774529Z","shell.execute_reply":"2026-09-11T02:50:08.729861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nimport glob\nimport os\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:50:08.732265Z","iopub.execute_input":"2026-09-11T02:50:08.732731Z","iopub.status.idle":"2026-09-11T02:50:24.348321Z","shell.execute_reply.started":"2026-09-11T02:50:08.732707Z","shell.execute_reply":"2026-09-11T02:50:24.347634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"zip_files = glob.glob(\"../input/**/*.zip\", recursive=True)\n\nfor file in zip_files:\n    with zipfile.ZipFile(file, 'r') as z:\n        z.extractall(\"./\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:50:24.352449Z","iopub.execute_input":"2026-09-11T02:50:24.352672Z","iopub.status.idle":"2026-09-11T02:50:46.616419Z","shell.execute_reply.started":"2026-09-11T02:50:24.352650Z","shell.execute_reply":"2026-09-11T02:50:46.615717Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_train_files = os.listdir(\"./train\")\ndf = pd.DataFrame({\"filename\": all_train_files})\ndf[\"category\"] = df[\"filename\"].apply(lambda x: \"dog\" if \"dog\" in x else \"cat\")\ntrain_df, val_df = train_test_split(df, test_size=0.2, random_state=99)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:51:24.655453Z","iopub.execute_input":"2026-09-11T02:51:24.656007Z","iopub.status.idle":"2026-09-11T02:51:24.686352Z","shell.execute_reply.started":"2026-09-11T02:51:24.655977Z","shell.execute_reply":"2026-09-11T02:51:24.685659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:51:26.754893Z","iopub.execute_input":"2026-09-11T02:51:26.755174Z","iopub.status.idle":"2026-09-11T02:51:26.761387Z","shell.execute_reply.started":"2026-09-11T02:51:26.755152Z","shell.execute_reply":"2026-09-11T02:51:26.760655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(val_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:51:28.754866Z","iopub.execute_input":"2026-09-11T02:51:28.755715Z","iopub.status.idle":"2026-09-11T02:51:28.761561Z","shell.execute_reply.started":"2026-09-11T02:51:28.755683Z","shell.execute_reply":"2026-09-11T02:51:28.760587Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_size = (150, 150)\nbatch = 32\ngen = ImageDataGenerator(rescale=1.0/255.0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:51:49.355717Z","iopub.execute_input":"2026-09-11T02:51:49.356694Z","iopub.status.idle":"2026-09-11T02:51:49.360941Z","shell.execute_reply.started":"2026-09-11T02:51:49.356657Z","shell.execute_reply":"2026-09-11T02:51:49.360226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_gen = gen.flow_from_dataframe(train_df, \"./train\", x_col=\"filename\", y_col=\"category\", target_size=img_size, class_mode=\"binary\", batch_size=batch)\nval_gen = gen.flow_from_dataframe(val_df, \"./train\", x_col=\"filename\", y_col=\"category\", target_size=img_size, class_mode=\"binary\", batch_size=batch)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:52:02.345006Z","iopub.execute_input":"2026-09-11T02:52:02.345675Z","iopub.status.idle":"2026-09-11T02:52:02.538685Z","shell.execute_reply.started":"2026-09-11T02:52:02.345628Z","shell.execute_reply":"2026-09-11T02:52:02.537917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_files = os.listdir(\"./test1\")\ntest_df = pd.DataFrame({\"filename\": test_files})\ntest_gen = gen.flow_from_dataframe(test_df, \"./test1\", x_col=\"filename\", class_mode=None, target_size=img_size, batch_size=batch, shuffle=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:52:13.170092Z","iopub.execute_input":"2026-09-11T02:52:13.170809Z","iopub.status.idle":"2026-09-11T02:52:13.259223Z","shell.execute_reply.started":"2026-09-11T02:52:13.170778Z","shell.execute_reply":"2026-09-11T02:52:13.258558Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),\n    MaxPooling2D(2,2),\n    \n    Conv2D(64, (3,3), activation='relu'),\n    MaxPooling2D(2,2),\n    \n    Conv2D(128, (3,3), activation='relu'),\n    MaxPooling2D(2,2),\n    \n    Flatten(),\n    Dense(512, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')\n])\n\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:52:44.155516Z","iopub.execute_input":"2026-09-11T02:52:44.156464Z","iopub.status.idle":"2026-09-11T02:52:46.733787Z","shell.execute_reply.started":"2026-09-11T02:52:44.156433Z","shell.execute_reply":"2026-09-11T02:52:46.733134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:54:46.725074Z","iopub.execute_input":"2026-09-11T02:54:46.725464Z","iopub.status.idle":"2026-09-11T02:54:46.746879Z","shell.execute_reply.started":"2026-09-11T02:54:46.725437Z","shell.execute_reply":"2026-09-11T02:54:46.746254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_gen, validation_data=val_gen, epochs=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T02:55:06.810004Z","iopub.execute_input":"2026-09-11T02:55:06.811298Z","iopub.status.idle":"2026-09-11T03:01:08.369268Z","shell.execute_reply.started":"2026-09-11T02:55:06.811253Z","shell.execute_reply":"2026-09-11T03:01:08.368448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_imgs = next(test_gen)[:10]\npreds = model.predict(test_imgs)\nplt.figure(figsize=(15, 6))\n\nfor i in range(10):\n    plt.subplot(2, 5, i+1)\n    plt.imshow(test_imgs[i])\n    label = \"Dog\" if preds[i][0] > 0.5 else \"Cat\"    \n    plt.title(f\"{label} ({preds[i][0]:.2f})\")\n    plt.axis('off')\n    \nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-11T03:05:56.575585Z","iopub.execute_input":"2026-09-11T03:05:56.575902Z","iopub.status.idle":"2026-09-11T03:05:58.317424Z","shell.execute_reply.started":"2026-09-11T03:05:56.575878Z","shell.execute_reply":"2026-09-11T03:05:58.316362Z"}},"outputs":[],"execution_count":null}]}