An end-to-end MLOps project demonstrating the evolution of a deep learning solution from a research notebook to a robust, modular, and interactive web application. This repository builds "FoodVision Mini," a model that classifies images of pizza, steak, and sushi, served through both Flask and FastAPI.
- π― Project Journey: From Notebook to MLOps
- β¨ Key Features
- ποΈ Architectural Overview
- π οΈ Tech Stack
- π Project Structure
- π Getting Started
- π οΈ Usage
- π Future Work
- π Acknowledgements
This repository is more than just a collection of scripts; it's a practical demonstration of the MLOps lifecycle, showing how a project matures from research to a deployable application.
- Phase 1: Research (Jupyter Notebook): The project began in a notebook (
research.py), the ideal environment for rapid experimentation and model prototyping. While great for research, notebooks are difficult to version, test, and deploy. - Phase 2: Modularization: The notebook's code was refactored into a modular Python package (
food_vision). This separated concerns like data setup, model building, and the training engine, making the code reusable and testable. - Phase 3: Automation (CLIs): With a modular core, we built
train.pyandpredict.py. These Command-Line Interfaces allow for automated, repeatable training runs and predictions, essential for scripting experiments. - Phase 4: Interaction (Web Apps): To make the model accessible, we built two web UIsβone using Flask and one with FastAPI. This introduced new challenges, like handling long-running training jobs without blocking the UI.
- Phase 5: Decoupling (Shared Logic): The final architectural step was to extract all application logic (state management, prediction handling) into a shared, framework-agnostic package (
webapp). This left the Flask and FastAPI files as "thin wrappers," resulting in a clean, maintainable system aligned with modern software design.
- π§ Deep Learning Core: Built with PyTorch, leveraging transfer learning with
EfficientNetB0,B2, andB4. - ποΈ 3-Tier Modular Architecture: Clean separation between the ML Core (
food_vision), Web App Logic (webapp), and Web Frameworks (app_flask.py,app_fastapi.py). - π Dual Web Frameworks: Provides identical web applications in Flask and FastAPI.
- πͺ Background Training: Train new models directly from the UI without blocking, thanks to background process management.
- π‘ Live Status Updates: The UI uses JavaScript polling for real-time feedback on training status (Idle, Running, Completed).
- πΌοΈ Interactive Predictions: Predict by uploading an image or clicking a sample image for an instant demo.
- π Device Agnostic: Automatically utilizes NVIDIA GPUs (CUDA), Apple Silicon (MPS), or CPU.
The project is intentionally designed in layers to promote maintainability and scalability.
food_vision(The ML Brain): This is the self-contained machine learning core. It knows everything about our models and data but knows nothing about the web.webapp(The Application's Central Nervous System): This is the framework-agnostic "business logic" layer. It orchestrates the application's functionality, acting as the bridge between the web interface and the ML core. It handles state, configuration, and actions like starting training or making predictions.app_*.py(The Web Interface): Theapp_fastapi.pyandapp_flask.pyfiles are thin wrappers. Their only responsibilities are to define URL endpoints, parse incoming requests, call thewebapplogic layer to do the actual work, and return a response.
This separation ensures that we could swap Flask for Django, or FastAPI for another framework, without rewriting any of the core ML or application logic.
| Component | Tools/Technologies | Description |
|---|---|---|
| ML Framework | PyTorch, torchvision |
The core deep learning library used for building, training, and running the EfficientNet models. |
| Experiment Tracking | TensorBoard |
Used for visualizing training metrics like loss and accuracy, helping to compare experiment runs. |
| Web Backend | FastAPI, Flask |
Two distinct, popular Python frameworks used to build the interactive web application interface. |
| Web Server | Uvicorn |
A high-performance ASGI server required to run the FastAPI application. |
| CLI Tools | argparse |
Standard Python library used to create user-friendly command-line interfaces for train.py and predict.py. |
| Data Handling | Pillow, requests |
Libraries used for opening and processing images, and for downloading datasets from URLs. |
| Environment Management | python-dotenv |
Manages environment variables, such as the Flask secret key, keeping them out of the source code. |
PyTorch-FoodVision-Mini/
βββ food_vision/ # Core machine learning package
β βββ __init__.py
β βββ data_setup.py
β βββ engine.py
β βββ model_builder.py
β βββ predict.py
β βββ utils.py
βββ webapp/ # Shared, framework-agnostic web logic
β βββ __init__.py
β βββ config.py
β βββ logic.py
β βββ state.py
β βββ utils.py
βββ static/ # CSS, JavaScript, and sample images
β βββ css/style.css
β βββ js/main.js
β βββ samples/
βββ templates/ # Shared HTML templates for the UI
β βββ index.html
β βββ result.html
βββ app_flask.py # Entry point for the Flask application
βββ app_fastapi.py # Entry point for the FastAPI application
βββ train.py # CLI for training models
βββ predict.py # CLI for making predictions
βββ requirements.txt # Project dependencies
βββ README.md
- Python 3.9+
- (Optional but Recommended) A CUDA-enabled GPU for faster training.
git clone https://github.com/GoJo-Rika/PyTorch-FoodVision-Mini.git
cd PyTorch-FoodVision-MiniWe recommend using uv, a fast, next-generation Python package manager, for setup.
-
Install
uvon your system if you haven't already.# On macOS and Linux curl -LsSf https://astral.sh/uv/install.sh | sh # On Windows powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
-
Create a virtual environment and install dependencies with a single command:
uv sync
This command automatically creates a
.venvfolder in your project directory and installs all listed packages fromrequirements.txt.Note: For a comprehensive guide on
uv, check out this detailed tutorial: uv-tutorial-guide.
If you prefer to use the standard venv and pip:
-
Create and activate a virtual environment:
python3 -m venv .venv source .venv/bin/activate # On Windows use: .venv\Scripts\activate
-
Install the required dependencies:
pip install -r requirements.txt
You can run either the Flask or FastAPI application. They provide the same UI and functionality.
# Make sure your virtual environment is active
python app_flask.pyNavigate to http://127.0.0.1:5001.
# Make sure your virtual environment is active
uvicorn app_fastapi:app --reloadNavigate to http://127.0.0.1:8000.
For automation or advanced use, you can use the CLI scripts.
# Train a new model (e.g., EffNetB2 on 20% data for 10 epochs)
python train.py \
--model effnetb2 \
--data_name "pizza_steak_sushi_20_percent" \
--epochs 10 \
--data_url "https://github.com/GoJo-Rika/PyTorch-FoodVision-Mini/raw/main/data/pizza_steak_sushi_20_percent.zip"
# Make a prediction with a trained model
python predict.py
--image_path "https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/04-pizza-dad.jpeg" \
--model_path "models/effnetb0_pizza_steak_sushi_10_percent_5_epochs.pth" \
--model_name "effnetb0"This project provides a solid foundation. Here are some advanced MLOps features that could be added:
- Asynchronous Training with a Job Queue: Replace the
subprocessmodule with a robust system like Celery and Redis. This would allow for handling multiple training requests, retries, and better process management. - Model Registry Integration: Use a tool like MLflow to track experiments, log hyperparameters, and version model artifacts for better reproducibility.
- CI/CD Pipeline: Implement a GitHub Actions workflow to automatically run tests, lint code, and deploy the application.
- Containerization: Dockerize the application to ensure a consistent, reproducible environment for deployment on any cloud service.
- Add a Project Walkthrough GIF: Record a short GIF or video demonstrating the web UI in action (training and prediction) and replace the placeholder below.
- Implement a
Makefile: Create aMakefileto simplify common commands (e.g.,make run-flask,make run-fastapi,make install). - Add Unit Tests: Implement unit tests for the functions in the
webapp/logic.pymodule to ensure reliability and prevent regressions.
This project is heavily inspired by and based on the incredible PyTorch for Deep Learning course by Daniel Bourke.