{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","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"},{"sourceId":7281698,"sourceType":"datasetVersion","datasetId":4222114}],"dockerImageVersionId":30498,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-25T06:43:10.812980Z","iopub.execute_input":"2023-12-25T06:43:10.813710Z","iopub.status.idle":"2023-12-25T06:43:10.819200Z","shell.execute_reply.started":"2023-12-25T06:43:10.813665Z","shell.execute_reply":"2023-12-25T06:43:10.818000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport os\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:28:24.060913Z","iopub.execute_input":"2023-12-26T05:28:24.061615Z","iopub.status.idle":"2023-12-26T05:28:33.149721Z","shell.execute_reply.started":"2023-12-26T05:28:24.061587Z","shell.execute_reply":"2023-12-26T05:28:33.148966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_data=pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:28:36.458025Z","iopub.execute_input":"2023-12-26T05:28:36.458758Z","iopub.status.idle":"2023-12-26T05:28:36.487568Z","shell.execute_reply.started":"2023-12-26T05:28:36.458729Z","shell.execute_reply":"2023-12-26T05:28:36.486859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:28:38.883584Z","iopub.execute_input":"2023-12-26T05:28:38.883943Z","iopub.status.idle":"2023-12-26T05:28:38.900033Z","shell.execute_reply.started":"2023-12-26T05:28:38.883917Z","shell.execute_reply":"2023-12-26T05:28:38.898910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_data.describe()","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:28:44.134599Z","iopub.execute_input":"2023-12-26T05:28:44.135456Z","iopub.status.idle":"2023-12-26T05:28:44.164422Z","shell.execute_reply.started":"2023-12-26T05:28:44.135422Z","shell.execute_reply":"2023-12-26T05:28:44.163607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#lets view an image\nfrom IPython.display import Image,display\nprint(f\"--------------{label_data['breed'][0]}------------------\")\nImage(\"/kaggle/input/dog-breed-identification/train/000bec180eb18c7604dcecc8fe0dba07.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:28:49.076006Z","iopub.execute_input":"2023-12-26T05:28:49.076393Z","iopub.status.idle":"2023-12-26T05:28:49.108169Z","shell.execute_reply.started":"2023-12-26T05:28:49.076361Z","shell.execute_reply":"2023-12-26T05:28:49.107309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport matplotlib.pyplot as plt\nfrom matplotlib.ticker import MaxNLocator\nfrom IPython.display import display\n\n# image path\nimage_path = '/kaggle/input/dog-breed-identification/train/'\n\n#Randomly select six indices\nrandom_indices = random.sample(range(len(label_data)), 6)\n\n#subplot with 2 rows and 3 columns\nfig, axes = plt.subplots(2, 3, figsize=(15, 10))\naxes = axes.flatten()\n\n# Display the randomly selected breeds and images\nfor i, index in enumerate(random_indices):\n    breed = label_data['breed'][index]\n    file_path = image_path + label_data['id'][index] + '.jpg'\n\n    # Load and display the image\n    img = plt.imread(file_path)\n    \n    axes[i].imshow(img)\n    axes[i].set_title(f\"--------------{breed}------------------\")\n    axes[i].axis('off')\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:28:52.432313Z","iopub.execute_input":"2023-12-26T05:28:52.432686Z","iopub.status.idle":"2023-12-26T05:28:54.071848Z","shell.execute_reply.started":"2023-12-26T05:28:52.432656Z","shell.execute_reply":"2023-12-26T05:28:54.070828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Getting Images and their lables","metadata":{}},{"cell_type":"code","source":"label_class=label_data[\"breed\"].to_numpy()\nlabel_class[:120]\n","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:29:03.584670Z","iopub.execute_input":"2023-12-26T05:29:03.585615Z","iopub.status.idle":"2023-12-26T05:29:03.592394Z","shell.execute_reply.started":"2023-12-26T05:29:03.585580Z","shell.execute_reply":"2023-12-26T05:29:03.591476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('label_class_save.npy', label_class)\n\n# Load the array from the binary file\nloaded_label_class = np.load('label_class_save.npy',allow_pickle=True)\n\n# Verify if the order is preserved\nprint(np.array_equal(label_class, loaded_label_class))","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:29:10.607087Z","iopub.execute_input":"2023-12-26T05:29:10.607472Z","iopub.status.idle":"2023-12-26T05:29:10.616507Z","shell.execute_reply.started":"2023-12-26T05:29:10.607439Z","shell.execute_reply":"2023-12-26T05:29:10.615304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_label_class","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:29:16.035976Z","iopub.execute_input":"2023-12-26T05:29:16.037116Z","iopub.status.idle":"2023-12-26T05:29:16.043108Z","shell.execute_reply.started":"2023-12-26T05:29:16.037076Z","shell.execute_reply":"2023-12-26T05:29:16.042075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#find the unique label values\nunique_breeds=np.unique(label_class)\nlen(unique_breeds)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:29:18.417321Z","iopub.execute_input":"2023-12-26T05:29:18.418030Z","iopub.status.idle":"2023-12-26T05:29:18.432803Z","shell.execute_reply.started":"2023-12-26T05:29:18.417999Z","shell.execute_reply":"2023-12-26T05:29:18.431903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\n\n# Set your file paths\ndata_dir = '/kaggle/input/dog-breed-identification/train'\nlabels_file = '/kaggle/input/dog-breed-identification/labels.csv'\n\n# Load labels from CSV\nlabels_df = pd.read_csv(labels_file)\n\n# Create a dictionary to map breed names to numerical labels\nclass_mapping = {breed: i for i, breed in enumerate(labels_df['breed'].unique())}\n\n# Add a 'label' column to the labels dataframe\nlabels_df['label'] = labels_df['breed'].map(class_mapping)\n\n# Load images and labels\nX = []\ny = []\n\nfor index, row in labels_df.iterrows():\n    img_path = os.path.join(data_dir, f\"{row['id']}.jpg\")\n    img = load_img(img_path, target_size=(224, 224)) \n    img_array = img_to_array(img)\n    X.append(img_array)\n    y.append(row['label'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert lists to numpy arrays\nX = np.array(X)\ny = np.array(y)\n\n# Convert labels to one-hot encoding\ny_one_hot = tf.keras.utils.to_categorical(y, num_classes=len(class_mapping))\n\n# Split the data into train and test sets\nX_train, X_test, y_train, y_test = train_test_split(X, y_one_hot, test_size=0.2, random_state=42)\n\n# Create a tf.data.Dataset for efficient batch processing\nbatch_size = 32\ntrain_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train)).batch(batch_size)\ntest_dataset = tf.data.Dataset.from_tensor_slices((X_test, y_test)).batch(batch_size)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:45:26.887730Z","iopub.execute_input":"2023-12-25T06:45:26.888012Z","iopub.status.idle":"2023-12-25T06:45:26.896699Z","shell.execute_reply.started":"2023-12-25T06:45:26.887987Z","shell.execute_reply":"2023-12-25T06:45:26.895462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# Lets build Sequencial as well as Functinal API Feature Extractor Model","metadata":{}},{"cell_type":"markdown","source":"`Base model->GlobalAveragePooling Layer(doing feature extract)->output layer`","metadata":{}},{"cell_type":"markdown","source":"# Lets create functional api","metadata":{}},{"cell_type":"code","source":"#1. create a base model with tf.keras.applications\nimport tensorflow as tf\nbase_model=tf.keras.applications.EfficientNetB7(include_top=False)\n                                               #include top=false because it train on imagenet 1000 class image\n                                               # but we are working with 10 classes\n\n#2. Freeze the base model(so the underlying pre_trained pattern arent update)\nbase_model.trainable=False\n\n#3. Crete inpute into our model\ninput=tf.keras.layers.Input(shape=(224,224,3),name=\"inpute_layer\")\n\n#4. If using like resNet50V2 we have to normalize inputs\n#(you dont have to do for efficientnet because efficientnet have rescaling build in )\n#x=tf.keras.layers.experimental.preprocessing.Rescaling(1./255)(input)\n\n#5. Pass the inputs to the base model\nx=base_model(input)\n#6. Average Pool the outputs of the base model(aggregate all the most important information,reduce number of computations)\nx=tf.keras.layers.GlobalAveragePooling2D(name=\"global_avrage_pooling\")(x)\n\n#7. Create the inpute with the activation layer\noutputs=tf.keras.layers.Dense(120,activation=\"softmax\",name=\"Output_layer\")(x)\n\n#8. Combine the inpute with output in mode\nmodel_1=tf.keras.Model(input,outputs)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:45:26.897927Z","iopub.execute_input":"2023-12-25T06:45:26.898249Z","iopub.status.idle":"2023-12-25T06:45:38.045870Z","shell.execute_reply.started":"2023-12-25T06:45:26.898224Z","shell.execute_reply":"2023-12-25T06:45:38.045017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#complile the model\nmodel_1.compile(loss=tf.keras.losses.categorical_crossentropy,\n                optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),\n                metrics=['accuracy'])\nmodel_1.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:45:38.047210Z","iopub.execute_input":"2023-12-25T06:45:38.047595Z","iopub.status.idle":"2023-12-25T06:45:38.134947Z","shell.execute_reply.started":"2023-12-25T06:45:38.047562Z","shell.execute_reply":"2023-12-25T06:45:38.134083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Callbacks\n  *  Tensorbord callback\n  *  checkpoint callbacks it will be very helpfull during model fine tuning","metadata":{}},{"cell_type":"code","source":"checkpoint_path=\"Dog_vision/checkpoint.ckpt\"\n#create ModelCheckPoint callbacks that save the model weight only\ncheckpoint_callback=tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,\n                                                       save_weights_only=True,\n                                                       save_best_only=True,\n                                                       save_freq=\"epoch\",\n                                                       verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:45:38.136034Z","iopub.execute_input":"2023-12-25T06:45:38.136294Z","iopub.status.idle":"2023-12-25T06:45:38.141074Z","shell.execute_reply.started":"2023-12-25T06:45:38.136271Z","shell.execute_reply":"2023-12-25T06:45:38.140271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import datetime\ndef create_tensorboard_callback(dir_name, experiment_name):\n  \"\"\"\n  Creates a TensorBoard callback instand to store log files.\n\n  Stores log files with the filepath:\n    \"dir_name/experiment_name/current_datetime/\"\n\n  Args:\n    dir_name: target directory to store TensorBoard log files\n    experiment_name: name of experiment directory (e.g. efficientnet_model_1)\n  \"\"\"\n  log_dir = dir_name + \"/\" + experiment_name + \"/\" + datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n  tensorboard_callback = tf.keras.callbacks.TensorBoard(\n      log_dir=log_dir\n  )\n  print(f\"Saving TensorBoard log files to: {log_dir}\")\n  return tensorboard_callback\n","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:45:38.142337Z","iopub.execute_input":"2023-12-25T06:45:38.142644Z","iopub.status.idle":"2023-12-25T06:45:38.153992Z","shell.execute_reply.started":"2023-12-25T06:45:38.142620Z","shell.execute_reply":"2023-12-25T06:45:38.153243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_epoch=5;\nhistory_model_1_efficientB7=model_1.fit(train_dataset,\n           steps_per_epoch=len(train_dataset),\n           epochs=5,\n           batch_size=32,\n           validation_data=test_dataset,\n           validation_steps=len(test_dataset),\n          callbacks=[checkpoint_callback])","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:45:38.155037Z","iopub.execute_input":"2023-12-25T06:45:38.155273Z","iopub.status.idle":"2023-12-25T06:55:43.640649Z","shell.execute_reply.started":"2023-12-25T06:45:38.155252Z","shell.execute_reply":"2023-12-25T06:55:43.639833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1_efficientb7_score=model_1.evaluate(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T07:12:35.813125Z","iopub.execute_input":"2023-12-25T07:12:35.813880Z","iopub.status.idle":"2023-12-25T07:12:57.830460Z","shell.execute_reply.started":"2023-12-25T07:12:35.813848Z","shell.execute_reply":"2023-12-25T07:12:57.829556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lets fine tune our model_1 on augmenated data","metadata":{}},{"cell_type":"code","source":"model_1.load_weights(checkpoint_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:56:05.739736Z","iopub.execute_input":"2023-12-25T06:56:05.740367Z","iopub.status.idle":"2023-12-25T06:56:17.617100Z","shell.execute_reply.started":"2023-12-25T06:56:05.740329Z","shell.execute_reply":"2023-12-25T06:56:17.616196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Are these layers trainable?\nfor layer in model_1.layers:\n  print(layer, layer.trainable)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:56:17.618320Z","iopub.execute_input":"2023-12-25T06:56:17.618696Z","iopub.status.idle":"2023-12-25T06:56:17.624327Z","shell.execute_reply.started":"2023-12-25T06:56:17.618655Z","shell.execute_reply":"2023-12-25T06:56:17.623262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Access the base_model layers of model_1\nmodel_1_base_model = model_1.layers[1]\nmodel_1_base_model.name","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:56:17.625564Z","iopub.execute_input":"2023-12-25T06:56:17.625860Z","iopub.status.idle":"2023-12-25T06:56:17.635459Z","shell.execute_reply.started":"2023-12-25T06:56:17.625831Z","shell.execute_reply":"2023-12-25T06:56:17.634552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make all the layers in model_1_base_model trainable\nmodel_1_base_model.trainable = True\n\n# Freeze all layers except for the last 10\nfor layer in model_1_base_model.layers[:-10]:\n  layer.trainable = False\n\n# Recompile (we have to recompile our models every time we make a change)\nmodel_1.compile(loss=\"categorical_crossentropy\",\n                optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), # when fine-tuning you typically want to lower the learning rate by 10x*\n                metrics=[\"accuracy\"])\nprint(len(model_1.trainable_variables))","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:56:17.636611Z","iopub.execute_input":"2023-12-25T06:56:17.637187Z","iopub.status.idle":"2023-12-25T06:56:17.718640Z","shell.execute_reply.started":"2023-12-25T06:56:17.637152Z","shell.execute_reply":"2023-12-25T06:56:17.717833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fine-tune for another 10 epochs\nfine_tune_epochs = initial_epoch + 5\n\n# Refit the model (same as model_1 except with more trainable layers)\nhistory_model_1_data_aug_efficientB7_fineTune = model_1.fit(train_dataset,\n                                                steps_per_epoch=len(train_dataset),\n                                                epochs=fine_tune_epochs,\n                                                batch_size=32,\n                                                validation_data=test_dataset,\n                                                validation_steps=len(test_dataset),\n                                                initial_epoch=history_model_1_efficientB7.epoch[-1]  # start from the previous last epoch\n                                                           )","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:56:17.719746Z","iopub.execute_input":"2023-12-25T06:56:17.720071Z","iopub.status.idle":"2023-12-25T07:08:09.947180Z","shell.execute_reply.started":"2023-12-25T06:56:17.720038Z","shell.execute_reply":"2023-12-25T07:08:09.946383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.evaluate(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T07:08:09.948550Z","iopub.execute_input":"2023-12-25T07:08:09.948840Z","iopub.status.idle":"2023-12-25T07:08:32.107544Z","shell.execute_reply.started":"2023-12-25T07:08:09.948815Z","shell.execute_reply":"2023-12-25T07:08:32.106672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tyu","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### mixed_precision\nspeedup the training upto 3x \n* one change we have to do is that in output layer we have to add `dtype='float32'`","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import mixed_precision\nmixed_precision.set_global_policy('mixed_float16')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T07:13:23.437863Z","iopub.execute_input":"2023-12-25T07:13:23.438527Z","iopub.status.idle":"2023-12-25T07:13:23.443374Z","shell.execute_reply.started":"2023-12-25T07:13:23.438492Z","shell.execute_reply":"2023-12-25T07:13:23.442352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lets try even Big Model","metadata":{}},{"cell_type":"code","source":"base_model=tf.keras.applications.efficientnet_v2.EfficientNetV2L(include_top=False)\n                                               #include top=false because it train on imagenet 1000 class image\n                                               # but we are working with 10 classes\n\n#2. Freeze the base model(so the underlying pre_trained pattern arent update)\nbase_model.trainable=False\n\n#3. Crete inpute into our model\ninput=tf.keras.layers.Input(shape=(224,224,3),name=\"inpute_layer\")\n\n#4. If using like resNet50V2 we have to normalize inputs\n#(you dont have to do for efficientnet because efficientnet have rescaling build in )\n#x=tf.keras.layers.experimental.preprocessing.Rescaling(1./255)(input)\n\n#5. Pass the inputs to the base model\nx=base_model(input)\n#6. Average Pool the outputs of the base model(aggregate all the most important information,reduce number of computations)\nx=tf.keras.layers.GlobalAveragePooling2D(name=\"global_avrage_pooling\")(x)\n\n#7. Create the inpute with the activation layer\noutputs=tf.keras.layers.Dense(120,activation=\"softmax\",dtype='float32',name=\"Output_layer\")(x)\n\n#8. Combine the inpute with output in mode\nmodel_2=tf.keras.Model(input,outputs)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-25T07:13:27.624400Z","iopub.execute_input":"2023-12-25T07:13:27.624782Z","iopub.status.idle":"2023-12-25T07:13:41.701021Z","shell.execute_reply.started":"2023-12-25T07:13:27.624750Z","shell.execute_reply":"2023-12-25T07:13:41.700134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#complile the model\nmodel_2.compile(loss=tf.keras.losses.categorical_crossentropy,\n                optimizer=tf.keras.optimizers.Adam(learning_rate=0.00001),\n                metrics=['accuracy'])\nmodel_2.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-25T07:31:48.360702Z","iopub.execute_input":"2023-12-25T07:31:48.361079Z","iopub.status.idle":"2023-12-25T07:31:48.457805Z","shell.execute_reply.started":"2023-12-25T07:31:48.361048Z","shell.execute_reply":"2023-12-25T07:31:48.456826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_epoch=5;\nhistory_model_2_data_aug_efficientL2=model_2.fit(train_dataset,\n           steps_per_epoch=len(train_dataset),\n           epochs=initial_epoch,\n           batch_size=32,\n           validation_data=test_dataset,\n           validation_steps=len(test_dataset),\n           callbacks=[checkpoint_callback])","metadata":{"execution":{"iopub.status.busy":"2023-12-25T07:31:51.979916Z","iopub.execute_input":"2023-12-25T07:31:51.980610Z","iopub.status.idle":"2023-12-25T07:37:14.442502Z","shell.execute_reply.started":"2023-12-25T07:31:51.980576Z","shell.execute_reply":"2023-12-25T07:37:14.441702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_2.load_weights(checkpoint_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-25T07:26:36.424444Z","iopub.execute_input":"2023-12-25T07:26:36.425349Z","iopub.status.idle":"2023-12-25T07:26:55.694392Z","shell.execute_reply.started":"2023-12-25T07:26:36.425314Z","shell.execute_reply":"2023-12-25T07:26:55.693297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_2.evaluate(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T07:38:28.328978Z","iopub.execute_input":"2023-12-25T07:38:28.329894Z","iopub.status.idle":"2023-12-25T07:38:38.929163Z","shell.execute_reply.started":"2023-12-25T07:38:28.329858Z","shell.execute_reply":"2023-12-25T07:38:38.928360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### we can see model 2 with efficientnetV2L outperform all the model omg","metadata":{}},{"cell_type":"code","source":"label_class=label_data[\"breed\"].to_numpy()\nlabel_class","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:33:20.244350Z","iopub.execute_input":"2023-12-26T05:33:20.245191Z","iopub.status.idle":"2023-12-26T05:33:20.251607Z","shell.execute_reply.started":"2023-12-26T05:33:20.245138Z","shell.execute_reply":"2023-12-26T05:33:20.250677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nnp.save('label_class.npy', label_class)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-25T07:45:40.576845Z","iopub.execute_input":"2023-12-25T07:45:40.577229Z","iopub.status.idle":"2023-12-25T07:45:40.583347Z","shell.execute_reply.started":"2023-12-25T07:45:40.577199Z","shell.execute_reply":"2023-12-25T07:45:40.582214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_mapping","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:33:24.060930Z","iopub.execute_input":"2023-12-26T05:33:24.061583Z","iopub.status.idle":"2023-12-26T05:33:24.071088Z","shell.execute_reply.started":"2023-12-26T05:33:24.061550Z","shell.execute_reply":"2023-12-26T05:33:24.070199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breed_names = list(class_mapping.keys())\nbreed_names[:10]==label_class[:10]","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:46:19.583724Z","iopub.execute_input":"2023-12-26T05:46:19.584479Z","iopub.status.idle":"2023-12-26T05:46:19.591225Z","shell.execute_reply.started":"2023-12-26T05:46:19.584443Z","shell.execute_reply":"2023-12-26T05:46:19.590199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('label_class.npy', breed_names)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:50:00.919942Z","iopub.execute_input":"2023-12-26T05:50:00.920317Z","iopub.status.idle":"2023-12-26T05:50:00.925975Z","shell.execute_reply.started":"2023-12-26T05:50:00.920287Z","shell.execute_reply":"2023-12-26T05:50:00.924803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom IPython.display import Image,display\ndef make_prediction(image_path):\n    image_path = image_path\n    img = load_img(image_path, target_size=(224, 224))\n    img_array = img_to_array(img)\n    img_array = tf.expand_dims(img_array, 0)\n\n    # Make predictions\n    predictions = model_2.predict(img_array)\n\n   # Decode predictions\n    predicted_class = tf.argmax(predictions, axis=1).numpy()[0]\n    print(f\"Prediction Probability: {np.max(predictions)}\")\n    #lets view an image\n    print(f\"-----------------------{breed_names[predicted_class]}---------------------\")\n    display(Image(image_path))\n","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:47:14.299764Z","iopub.execute_input":"2023-12-26T05:47:14.300571Z","iopub.status.idle":"2023-12-26T05:47:14.307083Z","shell.execute_reply.started":"2023-12-26T05:47:14.300540Z","shell.execute_reply":"2023-12-26T05:47:14.306153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path=\"/kaggle/input/dog-breed-identification/train/000bec180eb18c7604dcecc8fe0dba07.jpg\"\nmake_prediction(image_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:47:17.894279Z","iopub.execute_input":"2023-12-26T05:47:17.894995Z","iopub.status.idle":"2023-12-26T05:47:18.053843Z","shell.execute_reply.started":"2023-12-26T05:47:17.894961Z","shell.execute_reply":"2023-12-26T05:47:18.052901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path=\"/kaggle/input/dog-breed-identification/train/001513dfcb2ffafc82cccf4d8bbaba97.jpg\"\nmake_prediction(image_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-26T05:47:28.348789Z","iopub.execute_input":"2023-12-26T05:47:28.349164Z","iopub.status.idle":"2023-12-26T05:47:28.478046Z","shell.execute_reply.started":"2023-12-26T05:47:28.349133Z","shell.execute_reply":"2023-12-26T05:47:28.477079Z"},"trusted":true},"execution_count":null,"outputs":[]}]}