File size: 10,153 Bytes
7195b15 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
{
"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
}
|