{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.chdir('../')\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d:\\\\MLOps-Project\\\\Kidney-disease-classification-mlops'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%pwd" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from dataclasses import dataclass\n", "from pathlib import Path\n", "\n", "@dataclass(frozen=True)\n", "class TrainingConfig:\n", " root_dir : Path\n", " training_model_path : Path\n", " updata_base_model_path : Path\n", " training_data: Path\n", " params_epochs : int\n", " params_is_augmentation : bool\n", " params_batch_size : int\n", " params_image_size : list\n", " " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from cnnClassifier.utils.common import read_yaml, create_directories\n", "from cnnClassifier.constant import *\n", "\n", "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", " def get_training_config(self) -> TrainingConfig:\n", " training = self.config.training\n", " prepare_base_model =self.config.prepare_base_model\n", " params = self.params\n", " training_data = os.path.join(self.config.data_ingestion.unzip_dir, 'kidney-ct-scan-image') \n", " \n", " create_directories([\n", " Path(training.root_dir)\n", " ])\n", " \n", " training_config = TrainingConfig(\n", " root_dir= Path(training.root_dir),\n", " training_model_path=Path(training.trained_model_path),\n", " updata_base_model_path=Path(prepare_base_model.updated_base_model_path),\n", " training_data = Path(training_data),\n", " params_epochs = params.EPOCHS, \n", " params_batch_size= params.BATCH_SIZE,\n", " params_is_augmentation= params.AUGMENTATION,\n", " params_image_size= params.IMAGE_SIZE\n", " )\n", " \n", " return training_config\n", " " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "class Training:\n", " def __init__(self, confg : TrainingConfig):\n", " self.config = confg\n", " \n", " def get_base_model(self):\n", " self.model = tf.keras.models.load_model(\n", " self.config.updata_base_model_path\n", " )\n", " \n", " \n", " def train_vaid_generator(self):\n", " datagenerator_kwargs = dict(\n", " rescale = 1 / 255,\n", " validation_split = 0.20\n", " )\n", " \n", " dataflow_kwargs = dict(\n", " target_size = self.config.params_image_size[:-1],\n", " batch_size = self.config.params_batch_size,\n", " interpolation = 'bilinear'\n", " )\n", " valid_datagernerator = tf.keras.preprocessing.image.ImageDataGenerator(\n", " **datagenerator_kwargs\n", " )\n", " \n", " self.valid_generator = valid_datagernerator.flow_from_directory(\n", " directory = self.config.training_data,\n", " subset = 'validation',\n", " shuffle = True,\n", " **dataflow_kwargs\n", " )\n", " \n", " if self.config.params_is_augmentation:\n", " train_datagenerator = tf.keras.preprocessing.image.ImageDataGenerator(\n", " \n", " \n", " rotation_range = 40,\n", " horizontal_flip = True,\n", " width_shift_range = 0.2,\n", " height_shift_range = 0.2,\n", " shear_range = 0.2,\n", " zoom_range = 0.2,\n", " **datagenerator_kwargs\n", " )\n", " \n", " \n", " else:\n", " train_datagenerator = valid_datagernerator\n", " self.train_generator = train_datagenerator.flow_from_directory(\n", " directory = self.config.training_data,\n", " subset = 'training',\n", " shuffle = True,\n", " **dataflow_kwargs\n", " )\n", " \n", " @staticmethod\n", " def save_model(path: Path, model: tf.keras.Model):\n", " model.save(path)\n", " \n", " def train(self):\n", " self.steps_per_epchs = self.train_generator.samples // self.train_generator.batch_size\n", " self.validation_steps = self.valid_generator.samples // self.valid_generator.batch_size\n", " \n", " self.model.fit(\n", " self.train_generator,\n", " epochs = self.config.params_epochs,\n", " steps_per_epoch = self.steps_per_epchs,\n", " validation_steps = self.validation_steps,\n", " validation_data = self.valid_generator\n", " )\n", " \n", " self.save_model(\n", " path = self.config.training_data,\n", " model = self.model\n", " )\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2024-07-31 20:16:53,704: INFO: common: yaml file: config\\config.yaml loaded successfully]\n", "[2024-07-31 20:16:53,707: INFO: common: yaml file: params.yaml loaded successfully]\n", "[2024-07-31 20:16:53,709: INFO: common: Created directory at: artifacts]\n", "[2024-07-31 20:16:53,711: INFO: common: Created directory at: artifacts\\training]\n", "Found 93 images belonging to 2 classes.\n", "Found 372 images belonging to 2 classes.\n", "Epoch 1/10\n", "[2024-07-31 20:16:55,760: WARNING: module_wrapper: From c:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\utils\\tf_utils.py:492: The name tf.ragged.RaggedTensorValue is deprecated. Please use tf.compat.v1.ragged.RaggedTensorValue instead.\n", "]\n", "23/23 [==============================] - 32s 1s/step - loss: 0.6976 - accuracy: 0.5983 - val_loss: 0.5528 - val_accuracy: 0.6750\n", "Epoch 2/10\n", "23/23 [==============================] - 18s 776ms/step - loss: 0.5961 - accuracy: 0.7022 - val_loss: 0.5576 - val_accuracy: 0.8250\n", "Epoch 3/10\n", "23/23 [==============================] - 18s 780ms/step - loss: 0.5489 - accuracy: 0.7612 - val_loss: 0.6042 - val_accuracy: 0.5250\n", "Epoch 4/10\n", "23/23 [==============================] - 18s 779ms/step - loss: 0.5166 - accuracy: 0.8006 - val_loss: 0.5593 - val_accuracy: 0.5750\n", "Epoch 5/10\n", "23/23 [==============================] - 18s 774ms/step - loss: 0.4863 - accuracy: 0.7949 - val_loss: 0.6155 - val_accuracy: 0.5250\n", "Epoch 6/10\n", "23/23 [==============================] - 18s 789ms/step - loss: 0.4486 - accuracy: 0.8062 - val_loss: 0.5774 - val_accuracy: 0.5250\n", "Epoch 7/10\n", "23/23 [==============================] - 18s 772ms/step - loss: 0.4574 - accuracy: 0.8034 - val_loss: 0.5751 - val_accuracy: 0.5125\n", "Epoch 8/10\n", "23/23 [==============================] - 18s 772ms/step - loss: 0.4493 - accuracy: 0.7949 - val_loss: 0.5814 - val_accuracy: 0.5125\n", "Epoch 9/10\n", "23/23 [==============================] - 18s 772ms/step - loss: 0.4414 - accuracy: 0.7921 - val_loss: 0.5636 - val_accuracy: 0.5125\n", "Epoch 10/10\n", "23/23 [==============================] - 18s 794ms/step - loss: 0.4290 - accuracy: 0.8090 - val_loss: 0.5743 - val_accuracy: 0.5000\n", "[2024-07-31 20:20:09,590: INFO: builder_impl: Assets written to: artifacts\\data_ingestion\\unzip\\kidney-ct-scan-image\\assets]\n" ] } ], "source": [ "try:\n", " config = ConfigurationManager()\n", " training_config = config.get_training_config()\n", " training = Training(confg=training_config)\n", " training.get_base_model()\n", " training.train_vaid_generator()\n", " training.train()\n", " \n", "except Exception as e:\n", " raise e" ] }, { "cell_type": "markdown", "metadata": {}, "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 }