{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nfrom IPython.display import Image\nfrom sklearn.model_selection import train_test_split\n\nfrom matplotlib.pyplot import imread\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nimport tensorflow_hub as hub\n\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:28.024104Z","iopub.execute_input":"2023-10-30T07:43:28.024753Z","iopub.status.idle":"2023-10-30T07:43:28.030947Z","shell.execute_reply.started":"2023-10-30T07:43:28.024720Z","shell.execute_reply":"2023-10-30T07:43:28.029916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Check GPU Availability in Tensorflow**","metadata":{}},{"cell_type":"code","source":"gpus = tf.config.experimental.list_physical_devices('GPU')\nfor gpu in gpus:\n    print(\"Name:\", gpu.name, \"  Type:\", gpu.device_type)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:29.342665Z","iopub.execute_input":"2023-10-30T07:43:29.343591Z","iopub.status.idle":"2023-10-30T07:43:29.460318Z","shell.execute_reply.started":"2023-10-30T07:43:29.343552Z","shell.execute_reply":"2023-10-30T07:43:29.459271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.test.is_gpu_available()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:30.024949Z","iopub.execute_input":"2023-10-30T07:43:30.025610Z","iopub.status.idle":"2023-10-30T07:43:32.391189Z","shell.execute_reply.started":"2023-10-30T07:43:30.025576Z","shell.execute_reply":"2023-10-30T07:43:32.389997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Import data**","metadata":{}},{"cell_type":"markdown","source":"**Select labels**","metadata":{}},{"cell_type":"code","source":"labels_csv = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")\nsample = pd.read_csv(\"/kaggle/input/dog-breed-identification/sample_submission.csv\")\nprint(labels_csv.head())\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:32.393161Z","iopub.execute_input":"2023-10-30T07:43:32.393957Z","iopub.status.idle":"2023-10-30T07:43:32.769675Z","shell.execute_reply.started":"2023-10-30T07:43:32.393916Z","shell.execute_reply":"2023-10-30T07:43:32.768260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Take the paths of all the photos**","metadata":{}},{"cell_type":"code","source":"filenames = [\"/kaggle/input/dog-breed-identification/train/\" + fname + \".jpg\" for fname in labels_csv[\"id\"]]\nfilenames_test_list = [\"/kaggle/input/dog-breed-identification/test/\" + fname + \".jpg\" for fname in sample[\"id\"]]\n\n#Show the first 3 paths\nfilenames[:3]","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:32.771898Z","iopub.execute_input":"2023-10-30T07:43:32.772241Z","iopub.status.idle":"2023-10-30T07:43:32.797903Z","shell.execute_reply.started":"2023-10-30T07:43:32.772210Z","shell.execute_reply":"2023-10-30T07:43:32.796712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Take all the breeds**","metadata":{}},{"cell_type":"code","source":"labels = labels_csv[\"breed\"].to_numpy()\nprint(len(labels),labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:32.801011Z","iopub.execute_input":"2023-10-30T07:43:32.801627Z","iopub.status.idle":"2023-10-30T07:43:32.808703Z","shell.execute_reply.started":"2023-10-30T07:43:32.801494Z","shell.execute_reply":"2023-10-30T07:43:32.807626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Find the total breeds**","metadata":{}},{"cell_type":"code","source":"unique_breeds = np.unique(labels)\nprint(\"There are\", len(unique_breeds), \"breeds in total\")","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:32.810263Z","iopub.execute_input":"2023-10-30T07:43:32.810784Z","iopub.status.idle":"2023-10-30T07:43:32.827781Z","shell.execute_reply.started":"2023-10-30T07:43:32.810757Z","shell.execute_reply":"2023-10-30T07:43:32.826736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Encode the labels as we want using boolean operations.**","metadata":{}},{"cell_type":"code","source":"boolean_labels = [label == unique_breeds for label in labels]\nboolean_labels = tf.where(boolean_labels, 1, 0)\nboolean_labels[:2]\nlen(boolean_labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:32.829608Z","iopub.execute_input":"2023-10-30T07:43:32.829932Z","iopub.status.idle":"2023-10-30T07:43:33.477034Z","shell.execute_reply.started":"2023-10-30T07:43:32.829898Z","shell.execute_reply":"2023-10-30T07:43:33.476028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**So we have the following**","metadata":{}},{"cell_type":"code","source":"X = filenames\ny = boolean_labels\nprint(\"The len of the whole training set is:\", len(X))\nprint(len(X)==len(y))","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:33.478644Z","iopub.execute_input":"2023-10-30T07:43:33.479419Z","iopub.status.idle":"2023-10-30T07:43:33.485116Z","shell.execute_reply.started":"2023-10-30T07:43:33.479358Z","shell.execute_reply":"2023-10-30T07:43:33.484191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Lets separate the training set and the dev set**","metadata":{}},{"cell_type":"code","source":"X_train, X_dev, y_train, y_dev = train_test_split(X,\n                                                  y.numpy(),\n                                                  test_size=0.05,\n                                                  random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:33.486546Z","iopub.execute_input":"2023-10-30T07:43:33.487102Z","iopub.status.idle":"2023-10-30T07:43:33.504735Z","shell.execute_reply.started":"2023-10-30T07:43:33.487070Z","shell.execute_reply":"2023-10-30T07:43:33.503844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The length of the training set is:\", len(X_train))\nprint(\"The length of the dev set is:\", len(X_dev))\nprint(\"The length of the test set is:\", len(filenames_test_list))","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:33.507350Z","iopub.execute_input":"2023-10-30T07:43:33.507933Z","iopub.status.idle":"2023-10-30T07:43:33.513087Z","shell.execute_reply.started":"2023-10-30T07:43:33.507902Z","shell.execute_reply":"2023-10-30T07:43:33.512205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Turn images into Tensors**","metadata":{}},{"cell_type":"markdown","source":"**Create a function for preprocessing images**","metadata":{}},{"cell_type":"code","source":"IMG_SIZE = 224\ndef process_image(image_path, img_size=IMG_SIZE):\n  # Read in an image file\n  image = tf.io.read_file(image_path)\n    # Turn the jpeg image into numerical Tensor with 3 colour channels (Red, Green, Blue)\n  image = tf.image.decode_jpeg(image, channels=3)\n   # Convert the colour channel values from 0-255 to 0-1 values\n  image = tf.image.convert_image_dtype(image, tf.float32)\n   # Resize the image to our desired value (224, 224)\n  image = tf.image.resize(image, size=[IMG_SIZE, IMG_SIZE])\n\n  return image","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:33.992174Z","iopub.execute_input":"2023-10-30T07:43:33.992583Z","iopub.status.idle":"2023-10-30T07:43:33.999230Z","shell.execute_reply.started":"2023-10-30T07:43:33.992548Z","shell.execute_reply":"2023-10-30T07:43:33.998138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Group each tensor with the desired output**","metadata":{}},{"cell_type":"code","source":"def get_image_label(image_path, label):\n  image = process_image(image_path)\n  return image,label","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:34.775117Z","iopub.execute_input":"2023-10-30T07:43:34.775530Z","iopub.status.idle":"2023-10-30T07:43:34.780775Z","shell.execute_reply.started":"2023-10-30T07:43:34.775497Z","shell.execute_reply":"2023-10-30T07:43:34.779738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_data(X, y):\n    data  = tf.data.Dataset.from_tensor_slices((tf.constant(X),\n                                                tf.constant(y)))\n    # Create (image, label) tuples (this also turns the images path into a preprocessed image)\n    data = data.map(get_image_label)\n\n    return data","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:35.080255Z","iopub.execute_input":"2023-10-30T07:43:35.080621Z","iopub.status.idle":"2023-10-30T07:43:35.086268Z","shell.execute_reply.started":"2023-10-30T07:43:35.080594Z","shell.execute_reply":"2023-10-30T07:43:35.085122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Now we have to apply this procedure to the whole dataset**","metadata":{}},{"cell_type":"code","source":"Train_data = create_data(X_train, y_train)\nDev_data = create_data(X_dev, y_dev)\n\nfilenames_test = tf.data.Dataset.from_tensor_slices(filenames_test_list)\nTest = filenames_test.map(process_image)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:37.177540Z","iopub.execute_input":"2023-10-30T07:43:37.178622Z","iopub.status.idle":"2023-10-30T07:43:37.370252Z","shell.execute_reply.started":"2023-10-30T07:43:37.178571Z","shell.execute_reply":"2023-10-30T07:43:37.369317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data analysis**","metadata":{}},{"cell_type":"markdown","source":"**Visualize some of our images**","metadata":{}},{"cell_type":"code","source":"def display_photo(image_number,data):\n    iteracion= 0\n    for element,label in data:\n        if iteracion==image_number:\n            plt.imshow(element)\n            print(np.array(unique_breeds)[label==1])\n            break\n        else:\n            iteracion+=1\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:39.584386Z","iopub.execute_input":"2023-10-30T07:43:39.584809Z","iopub.status.idle":"2023-10-30T07:43:39.590905Z","shell.execute_reply.started":"2023-10-30T07:43:39.584779Z","shell.execute_reply":"2023-10-30T07:43:39.589863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_photo(4,Train_data)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:39.763817Z","iopub.execute_input":"2023-10-30T07:43:39.764158Z","iopub.status.idle":"2023-10-30T07:43:40.142178Z","shell.execute_reply.started":"2023-10-30T07:43:39.764131Z","shell.execute_reply":"2023-10-30T07:43:40.140880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_photo(7,Dev_data)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:40.143739Z","iopub.execute_input":"2023-10-30T07:43:40.144016Z","iopub.status.idle":"2023-10-30T07:43:40.660200Z","shell.execute_reply.started":"2023-10-30T07:43:40.143990Z","shell.execute_reply":"2023-10-30T07:43:40.659051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualize how many images there are of each model**","metadata":{}},{"cell_type":"code","source":"labels_csv[\"breed\"].value_counts().plot.bar(figsize=(20, 10));","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:44.065870Z","iopub.execute_input":"2023-10-30T07:43:44.066279Z","iopub.status.idle":"2023-10-30T07:43:45.457333Z","shell.execute_reply.started":"2023-10-30T07:43:44.066250Z","shell.execute_reply":"2023-10-30T07:43:45.456305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **CNN model**\n","metadata":{}},{"cell_type":"code","source":"# Define the CNN model\nmodel = models.Sequential()\n\n# Convolutional layers\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=(2, 2)))\n\n# Flatten the feature maps\nmodel.add(layers.Flatten())\n\n# Fully connected layers\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dense(120, activation='softmax'))  # 'num_classes' is the number of output classes\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Print a summary of the model architecture\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:45.458906Z","iopub.execute_input":"2023-10-30T07:43:45.459214Z","iopub.status.idle":"2023-10-30T07:43:45.593414Z","shell.execute_reply.started":"2023-10-30T07:43:45.459186Z","shell.execute_reply":"2023-10-30T07:43:45.592569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Code for visualizng the results**","metadata":{}},{"cell_type":"code","source":"def plot_results(history, epochs):\n    acc = history.history['accuracy']\n    val_acc = history.history['val_accuracy']\n\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n\n    epochs_range = range(epochs)\n\n    plt.figure(figsize=(12, 6))\n    plt.subplot(1, 2, 1)\n    plt.plot(epochs_range, acc, label='Training Accuracy')\n    plt.plot(epochs_range, val_acc, label='Validation Accuracy')\n    plt.legend()\n    plt.title('Training and Validation Accuracy')\n\n    plt.subplot(1, 2, 2)\n    plt.plot(epochs_range, loss, label='Training Loss')\n    plt.plot(epochs_range, val_loss, label='Validation Loss')\n    plt.legend()\n    plt.title('Training and Validation Loss')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:46.658449Z","iopub.execute_input":"2023-10-30T07:43:46.658818Z","iopub.status.idle":"2023-10-30T07:43:46.666539Z","shell.execute_reply.started":"2023-10-30T07:43:46.658787Z","shell.execute_reply":"2023-10-30T07:43:46.665476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Fit the model**","metadata":{}},{"cell_type":"code","source":"train_data_batched_1 = Train_data.batch(64)\ndev_data_batched_1 = Dev_data.batch(64)\nepochs= 10","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:49.321117Z","iopub.execute_input":"2023-10-30T07:43:49.321874Z","iopub.status.idle":"2023-10-30T07:43:49.329601Z","shell.execute_reply.started":"2023-10-30T07:43:49.321838Z","shell.execute_reply":"2023-10-30T07:43:49.328537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_data_batched_1, epochs= epochs, validation_data=dev_data_batched_1)\nplot_results(history, epochs)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Deal with Overfitting**","metadata":{}},{"cell_type":"markdown","source":"**CNN with Dropout**","metadata":{}},{"cell_type":"code","source":"# Define the CNN model\nmodel = models.Sequential()\n\n# Convolutional layers\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Flatten the feature maps\nlayers.Dropout(0.5)\nmodel.add(layers.Flatten())\n\n# Fully connected layers\nmodel.add(layers.Dense(128, activation='relu'))\nlayers.Dropout(0.5)\nmodel.add(layers.Dense(120, activation='softmax'))  # 'num_classes' is the number of output classes\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Print a summary of the model architecture\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:52.788733Z","iopub.execute_input":"2023-10-30T07:43:52.789138Z","iopub.status.idle":"2023-10-30T07:43:52.867109Z","shell.execute_reply.started":"2023-10-30T07:43:52.789106Z","shell.execute_reply":"2023-10-30T07:43:52.866321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_data_batched_1, epochs= epochs, validation_data=dev_data_batched_1)\nplot_results(history, epochs)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Augmentation**","metadata":{}},{"cell_type":"code","source":"#Generate another train data with different batch size for tests\ntrain_data_batched_2 = Train_data.batch(128)\ndev_data_batched_2 = Dev_data.batch(128)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:55.826629Z","iopub.execute_input":"2023-10-30T07:43:55.827002Z","iopub.status.idle":"2023-10-30T07:43:55.833360Z","shell.execute_reply.started":"2023-10-30T07:43:55.826971Z","shell.execute_reply":"2023-10-30T07:43:55.832351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_augmentation = keras.Sequential(\n  [\n    layers.RandomFlip(\"horizontal\"),\n    layers.RandomRotation(0.1),\n    layers.RandomZoom(0.1),\n  ]\n)\naugmented_train_data = train_data_batched_2.map(lambda x, y: (data_augmentation(x, training=True), y))\naugmented_train_data = augmented_train_data.repeat(3)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:56.471682Z","iopub.execute_input":"2023-10-30T07:43:56.472535Z","iopub.status.idle":"2023-10-30T07:43:56.829125Z","shell.execute_reply.started":"2023-10-30T07:43:56.472498Z","shell.execute_reply":"2023-10-30T07:43:56.828274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the CNN model\nmodel = models.Sequential()\n\n    # Convolutional layers\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n    # Flatten the feature maps\nlayers.Dropout(0.2)\nmodel.add(layers.Flatten())\n\n    # Fully connected layers\nmodel.add(layers.Dense(128, activation='relu'))\nlayers.Dropout(0.2)\nmodel.add(layers.Dense(120, activation='softmax'))  # 'num_classes' is the number of output classes\n\n    # Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n    # Print a summary of the model architecture\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:43:57.239817Z","iopub.execute_input":"2023-10-30T07:43:57.240529Z","iopub.status.idle":"2023-10-30T07:43:57.311262Z","shell.execute_reply.started":"2023-10-30T07:43:57.240497Z","shell.execute_reply":"2023-10-30T07:43:57.310529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(augmented_train_data, epochs= 20, validation_data=dev_data_batched_2)\nplot_results(history, 20)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **MobileNet v1**","metadata":{}},{"cell_type":"markdown","source":"**MobileNet v1-like architecture from scratch in TensorFlow**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Input, Conv2D, DepthwiseConv2D, BatchNormalization, ReLU, GlobalAveragePooling2D, Dense\nfrom tensorflow.keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:00.001893Z","iopub.execute_input":"2023-10-30T07:44:00.002620Z","iopub.status.idle":"2023-10-30T07:44:00.008236Z","shell.execute_reply.started":"2023-10-30T07:44:00.002588Z","shell.execute_reply":"2023-10-30T07:44:00.007252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (224, 224, 3)\nnum_classes = 120 \n\nmodel = Sequential()\n\n# Initial Convolution Block\nmodel.add(Conv2D(32, (3, 3), strides=(2, 2), padding='same', input_shape=input_shape))\nmodel.add(BatchNormalization())\nmodel.add(ReLU())\n\n\n#Repeat this 13 times\nfor i in range(5):\n    # Depthwise Separable Convolution Block\n    model.add(DepthwiseConv2D((3, 3), padding='same'))\n    model.add(BatchNormalization())\n    model.add(ReLU())\n\n    model.add(Conv2D(64, (1, 1), padding='same'))\n    model.add(BatchNormalization())\n    model.add(ReLU())\n    \n    \n# Global Average Pooling Layer\nmodel.add(GlobalAveragePooling2D())\n\n# Fully Connected Layer\nmodel.add(Dense(1024, activation='relu'))\n\n# Output Layer\nmodel.add(Dense(num_classes, activation='softmax'))\n\n# Summary of the model architecture\n#model.summary()\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:00.640039Z","iopub.execute_input":"2023-10-30T07:44:00.640775Z","iopub.status.idle":"2023-10-30T07:44:00.988412Z","shell.execute_reply.started":"2023-10-30T07:44:00.640742Z","shell.execute_reply":"2023-10-30T07:44:00.987305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_data_batched_1, epochs= 30, validation_data=dev_data_batched_1)\nplot_results(history, 30)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Imported MobileNet v1**","metadata":{}},{"cell_type":"code","source":"# Setup input shape to the model\nINPUT_SHAPE = [None, 224, 224, 3] # batch, height, width, colour channels\n\n# Setup output shape of our model\nOUTPUT_SHAPE = len(unique_breeds)\n\n# Setup model URL from TensorFlow Hub\nMODEL_URL = \"https://tfhub.dev/google/aiy/vision/classifier/plants_V1/1\"","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:03.521046Z","iopub.execute_input":"2023-10-30T07:44:03.521738Z","iopub.status.idle":"2023-10-30T07:44:03.526767Z","shell.execute_reply.started":"2023-10-30T07:44:03.521703Z","shell.execute_reply":"2023-10-30T07:44:03.525733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(input_shape=INPUT_SHAPE, output_shape=OUTPUT_SHAPE, model_url=MODEL_URL):\n    print(\"Building model with:\", MODEL_URL)\n\n\n  # Setup the model layers\n    model = tf.keras.Sequential([\n    hub.KerasLayer(MODEL_URL), # Layer 1 (input layer)\n    tf.keras.layers.Dense(units=OUTPUT_SHAPE,\n                          activation=\"softmax\") # Layer 2 (output layer)\n  ])\n  # Compile the model\n    model.compile(\n    loss=tf.keras.losses.CategoricalCrossentropy(),\n    optimizer=tf.keras.optimizers.Adam(),\n    metrics=[\"accuracy\"]\n  )\n\n  # Build the model\n    model.build(INPUT_SHAPE)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:04.092084Z","iopub.execute_input":"2023-10-30T07:44:04.093013Z","iopub.status.idle":"2023-10-30T07:44:04.101016Z","shell.execute_reply.started":"2023-10-30T07:44:04.092955Z","shell.execute_reply":"2023-10-30T07:44:04.099805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.summary()\nNUM_EPOCHS = 30\n# Create a model\nmodel = create_model()\n# Fit the model to the data passing it the callbacks we created\nhistory=model.fit(x=train_data_batched_1, epochs=NUM_EPOCHS, validation_data=dev_data_batched_1, validation_freq=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_results(history, 30)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **MobileNet v2**","metadata":{}},{"cell_type":"markdown","source":"**MobileNet v2-like architecture from scratch in TensorFlow**","metadata":{}},{"cell_type":"code","source":"# Define the input shape (224x224x3 for MobileNetV2)\ninput_shape = (224, 224, 3)\nnum_classes = 120  \n\nmodel = Sequential()\n\n# Initial Convolution Block\nmodel.add(Conv2D(32, (3, 3), strides=(2, 2), padding='same', input_shape=input_shape))\nmodel.add(BatchNormalization())\nmodel.add(ReLU())\n\n# Inverted Residual Block 1\nfor i in range(4):\n    model.add(Conv2D(32, (1, 1), padding='same'))\n    model.add(BatchNormalization())\n    model.add(ReLU())\n\n    model.add(DepthwiseConv2D((3, 3), padding='same'))\n    model.add(BatchNormalization())\n    model.add(ReLU())\n\n    model.add(Conv2D(64, (1, 1), padding='same'))\n    model.add(BatchNormalization())\n\n# Global Average Pooling Layer\nmodel.add(GlobalAveragePooling2D())\n\n# Fully Connected Layer\nmodel.add(Dense(1280, activation='relu'))\n\n# Output Layer\nmodel.add(Dense(num_classes, activation='softmax'))\n\n# Summary of the model architecture\n#model.summary()\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nhistory = model.fit(train_data_batched_1, epochs= 30, validation_data=dev_data_batched_1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:26.027606Z","iopub.execute_input":"2023-10-30T07:44:26.028002Z","iopub.status.idle":"2023-10-30T07:44:29.439261Z","shell.execute_reply.started":"2023-10-30T07:44:26.027971Z","shell.execute_reply":"2023-10-30T07:44:29.438026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_results(history, 30)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Imported MobileNet v2**","metadata":{}},{"cell_type":"code","source":"MODEL_URL = \"https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/5\"","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:33.799153Z","iopub.execute_input":"2023-10-30T07:44:33.799806Z","iopub.status.idle":"2023-10-30T07:44:33.804021Z","shell.execute_reply.started":"2023-10-30T07:44:33.799773Z","shell.execute_reply":"2023-10-30T07:44:33.802986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\n#model.summary()\nNUM_EPOCHS = 10\n# Create a model\nmodel = create_model()\n\n# Fit the model to the data passing it the callbacks we created\nhistory = model.fit(x=train_data_batched_1,epochs=NUM_EPOCHS, validation_data=dev_data_batched_1, validation_freq=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_results(history, 10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We have large variance lest test what happen if we perform the same model with data augmentation**","metadata":{}},{"cell_type":"markdown","source":"# **Imported MobileNet v2 with data augmentation**","metadata":{}},{"cell_type":"code","source":"model = create_model()\n#model.summary()\nNUM_EPOCHS = 10\n# Create a model\nmodel = create_model()\n\n# Fit the model to the data passing it the callbacks we created\nhistory = model.fit(x=augmented_train_data,epochs=NUM_EPOCHS, validation_data=dev_data_batched_1, validation_freq=1)\nplot_results(history, 10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **ResNets**","metadata":{}},{"cell_type":"code","source":"def residual_block(x, filters, stride=1):\n    shortcut = x\n    \n    # First convolution layer\n    x = Conv2D(filters, (3, 3), strides=stride, padding='same')(x)\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    \n    # Second convolution layer\n    x = Conv2D(filters, (3, 3), padding='same')(x)\n    x = BatchNormalization()(x)\n    \n    # Shortcut connection\n    if stride != 1 or shortcut.shape[-1] != filters:\n        shortcut = Conv2D(filters, (1, 1), strides=stride, padding='same')(shortcut)\n    \n    x = Add()([x, shortcut])\n    x = ReLU()(x)\n    \n    return x","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:40.596194Z","iopub.execute_input":"2023-10-30T07:44:40.596888Z","iopub.status.idle":"2023-10-30T07:44:40.605510Z","shell.execute_reply.started":"2023-10-30T07:44:40.596853Z","shell.execute_reply":"2023-10-30T07:44:40.604573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create the ResNet model**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Input, Add\n\ninput_layer = Input(shape=input_shape)\nx = Conv2D(64, (3, 3), padding='same')(input_layer)\nx = BatchNormalization()(x)\nx = ReLU()(x)\n\n# Add residual blocks\nx = residual_block(x, 64)\nx = residual_block(x, 64)\n\nx = residual_block(x, 128, stride=2)\nx = residual_block(x, 128)\n\nx = residual_block(x, 256, stride=2)\nx = residual_block(x, 256)\n\n\n# Global Average Pooling Layer\nx = GlobalAveragePooling2D()(x)\n\n# Output Layer\noutput_layer = Dense(num_classes, activation='softmax')(x)\n\nmodel = Model(inputs=input_layer, outputs=output_layer)\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Summary of the model architecture\n# model.summary()\nhistory = model.fit(train_data_batched_1, epochs= 10, validation_data=dev_data_batched_1)\nplot_results(history, 10)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:42.066479Z","iopub.execute_input":"2023-10-30T07:44:42.066877Z","iopub.status.idle":"2023-10-30T07:44:45.501530Z","shell.execute_reply.started":"2023-10-30T07:44:42.066837Z","shell.execute_reply":"2023-10-30T07:44:45.500293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Imported ResNet**","metadata":{}},{"cell_type":"code","source":"MODEL_URL = \"https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/5\"","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:48.422409Z","iopub.execute_input":"2023-10-30T07:44:48.422815Z","iopub.status.idle":"2023-10-30T07:44:48.428068Z","shell.execute_reply.started":"2023-10-30T07:44:48.422782Z","shell.execute_reply":"2023-10-30T07:44:48.426795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_EPOCHS = 10\n# Create a model\nmodel = create_model()\nhistory = model.fit(x=train_data_batched_1,epochs=NUM_EPOCHS, validation_data=dev_data_batched_1, validation_freq=1)\nplot_results(history, 10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Imported ResNet with data augmentation**","metadata":{}},{"cell_type":"code","source":"NUM_EPOCHS = 10\n# Create a model\nmodel = create_model()\nhistory = model.fit(x=augmented_train_data,epochs=NUM_EPOCHS, validation_data=dev_data_batched_1, validation_freq=1)\nplot_results(history, 10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Transfer Learning with ResNet**\n","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\n\nnum_classes = 120\nresnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\nmy_new_model = Sequential()\nmy_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))\nmy_new_model.add(Dense(num_classes, activation='softmax'))\n\n# Say not to train first layer (ResNet) model. It is already trained\nmy_new_model.layers[0].trainable = False\n\n#my_new_model.summary()\nmy_new_model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nhistory = my_new_model.fit(train_data_batched_1,epochs=30, validation_data=dev_data_batched_1,validation_steps=1)\nplot_results(history, 30)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:58.041328Z","iopub.execute_input":"2023-10-30T07:44:58.041881Z","iopub.status.idle":"2023-10-30T07:45:06.577082Z","shell.execute_reply.started":"2023-10-30T07:44:58.041845Z","shell.execute_reply":"2023-10-30T07:45:06.574878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Transfer Learning with Inception Network**\n","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import Xception\n\n\nmy_new_model = Sequential()\nmy_new_model.add(Xception(include_top=False, pooling='avg',weights=\"imagenet\"))\nmy_new_model.add(Dense(num_classes, activation='softmax'))\n\n# Say not to train first layer (ResNet) model. It is already trained\nmy_new_model.layers[0].trainable = False\n\n#my_new_model.summary()\nmy_new_model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nhistory =my_new_model.fit(train_data_batched_1,epochs=10, validation_data=dev_data_batched_1,validation_steps=1)\nplot_results(history, 10)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:45:14.726229Z","iopub.execute_input":"2023-10-30T07:45:14.727101Z","iopub.status.idle":"2023-10-30T07:52:16.436342Z","shell.execute_reply.started":"2023-10-30T07:45:14.727065Z","shell.execute_reply":"2023-10-30T07:52:16.435209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This seems to be our best model**","metadata":{}},{"cell_type":"markdown","source":"#  **Transfer Learning with Inception Network and data augmentation**","metadata":{}},{"cell_type":"code","source":"history = my_new_model.fit(augmented_train_data,epochs=10, validation_data=dev_data_batched_1,validation_steps=1)\nplot_results(history, 10)\nhistory","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_new_model.layers","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Predictions**","metadata":{}},{"cell_type":"code","source":"test_data_batched_1 = Test.batch(128)\n\npredictions = my_new_model.predict(test_data_batched_1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:52:23.998627Z","iopub.execute_input":"2023-10-30T07:52:23.999489Z","iopub.status.idle":"2023-10-30T07:53:20.350919Z","shell.execute_reply.started":"2023-10-30T07:52:23.999455Z","shell.execute_reply":"2023-10-30T07:53:20.349964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict = pd.DataFrame(predictions, columns = unique_breeds)\n\ntest_predict.insert(0, 'id',  [os.path.splitext(os.path.basename(path))[0] for path in filenames_test_list])\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:55:24.932023Z","iopub.execute_input":"2023-10-30T07:55:24.932784Z","iopub.status.idle":"2023-10-30T07:55:24.972082Z","shell.execute_reply.started":"2023-10-30T07:55:24.932749Z","shell.execute_reply":"2023-10-30T07:55:24.971201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:55:27.020149Z","iopub.execute_input":"2023-10-30T07:55:27.020885Z","iopub.status.idle":"2023-10-30T07:55:27.054212Z","shell.execute_reply.started":"2023-10-30T07:55:27.020855Z","shell.execute_reply":"2023-10-30T07:55:27.053196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict.to_csv('submission1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:55:30.856924Z","iopub.execute_input":"2023-10-30T07:55:30.857617Z","iopub.status.idle":"2023-10-30T07:55:32.687276Z","shell.execute_reply.started":"2023-10-30T07:55:30.857578Z","shell.execute_reply":"2023-10-30T07:55:32.686429Z"},"trusted":true},"execution_count":null,"outputs":[]}]}