Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #90

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Dev #90

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added cli.log
Empty file.
Binary file added config/IR&onnx_for_416_Petr_1/yoeo.bin
Binary file not shown.
631 changes: 631 additions & 0 deletions config/IR&onnx_for_416_Petr_1/yoeo.mapping

Large diffs are not rendered by default.

Binary file added config/IR&onnx_for_416_Petr_1/yoeo.onnx
Binary file not shown.
5,155 changes: 5,155 additions & 0 deletions config/IR&onnx_for_416_Petr_1/yoeo.xml

Large diffs are not rendered by default.

Binary file added config/IR&onnx_for_416_mine_759/yoeo_mine_759.bin
Binary file not shown.
631 changes: 631 additions & 0 deletions config/IR&onnx_for_416_mine_759/yoeo_mine_759.mapping

Large diffs are not rendered by default.

Binary file not shown.
5,155 changes: 5,155 additions & 0 deletions config/IR&onnx_for_416_mine_759/yoeo_mine_759.xml

Large diffs are not rendered by default.

Binary file removed data/samples/frame0256.jpg
Binary file not shown.
Binary file removed data/samples/frame1563.jpg
Binary file not shown.
Binary file removed data/samples/frame2146.jpg
Binary file not shown.
Binary file removed data/samples/frame2150.jpg
Binary file not shown.
Binary file removed data/samples/frame6554.jpg
Binary file not shown.
Binary file removed data/samples/montreal-game02_aa_000001.png
Binary file not shown.
Binary file removed data/samples/montreal-game02_ad_000165.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 5 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions pth2pt_YOEO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import torch
from yoeo import models
import torchvision
from detectron2.modeling import build_model

def inference_func(model, image):
inputs = [{"image": image}]
return model.inference(inputs, do_postprocess=False)[0]

cfg = "/home/ss21mipt/Documents/starkit/DIPLOMA/YOEO/config/yoeo.cfg"
# weights = "/home/ss21mipt/Documents/starkit/DIPLOMA/YOEO/weights/yoeo_mine_759.pth"
weights = "/home/ss21mipt/Documents/starkit/DIPLOMA/YOEO/weights/yoeo.pth"
# print("cfg.MODEL.WEIGHTS: ",cfg.MODEL.WEIGHTS) ## RETURNS : cfg.MODEL.WEIGHTS: drive/Detectron2/model_final.pth

model = models.load_model(cfg, weights, "cpu")
print(model)

example = torch.rand(1, 3, 416, 416)
# wrapper = TracingAdapter(module, example, inference_func)
# model.eval()
traced_script_module = torch.jit.trace(model, example)
# traced_script_module = torch.jit.trace(wrapper, (example,))
traced_script_module.save("/home/ss21mipt/Documents/starkit/DIPLOMA/YOEO/weights/model-final.pt")
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ build-backend = "poetry.core.masonry.api"
yoeo-detect = "yoeo.detect:run"
yoeo-train = "yoeo.train:run"
yoeo-test = "yoeo.test:run"
yoeo-detectcam = "yoeo.detectwebcam:run"
yoeo-detectcam_IR = "yoeo.detectwebcam_IR:run"
yoeo-detectcam_pt = "yoeo.detectwebcam_pt:run"
Binary file added runs/segment/predict2/0.mp4
Binary file not shown.
Binary file added runs/segment/predict3/0.mp4
Binary file not shown.
Binary file added runs/segment/predict4/0.mp4
Binary file not shown.
66 changes: 66 additions & 0 deletions scripts/convertONNXModelToOpenVinoIR.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#! /usr/bin/env python3
import argparse
import os


def convert_model(onnx_path: str, output_path: str) -> None:
command = assemble_command(onnx_path, output_path)
run_command(command)


def assemble_command(onnx_path: str, output_path: str) -> str:
# https://docs.openvino.ai/latest/notebooks/102-pytorch-onnx-to-openvino-with-output.html (April 7, 2022)
mo_command = f"""mo
--input_model "{onnx_path}"
--output_dir "{output_path}"
--input InputLayer
--output Detections,Segmentations
--framework onnx
--static_shape
--batch 1
"""
mo_command = " ".join(mo_command.split())

