{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":30776,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport os\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import models\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport json\nimport plotly.express as px\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras import layers","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-29T08:08:47.382013Z","iopub.execute_input":"2024-09-29T08:08:47.382513Z","iopub.status.idle":"2024-09-29T08:09:01.092404Z","shell.execute_reply.started":"2024-09-29T08:08:47.382471Z","shell.execute_reply":"2024-09-29T08:09:01.091613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the labelling \njson_files = \"/kaggle/input/cassava-leaf-disease-classification/label_num_to_disease_map.json\"\n\nif os.path.exists(json_files): # check if the file exists\n    with open(json_files , \"r\") as json_file: # Open the file if exists\n        data = json.load(json_file) # load json file \n        labelling_data = pd.DataFrame(list(data.items()) , columns = [\"labels\" , \"Diseases\"]) # making a dataframe for more good looking\n        print(labelling_data) # Printing the labelling dataframe","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.094455Z","iopub.execute_input":"2024-09-29T08:09:01.095641Z","iopub.status.idle":"2024-09-29T08:09:01.117951Z","shell.execute_reply.started":"2024-09-29T08:09:01.095591Z","shell.execute_reply":"2024-09-29T08:09:01.117059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = pd.read_csv(\"/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.11916Z","iopub.execute_input":"2024-09-29T08:09:01.119532Z","iopub.status.idle":"2024-09-29T08:09:01.135507Z","shell.execute_reply.started":"2024-09-29T08:09:01.119497Z","shell.execute_reply":"2024-09-29T08:09:01.134789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.137356Z","iopub.execute_input":"2024-09-29T08:09:01.137633Z","iopub.status.idle":"2024-09-29T08:09:01.147659Z","shell.execute_reply.started":"2024-09-29T08:09:01.137601Z","shell.execute_reply":"2024-09-29T08:09:01.146775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/cassava-leaf-disease-classification/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.148857Z","iopub.execute_input":"2024-09-29T08:09:01.149211Z","iopub.status.idle":"2024-09-29T08:09:01.176595Z","shell.execute_reply.started":"2024-09-29T08:09:01.149176Z","shell.execute_reply":"2024-09-29T08:09:01.175749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.177578Z","iopub.execute_input":"2024-09-29T08:09:01.177878Z","iopub.status.idle":"2024-09-29T08:09:01.190717Z","shell.execute_reply.started":"2024-09-29T08:09:01.177821Z","shell.execute_reply":"2024-09-29T08:09:01.189883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Updating the df with their real classes\n\n\nclasses = ({\n    0:\"Cassava Bacterial Blight (CBB)\",\n    1:\"Cassava Brown Streak Disease (CBSD)\",\n    2:\"Cassava Green Mottle (CGM)\",\n    3:\"Cassava Mosaic Disease (CMD)\",\n    4 :\"Healthy\"})\n\ndf[\"classes\"] = df[\"label\"].map(classes)\ndf","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.191811Z","iopub.execute_input":"2024-09-29T08:09:01.192153Z","iopub.status.idle":"2024-09-29T08:09:01.208717Z","shell.execute_reply.started":"2024-09-29T08:09:01.192118Z","shell.execute_reply":"2024-09-29T08:09:01.207693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating a new column that have a full path of the image in df dataframe\n\ndef image_paths(x):\n    root_dir = (\"/kaggle/input/cassava-leaf-disease-classification/train_images\")\n    path = os.path.join(root_dir , x)\n    return path\n\ndf[\"image_path\"] = df[\"image_id\"].map(image_paths)\ndf","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.20974Z","iopub.execute_input":"2024-09-29T08:09:01.210089Z","iopub.status.idle":"2024-09-29T08:09:01.268482Z","shell.execute_reply.started":"2024-09-29T08:09:01.210041Z","shell.execute_reply":"2024-09-29T08:09:01.26764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA ( EXPLORATARY DATA ANALAYSIS )","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes_counts = df[\"classes\"].value_counts()\nclasses_counts.plot(kind = \"bar\")\nplt.xlabel(\"Dieseas Name\")\nplt.ylabel(\"value_counts\")","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.269457Z","iopub.execute_input":"2024-09-29T08:09:01.269734Z","iopub.status.idle":"2024-09-29T08:09:01.575256Z","shell.execute_reply.started":"2024-09-29T08:09:01.269702Z","shell.execute_reply":"2024-09-29T08:09:01.574454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes_counts = df[\"classes\"].value_counts().reset_index()\nclasses_counts","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.578457Z","iopub.execute_input":"2024-09-29T08:09:01.578759Z","iopub.status.idle":"2024-09-29T08:09:01.591061Z","shell.execute_reply.started":"2024-09-29T08:09:01.578726Z","shell.execute_reply":"2024-09-29T08:09:01.590101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes_counts = df[\"classes\"].value_counts().reset_index()\nfig = px.bar(classes_counts , x = \"classes\" , y = \"count\")\nfig","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:01.592198Z","iopub.execute_input":"2024-09-29T08:09:01.592546Z","iopub.status.idle":"2024-09-29T08:09:02.986296Z","shell.execute_reply.started":"2024-09-29T08:09:01.592508Z","shell.execute_reply":"2024-09-29T08:09:02.985394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" ###### 1st result of EDA Data is  - Imbalanced Data","metadata":{}},{"cell_type":"code","source":"# Image Dimension\n\nimport cv2\nimage = \"/kaggle/input/cassava-leaf-disease-classification/train_images/1000015157.jpg\"\nimage = cv2.imread(image)\nimage_shape = image.shape\nimage_shape","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:02.987437Z","iopub.execute_input":"2024-09-29T08:09:02.98774Z","iopub.status.idle":"2024-09-29T08:09:03.189571Z","shell.execute_reply.started":"2024-09-29T08:09:02.987706Z","shell.execute_reply":"2024-09-29T08:09:03.188672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:03.19075Z","iopub.execute_input":"2024-09-29T08:09:03.191172Z","iopub.status.idle":"2024-09-29T08:09:03.202394Z","shell.execute_reply.started":"2024-09-29T08:09:03.191128Z","shell.execute_reply":"2024-09-29T08:09:03.201364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['label'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:03.203586Z","iopub.execute_input":"2024-09-29T08:09:03.203905Z","iopub.status.idle":"2024-09-29T08:09:03.212469Z","shell.execute_reply.started":"2024-09-29T08:09:03.203857Z","shell.execute_reply":"2024-09-29T08:09:03.211532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get unique classes\nunique_classes = df['label'].unique()\n\n# Set number of images per class\nimages_per_class = 5\n\n# Initialize a subplot index\nsubplot_index = 1\n\n# Plot 5 images from each class\nfor label in unique_classes:\n    # Filter DataFrame for the current class\n    class_df = df[df['label'] == label]\n    \n    # Sample 5 images or all if less than 5\n    samples = class_df.sample(n=min(images_per_class, len(class_df)), random_state=1)  # Use random_state for reproducibility\n    \n    # Plot the images\n    for _, row in samples.iterrows():\n        plt.subplot(len(unique_classes), images_per_class, subplot_index)  # Correct subplot index\n        img = cv2.imread(row['image_path'])  # Read the image\n        plt.imshow(img)\n        plt.title(row['classes'] , fontsize = 5.6)\n        plt.axis('off')\n        subplot_index += 1  # Increment the subplot index\n\nplt.tight_layout()  # Adjust layout for better spacing\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:03.213624Z","iopub.execute_input":"2024-09-29T08:09:03.214295Z","iopub.status.idle":"2024-09-29T08:09:06.712411Z","shell.execute_reply.started":"2024-09-29T08:09:03.214241Z","shell.execute_reply":"2024-09-29T08:09:06.711532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Loading","metadata":{}},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:06.713478Z","iopub.execute_input":"2024-09-29T08:09:06.713779Z","iopub.status.idle":"2024-09-29T08:09:06.725548Z","shell.execute_reply.started":"2024-09-29T08:09:06.713744Z","shell.execute_reply":"2024-09-29T08:09:06.724706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Test Split into train and val\ntrain_dataset , val_dataset = train_test_split(df , test_size = 0.3 , random_state = 42 , shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:06.72687Z","iopub.execute_input":"2024-09-29T08:09:06.727454Z","iopub.status.idle":"2024-09-29T08:09:06.925315Z","shell.execute_reply.started":"2024-09-29T08:09:06.727419Z","shell.execute_reply":"2024-09-29T08:09:06.924466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Loading From Dataframe\n\ndef image_preprocess(x):\n    image = tf.io.read_file(x)\n    image = tf.io.decode_jpeg(image , channels = 3)\n    image = tf.image.resize(image , [224 , 224])\n    image = image/255\n    return image\n\ndef tensorflow_dataset(df , batch_size):\n    dataset = tf.data.Dataset.from_tensor_slices((df[\"image_path\"].values , df[\"label\"])) # First make the tensorflow dataset\n    dataset = dataset.map(lambda x,y : (image_preprocess(x),y)) # Apply the image Preprocessing on the dataset\n    dataset = dataset.batch(batch_size)\n    dataset = dataset.prefetch(buffer_size = tf.data.AUTOTUNE)\n    return dataset\n\ntrain_dataset = tensorflow_dataset(train_dataset , 32)\nval_dataset = tensorflow_dataset(val_dataset , 32)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:06.926714Z","iopub.execute_input":"2024-09-29T08:09:06.927257Z","iopub.status.idle":"2024-09-29T08:09:07.777816Z","shell.execute_reply.started":"2024-09-29T08:09:06.927208Z","shell.execute_reply":"2024-09-29T08:09:07.777065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:07.778868Z","iopub.execute_input":"2024-09-29T08:09:07.779156Z","iopub.status.idle":"2024-09-29T08:09:07.78517Z","shell.execute_reply.started":"2024-09-29T08:09:07.779123Z","shell.execute_reply":"2024-09-29T08:09:07.784143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dataset","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:07.786295Z","iopub.execute_input":"2024-09-29T08:09:07.786592Z","iopub.status.idle":"2024-09-29T08:09:07.79839Z","shell.execute_reply.started":"2024-09-29T08:09:07.78656Z","shell.execute_reply":"2024-09-29T08:09:07.797567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Augmentation \n\ndata_augmentation = tf.keras.Sequential([\n    layers.RandomFlip(\"horizontal_and_vertical\"),\n    layers.RandomZoom(0.2) ,\n    layers.RandomRotation(0.5), \n    layers.RandomContrast(0.3)\n])","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:07.799591Z","iopub.execute_input":"2024-09-29T08:09:07.79992Z","iopub.status.idle":"2024-09-29T08:09:07.823086Z","shell.execute_reply.started":"2024-09-29T08:09:07.799886Z","shell.execute_reply":"2024-09-29T08:09:07.822173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_dataset.map(lambda x,y : (data_augmentation(x),y))","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:09:07.824359Z","iopub.execute_input":"2024-09-29T08:09:07.824642Z","iopub.status.idle":"2024-09-29T08:09:08.040933Z","shell.execute_reply.started":"2024-09-29T08:09:07.82461Z","shell.execute_reply":"2024-09-29T08:09:08.040219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Buidling","metadata":{}},{"cell_type":"markdown","source":"## 1. Inception Net","metadata":{}},{"cell_type":"code","source":"def inception_module(x , filter_1 , filter_3_reduce , filter_3 ,filter_5 , filter_5_reduce , MaxPool_filter):\n\n  # 1st convolutional Branch\n  conv_1  = layers.Conv2D(filter_1 , (1,1) ,  activation = \"relu\" , padding = \"same\")(x)\n\n  # 2nd Convolutional branch\n  conv_2 = layers.Conv2D(filter_3_reduce , (1,1) , strides = (1,1) , activation = \"relu\" , padding = \"same\")(x)\n  conv_2 = layers.Conv2D(filter_3 , (3,3) , padding = \"same\" , activation = \"relu\")(conv_2)\n\n  # 3rd Convolutional Branch\n  conv_3 = layers.Conv2D(filter_5_reduce , (1,1) , strides = (1,1) ,padding = \"same\" ,  activation = \"relu\" )(x)\n  conv_3 = layers.Conv2D(filter_5 , (5,5) , strides = (1,1) , padding = \"same\" , activation = \"relu\")(conv_3)\n\n  # 4th Branch\n  max_pool = layers.MaxPooling2D((3,3) , strides = (1,1) , padding = \"same\")(x)\n  conv_4  = layers.Conv2D(MaxPool_filter , (1,1) , strides = (1,1) , activation = \"relu\" , padding = \"same\")(max_pool)\n\n  # Concatenate\n\n  output_layer = layers.concatenate([conv_1 , conv_2 , conv_3 , conv_4] , axis = -1)\n\n  return output_layer\n\ndef auxiliary_classifier(x , num_classes):\n  aux = layers.MaxPooling2D((5,5) , strides = (3,3))(x)\n  aux = layers.Conv2D(64 , (1,1) , padding = \"same\" , activation = \"relu\")(aux)\n  aux = layers.Flatten()(aux)\n  aux = layers.Dense(1024 , activation  = \"relu\")(aux)\n  aux = layers.Dense(num_classes , activation = \"softmax\")(aux)\n\n  return aux\n\n\ndef inception_net(input_shape , num_classes):\n  input_layer  = tf.keras.layers.Input(shape = input_shape)\n\n  # Intialize the the buttom layer\n  x = layers.Conv2D(16 , (7,7) , strides = (2,2) , activation = \"relu\")(input_layer)\n  x = layers.MaxPooling2D((3,3) , strides = (2,2))(x)\n  x = layers.BatchNormalization()(x)\n  x = layers.Conv2D(32 , (1,1)  , strides= (1,1) , activation = \"relu\")(x)\n  x = layers.Conv2D(64 , (3,3) , strides = (1,1) , activation = \"relu\")(x)\n  x = layers.BatchNormalization()(x)\n  x = layers.MaxPooling2D((3,3) , strides = (2,2))(x)\n\n  # 1st part of the architecture\n\n  x = inception_module(x , 32 , 64 , 32 , 24 , 64 , 64)\n  x = inception_module(x , 180 , 220 , 132 , 128 , 64 , 64)\n  x = inception_module(x , 189 , 320 , 112 , 108 , 108 , 64)\n\n  # first auxiliary classification layer\n\n  aux1 = auxiliary_classifier(x , num_classes)\n\n\n  x = inception_module(x , 190 , 120 , 124 , 102 , 198 , 64)\n  # 2nd part of the Architecture\n\n  x = inception_module(x , 120 , 100 , 38 , 48 , 64 , 64)\n  x = inception_module(x , 112 , 98 , 28 , 56 , 64 , 64)\n\n  # second Auxiliary Classifier\n\n  aux2 = auxiliary_classifier(x , num_classes)\n\n  x = inception_module(x , 120 , 100 , 28 , 38 , 48 , 64)\n\n  # 3rd Part of the Architecture\n\n  x = layers.MaxPooling2D((3,3) , strides = (2,2))(x)\n  x = inception_module(x , 180 , 78 , 26 , 56 , 64 , 64)\n  x = inception_module(x , 98 , 88 , 36 , 56 , 64 , 64)\n\n  x = layers.GlobalAveragePooling2D()(x)\n\n  x = layers.Flatten()(x)\n  output_layer = layers.Dense(num_classes , activation = \"softmax\")(x)\n\n  model1 = models.Model(inputs = input_layer , outputs = [aux1 , aux2 , output_layer])\n\n  return model1\n\n\ninput_shape = (224 , 224 ,3)\ngoogle_net = inception_net(input_shape , num_classes = 5)\n\ngoogle_net.compile(\n    optimizer=\"Adam\",\n    loss=[\"sparse_categorical_crossentropy\", \"sparse_categorical_crossentropy\", \"sparse_categorical_crossentropy\"],\n    loss_weights=[1.0, 0.3, 0.3],  # For example, you can weigh the auxiliary classifiers differently\n    metrics=[\"accuracy\" , \"accuracy\" , \"accuracy\"]\n)\ngoogle_net.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:15:38.291308Z","iopub.execute_input":"2024-09-29T10:15:38.291698Z","iopub.status.idle":"2024-09-29T10:15:38.898986Z","shell.execute_reply.started":"2024-09-29T10:15:38.29166Z","shell.execute_reply":"2024-09-29T10:15:38.897986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = google_net.fit(train_dataset,\n               validation_data = val_dataset,\n               epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:15:45.544279Z","iopub.execute_input":"2024-09-29T10:15:45.545122Z","iopub.status.idle":"2024-09-29T10:52:27.149965Z","shell.execute_reply.started":"2024-09-29T10:15:45.545078Z","shell.execute_reply":"2024-09-29T10:52:27.148968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"] , label = \"Training Loss\")\nplt.plot(history.history[\"val_loss\"] , label = \"Validation Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:52:27.152251Z","iopub.execute_input":"2024-09-29T10:52:27.152585Z","iopub.status.idle":"2024-09-29T10:52:27.391278Z","shell.execute_reply.started":"2024-09-29T10:52:27.152548Z","shell.execute_reply":"2024-09-29T10:52:27.390415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:53:36.468347Z","iopub.execute_input":"2024-09-29T10:53:36.469065Z","iopub.status.idle":"2024-09-29T10:53:36.475587Z","shell.execute_reply.started":"2024-09-29T10:53:36.469022Z","shell.execute_reply":"2024-09-29T10:53:36.474641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"dense_8_accuracy\"] , label = \"Training Accuracy\")\nplt.plot(history.history[\"val_dense_8_accuracy\"] , label = \"Validation Accuracy\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:54:27.058558Z","iopub.execute_input":"2024-09-29T10:54:27.059166Z","iopub.status.idle":"2024-09-29T10:54:27.299896Z","shell.execute_reply.started":"2024-09-29T10:54:27.05912Z","shell.execute_reply":"2024-09-29T10:54:27.298986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import InceptionV3\n","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:14:32.555004Z","iopub.execute_input":"2024-09-29T09:14:32.555351Z","iopub.status.idle":"2024-09-29T09:14:32.56148Z","shell.execute_reply.started":"2024-09-29T09:14:32.555312Z","shell.execute_reply":"2024-09-29T09:14:32.560632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = InceptionV3(include_top=False, \n                         weights='imagenet', \n                         classes=5, \n                         classifier_activation='softmax')","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:14:32.811653Z","iopub.execute_input":"2024-09-29T09:14:32.811999Z","iopub.status.idle":"2024-09-29T09:15:41.050783Z","shell.execute_reply.started":"2024-09-29T09:14:32.811961Z","shell.execute_reply":"2024-09-29T09:15:41.049789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:15:41.052985Z","iopub.execute_input":"2024-09-29T09:15:41.053295Z","iopub.status.idle":"2024-09-29T09:15:41.457584Z","shell.execute_reply.started":"2024-09-29T09:15:41.053259Z","shell.execute_reply":"2024-09-29T09:15:41.456677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers:\n    layer.trainbale = False","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:15:41.458692Z","iopub.execute_input":"2024-09-29T09:15:41.458999Z","iopub.status.idle":"2024-09-29T09:15:41.468511Z","shell.execute_reply.started":"2024-09-29T09:15:41.458964Z","shell.execute_reply":"2024-09-29T09:15:41.467535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" x = base_model.output\n\nx = layers.GlobalAveragePooling2D()(x)\n\nx = layers.Dense(1024 , activation = \"relu\")(x)\n\noutput = layers.Dense(5 , activation = \"softmax\")(x)\n\nmodel1 = tf.keras.models.Model(inputs = base_model.input , outputs = output)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:16:04.349941Z","iopub.execute_input":"2024-09-29T09:16:04.350919Z","iopub.status.idle":"2024-09-29T09:16:04.415689Z","shell.execute_reply.started":"2024-09-29T09:16:04.350873Z","shell.execute_reply":"2024-09-29T09:16:04.41487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.compile(loss = \"sparse_categorical_crossentropy\" , optimizer = \"adam\" , metrics = [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:16:05.653442Z","iopub.execute_input":"2024-09-29T09:16:05.65386Z","iopub.status.idle":"2024-09-29T09:16:05.668397Z","shell.execute_reply.started":"2024-09-29T09:16:05.653795Z","shell.execute_reply":"2024-09-29T09:16:05.666765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart = time.time()\nhistory_2 = model1.fit(train_dataset , \n         validation_data = val_dataset , \n         epochs = 10)\nend = time.time()\nprint(f\"total_time{start - end}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:16:17.679449Z","iopub.execute_input":"2024-09-29T09:16:17.679845Z","iopub.status.idle":"2024-09-29T10:01:43.076886Z","shell.execute_reply.started":"2024-09-29T09:16:17.679788Z","shell.execute_reply":"2024-09-29T10:01:43.075969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_2.history","metadata":{"execution":{"iopub.status.busy":"2024-09-29T11:56:24.341943Z","iopub.execute_input":"2024-09-29T11:56:24.342578Z","iopub.status.idle":"2024-09-29T11:56:24.349267Z","shell.execute_reply.started":"2024-09-29T11:56:24.342534Z","shell.execute_reply":"2024-09-29T11:56:24.348374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\nbase_model = ResNet50( weights  = \"imagenet\" , include_top = False , input_shape = (224 ,224 , 3))\n","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:54:43.578553Z","iopub.execute_input":"2024-09-29T10:54:43.578943Z","iopub.status.idle":"2024-09-29T10:54:45.587874Z","shell.execute_reply.started":"2024-09-29T10:54:43.578902Z","shell.execute_reply":"2024-09-29T10:54:45.587053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Freezing the layers\nfor layer in base_model.layers:\n  layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:54:45.589791Z","iopub.execute_input":"2024-09-29T10:54:45.590462Z","iopub.status.idle":"2024-09-29T10:54:45.599197Z","shell.execute_reply.started":"2024-09-29T10:54:45.590413Z","shell.execute_reply":"2024-09-29T10:54:45.598143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:54:52.012845Z","iopub.execute_input":"2024-09-29T10:54:52.013217Z","iopub.status.idle":"2024-09-29T10:54:52.24295Z","shell.execute_reply.started":"2024-09-29T10:54:52.013178Z","shell.execute_reply":"2024-09-29T10:54:52.242054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Dense , Flatten , Dropout\nx = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)\n\n# Add a fully connected layer with the number of classes\nx = Dense(256 , activation = \"relu\")(x)\nx = Dropout(0.5)(x)\n\n# Finally ouput_layers with softmax activation for classification\noutputs = Dense(5, activation = \"softmax\")(x)\n\n# Build the ,model\nmodel1 = tf.keras.Model(inputs = base_model.input , outputs = outputs)\nmodel1.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:55:59.314655Z","iopub.execute_input":"2024-09-29T10:55:59.31531Z","iopub.status.idle":"2024-09-29T10:55:59.591128Z","shell.execute_reply.started":"2024-09-29T10:55:59.31526Z","shell.execute_reply":"2024-09-29T10:55:59.590215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.compile(loss = \"sparse_categorical_crossentropy\" , metrics = [\"Accuracy\"] , optimizer = \"Adam\")","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:56:25.052024Z","iopub.execute_input":"2024-09-29T10:56:25.052723Z","iopub.status.idle":"2024-09-29T10:56:25.062068Z","shell.execute_reply.started":"2024-09-29T10:56:25.052679Z","shell.execute_reply":"2024-09-29T10:56:25.061155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.fit(train_dataset ,\n           validation_data =  val_dataset ,\n           epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:56:25.318018Z","iopub.execute_input":"2024-09-29T10:56:25.318319Z","iopub.status.idle":"2024-09-29T11:34:42.13398Z","shell.execute_reply.started":"2024-09-29T10:56:25.318284Z","shell.execute_reply":"2024-09-29T11:34:42.133054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\n\n# Data\nepochs = np.arange(1, 11)  # 10 epochs\naccuracy = [0.6524003744125366, 0.7152300477027893, 0.7358616590499878, 0.7520197629928589,\n            0.7760566473007202, 0.7868064641952515, 0.7959538102149963, 0.7866728901863098,\n            0.7974227070808411, 0.8086399435997009]\nloss = [0.9454360604286194, 0.7762295603752136, 0.728477954864502, 0.6802324056625366,\n        0.618713915348053, 0.5931102633476257, 0.5761855244636536, 0.5988919734954834,\n        0.5628066658973694, 0.5399556159973145]\n\nval_accuracy = [0.4786604344844818, 0.736760139465332, 0.6462616920471191, 0.6095015406608582,\n                0.6246106028556824, 0.7160435914993286, 0.15856698155403137, 0.6493769288063049,\n                0.7534267902374268, 0.6207165122032166]\nval_loss = [1.5976072549819946, 1.147392749786377, 1.1563212871551514, 1.0719043016433716,\n            1.0053821802139282, 0.8006401062011719, 1.9899176359176636, 424.15179443359375,\n            0.7461416721343994, 1.0945676565170288]\n\n# Create 3D plot\nfig = plt.figure(figsize=(10, 7))\nax = fig.add_subplot(111, projection='3d')\n\n# Plot training loss and accuracy\nax.plot3D(epochs, loss, accuracy, label='Training Loss vs Accuracy', color='blue', marker='o')\n\n# Plot validation loss and accuracy\nax.plot3D(epochs, val_loss, val_accuracy, label='Validation Loss vs Accuracy', color='green', marker='x')\n\n# Set axis labels\nax.set_xlabel('Epochs')\nax.set_ylabel('Loss')\nax.set_zlabel('Accuracy')\n\n# Title and legend\nax.set_title('3D Plot of Loss and Accuracy Over Epochs (Training and Validation)')\nax.legend()\n\n# Show the plot\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-29T11:57:34.675536Z","iopub.execute_input":"2024-09-29T11:57:34.675935Z","iopub.status.idle":"2024-09-29T11:57:34.977229Z","shell.execute_reply.started":"2024-09-29T11:57:34.675894Z","shell.execute_reply":"2024-09-29T11:57:34.97632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.graph_objs as go\nimport numpy as np\n\n# Data\nepochs = np.arange(1, 11)  # 10 epochs\naccuracy = [0.6524003744125366, 0.7152300477027893, 0.7358616590499878, 0.7520197629928589,\n            0.7760566473007202, 0.7868064641952515, 0.7959538102149963, 0.7866728901863098,\n            0.7974227070808411, 0.8086399435997009]\nloss = [0.9454360604286194, 0.7762295603752136, 0.728477954864502, 0.6802324056625366,\n        0.618713915348053, 0.5931102633476257, 0.5761855244636536, 0.5988919734954834,\n        0.5628066658973694, 0.5399556159973145]\n\nval_accuracy = [0.4786604344844818, 0.736760139465332, 0.6462616920471191, 0.6095015406608582,\n                0.6246106028556824, 0.7160435914993286, 0.15856698155403137, 0.6493769288063049,\n                0.7534267902374268, 0.6207165122032166]\nval_loss = [1.5976072549819946, 1.147392749786377, 1.1563212871551514, 1.0719043016433716,\n            1.0053821802139282, 0.8006401062011719, 1.9899176359176636, 424.15179443359375,\n            0.7461416721343994, 1.0945676565170288]\n\n# Create traces for training data\ntrace_train = go.Scatter3d(\n    x=epochs, y=loss, z=accuracy,\n    mode='markers+lines',\n    name='Training Loss vs Accuracy',\n    marker=dict(size=5, color='blue'),\n    line=dict(color='blue')\n)\n\n# Create traces for validation data\ntrace_val = go.Scatter3d(\n    x=epochs, y=val_loss, z=val_accuracy,\n    mode='markers+lines',\n    name='Validation Loss vs Accuracy',\n    marker=dict(size=5, color='green'),\n    line=dict(color='green')\n)\n\n# Layout for the plot\nlayout = go.Layout(\n    title='3D Plot of Loss and Accuracy Over Epochs (Training and Validation)',\n    scene=dict(\n        xaxis_title='Epochs',\n        yaxis_title='Loss',\n        zaxis_title='Accuracy'\n    ),\n    legend=dict(x=0.7, y=0.1)\n)\n\n# Combine both traces into a figure\nfig = go.Figure(data=[trace_train, trace_val], layout=layout)\n\n# Show the plot\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-29T11:58:45.844166Z","iopub.execute_input":"2024-09-29T11:58:45.844822Z","iopub.status.idle":"2024-09-29T11:58:45.955042Z","shell.execute_reply.started":"2024-09-29T11:58:45.844779Z","shell.execute_reply":"2024-09-29T11:58:45.954134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.graph_objs as go\nimport numpy as np\n\n# Data\nepochs = np.arange(1, 11)  # 10 epochs\naccuracy = [0.6524003744125366, 0.7152300477027893, 0.7358616590499878, 0.7520197629928589,\n            0.7760566473007202, 0.7868064641952515, 0.7959538102149963, 0.7866728901863098,\n            0.7974227070808411, 0.8086399435997009]\nloss = [0.9454360604286194, 0.7762295603752136, 0.728477954864502, 0.6802324056625366,\n        0.618713915348053, 0.5931102633476257, 0.5761855244636536, 0.5988919734954834,\n        0.5628066658973694, 0.5399556159973145]\n\nval_accuracy = [0.4786604344844818, 0.736760139465332, 0.6462616920471191, 0.6095015406608582,\n                0.6246106028556824, 0.7160435914993286, 0.15856698155403137, 0.6493769288063049,\n                0.7534267902374268, 0.6207165122032166]\nval_loss = [1.5976072549819946, 1.147392749786377, 1.1563212871551514, 1.0719043016433716,\n            1.0053821802139282, 0.8006401062011719, 1.9899176359176636, 424.15179443359375,\n            0.7461416721343994, 1.0945676565170288]\n\n# Convert data to 2D arrays for surface plotting\n# We assume you want to plot the training loss/accuracy as one sheet and validation as another\nepochs_2d, accuracy_2d = np.meshgrid(epochs, [1, 2])  # 1 for training, 2 for validation\nloss_2d = np.array([loss, val_loss])  # Combine loss\naccuracy_2d_values = np.array([accuracy, val_accuracy])  # Combine accuracy\n\n# Surface plot for Loss vs Accuracy\nsurface_train = go.Surface(\n    x=epochs_2d, y=loss_2d, z=accuracy_2d_values,\n    colorscale='Viridis',\n    name='Training and Validation Loss vs Accuracy',\n)\n\n# Layout for the plot\nlayout = go.Layout(\n    title='3D Surface Plot of Loss and Accuracy Over Epochs',\n    scene=dict(\n        xaxis_title='Epochs',\n        yaxis_title='Loss',\n        zaxis_title='Accuracy'\n    ),\n    autosize=True\n)\n\n# Combine into a figure\nfig = go.Figure(data=[surface_train], layout=layout)\n\n# Show the plot\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-29T12:00:41.653643Z","iopub.execute_input":"2024-09-29T12:00:41.65438Z","iopub.status.idle":"2024-09-29T12:00:41.68602Z","shell.execute_reply.started":"2024-09-29T12:00:41.654339Z","shell.execute_reply":"2024-09-29T12:00:41.685105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.graph_objs as go\nimport numpy as np\n\n# Data\nepochs = np.arange(1, 11)  # 10 epochs\naccuracy = [0.6524003744125366, 0.7152300477027893, 0.7358616590499878, 0.7520197629928589,\n            0.7760566473007202, 0.7868064641952515, 0.7959538102149963, 0.7866728901863098,\n            0.7974227070808411, 0.8086399435997009]\nloss = [0.9454360604286194, 0.7762295603752136, 0.728477954864502, 0.6802324056625366,\n        0.618713915348053, 0.5931102633476257, 0.5761855244636536, 0.5988919734954834,\n        0.5628066658973694, 0.5399556159973145]\n\nval_accuracy = [0.4786604344844818, 0.736760139465332, 0.6462616920471191, 0.6095015406608582,\n                0.6246106028556824, 0.7160435914993286, 0.15856698155403137, 0.6493769288063049,\n                0.7534267902374268, 0.6207165122032166]\nval_loss = [1.5976072549819946, 1.147392749786377, 1.1563212871551514, 1.0719043016433716,\n            1.0053821802139282, 0.8006401062011719, 1.9899176359176636, 424.15179443359375,\n            0.7461416721343994, 1.0945676565170288]\n\n# Combine data into a grid for surface plotting\naccuracy_2d, epochs_2d = np.meshgrid(\n    np.array(accuracy + val_accuracy),  # Combine training and validation accuracy\n    epochs\n)\n\nloss_2d = np.array([loss + val_loss])  # Combine training and validation loss into a 2D array\n\n# Create surface plot for Loss vs Accuracy\nsurface = go.Surface(\n    x=accuracy_2d,  # Accuracy on the X-axis\n    y=loss_2d,      # Loss on the Y-axis\n    z=epochs_2d,    # Epochs on the Z-axis\n    colorscale='Viridis',\n    name='Loss vs Accuracy over Epochs',\n)\n\n# Layout for the plot\nlayout = go.Layout(\n    title='3D Surface Plot of Loss and Accuracy Over Epochs',\n    scene=dict(\n        xaxis_title='Accuracy',\n        yaxis_title='Loss',\n        zaxis_title='Epochs'\n    ),\n    autosize=True\n)\n\n# Combine into a figure\nfig = go.Figure(data=[surface], layout=layout)\n\n# Show the plot\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-29T12:02:29.604555Z","iopub.execute_input":"2024-09-29T12:02:29.60528Z","iopub.status.idle":"2024-09-29T12:02:29.624691Z","shell.execute_reply.started":"2024-09-29T12:02:29.605236Z","shell.execute_reply":"2024-09-29T12:02:29.623743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}