Skip to content

Commit 7c9c76e

Browse files
committed
init
1 parent 52b3e44 commit 7c9c76e

15 files changed

+1242
-0
lines changed

Dockerfile.cpu

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM python:3.10.3-slim-bullseye
2+
3+
# set environment variables
4+
ENV PYTHONDONTWRITEBYTECODE 1
5+
ENV PYTHONUNBUFFERED 1
6+
7+
RUN apt-get update \
8+
&& apt-get install -y \
9+
libfontconfig1 \
10+
libglib2.0-0 \
11+
libsm6 \
12+
libxext6 \
13+
libgl1 \
14+
libxrender1 \
15+
unzip \
16+
wget \
17+
&& rm -rf /var/lib/apt/lists/*
18+
19+
RUN rm -rf weights
20+
RUN mkdir weights
21+
22+
RUN wget --progress=bar:force https://enterprise-unitlab.s3.us-east-2.amazonaws.com/weights/yolov8x-seg_model.onnx -O weights/model.onnx
23+
24+
COPY ./requirement-cpu.txt .
25+
26+
RUN pip install --upgrade pip
27+
28+
# install dependencies
29+
RUN pip install -r requirement-cpu.txt
30+
31+
COPY . .

Dockerfile.gpu

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
FROM nvcr.io/nvidia/tensorrt:23.03-py3
2+
3+
ENV TZ=Europe/Minsk
4+
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
5+
6+
RUN apt-get update \
7+
&& apt-get install -y \
8+
libfontconfig1 \
9+
libglib2.0-0 \
10+
libsm6 \
11+
libxext6 \
12+
libgl1 \
13+
python3-pip \
14+
libxrender1 \
15+
unzip \
16+
wget \
17+
&& rm -rf /var/lib/apt/lists/*
18+
19+
20+
ENV PYTHONDONTWRITEBYTECODE 1
21+
ENV PYTHONUNBUFFERED 1
22+
23+
24+
RUN rm -rf weights
25+
RUN mkdir weights
26+
27+
RUN wget --progress=bar:force https://enterprise-unitlab.s3.us-east-2.amazonaws.com/weights/generalx-seg.engine -O weights/model.engine
28+
29+
30+
COPY ./requirement-gpu.txt .
31+
32+
RUN pip3 install --upgrade pip
33+
34+
RUN pip3 install --extra-index-url=https://pypi.ngc.nvidia.com --trusted-host pypi.ngc.nvidia.com -r requirement-gpu.txt
35+
36+
COPY . .
37+
38+
ENV LC_ALL C.UTF-8
39+
ENV LANG C.UTF-8
40+
ENV NVIDIA_VISIBLE_DEVICES all
41+
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
42+
LABEL com.nvidia.volumes.needed="nvidia_driver"

docker-compose-cpu.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '2'
2+
3+
services:
4+
aiworker1_yolo8_onnx:
5+
build:
6+
context: ./
7+
dockerfile: Dockerfile.cpu
8+
environment:
9+
- BACKEND_MODULE=ONNX
10+
command: gunicorn --timeout 60 -k gevent -b unix:/tmp/gunicorn.sock -w 1 --bind 0.0.0.0:2121 serve.wsgi:app
11+
ports:
12+
- "2121:2121"
13+
14+
ngnix-cpu-worker:
15+
image: nginx:alpine
16+
ports:
17+
- "8080:8080"
18+
volumes:
19+
- ./enrtrypoint/proxy/nginx-cpu-worker.conf:/etc/nginx/nginx.conf
20+
depends_on:
21+
- aiworker1_yolo8_onnx

docker-compose-gpu.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '2'
2+
3+
services:
4+
aiworker1_yolo8_tensorrt:
5+
build:
6+
context: ./
7+
dockerfile: Dockerfile.gpu
8+
deploy:
9+
resources:
10+
reservations:
11+
devices:
12+
- driver: nvidia
13+
device_ids: [ '0' ]
14+
capabilities: [ gpu ]
15+
environment:
16+
- BACKEND_MODULE=TENSORRT
17+
command: gunicorn --timeout 60 -k gevent -b unix:/tmp/gunicorn.sock -w 1 --bind 0.0.0.0:2222 serve.wsgi:app
18+
ports:
19+
- "2222:2222"
20+
21+
ngnix-gpu-worker:
22+
image: nginx:alpine
23+
ports:
24+
- "8080:8080"
25+
volumes:
26+
- ./enrtrypoint/proxy/nginx-gpu-worker.conf:/etc/nginx/nginx.conf
27+
depends_on:
28+
- aiworker1_yolo8_tensorrt
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
user nginx;
2+
worker_processes 4;
3+
error_log /var/log/nginx/error.log warn;
4+
pid /var/run/nginx.pid;
5+
events {
6+
worker_connections 2048;
7+
}
8+
9+
10+
http {
11+
include /etc/nginx/mime.types;
12+
default_type application/octet-stream;
13+
14+
15+
server {
16+
listen 8080;
17+
server_name example.com;
18+
19+
gzip on;
20+
gzip_types text/plain text/css text/javascript application/javascript application/json application/xml;
21+
22+
location /api {
23+
proxy_pass http://aiworker1_yolo8_onnx:2121;
24+
proxy_set_header Host $host;
25+
proxy_set_header X-Real-IP $remote_addr;
26+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
27+
}
28+
29+
30+
}
31+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
32+
'$status $body_bytes_sent "$http_referer" '
33+
'"$http_user_agent" "$http_x_forwarded_for"';
34+
access_log /var/log/nginx/access.log main;
35+
36+
sendfile on;
37+
keepalive_timeout 85;
38+
include /etc/nginx/conf.d/*.conf;
39+
40+
41+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
user nginx;
2+
worker_processes 4;
3+
error_log /var/log/nginx/error.log warn;
4+
pid /var/run/nginx.pid;
5+
events {
6+
worker_connections 2048;
7+
}
8+
9+
10+
http {
11+
include /etc/nginx/mime.types;
12+
default_type application/octet-stream;
13+
14+
15+
server {
16+
listen 8080;
17+
server_name example.com;
18+
19+
gzip on;
20+
gzip_types text/plain text/css text/javascript application/javascript application/json application/xml;
21+
22+
location /api {
23+
proxy_pass http://aiworker1_yolo8_tensorrt:2222;
24+
proxy_set_header Host $host;
25+
proxy_set_header X-Real-IP $remote_addr;
26+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
27+
}
28+
29+
30+
}
31+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
32+
'$status $body_bytes_sent "$http_referer" '
33+
'"$http_user_agent" "$http_x_forwarded_for"';
34+
access_log /var/log/nginx/access.log main;
35+
36+
sendfile on;
37+
keepalive_timeout 85;
38+
include /etc/nginx/conf.d/*.conf;
39+
40+
41+
}

modules/onnx_engine.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import onnxruntime
2+
from typing import Any, Dict, List
3+
4+
class ONNXModule:
5+
"""
6+
A class that encapsulates an ONNX model for inference.
7+
8+
Attributes:
9+
weight (str): Path to the ONNX model file.
10+
session (onnxruntime.InferenceSession): The ONNX Runtime inference session for the model.
11+
12+
Methods:
13+
__init__(self, weight: str): Initializes the EnhancedONNXModule instance.
14+
__init_engine(self): Initializes the ONNX Runtime inference engine.
15+
__call__(self, inputs: Dict[str, Any]): Performs inference on the given inputs.
16+
"""
17+
18+
def __init__(self, weight: str) -> None:
19+
"""
20+
Initializes the EnhancedONNXModule with the given ONNX model.
21+
22+
Parameters:
23+
weight (str): The path to the ONNX model file.
24+
"""
25+
self.weight = weight
26+
self.session: onnxruntime.InferenceSession = self.__init_engine()
27+
28+
def __init_engine(self) -> onnxruntime.InferenceSession:
29+
"""
30+
Initializes the ONNX Runtime inference engine with the model.
31+
32+
Returns:
33+
onnxruntime.InferenceSession: The initialized inference session.
34+
"""
35+
try:
36+
session = onnxruntime.InferenceSession(self.weight, providers=['CPUExecutionProvider'])
37+
return session
38+
except onnxruntime.OnnxRuntimeException as e:
39+
raise RuntimeError(f"Failed to initialize ONNX Runtime session: {e}")
40+
41+
def __call__(self, inputs: Dict[str, Any]) -> List[Any]:
42+
"""
43+
Performs inference on the provided inputs using the ONNX model.
44+
45+
Parameters:
46+
inputs (Dict[str, Any]): The inputs for the model inference. Keys are input names, and values are input tensors.
47+
48+
Returns:
49+
List[Any]: The outputs from the model inference.
50+
"""
51+
try:
52+
outputs = self.session.run(None, inputs)
53+
return outputs
54+
except Exception as e:
55+
raise RuntimeError(f"Inference failed: {e}")

0 commit comments

Comments
 (0)