Skip to content

Commit 1d75d81

Browse files
Tracinyongyang李普fanyunqianzhangqi3
authored
V0.0.6 dev (#49)
* Benchmake update. * Amend doc of vitis * [Feature] Vitis Quantization. * [Update] Update version. Co-authored-by: yongyang <[email protected]> Co-authored-by: 李普 <SENSETIME\[email protected]> Co-authored-by: fanyunqian <[email protected]> Co-authored-by: zhangqi3 <[email protected]>
1 parent ba2b0aa commit 1d75d81

File tree

15 files changed

+643
-328
lines changed

15 files changed

+643
-328
lines changed

docker/Dockerfile

Lines changed: 11 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -1,203 +1,15 @@
1-
FROM nvidia/cuda:10.0-cudnn7-devel-ubuntu18.04
1+
FROM xilinx/vitis-ai:1.4.1.978
22

3-
# Due to bad internet issue
4-
RUN rm /etc/apt/sources.list.d/*
5-
6-
# prepare build dir tmp
7-
RUN chmod 1777 /tmp \
8-
&& mkdir /scratch \
9-
&& chmod 1777 /scratch
10-
11-
# install build needed tools
12-
RUN apt-get update -y \
13-
&& DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata \
14-
&& apt-get install -y --no-install-recommends \
15-
software-properties-common \
16-
apt-transport-https \
17-
autoconf \
18-
automake \
19-
bc \
20-
build-essential \
21-
bzip2 \
22-
ca-certificates \
23-
curl \
24-
g++ \
25-
gdb \
26-
git \
27-
gnupg \
28-
locales \
29-
libboost-all-dev \
30-
libgflags-dev \
31-
libgoogle-glog-dev \
32-
libgtest-dev \
33-
libjson-c-dev \
34-
libjsoncpp-dev \
35-
libssl-dev \
36-
libtool \
37-
libunwind-dev \
38-
make \
39-
openssh-client \
40-
openssl \
41-
# python3 \
42-
python3-dev \
43-
# python3-minimal \
44-
# python3-numpy \
45-
# python3-opencv \
46-
python3-pip \
47-
# python3-setuptools \
48-
# python3-venv \
49-
software-properties-common \
50-
sudo \
51-
tree \
52-
unzip \
53-
vim \
54-
wget \
55-
yasm \
56-
zstd \
57-
libtool
58-
59-
# set python
60-
ENV PYTHONPATH=/opt/python:$PYTHONPATH
61-
ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
62-
63-
ENV PYENV_ROOT=/opt/pyenv \
64-
PATH=/opt/pyenv/shims:/opt/pyenv/bin:$PATH
65-
66-
RUN git clone --depth 1 https://github.com/pyenv/pyenv.git /opt/pyenv \
67-
&& eval "$(pyenv init -)" \
68-
&& env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.6.4 \
69-
&& pyenv global 3.6.4
70-
71-
RUN pip install --upgrade pip
72-
RUN pip install torch==1.8.1 torchvision flake8 scipy pytest
73-
74-
75-
# RUN mkdir /opt/hub && wget https://github.com/pytorch/vision/archive/master.zip -P /opt/hub && unzip /opt/hub/master.zip -d /opt/hub && mv /opt/hub/vision-master/ /opt/hub/pytorch_vision_master
76-
ENV TORCH_HOME=/opt/
77-
78-
RUN apt-get install -y \
79-
libavcodec-dev \
80-
libavformat-dev \
81-
libeigen3-dev \
82-
libgstreamer-plugins-base1.0-dev \
83-
libgstreamer1.0-dev \
84-
libgtest-dev \
85-
libgtk-3-dev \
86-
libgtk2.0-dev \
87-
libhdf5-dev \
88-
libjpeg-dev \
89-
libopenexr-dev \
90-
libpng-dev \
91-
libswscale-dev \
92-
libtiff-dev \
93-
libwebp-dev \
94-
# opencl-clhpp-headers \
95-
# opencl-headers \
96-
# pocl-opencl-icd \
97-
rpm \
98-
&& add-apt-repository -y ppa:ubuntu-toolchain-r/test \
99-
&& apt-get install -y \
100-
gcc-8 \
101-
g++-8 \
102-
gcc-9 \
103-
g++-9 \
104-
&& wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null \
105-
&& apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main' \
106-
&& apt-get update -y \
107-
&& apt-get install -y \
108-
cmake=3.16.0-0kitware1 \
109-
cmake-data=3.16.0-0kitware1 \
110-
kitware-archive-keyring \
111-
&& apt-get install -y ffmpeg
112-
113-
# set locales
114-
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen \
115-
&& echo "LC_ALL=en_US.UTF-8" >> /etc/environment \
116-
&& echo "LANG=en_US.UTF-8" > /etc/locale.conf \
117-
&& locale-gen en_US.UTF-8 \
118-
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 \
119-
&& apt-get install -y dialog \
120-
&& dpkg-reconfigure --frontend noninteractive locales
121-
122-
123-
# build gtest
124-
RUN cd /usr/src/gtest \
125-
&& mkdir -p build \
126-
&& cd build \
127-
&& cmake .. \
128-
&& make \
129-
&& make install
130-
131-
# set glob
132-
RUN cd /tmp \
133-
&& wget --progress=dot:mega -O glog.0.4.0.tar.gz https://codeload.github.com/google/glog/tar.gz/v0.4.0 \
134-
&& tar -xvf glog.0.4.0.tar.gz \
135-
&& cd glog-0.4.0 \
136-
&& ./autogen.sh \
137-
&& mkdir build \
138-
&& cd build \
139-
&& cmake -DBUILD_SHARED_LIBS=ON .. \
140-
&& make -j 4 \
141-
&& make install \
142-
&& rm -fr /tmp/*
143-
144-
145-
# set protobuf
146-
RUN /tmp; wget --progress=dot:mega https://codeload.github.com/google/protobuf/zip/v3.4.0 \
147-
&& unzip v3.4.0 \
148-
&& cd protobuf-3.4.0 \
149-
&& ./autogen.sh \
150-
&& ./configure \
151-
&& make -j 4 \
152-
&& make install \
153-
&& ldconfig \
154-
&& rm -fr /tmp/*
155-
156-
# set gflag
157-
RUN cd /tmp; wget --progress=dot:mega https://github.com/gflags/gflags/archive/v2.2.2.tar.gz \
158-
&& tar xvf v2.2.2.tar.gz \
159-
&& cd gflags-2.2.2 \
160-
&& mkdir build \
161-
&& cd build \
162-
&& cmake -DBUILD_SHARED_LIBS=ON .. \
163-
&& make -j 4 \
164-
&& make install \
165-
&& rm -fr /tmp/*
166-
167-
# set pybind
168-
RUN cd /tmp; git clone https://github.com/pybind/pybind11.git \
169-
&& cd pybind11 \
170-
&& git checkout v2.5.0 \
171-
&& mkdir build \
172-
&& cd build \
173-
&& cmake -DPYBIND11_TEST=OFF .. \
174-
&& make \
175-
&& make install \
176-
&& rm -fr /tmp/* \
177-
&& chmod 777 /usr/lib/python3/dist-packages
178-
179-
# set xir
180-
RUN cd /tmp \
181-
&& wget -O libunilog.deb https://www.xilinx.com/bin/public/openDownload?filename=libunilog_1.4.1-r82_amd64.deb \
182-
&& wget -O libtarget-factory.deb https://www.xilinx.com/bin/public/openDownload?filename=libtarget-factory_1.4.1-r85_amd64.deb \
183-
&& wget -O libxir.deb https://www.xilinx.com/bin/public/openDownload?filename=libxir_1.4.1-r91_amd64.deb \
184-
&& wget -O libvart.deb https://www.xilinx.com/bin/public/openDownload?filename=libvart_1.4.1-r130_amd64.deb \
185-
&& wget -O libvitis_ai_library.deb https://www.xilinx.com/bin/public/openDownload?filename=libvitis_ai_library_1.4.1-r114_amd64.deb \
186-
&& wget -O librt-engine.deb https://www.xilinx.com/bin/public/openDownload?filename=librt-engine_1.4.1-r195_amd64.deb \
187-
&& wget -O aks.deb https://www.xilinx.com/bin/public/openDownload?filename=aks_1.4.1-r78_amd64.deb \
188-
&& apt-get install -y --no-install-recommends /tmp/*.deb \
189-
&& rm -rf /tmp/* \
190-
&& ldconfig
191-
192-
# install mqbench requirement
193-
RUN pip install urllib3 onnx
194-
195-
# set xir path
196-
ENV PYTHONPATH=/usr/lib/python3/dist-packages/:$PYTHONPATH
197-
198-
# install mqbench
199-
RUN cd /root/ \
3+
RUN cd /home \
2004
&& git clone https://github.com/ModelTC/MQBench.git \
2015
&& cd MQBench \
202-
&& python setup.py develop
6+
&& sed '1,2d' requirements.txt > requirements.txt
7+
8+
RUN source /opt/vitis_ai/conda/etc/profile.d/conda.sh \
9+
&& source /home/vitis-ai-user/.bashrc \
10+
&& conda activate vitis-ai-pytorch \
11+
&& cd /home/MQBench \
12+
&& pip install onnx -i https://pypi.tuna.tsinghua.edu.cn/simple \
13+
&& pip install .
20314

15+
RUN echo "conda activate vitis-ai-pytorch" >> /home/vitis-ai-user/.bashrc
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
Object Detection Benchmark
22
==========================
33

4-
To be finished.
4+
Based on Mqbench and `EOD <https://github.com/ModelTC/EOD>`_ , we provide an object detection benchmark on COCO dataset.
5+
We test the two most popular object detection methods, retinanet and yolox on several Backends.
6+
7+
+----------+---------------+----------------+----------------+----------------+------+------+-------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------+
8+
| Backend | w_calibration | a_calibration | w_fakequantize | a_fakequantize | wbit | abit | `retinanet <https://github.com/ModelTC/EOD/blob/main/configs/det/retinanet/retinanet-r18-improve.yaml>`_ | `yolox <https://github.com/ModelTC/EOD/blob/main/configs/det/retinanet/yolox_s_ret_a1_comloc.yaml>`_ |
9+
+ + + + + + + +-------------------------+----------------------+------------------------------------------------------------+-------------------------+-------------------+--------------------------------------------------------------+
10+
| | | | | | | | pretrained@float32 | calibration@int8 | qat@int8 | pretrained@float32 | calibration@int8 | qat@int8 |
11+
+==========+===============+================+================+================+======+======+=========================+======================+============================================================+=========================+===================+==============================================================+
12+
| tensorrt | MinMax | EMAMinMax | Fixed | Fixed | 8 | 8 | 40.7 | 40.5 | 40.7 | 40.5 | 39.4 | 39.8 |
13+
+----------+---------------+----------------+----------------+----------------+------+------+-------------------------+----------------------+------------------------------------------------------------+-------------------------+-------------------+--------------------------------------------------------------+
14+
| snpe | MinMax | EMAMinMax | Fixed | Fixed | 8 | 8 | 40.7 | 39.7 | 40.2 | 40.5 | 38.1 | 39.8 |
15+
+----------+---------------+----------------+----------------+----------------+------+------+-------------------------+----------------------+------------------------------------------------------------+-------------------------+-------------------+--------------------------------------------------------------+
16+
| vitis | MinMaxFloor | ModeMinMaxFloor| Tqt | Tqt | 8 | 8 | 40.7 | 39.0 | 40.1 | 29.3* | 25.3 | 27.4 |
17+
+----------+---------------+----------------+----------------+----------------+------+------+-------------------------+----------------------+------------------------------------------------------------+-------------------------+-------------------+--------------------------------------------------------------+
18+
19+
Note*: We provide an simplified model of yolox to meet vitis operator support, which is winner solution for the Low Power Computer Vision Challenge 2021 (`LPCV2021 <https://github.com/ModelTC/LPCV2021_Winner_Solution>`_). The model is `yolox-lpcv <https://github.com/ModelTC/EOD/blob/main/configs/det/yolox/yolox_fpga.yaml>`_.
20+
21+
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Object detection with Mqbench
2+
================================
3+
This part, we introduce how to quantize an object detection model using mqbench.
4+
5+
Getting Started
6+
-----------------
7+
8+
**1**. **Clone the repositories.**
9+
10+
.. code-block:: python
11+
12+
git clone https://github.com/ModelTC/MQBench.git
13+
git clone https://github.com/ModelTC/EOD.git
14+
15+
16+
**2**. **Quantization aware training.**
17+
18+
.. code-block:: python
19+
20+
# Prepare your float pretrained model.
21+
cd eod/scripts
22+
# Follow the prompts to set config in train_quant.sh.
23+
sh train_quant.sh
24+
25+
26+
**We have several examples of qat config in EOD repository:**
27+
28+
For retinanet-tensorrt:
29+
- float pretrained config file: configs/det/retinanet/retinanet-r18-improve.yaml
30+
- qat config file: configs/det/retinanet/retinanet-r18-improve_quant_trt.yaml
31+
32+
For yolox-tensorrt:
33+
- float pretrained config file: configs/det/retinanet/yolox_s_ret_a1_comloc.yaml
34+
- qat config file: configs/det/retinanet/yolox_s_ret_a1_comloc_quant_trt.yaml
35+
36+
For yolox-vitis:
37+
- float pretrained config file: configs/det/yolox/yolox_fpga.yaml
38+
- qat config file: configs/det/yolox/yolox_fpga_quant_vitis.yaml
39+
40+
41+
**Something import in config file:**
42+
43+
- deploy_backend: Choose your deploy backend supported in mqbench.
44+
- ptq_only: If True, only ptq will be executed. If False, qat will be executed after ptq calibration.
45+
- extra_qconfig_dict: Choose your quantization config supported in mqbench.
46+
- leaf_module: Prevent torch.fx tool entering the module.
47+
- extra_quantizer_dict: Add some qat modules.
48+
- resume_model: The path to your float pretrained model.
49+
- tocaffe_friendly: It is recommended to set it to true, which will affect the output onnx model.
50+
51+
52+
**3**. **Resume training during qat.**
53+
54+
.. code-block:: python
55+
56+
cd eod/scripts
57+
# just set resume_model in config file to your model, we will do all the rest.
58+
sh train_quant.sh
59+
60+
61+
**4**. **Evaluate your quantized model.**
62+
63+
.. code-block:: python
64+
65+
cd eod/scripts
66+
# set resume_model in config file to your model
67+
# add -e to train_quant.sh
68+
sh train_quant.sh
69+
70+
71+
**5**. **Deploy.**
72+
73+
.. code-block:: python
74+
75+
cd eod/scripts
76+
# Follow the prompts to set config in quant_deploy.sh.
77+
sh quant_deploy.sh
78+
79+
80+
81+
Introduction of EOD-Mqbench Project
82+
----------------------------------------
83+
84+
The training codes start in eod/commands/train.py. The delpoy codes start in eod/commands/quant_deploy.py.
85+
86+
When you set the runner type to quant in config file, QuantRunner will be executed in eod/runner/quant_runner.py.
87+
88+
1. Firstly, build your float model in self.build_model().
89+
2. Load your float pretrained model/quantized model in self.load_ckpt().
90+
3. Use torch.fx to trace your model in self.quantize_model().
91+
4. Set your optimization and lr scheduler in self.build_trainer().
92+
5. Ptq and eval in self.calibrate()
93+
6. Train in self.train()
94+
95+
96+
**Something important:**
97+
98+
- Your model should be splited into network and post-processing. Fx should only trace the network.
99+
- Quantized model should be saved with the key of qat, as shown in self.save(). This will be used in self.resume_model_from_fp() and self.resume_model_from_quant().
100+
- We disable the ema in qat. If your ckpt has ema state, we will load ema state into model, as shown in self.load_ckpt().
101+
- Be careful when your quantized model has extra learnable parameters. You can check it in optimizer, such as eod/tasks/det/plugins/yolov5/utils/optimizer_helper.py. Lsq has been checked.
102+
- When you are going to deploy model, self.model.deploy should be set to True, as shown in eod/apis/quant_deploy.py. This will remove redundant nodes in your model.
103+
104+
105+
106+
107+
108+
109+
110+
111+

0 commit comments

Comments
 (0)