Skip to content

Commit e114740

Browse files
Merge pull request #5 from 5sControl/dev
Dev
2 parents 2459296 + 4a247f0 commit e114740

24 files changed

+5539
-28
lines changed

functional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def get_frame(h):
5656

5757
def put_rectangle(img, boxes, scores):
5858
for (x1, y1, x2, y2), score in zip(boxes.astype(int), scores):
59-
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
60-
cv2.putText(img, str(score), (x1, y1),
59+
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 255, 255), 2)
60+
cv2.putText(img, str(round(score, 3)), (x1, y1),
6161
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
6262
return img
6363

idle_models/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ RUN apt-get install ffmpeg libsm6 libxext6 -y
88
WORKDIR /var/www/5scontrol
99
COPY . .
1010
RUN mkdir -p /usr/src/app
11-
EXPOSE 5000
12-
CMD [ "flask", "run","--host","0.0.0.0","--port","5000"]
11+
EXPOSE 5001
12+
CMD [ "flask", "run","--host","0.0.0.0","--port","5001"]
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
import torch
2-
from ultralytics import YOLO
2+
from yolor.model import get_model
3+
import numpy as np
4+
from yolor.utils.datasets import letterbox
5+
from yolor.utils.general import non_max_suppression, scale_coords
36

47

58
class IdleObjectDetectionModel:
69
def __init__(self, path: str, conf_thresh, iou_thresh, classes) -> None:
7-
self.model = YOLO(path)
10+
self.model, self.device = get_model(path, "yolor/yolor_csp_x.cfg")
811
self.conf_thresh = conf_thresh
912
self.iou_thresh = iou_thresh
1013
self.classes = classes
1114

15+
def __preprocess_image__(self, img: np.array) -> np.array:
16+
self.img_shape = img.shape
17+
img = letterbox(img.copy(), new_shape=1280, auto_size=64)[0]
18+
img = img[:, :, ::-1].transpose(2, 0, 1)
19+
img = np.ascontiguousarray(img)
20+
img = torch.from_numpy(img).to(self.device)
21+
img = img.float()
22+
img /= 255.0
23+
img = img.unsqueeze(0)
24+
return img
25+
1226
@torch.no_grad()
13-
def __call__(self, img) -> list:
14-
results = self.model(
15-
source=img,
16-
conf=self.conf_thresh,
17-
iou=self.iou_thresh,
18-
max_det=600,
19-
classes=self.classes,
20-
verbose=False
21-
)[0].boxes
22-
return results.xyxy, results.conf
27+
def __call__(self, img: np.array) -> list:
28+
img = self.__preprocess_image__(img)
29+
pred = self.model(img, augment=False)[0]
30+
pred = non_max_suppression(
31+
pred, 0.45, 0.5, classes=[67], agnostic=False)[0]
32+
pred[:, :4] = scale_coords(
33+
img.shape[2:], pred[:, :4], self.img_shape).round()
34+
return pred[:, :4], pred[:, 4]

idle_models/configs/confs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
],
55
"iou_thres": 0.5,
66
"conf_thres": 0.3,
7-
"model_path": "idle_v0.3.11.pt",
8-
"port": 5000
7+
"model_path": "yolor/yolor_csp_x.pt",
8+
"port": 5001
99
}

idle_models/idle_v0.3.11.pt

Lines changed: 0 additions & 3 deletions
This file was deleted.

idle_models/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ pydantic==1.10.2
77
python-dotenv==1.0.0
88
PyYAML==6.0
99
requests==2.27.1
10-
ultralytics==8.0.112
1110
Flask==2.2.2

