Skip to content

Commit 32bdac8

Browse files
committed
add install script that relies on uv for managing install / dependencies
1 parent f49285a commit 32bdac8

File tree

1 file changed

+337
-0
lines changed

1 file changed

+337
-0
lines changed

scripts/install_uv.sh

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This clones and build model architectures and gets pretrained weights
4+
set -eu
5+
6+
TORCH_VERSION="2.0.0"
7+
TORCHVISION_VERSION="0.15.1"
8+
CUDA_VERSION=""
9+
MODEL="all"
10+
CPU="False"
11+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
12+
MODELS_ROOT_DIR="${SCRIPT_DIR}/.."
13+
DOWNLOAD_WEIGHTS="True"
14+
15+
# Constrain DNNL to avoid AVX512, which leads to non-deterministic operation across different CPUs...
16+
export DNNL_MAX_CPU_ISA=AVX2
17+
18+
#parse args
19+
while [[ $# -gt 0 ]]
20+
do
21+
key="$1"
22+
case $key in
23+
-h|--help)
24+
cat << _EOF_
25+
Installs CompressAI-Vision and its dependencies within a virtual environment.
26+
Before running, create a virtual env, i.e.:
27+
$ python3 -m venv venv
28+
$ source venv/bin/activate
29+
30+
RUN OPTIONS:
31+
[-m|--model, vision models to install, (detectron2/jde/yolox/mmpose/all) default=all]
32+
[-t|--torch torch version, default="2.0.0"]
33+
[--torchvision torchvision version, default="0.15.1"]
34+
[--cpu) build for cpu only)]
35+
[--cuda) provide cuda version e.g. "11.8", default: check nvcc output)]
36+
[--detectron2_url use this if you want to specify a pre-built detectron2 (find at
37+
"https://detectron2.readthedocs.io/en/latest/tutorials/install.html#install-pre-built-detectron2-linux-only"),
38+
not required for regular versions derived from cuda and torch versions above.
39+
default:"https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.9/index.html"]
40+
[--models_dir directory to install vision models to, default: compressai_vision_root]
41+
[--no-weights) prevents the installation script from downloading vision model parameters]
42+
43+
44+
EXAMPLE [bash install_models.sh -m detectron2 -t "1.9.1" --cuda "11.8" --compressai /path/to/compressai]
45+
46+
NOTE: the downlading of JDE pretrained weights might fail. Check that the size of following file is ~558MB.
47+
compressai_vision/weights/jde/jde.1088x608.uncertainty.pt
48+
The file can be downloaded at the following link (in place of the above file path):
49+
"https://docs.google.com/uc?export=download&id=1nlnuYfGNuHWZztQHXwVZSL_FvfE551pA"
50+
_EOF_
51+
exit;
52+
;;
53+
-m|--model) shift; MODEL="$1"; shift; ;;
54+
-t|--torch) shift; TORCH_VERSION="$1"; shift; ;;
55+
--torchvision) shift; TORCHVISION_VERSION="$1"; shift; ;;
56+
--cpu) CPU="True"; shift; ;;
57+
--cuda) shift; CUDA_VERSION="$1"; shift; ;;
58+
--detectron2_url) shift; DETECTRON2="$1"; shift; ;;
59+
--models_dir) shift; MODELS_ROOT_DIR="$1"; shift; ;;
60+
--no-weights) DOWNLOAD_WEIGHTS="False"; shift; ;;
61+
*) echo "[ERROR] Unknown parameter $1"; exit; ;;
62+
esac;
63+
done;
64+
65+
MODELS_SOURCE_DIR=${MODELS_ROOT_DIR}/models
66+
MODELS_WEIGHT_DIR=${MODELS_ROOT_DIR}/weights
67+
68+
mkdir -p ${MODELS_SOURCE_DIR}
69+
mkdir -p ${MODELS_WEIGHT_DIR}
70+
71+
## Make sure we have up-to-date pip and wheel
72+
uv add --upgrade pip wheel
73+
74+
## Detectron2
75+
if [ "${MODEL,,}" == "detectron2" ] || [ ${MODEL} == "all" ]; then
76+
77+
echo
78+
echo "Installing detectron2"
79+
echo
80+
81+
# clone
82+
if [ -z "$(ls -A ${MODELS_SOURCE_DIR}/detectron2)" ]; then
83+
git clone https://github.com/facebookresearch/detectron2.git ${MODELS_SOURCE_DIR}/detectron2
84+
fi
85+
cd ${MODELS_SOURCE_DIR}/detectron2
86+
87+
echo
88+
echo "checkout the version used for MPEG FCM"
89+
echo
90+
git -c advice.detachedHead=false checkout 175b2453c2bc4227b8039118c01494ee75b08136
91+
92+
if [ "${CUDA_VERSION}" == "" ] && [ "${CPU}" == "False" ]; then
93+
CUDA_VERSION=$(nvcc --version | sed -n 's/^.*release \([0-9]\+\.[0-9]\+\).*$/\1/p')
94+
if [ ${CUDA_VERSION} == "" ]; then
95+
echo "error with cuda, check your system, source env_cuda.sh or specify cuda version as argument."
96+
fi
97+
fi
98+
if [ -z "$CUDA_VERSION" ] || [ "$CPU" == "True" ]; then
99+
echo "installing on cpu"
100+
uv pip install torch torchvision torchaudio --default-index https://download.pytorch.org/whl/cpu
101+
wait
102+
else
103+
echo "cuda version: $CUDA_VERSION"
104+
uv pip install torch==${TORCH_VERSION}+cu${CUDA_VERSION//./} torchvision==${TORCHVISION_VERSION}+cu${CUDA_VERSION//./} --extra-index-url https://download.pytorch.org/whl/cu${CUDA_VERSION//./}
105+
wait
106+
fi
107+
108+
# '!' egating set -e when patching has been applied already
109+
! patch -p1 --forward <${SCRIPT_DIR}/0001-detectron2-fpn-bottom-up-separate.patch
110+
111+
uv sync
112+
113+
# back to project root
114+
cd ${SCRIPT_DIR}/..
115+
116+
if [ "${DOWNLOAD_WEIGHTS}" == "True" ]; then
117+
if [ -z "$(ls -A ${MODELS_WEIGHT_DIR}/detectron2)" ]; then
118+
echo
119+
echo "Downloading model weights"
120+
echo
121+
122+
# FASTER R-CNN X-101 32x8d FPN
123+
WEIGHT_DIR="${MODELS_WEIGHT_DIR}/detectron2/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x/139173657"
124+
mkdir -p ${WEIGHT_DIR}
125+
wget -nc https://dl.fbaipublicfiles.com/detectron2/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x/139173657/model_final_68b088.pkl -P ${WEIGHT_DIR}
126+
127+
# FASTER R-CNN R-50 FPN
128+
WEIGHT_DIR="${MODELS_WEIGHT_DIR}/detectron2/COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458"
129+
mkdir -p ${WEIGHT_DIR}
130+
wget -nc https://dl.fbaipublicfiles.com/detectron2/COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_280758.pkl -P ${WEIGHT_DIR}
131+
132+
# MASK R-CNN X-101 32x8d FPN
133+
WEIGHT_DIR="${MODELS_WEIGHT_DIR}/detectron2/COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x/139653917"
134+
mkdir -p ${WEIGHT_DIR}
135+
wget -nc https://dl.fbaipublicfiles.com/detectron2/COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x/139653917/model_final_2d9806.pkl -P ${WEIGHT_DIR}
136+
137+
# MASK R-CNN R-50 FPN
138+
WEIGHT_DIR="${MODELS_WEIGHT_DIR}/detectron2/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600"
139+
mkdir -p ${WEIGHT_DIR}
140+
wget -nc https://dl.fbaipublicfiles.com/detectron2/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl -P ${WEIGHT_DIR}
141+
142+
else
143+
echo
144+
echo "Detectron2 Weights directory not empty, using existing models"
145+
echo
146+
fi
147+
fi
148+
fi
149+
150+
if [ "${MODEL,,}" == "jde" ] || [ ${MODEL} == "all" ]; then
151+
152+
echo
153+
echo "Installing JDE"
154+
echo
155+
156+
157+
# install dependent packages
158+
uv add numpy motmetrics numba lap opencv-python munkres
159+
160+
# install cython manually from source code with patch
161+
if [ -z "$(ls -A ${SCRIPT_DIR}/cython_bbox)" ]; then
162+
git clone https://github.com/samson-wang/cython_bbox.git ${SCRIPT_DIR}/cython_bbox
163+
fi
164+
165+
cd ${SCRIPT_DIR}/cython_bbox
166+
# cython-bbox 0.1.3
167+
git checkout 9badb346a9222c98f828ba45c63fe3b7f2790ea2
168+
169+
# '!' negating set -e when patching has been applied already
170+
! patch -p1 --forward <../0001-compatible-with-numpy-1.24.1.patch
171+
uv pip install -e .
172+
173+
# clone
174+
if [ -z "$(ls -A ${MODELS_SOURCE_DIR}/Towards-Realtime-MOT)" ]; then
175+
git clone https://github.com/Zhongdao/Towards-Realtime-MOT.git ${MODELS_SOURCE_DIR}/Towards-Realtime-MOT
176+
fi
177+
cd ${MODELS_SOURCE_DIR}/Towards-Realtime-MOT
178+
179+
# git checkout
180+
echo
181+
echo "checkout branch compatible with MPEG FCM"
182+
echo
183+
git -c advice.detachedHead=false checkout c2654cdd7b69d39af669cff90758c04436025fe1
184+
185+
# Apply patch to interface with compressai-vision
186+
187+
# '!' negating set -e when patching has been applied already
188+
! patch -p1 --forward <${SCRIPT_DIR}/0001-interface-with-compressai-vision.patch
189+
190+
SITE_PACKAGES=$(python -c "import site; print(site.getsitepackages()[0])")
191+
# COPY JDE files into site-package under virtual environment
192+
if [ "${SITE_PACKAGES}" == "" ]; then
193+
echo "error with python site-packages directory, check your system and 'which python'"
194+
echo "ERROR: Fail to install JDE"
195+
fi
196+
197+
mkdir -p ${SITE_PACKAGES}/jde
198+
cp models.py ${SITE_PACKAGES}/jde
199+
cp -r tracker ${SITE_PACKAGES}/jde/
200+
cp -r utils ${SITE_PACKAGES}/jde/
201+
echo "Complete copying jde files to site-packages @ ${SITE_PACKAGES}/jde"
202+
203+
# back to project root
204+
cd ${SCRIPT_DIR}/..
205+
206+
# download weights
207+
if [ "${DOWNLOAD_WEIGHTS}" == "True" ]; then
208+
# NOTE commmented out for now as downloading via wget is blocked by provider
209+
# if [ -z "$(ls -A ${MODELS_WEIGHT_DIR}/jde)"]; then
210+
211+
# echo
212+
# echo "Downloading weights..."
213+
# echo
214+
215+
# WEIGHT_DIR="${MODELS_WEIGHT_DIR}/jde"
216+
# mkdir -p ${WEIGHT_DIR}
217+
218+
# FILEID='1nlnuYfGNuHWZztQHXwVZSL_FvfE551pA'
219+
# OUTFILE='jde.1088x608.uncertainty.pt'
220+
# wget -nc --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id='${FILEID} -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=${FILEID}" -O ${WEIGHT_DIR}/${OUTFILE} && rm -rf /tmp/cookies.txt
221+
# else
222+
# echo
223+
# echo "JDE Weights directory not empty, using existing model"
224+
# echo
225+
# fi
226+
#
227+
228+
echo
229+
echo "NOTE: JDE pretrained weights can't be downloaded automatically"
230+
echo "The file can be downloaded from the following link:"
231+
echo "https://docs.google.com/uc?export=download&id=1nlnuYfGNuHWZztQHXwVZSL_FvfE551pA"
232+
echo "and placed in the corresponding directory: ${MODELS_WEIGHT_DIR}/jde/jde.1088x608.uncertainty.pt"
233+
echo
234+
fi
235+
fi
236+
237+
if [ "${MODEL,,}" == "yolox" ] || [ ${MODEL} == "all" ]; then
238+
echo
239+
echo "Installing YOLOX (reference: https://github.com/Megvii-BaseDetection/YOLOX)"
240+
echo
241+
242+
# clone
243+
if [ -z "$(ls -A ${MODELS_SOURCE_DIR}/yolox)" ]; then
244+
git clone https://github.com/Megvii-BaseDetection/yolox.git ${MODELS_SOURCE_DIR}/yolox
245+
246+
# checkout specific commit on Nov.19, 2024 for now to avoid compatibility in the future.
247+
cd ${MODELS_SOURCE_DIR}/yolox
248+
git reset --hard d872c71bf63e1906ef7b7bb5a9d7a529c7a59e6a
249+
cd ${SCRIPT_DIR}/..
250+
fi
251+
252+
cd ${MODELS_SOURCE_DIR}/yolox
253+
# miminum requirments - no onnx, etc.
254+
cp ${SCRIPT_DIR}/yolox_requirements.txt requirements.txt
255+
256+
echo "haha1"
257+
uv pip install -e .
258+
echo "haha2"
259+
if [ "${DOWNLOAD_WEIGHTS}" == "True" ]; then
260+
if [ -z "$(ls -A ${MODELS_WEIGHT_DIR}/yolox)" ]; then
261+
echo
262+
echo "Downloading model weights"
263+
echo
264+
265+
# YOLOX-Darkent53
266+
WEIGHT_DIR="${MODELS_WEIGHT_DIR}/yolox/darknet53"
267+
mkdir -p ${WEIGHT_DIR}
268+
wget -nc https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_darknet.pth -P ${WEIGHT_DIR}
269+
else
270+
echo
271+
echo "YOLOX Weights directory is not empty, using existing models"
272+
echo
273+
fi
274+
fi
275+
# back to project root
276+
cd ${SCRIPT_DIR}/..
277+
fi
278+
279+
if [ "${MODEL,,}" == "mmpose" ] || [ ${MODEL} == "all" ]; then
280+
echo
281+
echo "Installing MMPOSE (reference: https://github.com/open-mmlab/mmpose/tree/main)"
282+
echo
283+
284+
uv pip install -U openmim
285+
uv run mim install "mmcv==2.0.1"
286+
287+
# clone
288+
if [ -z "$(ls -A ${MODELS_SOURCE_DIR}/mmpose)" ]; then
289+
git clone https://github.com/open-mmlab/mmpose.git ${MODELS_SOURCE_DIR}/mmpose
290+
291+
# checkout specific commit version to avoid compatibility in the future.
292+
cd ${MODELS_SOURCE_DIR}/mmpose
293+
git reset --hard 71ec36ebd63c475ab589afc817868e749a61491f
294+
cd ${SCRIPT_DIR}/..
295+
fi
296+
297+
cd ${MODELS_SOURCE_DIR}/mmpose
298+
# miminum requirments - no onnx, etc.
299+
uv pip install -r requirements.txt
300+
uv pip install -v -e .
301+
302+
uv run mim install mmdet==3.1.0
303+
304+
# during the installation isort version might be overwritten.
305+
# hence make sure back to the isort=5.13.2
306+
uv run pip install isort==5.13.2
307+
308+
if [ "${DOWNLOAD_WEIGHTS}" == "True" ]; then
309+
if [ -z "$(ls -A ${MODELS_WEIGHT_DIR}/mmpose)" ]; then
310+
echo
311+
echo "Downloading RTMO model weights"
312+
echo
313+
314+
# RTMO: Towards High-Performance One-Stage Real-Time Multi-Person Pose Estimation
315+
WEIGHT_DIR="${MODELS_WEIGHT_DIR}/mmpose/rtmo_coco"
316+
mkdir -p ${WEIGHT_DIR}
317+
wget -nc https://download.openmmlab.com/mmpose/v1/projects/rtmo/rtmo-l_16xb16-600e_coco-640x640-516a421f_20231211.pth -P ${WEIGHT_DIR}
318+
else
319+
echo
320+
echo "MMPOSE-RTMO Weights directory is not empty, using existing models"
321+
echo
322+
fi
323+
fi
324+
# back to project root
325+
cd ${SCRIPT_DIR}/..
326+
fi
327+
328+
echo
329+
echo "Installing compressai"
330+
echo
331+
uv pip install -e "${SCRIPT_DIR}/../compressai"
332+
333+
echo
334+
echo "Installing compressai-vision"
335+
echo
336+
337+
uv pip install -e "${SCRIPT_DIR}/.."

0 commit comments

Comments
 (0)