return mo_command


def run_command(command: str) -> None:
# https://docs.openvino.ai/latest/notebooks/102-pytorch-onnx-to-openvino-with-output.html (April 7, 2022)
print("Exporting ONNX model to IR...")

mo_result = os.system(command)

print("=" * 30)
if mo_result == 0:
print("Model conversion was successful")
else:
print("Model conversion failed")


def get_output_path(model_onnx: str) -> str:
return get_parent_dir(model_onnx)


def get_parent_dir(path: str) -> str:
absolute_path = os.path.abspath(path)
parent_dir, filename = os.path.split(absolute_path)

return parent_dir


def run():
parser = argparse.ArgumentParser(description='Convert ONNX Model to OpenVino IR')
parser.add_argument(
"model_onnx",
type=str,
help="full path to model file (.onnx)"
)

args = parser.parse_args()

output_path = get_output_path(args.model_onnx)
convert_model(args.model_onnx, output_path)


if __name__ == "__main__":
run()
100 changes: 100 additions & 0 deletions scripts/convertPyTorchModelToONNX.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#! /usr/bin/env python3
import argparse
import os.path
from typing import Tuple

import onnx
import torch

import yoeo.models


def convert_model(model_cfg: str, weights_pth: str, output_path: str) -> None:
pytorch_model = yoeo.models.load_model(model_cfg, weights_pth)
convert_to_onnx(model=pytorch_model, output_path=output_path)


def convert_to_onnx(model: yoeo.models.Darknet, output_path: str, image_size: int = 416, batch_size: int = 1) -> None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model.to(device)
dummy_input = torch.randn(batch_size, 3, image_size, image_size, device=device)

torch.onnx.export(
model,
dummy_input,
output_path,
verbose=False,
export_params=True,
input_names=["InputLayer"],
output_names=["Detections", "Segmentations"],
opset_version=11
)


def check_model(model_path: str) -> None:
onnx_model = load_onnx(model_path)
check_onnx(onnx_model)


def load_onnx(path: str) -> onnx.onnx_ml_pb2.ModelProto:
return onnx.load(path)


def check_onnx(model: onnx.onnx_ml_pb2.ModelProto) -> None:
# https://github.com/onnx/onnx/blob/main/docs/PythonAPIOverview.md (April 7, 2022)
print("="*30)
try:
onnx.checker.check_model(model)
except onnx.checker.ValidationError as e:
print('The model is invalid: %s' % e)
else:
print('The model is valid!')


def construct_path(model_cfg: str) -> str:
parent_dir = get_parent_dir(model_cfg)
filename = get_filename_wout_extension(model_cfg)

onnx_path = os.path.join(parent_dir, f"{filename}.onnx")

return onnx_path


def get_parent_dir(path: str) -> str:
absolute_path = os.path.abspath(path)
parent_dir, filename = os.path.split(absolute_path)

return parent_dir


def get_filename_wout_extension(path: str) -> str:
absolute_path = os.path.abspath(path)
parent_dir, filename = os.path.split(absolute_path)
filename, ext = os.path.splitext(filename)

return filename


def run():
parser = argparse.ArgumentParser(description='Convert PyTorch Model to ONNX')
parser.add_argument(
"model_cfg",
type=str,
help="full path to model file (.cfg). ONNX model will be output with the same filename as well."
)
parser.add_argument(
"model_weights",
type=str,
help="full path to model weights file (.pth or .weights)."
)

args = parser.parse_args()

onnx_path = construct_path(args.model_cfg)
convert_model(args.model_cfg, args.model_weights, onnx_path)
check_model(onnx_path)


if __name__ == "__main__":
run()
38 changes: 38 additions & 0 deletions tezises/inputs&outputs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
PYTORCH .cfg + .pth

