{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np          # linear algebra\nimport pandas as pd         # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom random import randint\nfrom tqdm import tqdm       # Progress Bars\nimport cv2\nimport matplotlib.pyplot as plt\nimport os\n\n\nDATA_DIR = \"../input\"\n\nprint(os.listdir(DATA_DIR))\n","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:35:44.508825Z","iopub.execute_input":"2024-07-18T15:35:44.509180Z","iopub.status.idle":"2024-07-18T15:35:44.515771Z","shell.execute_reply.started":"2024-07-18T15:35:44.509155Z","shell.execute_reply":"2024-07-18T15:35:44.514857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nfrom sklearn.preprocessing import label_binarize\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:35:44.517599Z","iopub.execute_input":"2024-07-18T15:35:44.518491Z","iopub.status.idle":"2024-07-18T15:35:44.535867Z","shell.execute_reply.started":"2024-07-18T15:35:44.518462Z","shell.execute_reply":"2024-07-18T15:35:44.535045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(os.path.join(DATA_DIR, \"dog-breed-identification/labels.csv\"))\n\n\nprint(\"Shape of the training data:\", df_train.shape)\nprint(df_train.head(10))\nprint(\"\\nData Information:\")\nprint(df_train.info())\nprint(\"\\nMissing values per column:\")\nprint(df_train.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:35:44.536808Z","iopub.execute_input":"2024-07-18T15:35:44.537088Z","iopub.status.idle":"2024-07-18T15:35:44.597927Z","shell.execute_reply.started":"2024-07-18T15:35:44.537066Z","shell.execute_reply":"2024-07-18T15:35:44.597065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(images, classes, img_width=224, img_height=224):\n    \n    # images (list):\n    # classes (list): \n    # img_width (int): \n    # img_height (int): \n    \n    assert len(images) == len(classes) == 9\n\n    \n    fig, axes = plt.subplots(3, 3, figsize=(15, 15), sharex=True)\n    fig.subplots_adjust(hspace=0.3, wspace=0.3)\n   \n    for i, ax in enumerate(axes.flat):\n        \n        img = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)\n        img = cv2.resize(img, (img_width, img_height))\n        \n        \n        ax.imshow(img)\n        \n        xlabel = \"Breed: {0}\".format(classes[i])\n        ax.set_xlabel(xlabel)\n        ax.xaxis.label.set_size(10)\n        \n        \n        ax.set_xticks([])\n        ax.set_yticks([])\n    \n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:35:44.600211Z","iopub.execute_input":"2024-07-18T15:35:44.600890Z","iopub.status.idle":"2024-07-18T15:35:44.608480Z","shell.execute_reply.started":"2024-07-18T15:35:44.600854Z","shell.execute_reply":"2024-07-18T15:35:44.607571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_width = 250\nimg_height = 250\nimages = []\nclasses = []\n\n\nTRAIN_DIR = os.path.join(DATA_DIR, 'dog-breed-identification/train')\n\n\nfor f, breed in tqdm(df_train.values):\n    try:\n        \n        img_path = os.path.join(TRAIN_DIR, f + '.jpg')\n        \n        \n        img = cv2.imread(img_path)\n        \n        \n        if img is None:\n            print(f\"Warning: Image {img_path} could not be loaded.\")\n            continue\n        \n        \n        img_resized = cv2.resize(img, (img_width, img_height))\n        \n        \n        images.append(img_resized)\n        classes.append(breed)\n        \n    except Exception as e:\n        print(f\"Error loading image {img_path}: {e}\")\nprint(\"Number of images loaded:\", len(images))\nprint(\"First 5 classes:\", classes[:5])","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:43:43.537866Z","iopub.execute_input":"2024-07-18T15:43:43.538245Z","iopub.status.idle":"2024-07-18T15:44:10.845584Z","shell.execute_reply.started":"2024-07-18T15:43:43.538216Z","shell.execute_reply":"2024-07-18T15:44:10.844543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_images_to_show = 9\nrandom_indices = [randint(0, len(images) - 1) for _ in range(num_images_to_show)]\nprint(\"Random indices:\", random_indices)\n\nimages_to_show = [images[i] for i in random_indices]\nclasses_to_show = [classes[i] for i in random_indices]\n\nprint(f\"Images to show: {len(images_to_show)}\")\nprint(f\"Classes to show: {len(classes_to_show)}\")\n\n\nplot_images(images_to_show, classes_to_show, img_width=img_width, img_height=img_height)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:44:56.492291Z","iopub.execute_input":"2024-07-18T15:44:56.493017Z","iopub.status.idle":"2024-07-18T15:44:57.802699Z","shell.execute_reply.started":"2024-07-18T15:44:56.492988Z","shell.execute_reply":"2024-07-18T15:44:57.801560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_breeds = ['scottish_deerhound', 'maltese_dog', 'bernese_mountain_dog']\n\n\nfiltered_labels = df_train[df_train['breed'].isin(selected_breeds)]\n\n\nfiltered_labels = filtered_labels.reset_index(drop=True)\n\n\nfiltered_labels.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:45:37.401261Z","iopub.execute_input":"2024-07-18T15:45:37.401626Z","iopub.status.idle":"2024-07-18T15:45:37.414112Z","shell.execute_reply.started":"2024-07-18T15:45:37.401593Z","shell.execute_reply":"2024-07-18T15:45:37.413211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\n\n\nfiltered_labels = filtered_labels.copy()\n\n\nCLASS_NAMES = filtered_labels['breed'].unique()\n\n\nimg_height, img_width = 224, 224\nX_data = np.zeros((len(filtered_labels), img_height, img_width, 3), dtype='float32')\nY_data = label_binarize(filtered_labels['breed'], classes=selected_breeds)\n\n\nfor i in tqdm(range(len(filtered_labels))):\n    img_path = f\"/kaggle/input/dog-breed-identification/train/{filtered_labels['id'][i]}.jpg\"\n    img = image.load_img(img_path, target_size=(img_height, img_width))\n    img_array = image.img_to_array(img)\n    X_data[i] = img_array / 255.0\n    \n\nprint('\\nTrain Images shape: ', X_data.shape, ' size: {:,}'.format(X_data.size))\nprint('One-hot encoded output shape: ', Y_data.shape, ' size: {:,}'.format(Y_data.size))","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:46:12.403347Z","iopub.execute_input":"2024-07-18T15:46:12.403708Z","iopub.status.idle":"2024-07-18T15:46:13.280623Z","shell.execute_reply.started":"2024-07-18T15:46:12.403680Z","shell.execute_reply":"2024-07-18T15:46:13.279701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (5,5), activation ='relu', input_shape = (224,224,3)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 16, kernel_size = (7,7), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 8, kernel_size = (5,5), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation = \"relu\", kernel_regularizer = 'l2'))\nmodel.add(Dense(64, activation = \"relu\", kernel_regularizer = 'l2'))\nmodel.add(Dense(len(CLASS_NAMES), activation=\"softmax\"))\n\n\nmodel.compile(loss = 'categorical_crossentropy', optimizer = Adam(0.0001),metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:47:37.402588Z","iopub.execute_input":"2024-07-18T15:47:37.403670Z","iopub.status.idle":"2024-07-18T15:47:38.345729Z","shell.execute_reply.started":"2024-07-18T15:47:37.403633Z","shell.execute_reply":"2024-07-18T15:47:38.344897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n\nX_train_and_val, X_test, Y_train_and_val, Y_test = train_test_split(X_data, Y_data, test_size=0.1, random_state=42)\n\n\nX_train, X_val, Y_train, Y_val = train_test_split(X_train_and_val, Y_train_and_val, test_size=0.2, random_state=42)\n\n\nprint('Training data shape:', X_train.shape)\nprint('Validation data shape:', X_val.shape)\nprint('Testing data shape:', X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:48:23.613720Z","iopub.execute_input":"2024-07-18T15:48:23.614321Z","iopub.status.idle":"2024-07-18T15:48:23.745577Z","shell.execute_reply.started":"2024-07-18T15:48:23.614292Z","shell.execute_reply":"2024-07-18T15:48:23.744653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, batch_size=128, epochs=100, validation_data=(X_val, Y_val))\n\n\nplt.figure(figsize=(12, 6))\n\n\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\n\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:49:19.216765Z","iopub.execute_input":"2024-07-18T15:49:19.217167Z","iopub.status.idle":"2024-07-18T15:50:53.014898Z","shell.execute_reply.started":"2024-07-18T15:49:19.217138Z","shell.execute_reply":"2024-07-18T15:50:53.013892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = model.predict(X_test)\nloss, accuracy = model.evaluate(X_test, Y_test)\nprint(f'Accuracy over the test set: {accuracy * 100:.2f}%')","metadata":{"execution":{"iopub.status.busy":"2024-07-18T15:59:20.028023Z","iopub.execute_input":"2024-07-18T15:59:20.028806Z","iopub.status.idle":"2024-07-18T15:59:27.138827Z","shell.execute_reply.started":"2024-07-18T15:59:20.028777Z","shell.execute_reply":"2024-07-18T15:59:27.137866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_test[2])\nplt.axis('off')  \nplt.show()\n\ntrue_label = filtered_labels['breed'][np.argmax(Y_test[2])]\npredicted_label = filtered_labels['breed'][np.argmax(Y_pred[2])]\n\nprint(\"Originally:\", true_label)\nprint(\"Predicted:\", predicted_label)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T16:01:02.729612Z","iopub.execute_input":"2024-07-18T16:01:02.730206Z","iopub.status.idle":"2024-07-18T16:01:02.946982Z","shell.execute_reply.started":"2024-07-18T16:01:02.730173Z","shell.execute_reply":"2024-07-18T16:01:02.946135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred_prob = model.predict(X_test)\n\n\nY_pred_labels = [CLASS_NAMES[np.argmax(pred)] for pred in Y_pred_prob]\n\n\ntest_indices = range(len(X_test))\n\n\ntest_ids = filtered_labels.loc[test_indices, 'id']\n\n\nresults_df = pd.DataFrame({'id': test_ids, 'breed': Y_pred_labels})\n\n\nresults_df.to_csv('submission.csv', index=False)\n","metadata":{},"execution_count":null,"outputs":[]}]}