-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from 5sControl/dev
Dev
- Loading branch information
Showing
24 changed files
with
5,539 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
Oops, something went wrong.