Skip to content

Commit

Permalink
add HRNet
Browse files Browse the repository at this point in the history
  • Loading branch information
xuewen committed Nov 9, 2020
0 parents commit 95c672e
Show file tree
Hide file tree
Showing 73 changed files with 23,669 additions and 0 deletions.
95 changes: 95 additions & 0 deletions HRNet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# IntelliJ project files
.idea
*.iml
out
gen

### Vim template
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist
*~

### IPythonNotebook template
# Temporary data
.ipynb_checkpoints/

### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
#lib/
#lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

*.ipynb
*.params
*.json
.vscode/

lib/pycocotools/_mask.c
lib/nms/cpu_nms.c

output/*
models/*
log/*
data/*
external/

draws/
plot/

1 change: 1 addition & 0 deletions HRNet/_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
107 changes: 107 additions & 0 deletions HRNet/coco_annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 转化成适合的格式便于测试mAP准确率
import os
import json
import numpy as np
import ipdb;pdb=ipdb.set_trace
import shutil
from argparse import ArgumentParser

np.random.seed(10101)
def find_items(images, anns):
lists = []
for img in images:
image_id = img['id']
for ann in anns:
if image_id == ann['image_id']:
lists.append(ann)
return lists

if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("-p", "--ants_path", help="input file path", default="./data/coco/annotations/person_keypoints_val2017.json") # 通过-p指定路径,标注文件名
args = parser.parse_args()
ants_path = args.ants_path
'''
with open("../shape/annotations/instances_train.json", "rt") as f:
data = json.loads(f.read())
print(data.keys())
'''
with open(ants_path, "rt") as f:
data = eval(f.read())
print('data.keys() = ', data.keys())
print('info = ', data['info'])
print('licenses num = ', len(data['licenses']))
# for i in range(len(data['licenses'])):
# print('licenses = ', data['licenses'][i])
print('images num = ', len(data['images']))
print('images = ', data['images'][0])

print('annotations num = ', len(data['annotations']))
print('annotations = ', data['annotations'][0])

print('categories num = ', len(data['categories']))
print('categories = ', data['categories'][0])

# data = data['annotations']
# anns = []
# id_num = 0
# image_id_num = 0
# images = []
# for dd in data:
# for d in dd[1:]:
# x1, y1, x2, y2 = d[:4]
# w, h = x2-x1, y2-y1
# bbox = [x1, y1, w, h]
# area = w*h
# image_name = dd[0]
# image_id = image_id_num
# id = id_num
# id_num += 1
# item = {"id": id, "image_id": image_id, "category_id": 1, "iscrowd": 0, 'area': area, "bbox": bbox}
# anns.append(item)
# img = {"id": image_id, "file_name": image_name}
# images.append(img)
# image_id_num += 1

# np.random.shuffle(images)
# len_val = int(len(images)*0.1)

# val_imgs = images[: len_val]
# val_anns = find_items(val_imgs, anns)

# train_imgs = images[len_val : ]
# train_anns = find_items(train_imgs, anns)

# images_path = "images/"
# train_path = "../pig/train/"
# if not os.path.exists(train_path):
# os.makedirs(train_path)
# val_path = "../pig/val/"
# if not os.path.exists(val_path):
# os.makedirs(val_path)
# anns_path = "../pig/annotations/"
# if not os.path.exists(anns_path):
# os.makedirs(anns_path)
# for img in val_imgs:
# image_name = img['file_name']
# file_image = images_path + image_name
# shutil.copy(file_image, val_path)
# for img in train_imgs:
# image_name = img['file_name']
# file_image = images_path + image_name
# shutil.copy(file_image, train_path)

# with open(anns_path + 'instances_val.json', 'wt') as f:
# val_data = {"categories": [{"id": 1, "name": "pig", "supercategory": "None"}],
# "images": val_imgs,
# "annotations": val_anns}
# f.write(json.dumps(val_data))

# with open(anns_path + 'instances_train.json', 'wt') as f:
# train_data = {"categories": [{"id": 1, "name": "pig", "supercategory": "None"}],
# "images": train_imgs,
# "annotations": train_anns}
# f.write(json.dumps(train_data))
3 changes: 3 additions & 0 deletions HRNet/demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output
models
videos
112 changes: 112 additions & 0 deletions HRNet/demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
FROM nvidia/cuda:10.2-cudnn7-devel-ubuntu16.04

ENV OPENCV_VERSION="3.4.6"

# Basic toolchain
RUN apt-get update && apt-get install -y \
apt-utils \
build-essential \
git \
wget \
unzip \
yasm \
pkg-config \
libcurl4-openssl-dev \
zlib1g-dev \
htop \
cmake \
nano \
python3-pip \
python3-dev \
python3-tk \
libx264-dev \
&& cd /usr/local/bin \
&& ln -s /usr/bin/python3 python \
&& pip3 install --upgrade pip \
&& apt-get autoremove -y

# Getting OpenCV dependencies available with apt
RUN apt-get update && apt-get install -y \
libeigen3-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libjasper-dev \
libswscale-dev \
libavcodec-dev \
libavformat-dev && \
apt-get autoremove -y

# Getting other dependencies
RUN apt-get update && apt-get install -y \
cppcheck \
graphviz \
doxygen \
p7zip-full \
libdlib18 \
libdlib-dev && \
apt-get autoremove -y


# Install OpenCV + OpenCV contrib (takes forever)
RUN mkdir -p /tmp && \
cd /tmp && \
wget --no-check-certificate -O opencv.zip https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip && \
wget --no-check-certificate -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip && \
unzip opencv.zip && \
unzip opencv_contrib.zip && \
mkdir opencv-${OPENCV_VERSION}/build && \
cd opencv-${OPENCV_VERSION}/build && \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_FFMPEG=ON \
-D WITH_OPENCL=ON \
-D WITH_V4L=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib-${OPENCV_VERSION}/modules \
.. && \
make -j$(nproc) && \
make install && \
echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf && \
ldconfig && \
cd /tmp && \
rm -rf opencv-${OPENCV_VERSION} opencv.zip opencv_contrib-${OPENCV_VERSION} opencv_contrib.zip && \
cd /

# Compile and install ffmpeg from source
RUN git clone https://github.com/FFmpeg/FFmpeg /root/ffmpeg && \
cd /root/ffmpeg && \
./configure --enable-gpl --enable-libx264 --enable-nonfree --disable-shared --extra-cflags=-I/usr/local/include && \
make -j8 && make install -j8

# clone deep-high-resolution-net
ARG POSE_ROOT=/pose_root
RUN git clone https://github.com/leoxiaobin/deep-high-resolution-net.pytorch.git $POSE_ROOT
WORKDIR $POSE_ROOT
RUN mkdir output && mkdir log

RUN pip3 install -r requirements.txt && \
pip3 install torch==1.1.0 \
torchvision==0.3.0 \
opencv-python \
pillow==6.2.1

# build deep-high-resolution-net lib
WORKDIR $POSE_ROOT/lib
RUN make

# install COCO API
ARG COCOAPI=/cocoapi
RUN git clone https://github.com/cocodataset/cocoapi.git $COCOAPI
WORKDIR $COCOAPI/PythonAPI
# Install into global site-packages
RUN make install

# download fastrrnn pretrained model for person detection
RUN python -c "import torchvision; model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True); model.eval()"

COPY inference.py $POSE_ROOT/tools
COPY inference-config.yaml $POSE_ROOT/
37 changes: 37 additions & 0 deletions HRNet/demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Inference hrnet

Inferencing the deep-high-resolution-net.pytoch without using Docker.

## Prep
1. Download the researchers' pretrained pose estimator from [google drive](https://drive.google.com/drive/folders/1hOTihvbyIxsm5ygDpbUuJ7O_tzv4oXjC?usp=sharing) to this directory under `models/`
2. Put the video file you'd like to infer on in this directory under `videos`
3. build the docker container in this directory with `./build-docker.sh` (this can take time because it involves compiling opencv)
4. update the `inference-config.yaml` file to reflect the number of GPUs you have available

## Running the Model
```
python inference.py --cfg inference-config.yaml \
--videoFile ../../multi_people.mp4 \
--writeBoxFrames \
--outputDir output \
TEST.MODEL_FILE ../models/pytorch/pose_coco/pose_hrnet_w32_256x192.pth
```

The above command will create a video under *output* directory and a lot of pose image under *output/pose* directory.
Even with usage of GPU (GTX1080 in my case), the person detection will take nearly **0.06 sec**, the person pose match will
take nearly **0.07 sec**. In total. inference time per frame will be **0.13 sec**, nearly 10fps. So if you prefer a real-time (fps >= 20)
pose estimation then you should try other approach.

## Result

Some output image is as:

![1 person](inference_1.jpg)
Fig: 1 person inference

![3 person](inference_3.jpg)
Fig: 3 person inference

![3 person](inference_5.jpg)
Fig: 3 person inference
1 change: 1 addition & 0 deletions HRNet/demo/build-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker build -t hrnet_demo_inference .
Binary file added HRNet/demo/hrnet-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 95c672e

Please sign in to comment.