Skip to content

Commit

Permalink
Merge pull request #797 from exadel-inc/cuda-11.x
Browse files Browse the repository at this point in the history
Cuda 11.x
  • Loading branch information
pospielov authored Jun 30, 2022
2 parents ae4ad03 + dd5e6a7 commit c742aa3
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 93 deletions.
4 changes: 2 additions & 2 deletions embedding-calculator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM ${BASE_IMAGE:-python:3.7-slim}

RUN apt-get update && apt-get install -y build-essential cmake git wget unzip \
curl yasm pkg-config libswscale-dev libtbb2 libtbb-dev libjpeg-dev \
libpng-dev libtiff-dev libavformat-dev libpq-dev libfreeimage3 \
libpng-dev libtiff-dev libavformat-dev libpq-dev libfreeimage3 python3-opencv \
&& rm -rf /var/lib/apt/lists/*

# install common python packages
Expand Down Expand Up @@ -49,4 +49,4 @@ EXPOSE 3000
COPY uwsgi.ini .
ENV UWSGI_PROCESSES=${UWSGI_PROCESSES:-2}
ENV UWSGI_THREADS=1
CMD ["uwsgi", "--ini", "uwsgi.ini"]
CMD ["uwsgi", "--ini", "uwsgi.ini"]
4 changes: 3 additions & 1 deletion embedding-calculator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ SHELL := /bin/bash
.DEFAULT_GOAL := default
.PHONY := default up build-images build-cuda

VERSION := 1.0.1
IMAGE := ${DOCKER_REGISTRY}compreface-core
CUDA_IMAGE = $(IMAGE)-base:base-cuda100-py37
CUDA_IMAGE = $(IMAGE)-base:base-cuda112-py37
APPERY_ARG := --build-arg APPERY_API_KEY=${APPERY_API_KEY}

MOBILENET_BUILD_ARGS := --build-arg FACE_DETECTION_PLUGIN=insightface.FaceDetector@retinaface_mnet025_v1 \
Expand Down Expand Up @@ -70,3 +71,4 @@ tools/tmp:
tools/benchmark_detection/tmp:
$(call get_from_remote_tgz,http://tamaraberg.com/faceDataset/originalPics.tar.gz,tools/benchmark_detection/tmp/originalPics)
$(call get_from_remote_tgz,http://vis-www.cs.umass.edu/fddb/FDDB-folds.tgz,tools/benchmark_detection/tmp)

11 changes: 9 additions & 2 deletions embedding-calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,14 @@ There are two build arguments for optimization:
* `INTEL_OPTIMIZATION` - enable Intel MKL optimization (true/false)


##### NVIDIA Runtime
##### GPU Setup (Windows):
1. Install or update Docker Desktop.
2. Make sure that you have Windows version 21H2 or higher.
3. Update your NVIDIA drivers.
4. Install or update WSL2 Linux kernel.
5. Make sure the WSL2 backend is enabled in Docker Desktop.

##### GPU Setup (Linux):

Install the nvidia-docker2 package and dependencies on the host machine:
```
Expand All @@ -171,7 +178,7 @@ sudo apt-get install -y nvidia-docker2
sudo systemctl restart docker
```

Build and run with enabled gpu
##### Build and run with enabled gpu
```
docker build . -t embedding-calculator-cuda -f gpu.Dockerfile
docker build . -t embedding-calculator-gpu --build-arg GPU_IDX=0 --build-arg BASE_IMAGE=embedding-calculator-cuda
Expand Down
69 changes: 13 additions & 56 deletions embedding-calculator/gpu.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,70 +1,27 @@
ARG UBUNTU_VERSION=18.04
ARG BASE_IMAGE
FROM ${BASE_IMAGE:-nvidia/cuda:11.2.2-cudnn8-runtime-ubuntu20.04}

ARG ARCH=
ARG CUDA=10.0
FROM nvidia/cuda${ARCH:+-$ARCH}:${CUDA}-base-ubuntu${UBUNTU_VERSION} as base
# ARCH and CUDA are specified again because the FROM directive resets ARGs
# (but their default value is retained if set previously)
ARG ARCH
ARG CUDA
ARG CUDNN=7.6.4.38-1
ARG CUDNN_MAJOR_VERSION=7
ARG LIB_DIR_PREFIX=x86_64
ARG LIBNVINFER=6.0.1-1
ARG LIBNVINFER_MAJOR_VERSION=6
ENV CUDA=$CUDA
ENV DEBIAN_FRONTEND=noninteractive
ENV CUDA=11.2

# Needed for string substitution
SHELL ["/bin/bash", "-c"]
# Pick up some TF dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
cuda-command-line-tools-${CUDA/./-} \
# There appears to be a regression in libcublas10=10.2.2.89-1 which
# prevents cublas from initializing in TF. See
# https://github.com/tensorflow/tensorflow/issues/9489#issuecomment-562394257
libcublas10=10.2.1.243-1 \
cuda-nvrtc-${CUDA/./-} \
cuda-cufft-${CUDA/./-} \
cuda-curand-${CUDA/./-} \
cuda-cusolver-${CUDA/./-} \
cuda-cusparse-${CUDA/./-} \
curl \
libcudnn7=${CUDNN}+cuda${CUDA} \
libfreetype6-dev \
libhdf5-serial-dev \
libzmq3-dev \
pkg-config \
software-properties-common \
unzip

# Install TensorRT if not building for PowerPC
RUN [[ "${ARCH}" = "ppc64le" ]] || { apt-get update && \
apt-get install -y --no-install-recommends libnvinfer${LIBNVINFER_MAJOR_VERSION}=${LIBNVINFER}+cuda${CUDA} \
libnvinfer-plugin${LIBNVINFER_MAJOR_VERSION}=${LIBNVINFER}+cuda${CUDA} \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*; }

# For CUDA profiling, TensorFlow requires CUPTI.
ENV LD_LIBRARY_PATH /usr/local/cuda/extras/CUPTI/lib64:/usr/local/cuda/lib64:$LD_LIBRARY_PATH

# Link the libcuda stub to the location where tensorflow is searching for it and reconfigure
# dynamic linker run-time bindings
RUN ln -s /usr/local/cuda/lib64/stubs/libcuda.so /usr/local/cuda/lib64/stubs/libcuda.so.1 \
&& echo "/usr/local/cuda/lib64/stubs" > /etc/ld.so.conf.d/z-cuda-stubs.conf \
&& ldconfig
curl \
pkg-config \
unzip \
python3-dev \
python3-distutils \
&& rm -rf /var/lib/apt/lists/*

# See http://bugs.python.org/issue19846
ENV LANG C.UTF-8

ARG PYTHON=python3.7
RUN add-apt-repository ppa:deadsnakes/ppa && apt-get update && apt-get install -y ${PYTHON} libpython3.7-dev libgl1-mesa-glx
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && ${PYTHON} get-pip.py
RUN ${PYTHON} -m pip --no-cache-dir install --upgrade pip setuptools
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py
RUN python3 -m pip --no-cache-dir install --upgrade pip setuptools

# Some TF tools expect a "python" binary
RUN ln -s $(which $PYTHON) /usr/local/bin/python

RUN ln -s $(which python3) /usr/local/bin/python

# Variables for Tensorflow
ENV TF_FORCE_GPU_ALLOW_GROWTH=true
Expand Down
2 changes: 1 addition & 1 deletion embedding-calculator/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ flasgger==0.9.5
Flask==1.1.2
itsdangerous==2.0.1 # itsdangerous is a Flask dependency, update it with Flask
jinja2<3.1.0 # jinja2 is a Flask dependency, update it with Flask
gdown~=4.3.1
gdown~=4.3.0
Werkzeug==1.0.1
PyYAML==5.4.1

Expand Down
4 changes: 3 additions & 1 deletion embedding-calculator/src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ENV(Constants):
GPU_IDX = int(get_env('GPU_IDX', '-1'))
INTEL_OPTIMIZATION = get_env_bool('INTEL_OPTIMIZATION')

RUN_MODE = get_env_bool('RUN_MODE', False)


LOGGING_LEVEL = logging._nameToLevel[ENV.LOGGING_LEVEL_NAME]
ENV_MAIN = ENV
ENV_MAIN = ENV
4 changes: 3 additions & 1 deletion embedding-calculator/src/init_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from PIL import ImageFile

from src._logging import init_logging
from src.constants import ENV


def _check_ci_build_args():
Expand All @@ -33,4 +34,5 @@ def init_runtime(logging_level):
assert sys.version_info >= (3, 7)
ImageFile.LOAD_TRUNCATED_IMAGES = True
_check_ci_build_args()
init_logging(logging_level)
ENV.RUN_MODE = True
init_logging(logging_level)
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@ def get_mxnet() -> Tuple[str, ...]:
mxnet_lib = 'mxnet-'
if ENV.GPU_IDX > -1 and cuda_version:
mxnet_lib += f"cu{cuda_version}"
if ENV.INTEL_OPTIMIZATION:
mxnet_lib += 'mkl'
mxnet_lib = mxnet_lib.rstrip('-')
return (f'{mxnet_lib}<1.7',)
return (f'{mxnet_lib}==1.9.0',)
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

from src.services.facescan.plugins.dependencies import get_tensorflow

requirements = get_tensorflow() # + ('mtcnn~=0.1.0',)
requirements = ('protobuf~=3.20.1',) + get_tensorflow() # + ('mtcnn~=0.1.0',)
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
from cached_property import cached_property

import numpy as np
import mxnet as mx
from mxnet.gluon.model_zoo import vision
from mxnet.gluon.data.vision import transforms

from src.services.dto import plugin_result
from src.services.imgtools.types import Array3D
from src.services.facescan.plugins import base
from src.services.facescan.plugins.insightface.insightface import InsightFaceMixin
from src.constants import ENV

if ENV.RUN_MODE:
import mxnet as mx
from mxnet.gluon.model_zoo import vision
from mxnet.gluon.data.vision import transforms


class MaskDetector(InsightFaceMixin, base.BasePlugin):
Expand All @@ -35,11 +38,12 @@ class MaskDetector(InsightFaceMixin, base.BasePlugin):
('mobilenet_v2_on_mafa_kaggle123', '1DYUIroNXkuYKQypYtCxQvAItLnrTTt5E'),
('resnet18_on_mafa_kaggle123', '1A3fNrvgrJqMw54cWRj47LNFNnFvTjmdj')
)
img_transforms = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
if ENV.RUN_MODE:
img_transforms = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])

@property
def input_image_size(self) -> Tuple[int, int]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
# permissions and limitations under the License.

from typing import Tuple
import mxnet as mx
import numpy as np
import cv2
from skimage import transform as trans

from src.constants import ENV


if ENV.RUN_MODE:
import mxnet as mx


def predict_landmark2d106(model, img,
crop_size: Tuple[int, int],
Expand Down Expand Up @@ -66,4 +71,4 @@ def trans_points2d(pts, M):
new_pt = np.dot(M, new_pt)
#print('new_pt', new_pt.shape, new_pt)
new_pts[i] = new_pt[0:2]
return new_pts
return new_pts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
from typing import List, Tuple
import attr
import numpy as np
import mxnet as mx
from cached_property import cached_property
from insightface.app import FaceAnalysis
from insightface.model_zoo import (model_store, face_detection,
face_recognition, face_genderage)
from insightface.utils import face_align

from src.constants import ENV
from src.services.dto.bounding_box import BoundingBoxDTO
Expand All @@ -37,6 +32,22 @@
logger = logging.getLogger(__name__)


if ENV.RUN_MODE:
import mxnet as mx

from insightface.app import FaceAnalysis
from insightface.model_zoo import (model_store, face_detection,
face_recognition, face_genderage)
from insightface.utils import face_align

class DetectionOnlyFaceAnalysis(FaceAnalysis):
rec_model = None
ga_model = None

def __init__(self, file):
self.det_model = face_detection.FaceDetector(file, 'net3')


class InsightFaceMixin:
_CTX_ID = ENV.GPU_IDX
_NMS = 0.4
Expand All @@ -48,14 +59,6 @@ def get_model_file(self, ml_model: base.MLModel):
return model_store.find_params_file(ml_model.path)


class DetectionOnlyFaceAnalysis(FaceAnalysis):
rec_model = None
ga_model = None

def __init__(self, file):
self.det_model = face_detection.FaceDetector(file, 'net3')


class FaceDetector(InsightFaceMixin, mixins.FaceDetectorMixin, base.BasePlugin):
ml_models = (
('retinaface_mnet025_v1', '1ggNFFqpe0abWz6V1A82rnxD6fyxB8W2c'),
Expand Down Expand Up @@ -179,7 +182,6 @@ class LandmarksDetector(mixins.LandmarksDetectorMixin, base.BasePlugin):
class Landmarks2d106DTO(plugin_result.LandmarksDTO):
"""
106-points facial landmarks
Points mark-up - https://github.com/deepinsight/insightface/tree/master/alignment/coordinateReg#visualization
"""
NOSE_POSITION = 86
Expand Down Expand Up @@ -211,4 +213,4 @@ def _landmark_model(self):
model.bind(for_training=False,
data_shapes=[('data', (1, 3, *self.CROP_SIZE))])
model.set_params(arg_params, aux_params)
return model
return model

0 comments on commit c742aa3

Please sign in to comment.