{"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-10T06:14:06.947098Z","iopub.execute_input":"2026-09-10T06:14:06.947390Z","iopub.status.idle":"2026-09-10T06:14:08.209120Z","shell.execute_reply.started":"2026-09-10T06:14:06.947366Z","shell.execute_reply":"2026-09-10T06:14:08.208078Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport zipfile\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:08.210685Z","iopub.execute_input":"2026-09-10T06:14:08.211553Z","iopub.status.idle":"2026-09-10T06:14:09.212259Z","shell.execute_reply.started":"2026-09-10T06:14:08.211525Z","shell.execute_reply":"2026-09-10T06:14:09.211265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_path = \"../input/competitions/dogs-vs-cats\" if os.path.exists(\"../input/competitions/dogs-vs-cats\") else \"../input/dogs-vs-cats\"\n\nwith zipfile.ZipFile(f\"{base_path}/train.zip\", \"r\") as z:\n    z.extractall(\".\")\n\nwith zipfile.ZipFile(f\"{base_path}/test1.zip\", \"r\") as z:\n    z.extractall(\".\")\nprint(os.listdir(\"../input\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:09.213384Z","iopub.execute_input":"2026-09-10T06:14:09.213876Z","iopub.status.idle":"2026-09-10T06:14:28.213703Z","shell.execute_reply.started":"2026-09-10T06:14:09.213847Z","shell.execute_reply":"2026-09-10T06:14:28.212967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filenames = os.listdir(\"./train\")\ncategories = [\"Dog\" if name.split(\".\")[0] == \"dog\" else \"Cat\" for name in filenames]\n\ndf = pd.DataFrame({\n    \"filename\": filenames,\n    \"category\": categories\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:28.215826Z","iopub.execute_input":"2026-09-10T06:14:28.216189Z","iopub.status.idle":"2026-09-10T06:14:28.249852Z","shell.execute_reply.started":"2026-09-10T06:14:28.216162Z","shell.execute_reply":"2026-09-10T06:14:28.248839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df, val_df = train_test_split(df, test_size=0.20, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:28.251029Z","iopub.execute_input":"2026-09-10T06:14:28.251330Z","iopub.status.idle":"2026-09-10T06:14:28.270168Z","shell.execute_reply.started":"2026-09-10T06:14:28.251304Z","shell.execute_reply":"2026-09-10T06:14:28.269036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_filenames = os.listdir(\"./test1\")\ntest_df = pd.DataFrame({\n    \"filename\": test_filenames\n})\n\nprint(f\"Train samples: {len(train_df)}, Val samples: {len(val_df)}, Test samples: {len(test_df)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:28.271649Z","iopub.execute_input":"2026-09-10T06:14:28.272030Z","iopub.status.idle":"2026-09-10T06:14:28.289349Z","shell.execute_reply.started":"2026-09-10T06:14:28.271989Z","shell.execute_reply":"2026-09-10T06:14:28.288424Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:28.290322Z","iopub.execute_input":"2026-09-10T06:14:28.290700Z","iopub.status.idle":"2026-09-10T06:14:44.542172Z","shell.execute_reply.started":"2026-09-10T06:14:28.290659Z","shell.execute_reply":"2026-09-10T06:14:44.541424Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE = (200, 200)\nBATCH_SIZE = 32","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:44.543156Z","iopub.execute_input":"2026-09-10T06:14:44.544355Z","iopub.status.idle":"2026-09-10T06:14:44.549627Z","shell.execute_reply.started":"2026-09-10T06:14:44.544326Z","shell.execute_reply":"2026-09-10T06:14:44.548632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\ntrain_it = train_datagen.flow_from_dataframe(\n    train_df,\n    directory=\"./train\",\n    x_col=\"filename\",\n    y_col=\"category\",\n    target_size=IMAGE_SIZE,\n    class_mode=\"binary\",\n    batch_size=BATCH_SIZE\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:44.550890Z","iopub.execute_input":"2026-09-10T06:14:44.551229Z","iopub.status.idle":"2026-09-10T06:14:44.749165Z","shell.execute_reply.started":"2026-09-10T06:14:44.551192Z","shell.execute_reply":"2026-09-10T06:14:44.748000Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_datagen = ImageDataGenerator(rescale=1./255)\nval_it = val_datagen.flow_from_dataframe(\n    val_df,\n    directory=\"./train\",\n    x_col=\"filename\",\n    y_col=\"category\",\n    target_size=IMAGE_SIZE,\n    class_mode=\"binary\",\n    batch_size=BATCH_SIZE\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:44.752551Z","iopub.execute_input":"2026-09-10T06:14:44.753003Z","iopub.status.idle":"2026-09-10T06:14:44.807353Z","shell.execute_reply.started":"2026-09-10T06:14:44.752971Z","shell.execute_reply":"2026-09-10T06:14:44.806656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\ntest_it = test_datagen.flow_from_dataframe(\n    test_df,\n    directory=\"./test1\",\n    x_col=\"filename\",\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=BATCH_SIZE,\n    shuffle=False\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:44.808509Z","iopub.execute_input":"2026-09-10T06:14:44.809235Z","iopub.status.idle":"2026-09-10T06:14:44.900079Z","shell.execute_reply.started":"2026-09-10T06:14:44.809194Z","shell.execute_reply":"2026-09-10T06:14:44.899315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.optimizers import SGD","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:44.901101Z","iopub.execute_input":"2026-09-10T06:14:44.901716Z","iopub.status.idle":"2026-09-10T06:14:44.915257Z","shell.execute_reply.started":"2026-09-10T06:14:44.901685Z","shell.execute_reply":"2026-09-10T06:14:44.914317Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model = VGG16(include_top=False, input_shape=(200, 200, 3))\nfor layer in base_model.layers:\n    layer.trainable = False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:44.916556Z","iopub.execute_input":"2026-09-10T06:14:44.916927Z","iopub.status.idle":"2026-09-10T06:14:48.379402Z","shell.execute_reply.started":"2026-09-10T06:14:44.916890Z","shell.execute_reply":"2026-09-10T06:14:48.378672Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = Flatten()(base_model.output)\nx = Dense(128, activation='relu', kernel_initializer='he_uniform')(x)\noutput = Dense(1, activation='sigmoid')(x)\n\nmodel_vgg16 = Model(inputs=base_model.input, outputs=output)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:48.380562Z","iopub.execute_input":"2026-09-10T06:14:48.380897Z","iopub.status.idle":"2026-09-10T06:14:48.421400Z","shell.execute_reply.started":"2026-09-10T06:14:48.380862Z","shell.execute_reply":"2026-09-10T06:14:48.420561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"opt = SGD(learning_rate=0.001, momentum=0.9)\nmodel_vgg16.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:48.422508Z","iopub.execute_input":"2026-09-10T06:14:48.422834Z","iopub.status.idle":"2026-09-10T06:14:48.446522Z","shell.execute_reply.started":"2026-09-10T06:14:48.422797Z","shell.execute_reply":"2026-09-10T06:14:48.445674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model_vgg16.fit(\n    train_it,\n    steps_per_epoch=len(train_it),\n    validation_data=val_it,\n    validation_steps=len(val_it),\n    epochs=5,\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:14:48.447802Z","iopub.execute_input":"2026-09-10T06:14:48.448609Z","iopub.status.idle":"2026-09-10T06:19:31.487895Z","shell.execute_reply.started":"2026-09-10T06:14:48.448566Z","shell.execute_reply":"2026-09-10T06:19:31.487123Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_images = test_it.__next__()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:19:31.489039Z","iopub.execute_input":"2026-09-10T06:19:31.489387Z","iopub.status.idle":"2026-09-10T06:19:31.553615Z","shell.execute_reply.started":"2026-09-10T06:19:31.489350Z","shell.execute_reply":"2026-09-10T06:19:31.552887Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_indices = np.random.choice(len(test_images), size=10, replace=False)\nsample_images = test_images[sample_indices]\n\npredictions_prob = model_vgg16.predict(sample_images)\npredicted_classes = (predictions_prob > 0.5).astype(int).flatten()\n\nlabels_map = {v: k for k, v in train_it.class_indices.items()}\n\nfig, axes = plt.subplots(2, 5, figsize=(16, 7))\naxes = axes.flatten()\n\nfor i, ax in enumerate(axes):\n    ax.imshow(sample_images[i])\n    pred_label = labels_map[predicted_classes[i]]\n    prob_val = predictions_prob[i][0]\n    \n    ax.set_title(f\"Pred: {pred_label}\\n(Prob: {prob_val:.2f})\")\n    ax.axis(\"off\")\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-10T06:19:31.554874Z","iopub.execute_input":"2026-09-10T06:19:31.555246Z","iopub.status.idle":"2026-09-10T06:19:35.717838Z","shell.execute_reply.started":"2026-09-10T06:19:31.555206Z","shell.execute_reply":"2026-09-10T06:19:35.716620Z"}},"outputs":[],"execution_count":null}]}