{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/shrey/Desktop/Kidney-Disease-Classifcation/research'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%pwd" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "os.chdir('../')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/shrey/Desktop/Kidney-Disease-Classifcation'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%pwd" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "os.environ[\"MLFLOW_TRACKING_URI\"]=\"https://dagshub.com/Shrey-patel-07/Kidney-Disease-Classifcation.mlflow\"\n", "os.environ[\"MLFLOW_TRACKING_USERNAME\"]=\"Shrey-patel-07\"\n", "os.environ[\"MLFLOW_TRACKING_PASSWORD\"]=\"6a425a20bb5b645c5efa1ca49ab28097a92a76ee\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-01-04 12:24:18.257148: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n", "2024-01-04 12:24:18.258826: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-01-04 12:24:18.285564: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", "2024-01-04 12:24:18.285595: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", "2024-01-04 12:24:18.286297: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", "2024-01-04 12:24:18.290364: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-01-04 12:24:18.290802: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", "2024-01-04 12:24:18.864121: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" ] } ], "source": [ "import tensorflow as tf" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-01-04 12:24:20.808055: E external/local_xla/xla/stream_executor/cuda/cuda_driver.cc:274] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected\n" ] } ], "source": [ "model = tf.keras.models.load_model('artifacts/training/model.h5')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from dataclasses import dataclass\n", "from pathlib import Path\n", "\n", "@dataclass(frozen=True)\n", "class EvaluationConfig:\n", " path_of_model: Path\n", " training_data: Path\n", " all_params: dict\n", " mlflow_uri: str\n", " params_image_size: list\n", " params_batch_size: int" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "os.chdir('src/')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from kidney_classification.constants import *\n", "from kidney_classification.utils.common import read_yaml, create_directories, save_json" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "class ConfigurationManager:\n", " def __init__(\n", " self,\n", " config_filepath=CONFIG_FILE_PATH,\n", " param_filepath=PARAMS_FILE_PATH\n", " ):\n", " self.config = read_yaml(config_filepath)\n", " self.params = read_yaml(param_filepath)\n", " create_directories([self.config.artifacts_root])\n", "\n", " def get_evaluation_config(self) -> EvaluationConfig:\n", " eval_config = EvaluationConfig(\n", " path_of_model=\"artifacts/training/model.h5\",\n", " training_data=\"artifacts/data_ingestion/CT-KIDNEY-DATASET-Normal-Cyst-Tumor-Stone\",\n", " mlflow_uri=\"https://dagshub.com/Shrey-patel-07/Kidney-Disease-Classifcation.mlflow\",\n", " all_params=self.params,\n", " params_image_size=self.params.IMAGE_SIZE,\n", " params_batch_size=self.params.BATCH_SIZE\n", " )\n", " return eval_config" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import mlflow\n", "import mlflow.keras\n", "from urllib.parse import urlparse" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "class Evaluation:\n", " def __init__(self, config: EvaluationConfig):\n", " self.config = config\n", " self.valid_generator = None # Initialize to None\n", "\n", " def _valid_generator(self):\n", " img_height, img_width = self.config.params_image_size[:-1]\n", "\n", " self.valid_generator = tf.keras.utils.image_dataset_from_directory(\n", " self.config.training_data,\n", " image_size=(img_height, img_width),\n", " validation_split=0.30,\n", " subset='validation',\n", " seed=123\n", " )\n", "\n", " self.valid_generator = self.valid_generator.map(lambda x, y: (x / 255, y))\n", " AUTOTUNE = tf.data.AUTOTUNE\n", " self.valid_generator = self.valid_generator.cache().prefetch(buffer_size=AUTOTUNE)\n", "\n", "\n", " @staticmethod\n", " def load_model(path: Path) -> tf.keras.Model:\n", " return tf.keras.models.load_model(path)\n", " \n", "\n", " def evaluation(self):\n", " self.model = self.load_model(self.config.path_of_model)\n", " self._valid_generator()\n", " self.score = self.model.evaluate(self.valid_generator)\n", " self.save_score()\n", "\n", " def save_score(self):\n", " scores = {\"loss\": self.score[0], \"accuracy\": self.score[1]}\n", " save_json(path=Path(\"scores.json\"), data=scores)\n", "\n", " def log_into_mlflow(self):\n", " mlflow.set_registry_uri(self.config.mlflow_uri)\n", " tracking_url_type_store = urlparse(mlflow.get_tracking_uri()).scheme\n", " \n", " with mlflow.start_run():\n", " mlflow.log_params(self.config.all_params)\n", " mlflow.log_metrics(\n", " {\"loss\": self.score[0], \"accuracy\": self.score[1]}\n", " )\n", " # Model registry does not work with file store\n", " if tracking_url_type_store != \"file\":\n", "\n", " # Register the model\n", " mlflow.keras.log_model(self.model, \"model\", registered_model_name=\"VGG16Model\")\n", " else:\n", " mlflow.keras.log_model(self.model, \"model\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "os.chdir('../')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2024-01-04 12:24:51,908: INFO: common yaml file: config/config.yaml loaded successfully]\n", "[2024-01-04 12:24:51,910: INFO: common yaml file: params.yaml loaded successfully]\n", "Found 12446 files belonging to 1 classes.\n", "Using 3733 files for validation.\n", "117/117 [==============================] - 147s 1s/step - loss: 3.2269e-06 - accuracy: 1.0000\n", "[2024-01-04 12:27:19,911: INFO: common json file saved at: scores.json]\n", "[2024-01-04 12:27:19,912: INFO: common json file saved at: scores.json]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2024/01/04 12:27:22 WARNING mlflow.tensorflow: You are saving a TensorFlow Core model or Keras model without a signature. Inference with mlflow.pyfunc.spark_udf() will not work unless the model's pyfunc representation accepts pandas DataFrames as inference inputs.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Assets written to: /tmp/tmpdrlg6a4p/model/data/model/assets\n", "[2024-01-04 12:27:23,234: INFO: builder_impl Assets written to: /tmp/tmpdrlg6a4p/model/data/model/assets]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/home/shrey/Desktop/Kidney-Disease-Classifcation/env/lib/python3.11/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils.\n", " warnings.warn(\"Setuptools is replacing distutils.\")\n", "Registered model 'VGG16Model' already exists. Creating a new version of this model...\n", "2024/01/04 12:27:47 INFO mlflow.store.model_registry.abstract_store: Waiting up to 300 seconds for model version to finish creation. Model name: VGG16Model, version 3\n", "Created version '3' of model 'VGG16Model'.\n" ] } ], "source": [ "try:\n", " config = ConfigurationManager()\n", " eval_config = config.get_evaluation_config()\n", " evaluation = Evaluation(eval_config)\n", " evaluation.evaluation()\n", " evaluation.log_into_mlflow()\n", " \n", "except Exception as e:\n", " raise e" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }