{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.chdir('../')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from dataclasses import dataclass\n", "from pathlib import Path\n", "\n", "\n", "@dataclass(frozen=True)\n", "class PrepareBaseModelConfig:\n", " root_dir: Path\n", " base_model_path: Path\n", " updated_base_model_path: Path\n", " params_image_size: list\n", " params_learning_rate: float\n", " params_include_top: bool\n", " params_weights: str\n", " params_classes: int" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from cnnClassifier.utils.common import read_yaml, create_directories\n", "from cnnClassifier.constant import *\n", "# Configuration\n", "class ConfigurationManager:\n", " def __init__(\n", " self,\n", " config_filepath = CONFIG_FILE_PATH,\n", " params_filepath = PARAMS_FILE_PATH):\n", "\n", " self.config = read_yaml(config_filepath)\n", " self.params = read_yaml(params_filepath)\n", "\n", " create_directories([self.config.atifacts_root])\n", "\n", " \n", "\n", " def get_prepare_base_model_config(self) -> PrepareBaseModelConfig:\n", " config = self.config.prepare_base_model\n", " \n", " create_directories([config.root_dir])\n", "\n", " prepare_base_model_config = PrepareBaseModelConfig(\n", " root_dir=Path(config.root_dir),\n", " base_model_path=Path(config.base_model_path),\n", " updated_base_model_path=Path(config.updated_base_model_path),\n", " params_image_size=self.params.IMAGE_SIZE,\n", " params_learning_rate=self.params.LEARNING_RATE,\n", " params_include_top=self.params.INCLUDE_TOP,\n", " params_weights=self.params.WEIGHTS,\n", " params_classes=self.params.CLASSES\n", " )\n", "\n", " return prepare_base_model_config" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2024-07-30 03:19:34,344: WARNING: module_wrapper: From c:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n", "]\n" ] } ], "source": [ "import os\n", "import urllib.request as request\n", "import tensorflow as tf\n", "\n", "class PrepareBaseModel:\n", " def __init__(self, config: PrepareBaseModelConfig):\n", " self.config = config\n", "\n", " \n", " def get_base_model(self):\n", " self.model = tf.keras.applications.vgg16.VGG16(\n", " input_shape=self.config.params_image_size,\n", " weights=self.config.params_weights,\n", " include_top=self.config.params_include_top\n", " )\n", "\n", " self.save_model(path=self.config.base_model_path, model=self.model)\n", "\n", " \n", "\n", " @staticmethod\n", " def _prepare_full_model(model, classes, freeze_all, freeze_till, learning_rate):\n", " if freeze_all:\n", " for layer in model.layers:\n", " model.trainable = False\n", " elif (freeze_till is not None) and (freeze_till > 0):\n", " for layer in model.layers[:-freeze_till]:\n", " model.trainable = False\n", "\n", " flatten_in = tf.keras.layers.Flatten()(model.output)\n", " prediction = tf.keras.layers.Dense(\n", " units=classes,\n", " activation=\"softmax\"\n", " )(flatten_in)\n", "\n", " full_model = tf.keras.models.Model(\n", " inputs=model.input,\n", " outputs=prediction\n", " )\n", "\n", " full_model.compile(\n", " optimizer=tf.keras.optimizers.SGD(learning_rate=learning_rate),\n", " loss=tf.keras.losses.CategoricalCrossentropy(),\n", " metrics=[\"accuracy\"]\n", " )\n", "\n", " full_model.summary()\n", " return full_model\n", " \n", " \n", " def update_base_model(self):\n", " self.full_model = self._prepare_full_model(\n", " model=self.model,\n", " classes=self.config.params_classes,\n", " freeze_all=True,\n", " freeze_till=None,\n", " learning_rate=self.config.params_learning_rate\n", " )\n", "\n", " self.save_model(path=self.config.updated_base_model_path, model=self.full_model)\n", "\n", " \n", " \n", " @staticmethod\n", " def save_model(path: Path, model: tf.keras.Model):\n", " model.save(path)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2024-07-30 03:19:36,293: INFO: common: yaml file: config\\config.yaml loaded successfully]\n", "[2024-07-30 03:19:36,296: INFO: common: yaml file: params.yaml loaded successfully]\n", "[2024-07-30 03:19:36,298: INFO: common: Created directory at: artifacts]\n", "[2024-07-30 03:19:36,299: INFO: common: Created directory at: artifacts/prepare_base_model]\n", "[2024-07-30 03:19:36,531: WARNING: module_wrapper: From c:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\backend.py:1398: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.\n", "]\n", "[2024-07-30 03:19:36,660: WARNING: module_wrapper: From c:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\pooling\\max_pooling2d.py:161: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n", "]\n", "[2024-07-30 03:19:37,174: WARNING: saving_utils: Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.]\n", "Model: \"model\"\n", "_________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "=================================================================\n", " input_1 (InputLayer) [(None, 224, 224, 3)] 0 \n", " \n", " block1_conv1 (Conv2D) (None, 224, 224, 64) 1792 \n", " \n", " block1_conv2 (Conv2D) (None, 224, 224, 64) 36928 \n", " \n", " block1_pool (MaxPooling2D) (None, 112, 112, 64) 0 \n", " \n", " block2_conv1 (Conv2D) (None, 112, 112, 128) 73856 \n", " \n", " block2_conv2 (Conv2D) (None, 112, 112, 128) 147584 \n", " \n", " block2_pool (MaxPooling2D) (None, 56, 56, 128) 0 \n", " \n", " block3_conv1 (Conv2D) (None, 56, 56, 256) 295168 \n", " \n", " block3_conv2 (Conv2D) (None, 56, 56, 256) 590080 \n", " \n", " block3_conv3 (Conv2D) (None, 56, 56, 256) 590080 \n", " \n", " block3_pool (MaxPooling2D) (None, 28, 28, 256) 0 \n", " \n", " block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160 \n", " \n", " block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808 \n", " \n", " block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808 \n", " \n", " block4_pool (MaxPooling2D) (None, 14, 14, 512) 0 \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\engine\\training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.\n", " saving_api.save_model(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n", " block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808 \n", " \n", " block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808 \n", " \n", " block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808 \n", " \n", " block5_pool (MaxPooling2D) (None, 7, 7, 512) 0 \n", " \n", " flatten (Flatten) (None, 25088) 0 \n", " \n", " dense (Dense) (None, 2) 50178 \n", " \n", "=================================================================\n", "Total params: 14764866 (56.32 MB)\n", "Trainable params: 50178 (196.01 KB)\n", "Non-trainable params: 14714688 (56.13 MB)\n", "_________________________________________________________________\n" ] } ], "source": [ "try:\n", " config = ConfigurationManager()\n", " prepare_base_model_config = config.get_prepare_base_model_config()\n", " prepare_base_model = PrepareBaseModel(config=prepare_base_model_config)\n", " prepare_base_model.get_base_model()\n", " prepare_base_model.update_base_model()\n", "except Exception as e:\n", " raise e" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 2 }