Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Git files
.git
.gitignore

# Python
__pycache__
*.pyc
*.pyo
*.pyd
.Python
env
.venv
venv
.env

# IDE
.vscode
.idea
*.swp
*.swo

# OS
.DS_Store
Thumbs.db

# Build artifacts
build/
dist/
*.egg-info

# Work directories (will be mounted)
work_dir/*
show_dir/*

# Logs
*.log
logs/

# Documentation
docs/
*.md
!README.md

# Cache
.cache/
.pytest_cache/
107 changes: 107 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
FROM pytorch/pytorch:2.0.0-cuda11.7-cudnn8-devel

ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV CUDA_HOME=/usr/local/cuda
ENV PATH=${CUDA_HOME}/bin:${PATH}
ENV LD_LIBRARY_PATH=${CUDA_HOME}/lib64:${LD_LIBRARY_PATH}

# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
wget \
curl \
vim \
unzip \
libgl1-mesa-glx \
libglib2.0-0 \
libsm6 \
libxext6 \
libxrender-dev \
gcc \
g++ \
&& rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /workspace

# Install Python dependencies
RUN pip install --upgrade pip

# Install OpenMMLab suite with pre-compiled CUDA extensions
RUN pip install -U openmim
RUN mim install mmengine
RUN mim install "mmcv>=2.0.0rc4,<2.2.0" -f https://download.openmmlab.com/mmcv/dist/cu117/torch2.0.0/index.html
RUN mim install "mmdet>=3.0.0,<4.0.0"
RUN mim install "mmpretrain>=1.0.0"

# Install additional dependencies for UltraSam
RUN pip install \
tensorboard \
matplotlib \
seaborn \
scipy \
scikit-image \
Pillow \
opencv-python \
tqdm \
yapf

# Force install compatible NumPy version AFTER other dependencies to avoid conflicts
RUN pip install "numpy==1.26.4" --force-reinstall --no-deps

# Verify MMCV CUDA extensions are working
RUN python -c "import torch; print('PyTorch:', torch.__version__); print('CUDA available:', torch.cuda.is_available())"
RUN python -c "import mmcv; print('MMCV:', mmcv.__version__)"
RUN python -c "from mmcv.ops import roi_align; print('MMCV CUDA extensions: OK')"
RUN python -c "import mmdet; print('MMDetection:', mmdet.__version__)"
RUN python -c "import mmpretrain; print('MMPretrain:', mmpretrain.__version__)"

# Copy UltraSam code
COPY . /workspace/UltraSam/
WORKDIR /workspace/UltraSam

# Set Python path
ENV PYTHONPATH=/workspace/UltraSam:/workspace/UltraSam/endosam

# Create necessary directories
RUN mkdir -p work_dir show_dir

# Copy UltraSam weights from host directory, or download if not present
RUN echo "=== UltraSam weights setup ===" && \
if [ -f "UltraSam.pth" ]; then \
echo "Using existing UltraSam.pth from host directory" && \
ls -lh UltraSam.pth; \
else \
echo "UltraSam.pth not found, downloading..." && \
wget --progress=bar:force -O UltraSam.pth "https://s3.unistra.fr/camma_public/github/ultrasam/UltraSam.pth" && \
echo "Download complete. File size: $(ls -lh UltraSam.pth)"; \
fi && \
echo "Verifying UltraSam weights..." && \
file UltraSam.pth && \
echo "Backing up weights for volume mount scenarios..." && \
cp UltraSam.pth /tmp/UltraSam.pth

# Final verification
RUN echo "=== Build verification ===" && \
ls -la UltraSam.pth && \
echo "UltraSam.pth size: $(stat -f%z UltraSam.pth 2>/dev/null || stat -c%s UltraSam.pth) bytes" && \
echo "=== Environment ready ==="

# Create entrypoint script to handle volume mount scenarios
RUN echo '#!/bin/bash\n\
echo "=== Container startup ==="\n\
if [ ! -f "/workspace/UltraSam/UltraSam.pth" ]; then\n\
echo "UltraSam.pth not found in working directory, copying from backup..."\n\
cp /tmp/UltraSam.pth /workspace/UltraSam/UltraSam.pth\n\
echo "UltraSam.pth restored: $(ls -lh /workspace/UltraSam/UltraSam.pth)"\n\
else\n\
echo "UltraSam.pth found: $(ls -lh /workspace/UltraSam/UltraSam.pth)"\n\
fi\n\
echo "=== Ready for execution ==="\n\
exec "$@"' > /entrypoint.sh && chmod +x /entrypoint.sh

# Set entrypoint and default command
ENTRYPOINT ["/entrypoint.sh"]

CMD ["/bin/bash"]
62 changes: 60 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _Adrien Meyer, Aditya Murali, Farahdiba Zarin, Didier Mutter, Nicolas Padoy_
## Minimal working example
<details>
<summary>Click to expand Install</summary>
This example guide you to download and use UltraSam in inference mode in a sample dataset.
This example guides you to download and use UltraSam in inference mode in a sample dataset.
The sample dataset, coco-based, is in "./sample_dataset" (using MMOTU2D samples).

Clone the repo
Expand Down Expand Up @@ -51,6 +51,64 @@ It will run inference on the specified sample dataset, modified inline from the

</details>

## Docker Alternative

<details>
<summary>Click to expand Docker setup</summary>

Docker provides the simplest way to run UltraSam with all dependencies pre-configured.

### Prerequisites
- Docker Engine with GPU support
- NVIDIA Container Toolkit for GPU access
- NVIDIA drivers compatible with CUDA 11.7

### Quick Docker Setup

1. **Clone and build:**
```bash
git clone https://github.com/CAMMA-public/UltraSam
cd UltraSam
docker build -t ultrasam:latest .
```

2. **Run sample inference:**

To lauch the docker

```bash
docker run --rm --gpus all -it -v $(pwd):/workspace/UltraSam ultrasam:latest bash
```

Run a sample test script inside the container:

```bash
python visual_inference.py
```

To run a sample inference test using MMDet directly from host:

```bash
docker run --rm --gpus all -v $(pwd):/workspace/UltraSam \
ultrasam:latest bash -c "
cd /workspace/UltraSam && \
mim test mmdet configs/UltraSAM/UltraSAM_full/UltraSAM_box_refine.py \
--checkpoint UltraSam.pth \
--cfg-options test_dataloader.dataset.data_root='sample_dataset' \
test_dataloader.dataset.ann_file='sample_coco_MMOTU2D.json' \
test_dataloader.dataset.data_prefix.img='sample_images' \
test_evaluator.ann_file='sample_dataset/sample_coco_MMOTU2D.json' \
--work-dir ./work_dir/example \
--show-dir ./show_dir"
```

**Docker Features:**
- PyTorch 2.0.0 with CUDA 11.7 support
- Pre-compiled MMCV, MMDetection, and MMPretrain
- All dependencies with compatible versions (NumPy 1.26.4 enforced for tensor compatibility)
- Ready-to-use environment with PYTHONPATH configured

</details>

## Usage

Expand Down Expand Up @@ -82,7 +140,7 @@ pip install scipy

Pre-trained UltraSam model checkpoint is accessible [at this link](https://s3.unistra.fr/camma_public/github/ultrasam/UltraSam.pth).

To train / test, you will need a coco.json annotation file, and create a symbolik link to it, or modify the config files to point to your annotation file.
To train / test, you will need a coco.json annotation file, and create a symbolic link to it, or modify the config files to point to your annotation file.

To train from scratch, you can use the code in ```weights``` to download and convert SAM, MEDSAM and adapters weights.

Expand Down
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3.8'

services:
ultrasam:
build:
context: .
dockerfile: Dockerfile
container_name: ultrasam-gpu
runtime: nvidia
environment:
- NVIDIA_VISIBLE_DEVICES=all
- NVIDIA_DRIVER_CAPABILITIES=compute,utility
- CUDA_VISIBLE_DEVICES=0,1 # Adjust based on your GPU setup
volumes:
- .:/workspace/UltraSam
- ./work_dir:/workspace/UltraSam/work_dir
- ./show_dir:/workspace/UltraSam/show_dir
- ./sample_dataset:/workspace/UltraSam/sample_dataset
working_dir: /workspace/UltraSam
stdin_open: true
tty: true
shm_size: 8gb # Increase shared memory for multi-processing
command: /bin/bash
Loading