num 1 DETECTIONS SHAPE IStorch.Size([1, 6000, 8]), <class 'torch.Tensor'>
num 1 SEGMENTATIONS SHAPE IStorch.Size([1, 640, 640]), <class 'torch.Tensor'>

torchscipt

python .pt
SHAPE OF EVERY ELEM: torch.Size([1, 3, 20, 20, 8])
SHAPE OF EVERY ELEM: torch.Size([1, 3, 40, 40, 8])
SHAPE OF EVERY ELEM: torch.Size([1, 3, 640, 640])

cpp .pt
Shape of every elem: [1, 3, 20, 20, 8]
Shape of every elem: [1, 3, 40, 40, 8]
Shape of every elem: [1, 3, 640, 640]

IR

python .xml
outputs[
<ConstOutput: names[Detections] shape[1,2535,8] type: f32>,
<ConstOutput: names[Segmentations] shape[1,416,416] type: f32>
]>

cpp .xml
[1,2535,8]
[1,416,416]


зрение робана

Пути к весам и обработчику сеток: ~/env/common/vision_filters/ball_detection_wideangle.json
Чето там еще (трешолды и другие параметры) ~/env/common/vision_filters/colors_wideangle.json
пайплайн зрения прописан здесь ~/env/common/vision_filters/all.json



13 changes: 9 additions & 4 deletions yoeo/detect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#! /usr/bin/env python3

from __future__ import division

import os
Expand Down Expand Up @@ -59,6 +58,7 @@ def detect_directory(model_path, weights_path, img_path, classes, output_path,
output_path,
conf_thres,
nms_thres)

_draw_and_save_output_images(
img_detections, segmentations, imgs, img_size, output_path, classes)

Expand Down Expand Up @@ -134,11 +134,15 @@ def detect(model, dataloader, output_path, conf_thres, nms_thres):

for (img_paths, input_imgs) in tqdm.tqdm(dataloader, desc="Detecting"):
# Configure input
print(f"VOT TAKOE 1 {input_imgs}")
input_imgs = Variable(input_imgs.type(Tensor))

print(f"VOT TAKOE 2 {input_imgs}")

# Get detections
with torch.no_grad():
detections, segmentations = model(input_imgs)
print(f"VOT TAKOE 3 {input_imgs}")
detections = non_max_suppression(detections, conf_thres, nms_thres)

# Store image and detections
Expand Down Expand Up @@ -195,7 +199,7 @@ def _draw_and_save_output_image(image_path, detections, seg, img_size, output_pa
# Get segmentation
seg = seg.cpu().detach().numpy().astype(np.uint8)
# Draw all of it

print(f"ETO EST SEG {seg}")
# The amount of padding that was added
pad_x = max(img.shape[0] - img.shape[1], 0) * (img_size / max(img.shape[:2])) // 2
pad_y = max(img.shape[1] - img.shape[0], 0) * (img_size / max(img.shape[:2])) // 2
Expand All @@ -206,13 +210,14 @@ def _draw_and_save_output_image(image_path, detections, seg, img_size, output_pa
int(pad_x) : int(img_size - pad_x),
] * 255


print(f"MILLIARDNAYA {img}")
ax.imshow(
SegmentationMapsOnImage(
seg[
int(pad_y) : int(img_size - pad_y),
int(pad_x) : int(img_size - pad_x),
], shape=img.shape).draw_on_image(img)[0])
print("JJEEPPAAA")
# Rescale boxes to original image
detections = rescale_boxes(detections, img_size, img.shape[:2])
unique_labels = detections[:, -1].cpu().unique()
Expand Down Expand Up @@ -275,6 +280,7 @@ def _create_data_loader(img_path, batch_size, img_size, n_cpu):
:return: Returns DataLoader
:rtype: DataLoader
"""

dataset = ImageFolder(
img_path,
transform=transforms.Compose([DEFAULT_TRANSFORMS, Resize(img_size)]))
Expand Down Expand Up @@ -318,6 +324,5 @@ def run():
conf_thres=args.conf_thres,
nms_thres=args.nms_thres)


if __name__ == '__main__':
run()
Loading