{"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":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Dropout","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:12:48.722938Z","iopub.execute_input":"2026-07-16T04:12:48.723197Z","iopub.status.idle":"2026-07-16T04:13:06.290465Z","shell.execute_reply.started":"2026-07-16T04:12:48.723175Z","shell.execute_reply":"2026-07-16T04:13:06.289706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = \"/kaggle/input/competitions/dog-breed-identification/train\"\n\nlabels_path = \"/kaggle/input/competitions/dog-breed-identification/labels.csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:14:49.530049Z","iopub.execute_input":"2026-07-16T04:14:49.530543Z","iopub.status.idle":"2026-07-16T04:14:49.534630Z","shell.execute_reply.started":"2026-07-16T04:14:49.530514Z","shell.execute_reply":"2026-07-16T04:14:49.533882Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = pd.read_csv(labels_path)\n\nprint(labels.head())\n\nprint(\"Total Images :\", len(labels))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:14:52.298542Z","iopub.execute_input":"2026-07-16T04:14:52.299296Z","iopub.status.idle":"2026-07-16T04:14:52.336413Z","shell.execute_reply.started":"2026-07-16T04:14:52.299265Z","shell.execute_reply":"2026-07-16T04:14:52.335670Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Number of Dog Breeds :\")\n\nprint(labels[\"breed\"].nunique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:14:55.370417Z","iopub.execute_input":"2026-07-16T04:14:55.370833Z","iopub.status.idle":"2026-07-16T04:14:55.378175Z","shell.execute_reply.started":"2026-07-16T04:14:55.370805Z","shell.execute_reply":"2026-07-16T04:14:55.377400Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12,8))\n\nfor i in range(6):\n\n    image_path = os.path.join(\n\n        train_dir,\n\n        labels.iloc[i][\"id\"] + \".jpg\"\n\n    )\n\n    image = plt.imread(image_path)\n\n    plt.subplot(2,3,i+1)\n\n    plt.imshow(image)\n\n    plt.title(labels.iloc[i][\"breed\"])\n\n    plt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:14:58.533725Z","iopub.execute_input":"2026-07-16T04:14:58.534498Z","iopub.status.idle":"2026-07-16T04:14:59.122560Z","shell.execute_reply.started":"2026-07-16T04:14:58.534468Z","shell.execute_reply":"2026-07-16T04:14:59.121582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n\n    rescale=1./255,\n\n    validation_split=0.2\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:15:11.831140Z","iopub.execute_input":"2026-07-16T04:15:11.831739Z","iopub.status.idle":"2026-07-16T04:15:11.835891Z","shell.execute_reply.started":"2026-07-16T04:15:11.831707Z","shell.execute_reply":"2026-07-16T04:15:11.834963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels[\"filename\"] = labels[\"id\"] + \".jpg\"\n\ntrain_generator = datagen.flow_from_dataframe(\n\n    dataframe=labels,\n\n    directory=train_dir,\n\n    x_col=\"filename\",\n\n    y_col=\"breed\",\n\n    target_size=(128,128),\n\n    batch_size=32,\n\n    class_mode=\"categorical\",\n\n    subset=\"training\"\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:16:56.699909Z","iopub.execute_input":"2026-07-16T04:16:56.700692Z","iopub.status.idle":"2026-07-16T04:17:13.719617Z","shell.execute_reply.started":"2026-07-16T04:16:56.700662Z","shell.execute_reply":"2026-07-16T04:17:13.719046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"validation_generator = datagen.flow_from_dataframe(\n\n    dataframe=labels,\n\n    directory=train_dir,\n\n    x_col=\"filename\",\n\n    y_col=\"breed\",\n\n    target_size=(128,128),\n\n    batch_size=32,\n\n    class_mode=\"categorical\",\n\n    subset=\"validation\"\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:17:26.135230Z","iopub.execute_input":"2026-07-16T04:17:26.135650Z","iopub.status.idle":"2026-07-16T04:17:35.924896Z","shell.execute_reply.started":"2026-07-16T04:17:26.135621Z","shell.execute_reply":"2026-07-16T04:17:35.923898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_classes = len(train_generator.class_indices)\n\nmodel = Sequential([\n\n    Conv2D(\n        32,\n        (3,3),\n        activation='relu',\n        input_shape=(128,128,3)\n    ),\n\n    MaxPooling2D(2,2),\n\n    Conv2D(\n        64,\n        (3,3),\n        activation='relu'\n    ),\n\n    MaxPooling2D(2,2),\n\n    Conv2D(\n        128,\n        (3,3),\n        activation='relu'\n    ),\n\n    MaxPooling2D(2,2),\n\n    Flatten(),\n\n    Dense(\n        512,\n        activation='relu'\n    ),\n\n    Dropout(0.5),\n\n    Dense(\n        num_classes,\n        activation='softmax'\n    )\n\n])\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:18:54.030567Z","iopub.execute_input":"2026-07-16T04:18:54.031636Z","iopub.status.idle":"2026-07-16T04:18:55.610154Z","shell.execute_reply.started":"2026-07-16T04:18:54.031601Z","shell.execute_reply":"2026-07-16T04:18:55.609550Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n\n    optimizer='adam',\n\n    loss='categorical_crossentropy',\n\n    metrics=['accuracy']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:19:19.071450Z","iopub.execute_input":"2026-07-16T04:19:19.072263Z","iopub.status.idle":"2026-07-16T04:19:19.086685Z","shell.execute_reply.started":"2026-07-16T04:19:19.072231Z","shell.execute_reply":"2026-07-16T04:19:19.085594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n\n    train_generator,\n\n    validation_data=validation_generator,\n\n    epochs=10\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:19:23.199777Z","iopub.execute_input":"2026-07-16T04:19:23.200179Z","iopub.status.idle":"2026-07-16T04:24:36.907804Z","shell.execute_reply.started":"2026-07-16T04:19:23.200153Z","shell.execute_reply":"2026-07-16T04:24:36.906776Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\n\nplt.plot(\n\n    history.history[\"accuracy\"],\n\n    label=\"Training Accuracy\"\n\n)\n\nplt.plot(\n\n    history.history[\"val_accuracy\"],\n\n    label=\"Validation Accuracy\"\n\n)\n\nplt.legend()\n\nplt.grid()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:53:57.211689Z","iopub.execute_input":"2026-07-16T04:53:57.212730Z","iopub.status.idle":"2026-07-16T04:53:57.332366Z","shell.execute_reply.started":"2026-07-16T04:53:57.212691Z","shell.execute_reply":"2026-07-16T04:53:57.331571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\n\nplt.plot(\n\n    history.history[\"loss\"],\n\n    label=\"Training Loss\"\n\n)\n\nplt.plot(\n\n    history.history[\"val_loss\"],\n\n    label=\"Validation Loss\"\n\n)\n\nplt.legend()\n\nplt.grid()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:54:00.932952Z","iopub.execute_input":"2026-07-16T04:54:00.933735Z","iopub.status.idle":"2026-07-16T04:54:01.053557Z","shell.execute_reply.started":"2026-07-16T04:54:00.933707Z","shell.execute_reply":"2026-07-16T04:54:01.052637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_batch = next(validation_generator)\n\nimages, labels_batch = sample_batch\n\nprediction = model.predict(images)\n\npredicted_class = np.argmax(\n\n    prediction[0]\n\n)\n\nclass_labels = list(\n\n    validation_generator.class_indices.keys()\n\n)\n\nprint(\n\n    \"Predicted Breed:\",\n\n    class_labels[predicted_class]\n\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:54:04.370140Z","iopub.execute_input":"2026-07-16T04:54:04.370931Z","iopub.status.idle":"2026-07-16T04:54:04.931590Z","shell.execute_reply.started":"2026-07-16T04:54:04.370901Z","shell.execute_reply":"2026-07-16T04:54:04.930848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(images[0])\n\nplt.title(\n\n    class_labels[predicted_class]\n\n)\n\nplt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:54:13.863796Z","iopub.execute_input":"2026-07-16T04:54:13.864387Z","iopub.status.idle":"2026-07-16T04:54:14.016362Z","shell.execute_reply.started":"2026-07-16T04:54:13.864358Z","shell.execute_reply":"2026-07-16T04:54:14.015514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\n\n    \"/kaggle/working/dog_breed_classification.keras\"\n\n)\n\nprint(\"Model Saved Successfully\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-16T04:54:33.380207Z","iopub.execute_input":"2026-07-16T04:54:33.380866Z","iopub.status.idle":"2026-07-16T04:54:34.054821Z","shell.execute_reply.started":"2026-07-16T04:54:33.380836Z","shell.execute_reply":"2026-07-16T04:54:34.054135Z"}},"outputs":[],"execution_count":null}]}