Skip to content

Commit

Permalink
Merge pull request #6 from 5sControl/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
eugenos-programos authored Jul 12, 2023
2 parents e114740 + 3eec033 commit 7dc5f3f
Show file tree
Hide file tree
Showing 13 changed files with 906 additions and 39 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: Push Docker image
name: Push Algo Docker image

on:
push:
branches:
- 'main'
- 'dev'

jobs:
push_to_registry:
Expand All @@ -12,9 +13,6 @@ jobs:
steps:
- name: Check out the repo
uses: actions/checkout@v3
with:
lfs: true
- run: git lfs pull
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
Expand All @@ -31,4 +29,4 @@ jobs:
context: .
file: Dockerfile
push: True
tags: 5scontrol/idle_python:v0.3.11
tags: 5scontrol/idle_python:v0.4.0
35 changes: 35 additions & 0 deletions .github/workflows/server-docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Push Server Docker image

on:
push:
branches:
- 'main'
- 'dev'

jobs:
push_to_registry:
name: Push Docker image
runs-on: ubuntu-20.04
steps:
- name: Check out the repo
uses: actions/checkout@v3
with:
lfs: true
- run: git lfs pull
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: 5scontrol
password: dckr_pat_JeDvLRwvs54o_E4iwZtPtCE45iI
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: 5scontrol/idle_python_server
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: ./idle_models
file: Dockerfile
push: True
tags: 5scontrol/idle_python_server:v0.4.0
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.cache
.idea/
images/
models/
env/
__pycache__
.env
.vscode/
.vscode/
build/
cmake-build-debug/
4 changes: 2 additions & 2 deletions functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def put_rectangle(img, boxes, scores):
return img


def check_coordinates_diffs(coords_1: np.array, coords_2: np.array, threshold=THRESHOLD):
if coords_1 is None:
def check_coordinates_diffs(coords_1: np.array, coords_2: np.array, threshold=THRESHOLD) -> bool:
if coords_1 is None or coords_1.shape != coords_2.shape:
return True
diff = np.abs(coords_1 - coords_2).sum()
return diff > threshold
Expand Down
4 changes: 2 additions & 2 deletions idle_models/IdleObjectDetectionModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def __preprocess_image__(self, img: np.array) -> np.array:
return img

@torch.no_grad()
def __call__(self, img: np.array) -> list:
def __call__(self, img: np.array) -> torch.Tensor:
img = self.__preprocess_image__(img)
pred = self.model(img, augment=False)[0]
pred = non_max_suppression(
pred, 0.45, 0.5, classes=[67], agnostic=False)[0]
pred[:, :4] = scale_coords(
img.shape[2:], pred[:, :4], self.img_shape).round()
return pred[:, :4], pred[:, 4]
return pred[:, :5]
9 changes: 5 additions & 4 deletions idle_models/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from configs.load_configs import *
from IdleObjectDetectionModel import IdleObjectDetectionModel
import numpy as np
import io


app = Flask(__name__)
Expand All @@ -13,15 +14,15 @@
CLASSES
)

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

@app.route('/predict', methods=['POST'])
def predict():
if request.method == 'POST':
image = np.array(request.json['image']).astype(np.float32)
coords, confs = model(image)
image = convert_bytes2image(request.files["image"].read())
coords = model(image)
return jsonify(
{
"coordinates": coords.tolist(),
"confidences": confs.tolist()
"coordinates": coords.tolist()
}
)
4 changes: 2 additions & 2 deletions idle_models/configs/confs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
67
],
"iou_thres": 0.5,
"conf_thres": 0.3,
"model_path": "yolor/yolor_csp_x.pt",
"conf_thres": 0.5,
"model_path": "yolor/yolor_csp_x_star.pt",
"port": 5001
}
Empty file.
68 changes: 68 additions & 0 deletions idle_models/yolor/models/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import argparse

import torch

from yolor.utils.google_utils import attempt_download

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--weights', type=str, default='./yolov4.pt', help='weights path')
parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size')
parser.add_argument('--batch-size', type=int, default=1, help='batch size')
opt = parser.parse_args()
opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand
print(opt)

# Input
img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size(1,3,320,192) iDetection

# Load PyTorch model
attempt_download(opt.weights)
model = torch.load(opt.weights, map_location=torch.device('cpu'))['model'].float()
model.eval()
model.model[-1].export = True # set Detect() layer export=True
y = model(img) # dry run

# TorchScript export
try:
print('\nStarting TorchScript export with torch %s...' % torch.__version__)
f = opt.weights.replace('.pt', '.torchscript.pt') # filename
ts = torch.jit.trace(model, img)
ts.save(f)
print('TorchScript export success, saved as %s' % f)
except Exception as e:
print('TorchScript export failure: %s' % e)

# ONNX export
try:
import onnx

print('\nStarting ONNX export with onnx %s...' % onnx.__version__)
f = opt.weights.replace('.pt', '.onnx') # filename
model.fuse() # only for ONNX
torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'],
output_names=['classes', 'boxes'] if y is None else ['output'])

# Checks
onnx_model = onnx.load(f) # load onnx model
onnx.checker.check_model(onnx_model) # check onnx model
print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model
print('ONNX export success, saved as %s' % f)
except Exception as e:
print('ONNX export failure: %s' % e)

# CoreML export
try:
import coremltools as ct

print('\nStarting CoreML export with coremltools %s...' % ct.__version__)
# convert model from torchscript and apply pixel scaling as per detect.py
model = ct.convert(ts, inputs=[ct.ImageType(name='images', shape=img.shape, scale=1 / 255.0, bias=[0, 0, 0])])
f = opt.weights.replace('.pt', '.mlmodel') # filename
model.save(f)
print('CoreML export success, saved as %s' % f)
except Exception as e:
print('CoreML export failure: %s' % e)

# Finish
print('\nExport complete. Visualize with https://github.com/lutzroeder/netron.')
Loading

0 comments on commit 7dc5f3f

Please sign in to comment.