{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "29206888", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'f:\\\\Projects\\\\End-to-End-Chest-Cancer-Classification-using-MLflow-and-DVC\\\\research'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "%pwd" ] }, { "cell_type": "code", "execution_count": 2, "id": "7dce8d4e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'f:\\\\Projects\\\\End-to-End-Chest-Cancer-Classification-using-MLflow-and-DVC'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.chdir(\"../\")\n", "%pwd" ] }, { "cell_type": "code", "execution_count": 4, "id": "c4d0c484", "metadata": {}, "outputs": [], "source": [ "from dataclasses import dataclass\n", "from pathlib import Path\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": 5, "id": "26921811", "metadata": {}, "outputs": [], "source": [ "from cnnClassifier.constants import *\n", "from cnnClassifier.utils.common import read_yaml, create_directories\n", "\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.artifacts_root])\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": 6, "id": "0442bc6f", "metadata": {}, "outputs": [], "source": [ "import os\n", "import urllib.request as request\n", "from zipfile import ZipFile\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": 7, "id": "b21b58b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2025-08-20 01:44:50,956: INFO: common: yaml file: config\\config.yaml loaded successfully]\n", "[2025-08-20 01:44:50,982: INFO: common: yaml file: params.yaml loaded successfully]\n", "[2025-08-20 01:44:50,984: INFO: common: created directory at: artifacts]\n", "[2025-08-20 01:44:50,986: INFO: common: created directory at: artifacts/prepare_base_model]\n", "Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5\n", "58889256/58889256 [==============================] - 15s 0us/step\n", "[2025-08-20 01:45:09,603: 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", " \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: 14,764,866\n", "Trainable params: 50,178\n", "Non-trainable params: 14,714,688\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" ] } ], "metadata": { "kernelspec": { "display_name": "cnn_env", "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.3" } }, "nbformat": 4, "nbformat_minor": 5 }