MonaiLabelLite - A lightweight implementation of MonaiLabel for Docker

Monai Label is an excellent tool, widely regarded as one of the best available for developers and researchers working on deep learning applications in medical imaging. However, the recent additions to the Monai Label repository have significantly increased its size and complexity.
- This increase generally doesn’t impact users running the software locally or utilizing a GPU on their system.
- It also adds considerable value to the open-source community.
Impact on Building a Docker Image
When building Docker images, we strive for a lighter footprint. Unfortunately, integrating the latest Monai Label into your code can lead to substantial image bloat. Personally, I sought to deploy a Monai server on CPU for a custom segmentation algorithm I developed. Yet, with the latest version of Monai Label, my Docker image ballooned to 20 GB, causing performance issues on MacBooks due to the increased overhead of running Docker Desktop. Thus, I decided to create a streamlined version of Monai Label tailored for CPU usage.
Here are some components you can remove to achieve a lighter image:
- The SAM2 model is included by default for all applications when Python is 3.10 or higher. You can remove this if SAM functionality is not required for your code.
- If you’re not performing inference on a GPU, the GPU version of PyTorch can be omitted.
- Nvidia CUDA setup files can take up to 500 MB of space and can be excluded if not needed.
After eliminating these components and ensuring that the CPU version of libraries like PyTorch is installed, I managed to reduce the Docker image size from 20 GB to 10 GB.
My Approach to Reducing Image Size
- Created a forked repository of the original Monai Label.
- Removed SAM installation from
requirements.txt
to save storage. - Executed the
requirements.txt
independently, which unfortunately installs the GPU version of PyTorch. - Utilized a wheel to reinstall PyTorch and torchvision with CPU support.
- Cloned the Git repository and added it to the root path.
Docker code
FROM python:3.10-slim
WORKDIR /app
# Install git
RUN apt-get update && apt-get install -y --no-install-recommends git && rm -rf /var/lib/apt/lists/*
# Clone MONAI Label Lite - No installation of SAM
RUN git clone https://github.com/eksubin/MONAILabelLite
# Install Python dependencies
RUN pip install --no-cache-dir -r MONAILabelLite/requirements.txt
# Set MONAILabel script paths in environment variables
ENV PATH="/app/MONAILabelLite/monailabel/scripts:${PATH}"
# Remove torch gpu and install torch cpu
RUN pip uninstall -y torch torchvision
RUN pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cpu
# Remove the git - Not needed after the first operation
RUN apt-get update && apt-get remove -y git
# Copy the application code
COPY . .
# Expose the port the app runs on
EXPOSE 8000
# Command to run the application
CMD ["add command to run your application"]
Enjoy Reading This Article?
Here are some more articles you might like to read next: