Skip to content

Commit 7dc5f3f

Browse files
Merge pull request #6 from 5sControl/dev
Dev
2 parents e114740 + 3eec033 commit 7dc5f3f

13 files changed

+906
-39
lines changed

.github/workflows/docker-image.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
name: Push Docker image
1+
name: Push Algo Docker image
22

33
on:
44
push:
55
branches:
66
- 'main'
7+
- 'dev'
78

89
jobs:
910
push_to_registry:
@@ -12,9 +13,6 @@ jobs:
1213
steps:
1314
- name: Check out the repo
1415
uses: actions/checkout@v3
15-
with:
16-
lfs: true
17-
- run: git lfs pull
1816
- name: Login to Docker Hub
1917
uses: docker/login-action@v2
2018
with:
@@ -31,4 +29,4 @@ jobs:
3129
context: .
3230
file: Dockerfile
3331
push: True
34-
tags: 5scontrol/idle_python:v0.3.11
32+
tags: 5scontrol/idle_python:v0.4.0
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Push Server Docker image
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
- 'dev'
8+
9+
jobs:
10+
push_to_registry:
11+
name: Push Docker image
12+
runs-on: ubuntu-20.04
13+
steps:
14+
- name: Check out the repo
15+
uses: actions/checkout@v3
16+
with:
17+
lfs: true
18+
- run: git lfs pull
19+
- name: Login to Docker Hub
20+
uses: docker/login-action@v2
21+
with:
22+
username: 5scontrol
23+
password: dckr_pat_JeDvLRwvs54o_E4iwZtPtCE45iI
24+
- name: Extract metadata (tags, labels) for Docker
25+
id: meta
26+
uses: docker/metadata-action@v4
27+
with:
28+
images: 5scontrol/idle_python_server
29+
- name: Build and push Docker image
30+
uses: docker/build-push-action@v4
31+
with:
32+
context: ./idle_models
33+
file: Dockerfile
34+
push: True
35+
tags: 5scontrol/idle_python_server:v0.4.0

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
.cache
22
.idea/
33
images/
4-
models/
54
env/
65
__pycache__
76
.env
8-
.vscode/
7+
.vscode/
8+
build/
9+
cmake-build-debug/

functional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def put_rectangle(img, boxes, scores):
6262
return img
6363

6464

65-
def check_coordinates_diffs(coords_1: np.array, coords_2: np.array, threshold=THRESHOLD):
66-
if coords_1 is None:
65+
def check_coordinates_diffs(coords_1: np.array, coords_2: np.array, threshold=THRESHOLD) -> bool:
66+
if coords_1 is None or coords_1.shape != coords_2.shape:
6767
return True
6868
diff = np.abs(coords_1 - coords_2).sum()
6969
return diff > threshold

idle_models/IdleObjectDetectionModel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ def __preprocess_image__(self, img: np.array) -> np.array:
2424
return img
2525

2626
@torch.no_grad()
27-
def __call__(self, img: np.array) -> list:
27+
def __call__(self, img: np.array) -> torch.Tensor:
2828
img = self.__preprocess_image__(img)
2929
pred = self.model(img, augment=False)[0]
3030
pred = non_max_suppression(
3131
pred, 0.45, 0.5, classes=[67], agnostic=False)[0]
3232
pred[:, :4] = scale_coords(
3333
img.shape[2:], pred[:, :4], self.img_shape).round()
34-
return pred[:, :4], pred[:, 4]
34+
return pred[:, :5]

idle_models/app.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from configs.load_configs import *
44
from IdleObjectDetectionModel import IdleObjectDetectionModel
55
import numpy as np
6+
import io
67

78

89
app = Flask(__name__)
@@ -13,15 +14,15 @@
1314
CLASSES
1415
)
1516

17+
convert_bytes2image = lambda bytes: np.array(Image.open(io.BytesIO(bytes)), dtype=np.uint8)
1618

1719
@app.route('/predict', methods=['POST'])
1820
def predict():
1921
if request.method == 'POST':
20-
image = np.array(request.json['image']).astype(np.float32)
21-
coords, confs = model(image)
22+
image = convert_bytes2image(request.files["image"].read())
23+
coords = model(image)
2224
return jsonify(
2325
{
24-
"coordinates": coords.tolist(),
25-
"confidences": confs.tolist()
26+
"coordinates": coords.tolist()
2627
}
2728
)

idle_models/configs/confs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
67
44
],
55
"iou_thres": 0.5,
6-
"conf_thres": 0.3,
7-
"model_path": "yolor/yolor_csp_x.pt",
6+
"conf_thres": 0.5,
7+
"model_path": "yolor/yolor_csp_x_star.pt",
88
"port": 5001
99
}

idle_models/yolor/models/__init__.py

Whitespace-only changes.

idle_models/yolor/models/export.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import argparse
2+
3+
import torch
4+
5+
from yolor.utils.google_utils import attempt_download
6+
7+
if __name__ == '__main__':
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument('--weights', type=str, default='./yolov4.pt', help='weights path')
10+
parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size')
11+
parser.add_argument('--batch-size', type=int, default=1, help='batch size')
12+
opt = parser.parse_args()
13+
opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand
14+
print(opt)
15+
16+
# Input
17+
img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size(1,3,320,192) iDetection
18+
19+
# Load PyTorch model
20+
attempt_download(opt.weights)
21+
model = torch.load(opt.weights, map_location=torch.device('cpu'))['model'].float()
22+
model.eval()
23+
model.model[-1].export = True # set Detect() layer export=True
24+
y = model(img) # dry run
25+
26+
# TorchScript export
27+
try:
28+
print('\nStarting TorchScript export with torch %s...' % torch.__version__)
29+
f = opt.weights.replace('.pt', '.torchscript.pt') # filename
30+
ts = torch.jit.trace(model, img)
31+
ts.save(f)
32+
print('TorchScript export success, saved as %s' % f)
33+
except Exception as e:
34+
print('TorchScript export failure: %s' % e)
35+
36+
# ONNX export
37+
try:
38+
import onnx
39+
40+
print('\nStarting ONNX export with onnx %s...' % onnx.__version__)
41+
f = opt.weights.replace('.pt', '.onnx') # filename
42+
model.fuse() # only for ONNX
43+
torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'],
44+
output_names=['classes', 'boxes'] if y is None else ['output'])
45+
46+
# Checks
47+
onnx_model = onnx.load(f) # load onnx model
48+
onnx.checker.check_model(onnx_model) # check onnx model
49+
print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model
50+
print('ONNX export success, saved as %s' % f)
51+
except Exception as e:
52+
print('ONNX export failure: %s' % e)
53+
54+
# CoreML export
55+
try:
56+
import coremltools as ct
57+
58+
print('\nStarting CoreML export with coremltools %s...' % ct.__version__)
59+
# convert model from torchscript and apply pixel scaling as per detect.py
60+
model = ct.convert(ts, inputs=[ct.ImageType(name='images', shape=img.shape, scale=1 / 255.0, bias=[0, 0, 0])])
61+
f = opt.weights.replace('.pt', '.mlmodel') # filename
62+
model.save(f)
63+
print('CoreML export success, saved as %s' % f)
64+
except Exception as e:
65+
print('CoreML export failure: %s' % e)
66+
67+
# Finish
68+
print('\nExport complete. Visualize with https://github.com/lutzroeder/netron.')

0 commit comments

Comments
 (0)