Introduction to Deep Learning:
This lesson introduces the idea that deep learning, a subfield of machine learning, excels at tasks like image recognition. Convolutional Neural Networks (CNNs) are at the core of these
advancements.
Deep Learning Explanation:
Key Content:
Deep Learning in a Cloud Environment:
Deep learning often requires substantial computational resources (GPUs, TPUs). Saturn Cloud simplifies access to these resources by enabling cloud environments tailored for machine learning
workflows.
Deep Learning Explanation:
Key Content:
Introduction to Deep Learning with TensorFlow and Keras:
TensorFlow and Keras are fundamental tools for building deep learning models. This lesson explores their role in creating neural networks.
Deep Learning Explanation:
Key Content:
Leveraging Knowledge with Deep Learning:
Pretrained models like ResNet or Inception capture general knowledge about images, obtained through training on massive datasets.
Deep Learning Explanation:
Key Content:
Deep Learning Fundamentals for Images:
ConvNets use specific layers to extract visual features such as edges, textures, and shapes.
Deep Learning Explanation:
Key Content:
Boosting Results with Deep Learning:
Transfer learning uses pretrained networks to avoid training from scratch, saving time and improving performance.
Deep Learning Explanation:
Key Content:
The Role of Learning Rate in Deep Learning:
The learning rate is a critical hyperparameter that controls the speed of model convergence during backpropagation.
Deep Learning Explanation:
Key Content:
Saving and Reusing Progress in Deep Learning:
Checkpointing is crucial when training complex models for long periods or testing modifications without starting from scratch.
Deep Learning Explanation:
Key Content:
Building Deeper Models in Deep Learning:
Adding more layers increases a model’s learning capacity but also makes training more challenging.
Deep Learning Explanation:
Key Content:
Reducing Overfitting with Dropout:
Dropout is a regularization technique used in deep learning to randomly deactivate neurons during training.
Deep Learning Explanation:
Key Content:
Generating More Data for Deep Learning:
Deep learning models need vast amounts of data to generalize well. Data augmentation artificially enriches datasets through transformations.
Deep Learning Explanation:
Key Content:
Managing Challenges of Large Models in Deep Learning:
Architectures like GPT, BERT, or Vision Transformers (ViT) push the boundaries of deep learning capabilities.
Deep Learning Explanation:
Key Content:
Cardiovascular diseases (CVD) are among the leading causes of death worldwide. By analyzing common risk factors such as high cholesterol, chest pain, and age-related risks, this project aims to identify individuals at risk of developing CVD at an early stage. Early detection could significantly reduce the number of deaths attributed to these diseases.
The primary objective is to design a machine learning model capable of classifying patients based on their cardiovascular risk by leveraging existing medical data. Additionally, the model aims to provide insights into the correlations and relative importance of various risk factors to assist researchers.
The project utilizes the Kaggle Heart Failure Prediction Dataset, which offers comprehensive medical data relevant to CVD prediction.
Detailed data exploration is performed using the part_1_preprocessing.ipynb
notebook. This process includes data cleaning, feature engineering, and visualization to understand
distributions, correlations, and anomalies.
The project implements multiple deployment options:
Pipfile
specifies all necessary dependencies (numpy, scikit-learn, xgboost,
etc.).
training.py
script, enabling automated execution.
The Streamlit app is hosted on Streamlit Cloud and accessible via this URL: Streamlit App.
The project is assessed based on several dimensions:
This project demonstrates a comprehensive and rigorous approach to analyzing and predicting cardiovascular risks using machine learning. By combining in-depth analysis, high-performing models, and versatile deployment solutions, it addresses practical challenges in medical research while offering an application that healthcare professionals can readily use.
Obesity is a critical global health issue linked to poor eating habits and insufficient physical activity. It can lead to severe and even fatal consequences. This project aims to estimate obesity levels using a dataset of individuals from Colombia, Peru, and Mexico, offering insights into their health based on their eating habits and physical condition.
The dataset contains 17 attributes and 2,111 records, categorized into the following classes:
Seventy-seven percent of the data was synthetically generated using the SMOTE technique in Weka, while 23% was collected from users via a web platform.
Data analysis and model training were performed in Jupyter Notebook, and a streamlined pipeline was developed to preprocess the dataset and train the models. The process resulted in a trained
model and a serialized pipeline exported as obesity-levels-model.bin
.
Using Flask:
predict.py
.
pipenv install
), activate it, and run the Flask application.
cd scripts
python predict.py
You can test the model locally using the provided test.py
script.
Using Waitress:
waitress-serve --listen=0.0.0.0:9696 predict:app
Docker Deployment:
docker build -t estimation-obesity-levels .
docker run -p 9696:9696 -it estimation-obesity-levels:latest
Using AWS Elastic Beanstalk:
t2.micro
.
Once deployed, the application URL is available in the AWS Elastic Beanstalk environment section. Update the test.py
script with the new URL to test the application remotely.
Testing scripts (test.py
) are provided to evaluate the model locally or remotely. Update the url
variable to reflect the deployment location (local or AWS).
This project demonstrates the practical application of machine learning for public health, offering scalable solutions to assess obesity risks effectively.
This project uses data science techniques to analyze and predict SuperStore's sales performance, identifying actionable insights and optimization strategies to enhance profitability and revenue.
This project combines robust machine learning techniques and actionable insights, offering a comprehensive approach to enhancing SuperStore's business performance.
Machine Learning - Serverless Hosting
Machine learning models can be deployed using serverless computing services. When hosting these models, considerations need to be made regarding the size of the packages and security configurations.
Cloud providers such as Azure Functions, Google Cloud Functions (GCP), and AWS Lambda offer serverless computing services that allow you to execute code without managing or provisioning infrastructure. These services enable the hosting of machine learning models as serverless functions, where you can run inference in response to events without worrying about the underlying server management. Below is a technical breakdown of deploying machine learning models using these serverless platforms:
Azure Functions is part of Microsoft's serverless computing offering on Azure. Here's how to deploy a machine learning model using Azure Functions:
Select a Runtime: Azure Functions supports several runtimes, including .NET, Node.js, Python, and Java. Choose the one that matches your model’s requirements. For instance, if your model uses Python-based libraries like TensorFlow or PyTorch, select Python as the runtime.
Create a Function: You can create a new function using the Azure Portal, Visual Studio, or Azure CLI. Define triggers such as HTTP requests, Timer events, or Queue events, depending on your use case.
Dependencies and Environment: Install the necessary dependencies for your machine learning model. For Python-based models, you might use the pip
package
manager to install packages like TensorFlow, Scikit-learn, or PyTorch.
Code Integration: Integrate the code that loads and runs your machine learning model. You can use popular ML libraries, such as TensorFlow, PyTorch, or Scikit-learn. This code would typically load the model, perform inference, and return results based on the input data.
Deployment: Once your function is ready, deploy it using the Azure tools, such as the Azure Portal, Visual Studio, or Azure CLI.
AWS Lambda is a core part of AWS's serverless architecture. Here's how to host machine learning models on Lambda:
Choose a Runtime: AWS Lambda supports multiple runtimes, including Node.js, Python, Java, and more. Choose a runtime compatible with your model. If you're using TensorFlow or Keras models, Python is the most common choice.
Create a Lambda Function: Create a Lambda function using the AWS Management Console or AWS CLI. You can configure triggers, such as API Gateway, S3 events, or CloudWatch events.
Dependencies and Environment: Package any required dependencies and libraries into your Lambda function’s deployment package. If you are using Python, ensure that your
deployment package includes the relevant .whl
files or dependencies that AWS Lambda doesn’t natively support.
Code Integration: Write the code that loads the machine learning model and executes inference using libraries compatible with Lambda, such as TensorFlow Lite or PyTorch.
Deployment: Upload your deployment package to AWS Lambda via the AWS Management Console, AWS CLI, or tools like AWS SAM (Serverless Application Model).
Google Cloud Functions is another serverless option provided by Google Cloud Platform. Here's how you can deploy machine learning models:
Choose a Runtime: Google Cloud Functions supports runtimes like Node.js, Python, Go, and others. Select the one that works best with your model.
Create a Function: Create a Cloud Function using the Google Cloud Console, gcloud CLI, or other deployment methods. Configure triggers such as HTTP requests, Cloud Storage events, or Pub/Sub messages.
Dependencies and Environment: Install dependencies required for your model. For example, for Python-based models, use pip to install any necessary libraries.
Code Integration: Integrate the necessary code to load the model and perform inference. Popular machine learning libraries like TensorFlow, Keras, and Scikit-learn are widely used for model execution.
Deployment: Deploy the function to Google Cloud Functions using the Cloud Console or gcloud CLI.
Cold Starts: Both Azure Functions, AWS Lambda, and Google Cloud Functions may experience "cold starts." A cold start happens when the function is invoked for the first time after being idle, leading to higher latency during that initial execution. This is a crucial consideration for real-time applications where low latency is essential.
Resource Limits: Each serverless platform has resource limitations, such as maximum execution time, memory, and payload size. Ensure that your model's size and inference requirements are within the platform's constraints.
Integration with Other Services: You can integrate serverless functions with other cloud services, such as Azure Blob Storage, AWS S3, Google Cloud Storage, or BigQuery, to store and manage data, log events, or trigger functions based on specific criteria.
TensorFlow Lite for Edge Devices
TensorFlow Lite is a lightweight version of TensorFlow specifically designed for mobile devices and edge devices, offering optimized inference for resource-constrained environments. It's ideal for deploying machine learning models on devices with limited computational power and memory, such as smartphones, IoT devices, and embedded systems.
Model Optimization: TensorFlow Lite provides tools for converting and optimizing TensorFlow models to run efficiently on mobile and embedded devices. Optimization techniques like quantization reduce model size by lowering the precision of model weights and activations, thus reducing memory usage and improving inference speed.
Hardware Accelerators: TensorFlow Lite supports a range of hardware accelerators, including GPU and TPU on supported devices, enhancing inference performance.
Interpreter: TensorFlow Lite uses a lightweight interpreter that can execute models on edge devices, with support for both CPU and specialized accelerators like TPUs.
To convert a TensorFlow model (e.g., a SavedModel or Keras model) to TensorFlow Lite, follow these steps:
Install TensorFlow and TensorFlow Lite Converter: Ensure you have TensorFlow installed and also install the TensorFlow Lite Converter.
pip install tensorflow
pip install tflite-model-maker
Convert the Model: You can use the TensorFlow Lite Converter to convert your trained model to the .tflite
format.
import tensorflow as tf
from tensorflow.keras.models import load_model
model = load_model("path/to/model.h5")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open("model.tflite", "wb") as f:
f.write(tflite_model)
Optimization (Optional): Quantization or other optimization techniques can be applied to further reduce the model size for constrained devices, ensuring it fits within the resource limits.
Deploy to Serverless Platform (e.g., Azure Functions): After converting to .tflite
, you can deploy the model to serverless platforms like Azure
Functions by integrating the model loading and inference code. Ensure that the function is optimized to handle potential cold starts and resource limits.
Image Classification using TensorFlow Lite
In this section, we demonstrate how to download, preprocess, and classify images using a TensorFlow Lite model. The image will be processed through a series of functions:
Download Image: This function fetches an image from the provided URL and converts it into a PIL Image object.
from urllib import request
from PIL import Image
def download_image(url):
with request.urlopen(url) as resp:
buffer = resp.read()
return Image.open(BytesIO(buffer))
Prepare Image: This function resizes the image to the target size and ensures it’s in RGB format.
def prepare_image(img, target_size):
if img.mode != 'RGB':
img = img.convert('RGB')
return img.resize(target_size, Image.NEAREST)
Preprocess Image: The function converts the image to a NumPy array, normalizes the pixel values to the range [0, 1], and ensures the data type is float32.
def preprocess_image(img):
img = np.array(img).astype('float32')
return img / 255.0
Run Inference with TensorFlow Lite Model: Use the TensorFlow Lite model for inference:
def load_lite_model(path):
interpreter = tf.lite.Interpreter(model_path=path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
return interpreter, input_details, output_details
interpreter, input_details, output_details = load_lite_model('model.tflite')
interpreter.set_tensor(input_details[0]['index'], [img_normalized])
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(f"Classification Result: {round(output_data[0][0], 3)}")
By following these steps, you can deploy a TensorFlow Lite model in a serverless environment, preprocess the image data, and run inference in real-time on edge devices with minimal latency.
To run the image classification model within a Docker container, you can set up a Dockerfile that includes all necessary dependencies and your application code. The steps include creating the environment, installing dependencies, and running the application inside the container. You can use the following Dockerfile for deployment:
FROM agrigorev/zoomcamp-bees-wasps:v2
WORKDIR .
COPY main.py .
COPY img_ai ./img_ai/
COPY Pipfile Pipfile.lock ./
RUN pip install numpy
RUN pip install Pillow
RUN pip install https://github.com/alexeygrigorev/tflite-aws-lambda/raw/main/tflite/tflite_runtime-2.14.0-cp310-cp310-linux_x86_64.whl
CMD ["main.main"]
Once the Docker image is built, deploy it as a containerized function on your serverless platform. The Docker container will ensure a consistent runtime environment and provide scalability for inference tasks.
In this section, we dive deep into the integration of Kubernetes with TensorFlow Serving, a high-performance framework designed to deploy and serve machine learning models. We'll explore how to leverage Kubernetes' power to scale machine learning services, optimize resource usage, and ensure reliability in production environments. The key areas covered include the architecture of scalable ML systems, deployment workflows, GPU optimization, and scaling strategies using Kubernetes' built-in features.
The gateway service is responsible for the initial image processing. This includes downloading images, resizing, and converting them into numpy arrays—tasks that are relatively lightweight and do not require GPU acceleration. The gateway can be containerized in Docker and deployed in Kubernetes Pods for isolation and scalability.
This Dockerfile sets up a simple Python-based service to handle image preprocessing.
Once the images are preprocessed, they are sent to the TensorFlow Serving model for inference. TensorFlow Serving is highly optimized for inference tasks and is written in C++, ensuring performance is maximized for real-time predictions. Since inference tasks are computationally expensive, the model service is configured to run on GPU-enabled nodes.
Kubernetes excels at managing scalable applications, which is crucial for deploying machine learning models in production environments. We'll discuss both horizontal scaling (adding more replicas) and vertical scaling (increasing resource allocation for specific workloads) and how these can be managed with Kubernetes.
This configuration automatically scales the number of TensorFlow Serving pods between 2 and 10 based on CPU usage.
Once the local Kubernetes setup is functioning well, the next step is to transition to the cloud. AWS Elastic Kubernetes Service (EKS) is a fully managed service that simplifies the deployment, scaling, and management of Kubernetes clusters in the cloud. EKS integrates seamlessly with other AWS services like Elastic Load Balancing, IAM for security, and CloudWatch for monitoring.
To deploy your machine learning services in the cloud, you can use the eksctl CLI to create and manage your Kubernetes clusters on AWS.
In production systems, you often need to persist data between container restarts. Kubernetes supports Persistent Volumes (PV), which can be backed by various storage systems, including network-attached storage (NAS) or cloud-based services like AWS EBS.
This allows for managing data that should persist even when a container is terminated or restarted, such as storing models or datasets.
Kubernetes ConfigMaps and Secrets are used to store configuration data (non-sensitive) and sensitive information (like API keys, passwords), respectively. This allows for flexible management of environment variables, model parameters, and other configurations.
ConfigMaps are ideal for storing general application settings, whereas Secrets should be used to store sensitive data securely, with encryption at rest.
💡 Tip: Kubernetes is a powerful tool, but mastering it for machine learning requires understanding both infrastructure management and the needs of your specific workloads (e.g., GPUs for inference tasks, CPUs for lighter workloads).
🌟 Skin Lesion Analyzer
Detection and classification of skin lesions using ResNet50, MLOps, and Flask.
🗂️ Table of Contents
1. ✨ Introduction]
2. 🔍 Data Exploration (EDA)]
3. 🛠️ Data Preparation]
4. 🏋️♂️ Model Training
5. ✅ Testing and Evaluation
6. 🚀 Deployment with Streamlit
- 📦 Creating a Docker Image
- 🌐 Server Deployment
7. 📈 Monitoring and MLOps
- 🔄 Building an MLOps Pipeline
8. 📂 Data
9. 🙌 Contributors
✨ 1. Introduction
The Skin Lesion Analyzer is an application that uses artificial intelligence to detect and classify skin lesions from images.
This approach automates and improves the accuracy of dermatological diagnostics through deep learning algorithms.
Theoretical Context:
Image classification models rely on convolutional neural networks (CNNs), such as ResNet50. These models can detect complex patterns and differentiate lesion classes based on visual features.
🎯 Main Objectives:
- Provide a fast and accurate model for analyzing skin lesions.
- Deploy a user-friendly and accessible application.
- Integrate a continuous training and deployment pipeline to ensure consistent performance improvements.
---
## 🔍 2. Data Exploration (EDA)
Data Exploration is an essential step in data-driven projects. It helps understand the structure of the data and detect any anomalies or trends.Here are the different labels for this dataset.
Class indices and their corresponding labels:
Index 0: Actinic keratosis.
Index 1: Basal cell carcinoma.
Index 2: Dermatofibroma.
Index 3: Melanoma.
Index 4: Nevus.
Index 5: Pigmented benign keratosis.
Index 6: Squamous cell carcinoma.
Index 7: Vascular lesion.
Key Steps:
1. Analyze the distribution of classes (types of lesions).
2. Study metadata, such as age or anatomical location.
3. Visualize correlations between different attributes.
Theoretical Context:
The quality and diversity of data directly influence model performance. An imbalanced class distribution may require techniques like sampling or weighting to ensure reliable predictions.
🛠️ 3. Data Preparation
Data preparation is crucial to ensure uniform and relevant input to the model.
Key Steps:
1. Data Cleaning: Remove missing or anomalous values in the metadata.
2. Image Preprocessing: Resize images to 224x224 pixels to ensure compatibility with ResNet50.
3. Data Augmentation: Generate variations of images (rotation, zoom, flipping) to enrich the dataset and prevent overfitting.
4. Normalization: Adjust pixel values between 0 and 1 to stabilize training.
Theoretical Context:
Neural networks are sensitive to data scales. Normalization accelerates convergence and improves model robustness.
📷 Normalize images:
🏋️♂️ 4. Model Training
The model used, ResNet50, is a convolutional neural network pretrained on ImageNet. It is fine-tuned to classify skin lesions into 7 categories.
Training Steps:
1. Load cleaned and normalized data.
2. Perform data augmentation to improve robustness.
3. Train by adjusting the model's final layers.
Theoretical Context:
- Transfer Learning: Using a pretrained model reduces data requirements and training time.
- Fine-Tuning: Adapting a general-purpose model (like ResNet50) to a specific task.
📈 Visual Indicators:
✅ 5. Testing and Evaluation
Once trained, the model's performance is evaluated on an independent test set.
- Evaluation : Evaluation of Models on test set:
Metrics Used:
1. Accuracy: Proportion of correct predictions.
2. Recall: Model's ability to correctly identify positive cases.
3. F1-score: Harmonic mean of precision and recall, useful for imbalanced data.
4. Confusion Matrix: Visualizes classification errors.
Theoretical Context:
Metrics like F1-score are particularly useful in medical contexts where classification errors have critical implications.
Test whit new image and Grad-CAM :
Here is the result of our model on a new image, along with the explainability analysis using the Grad-CAM method. This technique allows us to visualize which areas of the image influenced the model's decision. By using Grad-CAM, we generate a heatmap highlighting the important regions of the image that the model focused on for making its prediction. This approach provides a better understanding of the model's decision-making process, making its predictions more transparent and interpretable, which is essential for ensuring the reliability of the model, especially in sensitive applications.
🚀 6. Deployment with Streamlit
Once validated, the model is integrated into a Streamlit application for interactive use.
📦 Creating a Docker Image
Docker is used to ensure application portability. A Docker image contains all necessary dependencies.
🌐 Server Deployment
The Docker image is deployed on a server or cloud platform (AWS, Azure, Google Cloud).
📈 7. Monitoring and MLOps
Monitoring and MLOps practices ensure the model's maintenance and continuous improvement after deployment.
Monitoring with Prometheus and Grafana:
- Collect performance metrics (response time, error rate).
- Real-time visualization on dashboards.
Building an MLOps Pipeline:
1. CI/CD: Continuous integration and deployment with GitHub Actions.
2. Kubernetes: Container orchestration for scalable deployment.
📂 8. Data
The dataset HAM10000 used in this project is a collection of 10,015 images of skin lesions, with detailed medical annotations.
## 🙌 **9. Contributors**
This project was created by:
-[M. Domche]
For any questions or suggestions,
✨ Thank you for your interest in this project!