File size: 6,882 Bytes
c58f0bb
 
 
bd9a893
c58f0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd9a893
bbc3fdc
 
 
 
 
 
f054fb1
 
bbc3fdc
c58f0bb
3968781
 
 
 
 
bbc3fdc
 
3968781
 
bbc3fdc
 
c58f0bb
 
 
 
 
 
 
 
 
 
3968781
 
bbc3fdc
3968781
 
 
bbc3fdc
 
3968781
bbc3fdc
c58f0bb
 
 
bbc3fdc
c58f0bb
 
bbc3fdc
c58f0bb
3968781
c58f0bb
 
 
 
3968781
 
c58f0bb
c05ac4f
 
 
 
 
 
 
 
 
 
 
 
bbc3fdc
 
09ba3b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Base image with FoundationPose dependencies split into CPU (L1) and GPU (L2)

# Stage 1: CPU-only base with Python deps
FROM docker.io/ubuntu:22.04 AS foundationpose-base-l1

ENV DEBIAN_FRONTEND=noninteractive

# Install system deps needed to build/run python packages
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    git \
    python3.10 \
    python3-pip \
    build-essential \
    cmake \
    ninja-build \
    libeigen3-dev \
    python3.10-dev \
    libboost-system-dev \
    libboost-program-options-dev \
    pybind11-dev \
    libgl1 \
    libglib2.0-0 \
    libgomp1 \
    libsm6 \
    libxext6 \
    libxrender1 \
    libxkbcommon0 \
    libx11-6 \
    libxrandr2 \
    libxi6 \
    libxinerama1 \
    libxcursor1 \
    libspatialindex-dev \
    && rm -rf /var/lib/apt/lists/*

# Set python as default
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 && \
    update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1

RUN python3 -m pip install --no-cache-dir --upgrade pip

# Core python deps (CPU-safe)
# Keep NumPy <2 for extension compatibility
RUN pip install --no-cache-dir \
    "numpy<2" \
    Pillow>=10.0.0 \
    gradio>=4.0.0 \
    huggingface-hub>=0.20.0 \
    scipy==1.12.0 \
    scikit-image==0.22.0 \
    scikit-learn==1.4.1.post1 \
    kornia==0.7.2 \
    einops==0.7.0 \
    timm==0.9.16 \
    pyyaml==6.0.1 \
    ruamel.yaml==0.18.6 \
    omegaconf==2.3.0 \
    h5py==3.10.0 \
    numba==0.59.1 \
    imageio==2.34.0 \
    joblib==1.3.2 \
    psutil==6.1.1 \
    albumentations==1.4.2 \
    imgaug==0.4.0 \
    seaborn==0.13.2 \
    plotly==5.20.0 \
    bokeh==3.4.0 \
    colorama==0.4.6 \
    GPUtil==1.4.0 \
    simplejson==3.19.2 \
    openpyxl==3.1.2 \
    xlsxwriter==3.2.0 \
    nodejs==0.1.1 \
    jupyterlab==4.1.5 \
    ipywidgets==8.1.2 \
    py-spy==0.3.14 \
    videoio==0.2.8 \
    pypng==0.20220715.0 \
    roma==1.4.4 \
    transformations==2024.6.1 \
    meshcat==0.3.2 \
    webdataset==0.2.86 \
    wandb==0.16.5 \
    g4f==0.2.7.1 \
    objaverse==0.1.7 \
    opencv-python==4.9.0.80 \
    opencv-contrib-python==4.9.0.80 \
    open3d==0.18.0 \
    pyglet==1.5.28 \
    pysdf==0.1.9 \
    trimesh==4.2.2 \
    xatlas==0.0.9 \
    rtree==1.2.0 \
    pyrender==0.1.45 \
    pyOpenGL>=3.1.0 \
    pyOpenGL_accelerate>=3.1.0 \
    pybullet==3.2.6 \
    pycocotools==2.0.7 \
    Panda3D==1.10.14 \
    pin==2.7.0 \
    && pip cache purge

# Stage 2: GPU-enabled base
FROM docker.io/nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04 AS foundationpose-base-l2

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

# Only build for T4 (7.5) - reduces compilation memory by 50%
ENV TORCH_CUDA_ARCH_LIST="7.5"

# Install system deps
RUN rm -f /etc/apt/sources.list.d/cuda*.list /etc/apt/sources.list.d/*.list && \
    apt-get update && apt-get install -y --no-install-recommends --allow-unauthenticated \
    ca-certificates \
    && apt-get clean && rm -rf /var/lib/apt/lists/* && \
    apt-get update && apt-get install -y --no-install-recommends \
    python3.10 \
    python3-pip \
    git \
    libgl1 \
    libglib2.0-0 \
    libgomp1 \
    libsm6 \
    libxext6 \
    libxrender1 \
    libxkbcommon0 \
    libx11-6 \
    libxrandr2 \
    libxi6 \
    libxinerama1 \
    libxcursor1 \
    libspatialindex-dev \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

# Set python as default
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 && \
    update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1

# Upgrade pip
RUN python3 -m pip install --no-cache-dir --upgrade pip

# Copy CPU-only python deps from L1
COPY --from=foundationpose-base-l1 /usr/local/lib/python3.10/dist-packages /usr/local/lib/python3.10/dist-packages
COPY --from=foundationpose-base-l1 /usr/local/bin /usr/local/bin

# Install PyTorch (CUDA 11.8)
RUN pip install --no-cache-dir torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://download.pytorch.org/whl/cu118

# GPU/torch-dependent deps
RUN pip install --no-cache-dir \
    fvcore==0.1.5.post20221221 \
    torchnet==0.0.4 \
    ultralytics==8.0.120 \
    warp-lang==1.0.2 \
    && pip cache purge

# Build deps required for CUDA extensions
RUN apt-get update && apt-get install -y --no-install-recommends \
    cmake \
    build-essential \
    ninja-build \
    libeigen3-dev \
    python3.10-dev \
    libboost-system-dev \
    libboost-program-options-dev \
    pybind11-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

EXPOSE 7860

# ----- L2 build steps to include all installation work -----
# Install SAM + pytorch3d
RUN pip install --no-cache-dir "numpy<2" transformers==4.41.2 \
    && pip install --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py310_cu118_pyt210/download.html

# Set MAX_JOBS=1 BEFORE any CUDA compilation to limit memory usage
ENV MAX_JOBS=1

# Clone FoundationPose source
RUN git clone --depth 1 https://github.com/NVlabs/FoundationPose.git /app/FoundationPose \
    && cd /app/FoundationPose/bundlesdf/mycuda \
    && sed -i 's/-std=c++14/-std=c++17/g' setup.py

# Build CPU-only C++ code
WORKDIR /app/FoundationPose
RUN cd mycpp && mkdir -p build && cd build && cmake .. && make

# Download model weights (246MB)
WORKDIR /app
COPY download_weights.py ./download_weights.py
RUN python3 download_weights.py

# Install nvdiffrast (CUDA rasterizer) - needs GPU, build here
RUN git clone --depth 1 https://github.com/NVlabs/nvdiffrast.git /tmp/nvdiffrast \
    && cd /tmp/nvdiffrast \
    && python3 setup.py build_ext --inplace
RUN python3 -c "import shutil, sysconfig, glob; from pathlib import Path; site=Path(sysconfig.get_paths()['purelib']); src=Path('/tmp/nvdiffrast/nvdiffrast'); dst=site/'nvdiffrast'; shutil.rmtree(dst, ignore_errors=True); shutil.copytree(src, dst); so_files=(glob.glob('/tmp/nvdiffrast/_nvdiffrast_c*.so') + glob.glob('/tmp/nvdiffrast/nvdiffrast/_nvdiffrast_c*.so') + glob.glob('/tmp/nvdiffrast/build/lib.*/*_nvdiffrast_c*.so')); [shutil.copy2(p, site) for p in so_files]"
RUN python3 -c "import sysconfig; from pathlib import Path; site=Path(sysconfig.get_paths()['purelib']); dist=site/'nvdiffrast-0.0.0.dist-info'; dist.mkdir(exist_ok=True); (dist/'METADATA').write_text('Metadata-Version: 2.1\\nName: nvdiffrast\\nVersion: 0.0.0\\n'); (dist/'WHEEL').write_text('Wheel-Version: 1.0\\nGenerator: manual\\nRoot-Is-Purelib: false\\nTag: py3-none-any\\n'); (dist/'top_level.txt').write_text('nvdiffrast\\n'); (dist/'RECORD').write_text('')"
RUN python3 -c "import nvdiffrast.torch"
RUN rm -rf /tmp/nvdiffrast

# Build CUDA extensions (requires GPU)
WORKDIR /app/FoundationPose
RUN cd bundlesdf/mycuda && pip install . --no-build-isolation