{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Importing Required Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import classification_report, confusion_matrix\n\n# deep learning libraries\nimport tensorflow as tf\nimport keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import applications\nfrom keras.models import Sequential, load_model\nfrom keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D, Flatten, Dense, Dropout\nfrom keras.preprocessing import image\n\nimport cv2\nimport os\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Loading and Displaying Labels Data","metadata":{}},{"cell_type":"code","source":"labels=pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")\nlabels.head(10)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Listing Directory Contents","metadata":{}},{"cell_type":"markdown","source":"####Description: This cell lists the files and directories in the input path '/kaggle/input/dog-breed-identification' to identify the available data files and folders, such as train, test, and CSV files.","metadata":{}},{"cell_type":"code","source":"path1=os.listdir('/kaggle/input/dog-breed-identification')\npath1","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Constructing Train Path\n\n#### Description: This cell constructs the full path to the training images directory by joining the base input path with the 'train' folder name, preparing for image file access.","metadata":{}},{"cell_type":"code","source":"train_path=os.path.join('/kaggle/input/dog-breed-identification',path1[2])\ntrain_path","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Constructing Test Path\n\n\n#### Description: This cell constructs the full path to the testing images directory by joining the base input path with the 'test' folder name, preparing for test data handling.","metadata":{}},{"cell_type":"code","source":"test_path=os.path.join('/kaggle/input/dog-breed-identification',path1[1])\ntest_path","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Collecting Training Image Paths\n\n\n#### Description: This cell walks through the training directory using os.walk() to collect and append the full paths of all image files into a list called 'train' for further processing.","metadata":{}},{"cell_type":"code","source":"train = []\nfor root, dirs, files in os.walk(train_path):\n    for file in files:\n        full_path = os.path.join(root, file)\n        train.append(full_path)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Checking Number of Training Images","metadata":{}},{"cell_type":"code","source":"len(train)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Displaying Training Image Paths","metadata":{}},{"cell_type":"code","source":"train","metadata":{"trusted":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Augmentation and Preprocessing Setup\n#### Description: This cell sets up ImageDataGenerator for training and validation with data augmentation techniques like rescaling, rotation, shifting, zooming, and flipping to enhance model generalization.","metadata":{}},{"cell_type":"code","source":"img=cv2.imread(train[0])\nplt.imshow(img)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imgRGB=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\nplt.imshow(imgRGB)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(labels)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test=[]\nfor root,dirs,files in os.walk(test_path):\n    for file in files:\n        full_path=os.path.join(root,file)\n        test.append(full_path)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test","metadata":{"trusted":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img=cv2.imread(test[0])\nplt.imshow(img)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\nplt.imshow(img)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Preparing Labels for Training\n#### Description: This cell adds '.jpg' extensions to image IDs in the labels DataFrame and converts breed names to strings for compatibility with the data generator.","metadata":{}},{"cell_type":"code","source":"def lab(ID):\n    return ID+'.jpg'","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels['id']=labels['id'].apply(lab)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample=pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\nlen(sample)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(test)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels.columns","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Splitting Data into Train and Validation Sets\n#### Description: This cell splits the labels DataFrame into training and validation sets using train_test_split from scikit-learn, with an 80-20 split ratio.","metadata":{}},{"cell_type":"code","source":"gen = ImageDataGenerator(\n    rescale=1./255.,         \n    horizontal_flip=True,     \n    validation_split=0.2      \n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Creating Training Data Generator\n#### Description: This cell creates a flow_from_dataframe generator for the training set, loading images from the directory, applying augmentations, and preparing batches for model training.","metadata":{}},{"cell_type":"code","source":"train_generator = gen.flow_from_dataframe(\n    dataframe=labels,         \n    directory=train_path,     \n    x_col='id',                \n    y_col='breed',            \n    subset=\"training\",         \n    color_mode=\"rgb\",\n    target_size=(331,331),     \n    class_mode=\"categorical\",  \n    batch_size=32,\n    shuffle=True,\n    seed=42\n)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Creating Validation Data Generator\n#### Description: This cell creates a flow_from_dataframe generator for the validation set, loading images without augmentation for evaluation during training.","metadata":{}},{"cell_type":"code","source":"val_generator=gen.flow_from_dataframe(\n    labels,\n    directory=train_path,\n    x_col='id',\n    y_col='breed',\n    subset=\"validation\",\n    class_mode='categorical',\n    batch_size=32,\n    color_mode=\"rgb\",\n    shuffle=True,\n    seed=42,\n    target_size=(331,331)\n    \n    \n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Visualizing Sample Training Images\n#### Description: This cell displays a grid of 12 sample images from the training generator along with their breed labels using Matplotlib for data inspection.","metadata":{}},{"cell_type":"code","source":"x,y = next(train_generator)\nprint(x.shape)\nprint(y.shape)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_name=train_generator.class_indices","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_name","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_name=list(image_name.keys())\nclass_name","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_image(image,label):\n    plt.figure(figsize=(12,12))\n    for i in range(16):\n        plt.subplot(4,4,i+1)\n        plt.imshow(image[i])\n        plt.title(class_name[np.argmax(label[i])])\n        plt.axis('off')\nplot_image(x,y)","metadata":{"trusted":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Loading Pretrained InceptionV3 Model\n#### Description: This cell loads the InceptionV3 pretrained model from Keras applications, excluding the top layers, and freezes its weights for transfer learning.","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.InceptionResNetV2(\n                     include_top=False,\n                     weights='imagenet',\n                     input_shape=(331,331,3)\n                     )","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Building Custom Model on Top of InceptionV3\n#### Description: This cell constructs a custom classification model by adding global average pooling, dense layers with dropout, and a softmax output layer for 120 dog breeds on top of the InceptionV3 base.","metadata":{}},{"cell_type":"code","source":"base_model.trainable=False","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"input_tensor = tf.keras.Input(shape=(331,331,3))\noutput_tensor = base_model(input_tensor)\nmodel = tf.keras.Sequential([\n        base_model,\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.BatchNormalization(),\n        tf.keras.layers.Dense(512, activation='relu'),\n        tf.keras.layers.Dense(256, activation='relu'),\n        tf.keras.layers.Dropout(0.5),\n        tf.keras.layers.Dense(128, activation='relu'),\n        tf.keras.layers.Dense(120, activation='softmax')\n    ])\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Compiling the Model\n#### Description: This cell compiles the model with Adam optimizer, categorical cross-entropy loss, and accuracy as the metric for multi-class classification.","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Setting Up Callbacks\n#### Description: This cell defines callbacks including ModelCheckpoint to save the best model based on validation loss and EarlyStopping to prevent overfitting by monitoring validation loss.","metadata":{}},{"cell_type":"code","source":"early = tf.keras.callbacks.EarlyStopping( patience=10,\n                                          min_delta=0.001,\n                                          restore_best_weights=True)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training the Model\n#### Description: This cell trains the model using the training and validation generators for 50 epochs, applying the defined callbacks to optimize the training process.","metadata":{}},{"cell_type":"code","source":"batch_size=32\nSTEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = val_generator.n//val_generator.batch_size\n\n\nhistory = model.fit(train_generator,\n                    steps_per_epoch=STEP_SIZE_TRAIN,\n                    validation_data=val_generator,\n                    validation_steps=STEP_SIZE_VALID,\n                    epochs=25,\n                    callbacks=[early])\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"InceptionResNetV2RGB 90\n","metadata":{}},{"cell_type":"markdown","source":"## Plotting Training History\n#### Description: This cell visualizes the training and validation accuracy/loss curves over epochs using Matplotlib to assess model performance and detect overfitting.","metadata":{}},{"cell_type":"code","source":"# store results\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\n\n# plot results\n# accuracy\nplt.figure(figsize=(10, 16))\nplt.rcParams['figure.figsize'] = [16, 9]\nplt.rcParams['font.size'] = 14\nplt.rcParams['axes.grid'] = True\nplt.rcParams['figure.facecolor'] = 'white'\nplt.subplot(2, 1, 1)\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.ylabel('Accuracy')\nplt.title(f'\\nTraining and Validation Accuracy. \\nTrain Accuracy: {str(acc[-1])}\\nValidation Accuracy: {str(val_acc[-1])}')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Saving the Trained Model\n#### Description: This cell saves the entire trained model to a file named '1.h5' in HDF5 format for later use or deployment.","metadata":{}},{"cell_type":"code","source":"model.save('1.h5')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}