idle_models/yolor/coco.names

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
person
2+
bicycle
3+
car
4+
motorcycle
5+
airplane
6+
bus
7+
train
8+
truck
9+
boat
10+
traffic light
11+
fire hydrant
12+
stop sign
13+
parking meter
14+
bench
15+
bird
16+
cat
17+
dog
18+
horse
19+
sheep
20+
cow
21+
elephant
22+
bear
23+
zebra
24+
giraffe
25+
backpack
26+
umbrella
27+
handbag
28+
tie
29+
suitcase
30+
frisbee
31+
skis
32+
snowboard
33+
sports ball
34+
kite
35+
baseball bat
36+
baseball glove
37+
skateboard
38+
surfboard
39+
tennis racket
40+
bottle
41+
wine glass
42+
cup
43+
fork
44+
knife
45+
spoon
46+
bowl
47+
banana
48+
apple
49+
sandwich
50+
orange
51+
broccoli
52+
carrot
53+
hot dog
54+
pizza
55+
donut
56+
cake
57+
chair
58+
couch
59+
potted plant
60+
bed
61+
dining table
62+
toilet
63+
tv
64+
laptop
65+
mouse
66+
remote
67+
keyboard
68+
cell phone
69+
microwave
70+
oven
71+
toaster
72+
sink
73+
refrigerator
74+
book
75+
clock
76+
vase
77+
scissors
78+
teddy bear
79+
hair drier
80+
toothbrush

idle_models/yolor/model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import torch
2+
from yolor.utils.torch_utils import select_device
3+
from yolor.models.models import Darknet
4+
5+
6+
def get_model(weights, cfg):
7+
imgsz = 1280
8+
device = select_device('cpu')
9+
model = Darknet(cfg, imgsz)
10+
model.load_state_dict(torch.load(weights, map_location=device)['model'])
11+
model.to(device).eval()
12+
return model, device

idle_models/yolor/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Activation functions
2+
3+
import torch
4+
import torch.nn as nn
5+
import torch.nn.functional as F
6+
7+
8+
# Swish https://arxiv.org/pdf/1905.02244.pdf ---------------------------------------------------------------------------
9+
class Swish(nn.Module): #
10+
@staticmethod
11+
def forward(x):
12+
return x * torch.sigmoid(x)
13+
14+
15+
class Hardswish(nn.Module): # export-friendly version of nn.Hardswish()
16+
@staticmethod
17+
def forward(x):
18+
# return x * F.hardsigmoid(x) # for torchscript and CoreML
19+
return x * F.hardtanh(x + 3, 0., 6.) / 6. # for torchscript, CoreML and ONNX
20+
21+
22+
class MemoryEfficientSwish(nn.Module):
23+
class F(torch.autograd.Function):
24+
@staticmethod
25+
def forward(ctx, x):
26+
ctx.save_for_backward(x)
27+
return x * torch.sigmoid(x)
28+
29+
@staticmethod
30+
def backward(ctx, grad_output):
31+
x = ctx.saved_tensors[0]
32+
sx = torch.sigmoid(x)
33+
return grad_output * (sx * (1 + x * (1 - sx)))
34+
35+
def forward(self, x):
36+
return self.F.apply(x)
37+
38+
39+
# Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------
40+
class Mish(nn.Module):
41+
@staticmethod
42+
def forward(x):
43+
return x * F.softplus(x).tanh()
44+
45+
46+
class MemoryEfficientMish(nn.Module):
47+
class F(torch.autograd.Function):
48+
@staticmethod
49+
def forward(ctx, x):
50+
ctx.save_for_backward(x)
51+
return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))
52+
53+
@staticmethod
54+
def backward(ctx, grad_output):
55+
x = ctx.saved_tensors[0]
56+
sx = torch.sigmoid(x)
57+
fx = F.softplus(x).tanh()
58+
return grad_output * (fx + x * sx * (1 - fx * fx))
59+
60+
def forward(self, x):
61+
return self.F.apply(x)
62+
63+
64+
# FReLU https://arxiv.org/abs/2007.11824 -------------------------------------------------------------------------------
65+
class FReLU(nn.Module):
66+
def __init__(self, c1, k=3): # ch_in, kernel
67+
super().__init__()
68+
self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1)
69+
self.bn = nn.BatchNorm2d(c1)
70+
71+
def forward(self, x):
72+
return torch.max(x, self.bn(self.conv(x)))

0 commit comments

Comments
 (0)