Skip to content

Commit

Permalink
Merge pull request #5 from 5sControl/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
eugenos-programos authored Jun 30, 2023
2 parents 2459296 + 4a247f0 commit e114740
Show file tree
Hide file tree
Showing 24 changed files with 5,539 additions and 28 deletions.
4 changes: 2 additions & 2 deletions functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def get_frame(h):

def put_rectangle(img, boxes, scores):
for (x1, y1, x2, y2), score in zip(boxes.astype(int), scores):
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
cv2.putText(img, str(score), (x1, y1),
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 255, 255), 2)
cv2.putText(img, str(round(score, 3)), (x1, y1),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
return img

Expand Down
4 changes: 2 additions & 2 deletions idle_models/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ RUN apt-get install ffmpeg libsm6 libxext6 -y
WORKDIR /var/www/5scontrol
COPY . .
RUN mkdir -p /usr/src/app
EXPOSE 5000
CMD [ "flask", "run","--host","0.0.0.0","--port","5000"]
EXPOSE 5001
CMD [ "flask", "run","--host","0.0.0.0","--port","5001"]
36 changes: 24 additions & 12 deletions idle_models/IdleObjectDetectionModel.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import torch
from ultralytics import YOLO
from yolor.model import get_model
import numpy as np
from yolor.utils.datasets import letterbox
from yolor.utils.general import non_max_suppression, scale_coords


class IdleObjectDetectionModel:
def __init__(self, path: str, conf_thresh, iou_thresh, classes) -> None:
self.model = YOLO(path)
self.model, self.device = get_model(path, "yolor/yolor_csp_x.cfg")
self.conf_thresh = conf_thresh
self.iou_thresh = iou_thresh
self.classes = classes

def __preprocess_image__(self, img: np.array) -> np.array:
self.img_shape = img.shape
img = letterbox(img.copy(), new_shape=1280, auto_size=64)[0]
img = img[:, :, ::-1].transpose(2, 0, 1)
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(self.device)
img = img.float()
img /= 255.0
img = img.unsqueeze(0)
return img

@torch.no_grad()
def __call__(self, img) -> list:
results = self.model(
source=img,
conf=self.conf_thresh,
iou=self.iou_thresh,
max_det=600,
classes=self.classes,
verbose=False
)[0].boxes
return results.xyxy, results.conf
def __call__(self, img: np.array) -> list:
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]
4 changes: 2 additions & 2 deletions idle_models/configs/confs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
],
"iou_thres": 0.5,
"conf_thres": 0.3,
"model_path": "idle_v0.3.11.pt",
"port": 5000
"model_path": "yolor/yolor_csp_x.pt",
"port": 5001
}
3 changes: 0 additions & 3 deletions idle_models/idle_v0.3.11.pt

This file was deleted.

1 change: 0 additions & 1 deletion idle_models/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ pydantic==1.10.2
python-dotenv==1.0.0
PyYAML==6.0
requests==2.27.1
ultralytics==8.0.112
Flask==2.2.2
80 changes: 80 additions & 0 deletions idle_models/yolor/coco.names
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
person
bicycle
car
motorcycle
airplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
couch
potted plant
bed
dining table
toilet
tv
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrush
12 changes: 12 additions & 0 deletions idle_models/yolor/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import torch
from yolor.utils.torch_utils import select_device
from yolor.models.models import Darknet


def get_model(weights, cfg):
imgsz = 1280
device = select_device('cpu')
model = Darknet(cfg, imgsz)
model.load_state_dict(torch.load(weights, map_location=device)['model'])
model.to(device).eval()
return model, device
1 change: 1 addition & 0 deletions idle_models/yolor/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

72 changes: 72 additions & 0 deletions idle_models/yolor/utils/activations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Activation functions

import torch
import torch.nn as nn
import torch.nn.functional as F


# Swish https://arxiv.org/pdf/1905.02244.pdf ---------------------------------------------------------------------------
class Swish(nn.Module): #
@staticmethod
def forward(x):
return x * torch.sigmoid(x)


class Hardswish(nn.Module): # export-friendly version of nn.Hardswish()
@staticmethod
def forward(x):
# return x * F.hardsigmoid(x) # for torchscript and CoreML
return x * F.hardtanh(x + 3, 0., 6.) / 6. # for torchscript, CoreML and ONNX


class MemoryEfficientSwish(nn.Module):
class F(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
ctx.save_for_backward(x)
return x * torch.sigmoid(x)

@staticmethod
def backward(ctx, grad_output):
x = ctx.saved_tensors[0]
sx = torch.sigmoid(x)
return grad_output * (sx * (1 + x * (1 - sx)))

def forward(self, x):
return self.F.apply(x)


# Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------
class Mish(nn.Module):
@staticmethod
def forward(x):
return x * F.softplus(x).tanh()


class MemoryEfficientMish(nn.Module):
class F(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
ctx.save_for_backward(x)
return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))

@staticmethod
def backward(ctx, grad_output):
x = ctx.saved_tensors[0]
sx = torch.sigmoid(x)
fx = F.softplus(x).tanh()
return grad_output * (fx + x * sx * (1 - fx * fx))

def forward(self, x):
return self.F.apply(x)


# FReLU https://arxiv.org/abs/2007.11824 -------------------------------------------------------------------------------
class FReLU(nn.Module):
def __init__(self, c1, k=3): # ch_in, kernel
super().__init__()
self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1)
self.bn = nn.BatchNorm2d(c1)

def forward(self, x):
return torch.max(x, self.bn(self.conv(x)))
Loading

0 comments on commit e114740

Please sign in to comment.