mayrajeo commited on
Commit
f079597
1 Parent(s): b0eaa25

test onnx models

Browse files
onnx_models/YOLOv8l/args.yaml ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ task: detect
2
+ mode: train
3
+ model: /scratch/project_2007454/ship_detection/yolo_models/yolov8l.pt
4
+ data: /run/nvme/job_15987187/data/processed//yolo_fold3.yaml
5
+ epochs: 200
6
+ patience: 30
7
+ batch: -1
8
+ imgsz: 640
9
+ save: true
10
+ save_period: -1
11
+ cache: disk
12
+ device: 0
13
+ workers: 5
14
+ project: /scratch/project_2007454/ship_detection/runs/yolov8l_Adam
15
+ name: fold_3
16
+ exist_ok: true
17
+ pretrained: false
18
+ optimizer: Adam
19
+ verbose: true
20
+ seed: 0
21
+ deterministic: true
22
+ single_cls: false
23
+ image_weights: false
24
+ rect: false
25
+ cos_lr: false
26
+ close_mosaic: 10
27
+ resume: false
28
+ amp: true
29
+ overlap_mask: true
30
+ mask_ratio: 4
31
+ dropout: 0.0
32
+ val: true
33
+ split: val
34
+ save_json: false
35
+ save_hybrid: false
36
+ conf: null
37
+ iou: 0.7
38
+ max_det: 300
39
+ half: false
40
+ dnn: false
41
+ plots: true
42
+ source: null
43
+ show: false
44
+ save_txt: false
45
+ save_conf: false
46
+ save_crop: false
47
+ hide_labels: false
48
+ hide_conf: false
49
+ vid_stride: 1
50
+ line_thickness: 3
51
+ visualize: false
52
+ augment: false
53
+ agnostic_nms: false
54
+ classes: null
55
+ retina_masks: false
56
+ boxes: true
57
+ format: torchscript
58
+ keras: false
59
+ optimize: false
60
+ int8: false
61
+ dynamic: false
62
+ simplify: false
63
+ opset: null
64
+ workspace: 4
65
+ nms: false
66
+ lr0: 0.001
67
+ lrf: 0.01
68
+ momentum: 0.9
69
+ weight_decay: 0.0
70
+ warmup_epochs: 3.0
71
+ warmup_momentum: 0.8
72
+ warmup_bias_lr: 0.1
73
+ box: 7.5
74
+ cls: 0.5
75
+ dfl: 1.5
76
+ fl_gamma: 0.0
77
+ label_smoothing: 0.0
78
+ nbs: 64
79
+ hsv_h: 0.015
80
+ hsv_s: 0.7
81
+ hsv_v: 0.4
82
+ degrees: 0.0
83
+ translate: 0.1
84
+ scale: 0.25
85
+ shear: 0.0
86
+ perspective: 0.0
87
+ flipud: 0.5
88
+ fliplr: 0.5
89
+ mosaic: 1.0
90
+ mixup: 0.0
91
+ copy_paste: 0.0
92
+ cfg: null
93
+ v5loader: false
94
+ tracker: botsort.yaml
95
+ save_dir: /scratch/project_2007454/ship_detection/runs/yolov8l_Adam/fold_3
onnx_models/YOLOv8l/best.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4db77122653c39a3e229de324005ea023428a63c896f8624a07c14f9c345d96b
3
+ size 174624801
onnx_models/YOLOv8m/args.yaml ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ task: detect
2
+ mode: train
3
+ model: /scratch/project_2007454/ship_detection/yolo_models/yolov8m.pt
4
+ data: /run/nvme/job_15987186/data/processed//yolo_fold3.yaml
5
+ epochs: 200
6
+ patience: 30
7
+ batch: -1
8
+ imgsz: 640
9
+ save: true
10
+ save_period: -1
11
+ cache: disk
12
+ device: 0
13
+ workers: 5
14
+ project: /scratch/project_2007454/ship_detection/runs/yolov8m_Adam
15
+ name: fold_3
16
+ exist_ok: true
17
+ pretrained: false
18
+ optimizer: Adam
19
+ verbose: true
20
+ seed: 0
21
+ deterministic: true
22
+ single_cls: false
23
+ image_weights: false
24
+ rect: false
25
+ cos_lr: false
26
+ close_mosaic: 10
27
+ resume: false
28
+ amp: true
29
+ overlap_mask: true
30
+ mask_ratio: 4
31
+ dropout: 0.0
32
+ val: true
33
+ split: val
34
+ save_json: false
35
+ save_hybrid: false
36
+ conf: null
37
+ iou: 0.7
38
+ max_det: 300
39
+ half: false
40
+ dnn: false
41
+ plots: true
42
+ source: null
43
+ show: false
44
+ save_txt: false
45
+ save_conf: false
46
+ save_crop: false
47
+ hide_labels: false
48
+ hide_conf: false
49
+ vid_stride: 1
50
+ line_thickness: 3
51
+ visualize: false
52
+ augment: false
53
+ agnostic_nms: false
54
+ classes: null
55
+ retina_masks: false
56
+ boxes: true
57
+ format: torchscript
58
+ keras: false
59
+ optimize: false
60
+ int8: false
61
+ dynamic: false
62
+ simplify: false
63
+ opset: null
64
+ workspace: 4
65
+ nms: false
66
+ lr0: 0.001
67
+ lrf: 0.01
68
+ momentum: 0.9
69
+ weight_decay: 0.0
70
+ warmup_epochs: 3.0
71
+ warmup_momentum: 0.8
72
+ warmup_bias_lr: 0.1
73
+ box: 7.5
74
+ cls: 0.5
75
+ dfl: 1.5
76
+ fl_gamma: 0.0
77
+ label_smoothing: 0.0
78
+ nbs: 64
79
+ hsv_h: 0.015
80
+ hsv_s: 0.7
81
+ hsv_v: 0.4
82
+ degrees: 0.0
83
+ translate: 0.1
84
+ scale: 0.25
85
+ shear: 0.0
86
+ perspective: 0.0
87
+ flipud: 0.5
88
+ fliplr: 0.5
89
+ mosaic: 1.0
90
+ mixup: 0.0
91
+ copy_paste: 0.0
92
+ cfg: null
93
+ v5loader: false
94
+ tracker: botsort.yaml
95
+ save_dir: /scratch/project_2007454/ship_detection/runs/yolov8m_Adam/fold_3
onnx_models/YOLOv8m/best.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f55e1dca428d4c5ec3c61b29ecbb67e68af9505580e18d1e3215e37a526d955c
3
+ size 103539108
onnx_models/YOLOv8n/args.yaml ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ task: detect
2
+ mode: train
3
+ model: /scratch/project_2007454/ship_detection/yolo_models/yolov8n.pt
4
+ data: /run/nvme/job_15987184/data/processed//yolo_fold3.yaml
5
+ epochs: 200
6
+ patience: 30
7
+ batch: -1
8
+ imgsz: 640
9
+ save: true
10
+ save_period: -1
11
+ cache: disk
12
+ device: 0
13
+ workers: 5
14
+ project: /scratch/project_2007454/ship_detection/runs/yolov8n_Adam
15
+ name: fold_3
16
+ exist_ok: true
17
+ pretrained: false
18
+ optimizer: Adam
19
+ verbose: true
20
+ seed: 0
21
+ deterministic: true
22
+ single_cls: false
23
+ image_weights: false
24
+ rect: false
25
+ cos_lr: false
26
+ close_mosaic: 10
27
+ resume: false
28
+ amp: true
29
+ overlap_mask: true
30
+ mask_ratio: 4
31
+ dropout: 0.0
32
+ val: true
33
+ split: val
34
+ save_json: false
35
+ save_hybrid: false
36
+ conf: null
37
+ iou: 0.7
38
+ max_det: 300
39
+ half: false
40
+ dnn: false
41
+ plots: true
42
+ source: null
43
+ show: false
44
+ save_txt: false
45
+ save_conf: false
46
+ save_crop: false
47
+ hide_labels: false
48
+ hide_conf: false
49
+ vid_stride: 1
50
+ line_thickness: 3
51
+ visualize: false
52
+ augment: false
53
+ agnostic_nms: false
54
+ classes: null
55
+ retina_masks: false
56
+ boxes: true
57
+ format: torchscript
58
+ keras: false
59
+ optimize: false
60
+ int8: false
61
+ dynamic: false
62
+ simplify: false
63
+ opset: null
64
+ workspace: 4
65
+ nms: false
66
+ lr0: 0.001
67
+ lrf: 0.01
68
+ momentum: 0.9
69
+ weight_decay: 0.0
70
+ warmup_epochs: 3.0
71
+ warmup_momentum: 0.8
72
+ warmup_bias_lr: 0.1
73
+ box: 7.5
74
+ cls: 0.5
75
+ dfl: 1.5
76
+ fl_gamma: 0.0
77
+ label_smoothing: 0.0
78
+ nbs: 64
79
+ hsv_h: 0.015
80
+ hsv_s: 0.7
81
+ hsv_v: 0.4
82
+ degrees: 0.0
83
+ translate: 0.1
84
+ scale: 0.25
85
+ shear: 0.0
86
+ perspective: 0.0
87
+ flipud: 0.5
88
+ fliplr: 0.5
89
+ mosaic: 1.0
90
+ mixup: 0.0
91
+ copy_paste: 0.0
92
+ cfg: null
93
+ v5loader: false
94
+ tracker: botsort.yaml
95
+ save_dir: /scratch/project_2007454/ship_detection/runs/yolov8n_Adam/fold_3
onnx_models/YOLOv8n/best.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:86c5fde83a420ecdc098c633588e02a0b69ce2d1b669e1d79cf45ce210a1b4d4
3
+ size 12183580
onnx_models/YOLOv8s/args.yaml ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ task: detect
2
+ mode: train
3
+ model: /scratch/project_2007454/ship_detection/yolo_models/yolov8s.pt
4
+ data: /run/nvme/job_15987185/data/processed//yolo_fold3.yaml
5
+ epochs: 200
6
+ patience: 30
7
+ batch: -1
8
+ imgsz: 640
9
+ save: true
10
+ save_period: -1
11
+ cache: disk
12
+ device: 0
13
+ workers: 5
14
+ project: /scratch/project_2007454/ship_detection/runs/yolov8s_Adam
15
+ name: fold_3
16
+ exist_ok: true
17
+ pretrained: false
18
+ optimizer: Adam
19
+ verbose: true
20
+ seed: 0
21
+ deterministic: true
22
+ single_cls: false
23
+ image_weights: false
24
+ rect: false
25
+ cos_lr: false
26
+ close_mosaic: 10
27
+ resume: false
28
+ amp: true
29
+ overlap_mask: true
30
+ mask_ratio: 4
31
+ dropout: 0.0
32
+ val: true
33
+ split: val
34
+ save_json: false
35
+ save_hybrid: false
36
+ conf: null
37
+ iou: 0.7
38
+ max_det: 300
39
+ half: false
40
+ dnn: false
41
+ plots: true
42
+ source: null
43
+ show: false
44
+ save_txt: false
45
+ save_conf: false
46
+ save_crop: false
47
+ hide_labels: false
48
+ hide_conf: false
49
+ vid_stride: 1
50
+ line_thickness: 3
51
+ visualize: false
52
+ augment: false
53
+ agnostic_nms: false
54
+ classes: null
55
+ retina_masks: false
56
+ boxes: true
57
+ format: torchscript
58
+ keras: false
59
+ optimize: false
60
+ int8: false
61
+ dynamic: false
62
+ simplify: false
63
+ opset: null
64
+ workspace: 4
65
+ nms: false
66
+ lr0: 0.001
67
+ lrf: 0.01
68
+ momentum: 0.9
69
+ weight_decay: 0.0
70
+ warmup_epochs: 3.0
71
+ warmup_momentum: 0.8
72
+ warmup_bias_lr: 0.1
73
+ box: 7.5
74
+ cls: 0.5
75
+ dfl: 1.5
76
+ fl_gamma: 0.0
77
+ label_smoothing: 0.0
78
+ nbs: 64
79
+ hsv_h: 0.015
80
+ hsv_s: 0.7
81
+ hsv_v: 0.4
82
+ degrees: 0.0
83
+ translate: 0.1
84
+ scale: 0.25
85
+ shear: 0.0
86
+ perspective: 0.0
87
+ flipud: 0.5
88
+ fliplr: 0.5
89
+ mosaic: 1.0
90
+ mixup: 0.0
91
+ copy_paste: 0.0
92
+ cfg: null
93
+ v5loader: false
94
+ tracker: botsort.yaml
95
+ save_dir: /scratch/project_2007454/ship_detection/runs/yolov8s_Adam/fold_3
onnx_models/YOLOv8s/best.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a4dbc91dd16140c661fb49474b81aedaeacb8063ee32eeb249b768b8e0a5f0b5
3
+ size 44664251
onnx_models/YOLOv8x/args.yaml ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ task: detect
2
+ mode: train
3
+ model: /scratch/project_2007454/ship_detection/yolo_models/yolov8x.pt
4
+ data: /run/nvme/job_15987188/data/processed//yolo_fold3.yaml
5
+ epochs: 200
6
+ patience: 30
7
+ batch: -1
8
+ imgsz: 640
9
+ save: true
10
+ save_period: -1
11
+ cache: disk
12
+ device: 0
13
+ workers: 5
14
+ project: /scratch/project_2007454/ship_detection/runs/yolov8x_Adam
15
+ name: fold_3
16
+ exist_ok: true
17
+ pretrained: false
18
+ optimizer: Adam
19
+ verbose: true
20
+ seed: 0
21
+ deterministic: true
22
+ single_cls: false
23
+ image_weights: false
24
+ rect: false
25
+ cos_lr: false
26
+ close_mosaic: 10
27
+ resume: false
28
+ amp: true
29
+ overlap_mask: true
30
+ mask_ratio: 4
31
+ dropout: 0.0
32
+ val: true
33
+ split: val
34
+ save_json: false
35
+ save_hybrid: false
36
+ conf: null
37
+ iou: 0.7
38
+ max_det: 300
39
+ half: false
40
+ dnn: false
41
+ plots: true
42
+ source: null
43
+ show: false
44
+ save_txt: false
45
+ save_conf: false
46
+ save_crop: false
47
+ hide_labels: false
48
+ hide_conf: false
49
+ vid_stride: 1
50
+ line_thickness: 3
51
+ visualize: false
52
+ augment: false
53
+ agnostic_nms: false
54
+ classes: null
55
+ retina_masks: false
56
+ boxes: true
57
+ format: torchscript
58
+ keras: false
59
+ optimize: false
60
+ int8: false
61
+ dynamic: false
62
+ simplify: false
63
+ opset: null
64
+ workspace: 4
65
+ nms: false
66
+ lr0: 0.001
67
+ lrf: 0.01
68
+ momentum: 0.9
69
+ weight_decay: 0.0
70
+ warmup_epochs: 3.0
71
+ warmup_momentum: 0.8
72
+ warmup_bias_lr: 0.1
73
+ box: 7.5
74
+ cls: 0.5
75
+ dfl: 1.5
76
+ fl_gamma: 0.0
77
+ label_smoothing: 0.0
78
+ nbs: 64
79
+ hsv_h: 0.015
80
+ hsv_s: 0.7
81
+ hsv_v: 0.4
82
+ degrees: 0.0
83
+ translate: 0.1
84
+ scale: 0.25
85
+ shear: 0.0
86
+ perspective: 0.0
87
+ flipud: 0.5
88
+ fliplr: 0.5
89
+ mosaic: 1.0
90
+ mixup: 0.0
91
+ copy_paste: 0.0
92
+ cfg: null
93
+ v5loader: false
94
+ tracker: botsort.yaml
95
+ save_dir: /scratch/project_2007454/ship_detection/runs/yolov8x_Adam/fold_3
onnx_models/YOLOv8x/best.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2367dee31d0eebd95661e86c70b6b7abbf43c5a913a51fda0056bd1fd3e4e33d
3
+ size 272693412
src/__init__.py ADDED
File without changes
src/sahi_onnx.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import logging
3
+ from typing import Any, Dict, List, Optional
4
+
5
+ import numpy as np
6
+
7
+ logger = logging.getLogger(__name__)
8
+
9
+ from sahi.models.base import DetectionModel
10
+ from sahi.prediction import ObjectPrediction
11
+ from sahi.utils.compatibility import fix_full_shape_list, fix_shift_amount_list
12
+ from sahi.utils.import_utils import check_requirements
13
+ from .yolov8_onnx import *
14
+
15
+ class Yolov8onnxDetectionModel(DetectionModel):
16
+ def check_dependencies(self) -> None:
17
+ check_requirements(["ultralytics"])
18
+
19
+ def load_model(self):
20
+ """
21
+ Detection model is initialized and set to self.model.
22
+ """
23
+ import yaml
24
+ from pathlib import Path
25
+ config = yaml.safe_load(Path(self.config_path).read_text())
26
+
27
+ try:
28
+ self.model = Yolov8onnx(
29
+ onnx_model=self.model_path,
30
+ input_width = config['imgsz'],
31
+ input_height = config['imgsz'],
32
+ confidence_thres=self.confidence_threshold,
33
+ iou_thres=0.5,
34
+ device=self.device
35
+ )
36
+ except Exception as e:
37
+ raise TypeError("model_path is not a valid yolov8 model path: ", e)
38
+
39
+ self.category_name_list = list(self.category_mapping.values())
40
+ self.category_name_list_len = len(self.category_name_list)
41
+
42
+
43
+ def perform_inference(self, image: np.ndarray):
44
+ """
45
+ Prediction is performed using self.model and the prediction result is set to self._original_predictions.
46
+ Args:
47
+ image: np.ndarray
48
+ A numpy array that contains the image to be predicted. 3 channel image should be in RGB order.
49
+ """
50
+
51
+ # Confirm model is loaded
52
+ if self.model is None:
53
+ raise ValueError("Model is not loaded, load it by calling .load_model()")
54
+ prediction_result = self.model.inference(image[:, :, ::-1]) # YOLOv8 expects numpy arrays to have BGR
55
+ #prediction_result = [
56
+ # result.boxes.data[result.boxes.data[:, 4] >= self.confidence_threshold] for result in prediction_result
57
+ #]
58
+ self._original_predictions = [prediction_result]
59
+
60
+ @property
61
+ def num_categories(self):
62
+ return self.category_name_list_len
63
+
64
+ @property
65
+ def has_mask(self):
66
+ return False
67
+
68
+ @property
69
+ def category_names(self):
70
+ return self.category_name_list
71
+
72
+ def _create_object_prediction_list_from_original_predictions(
73
+ self,
74
+ shift_amount_list: Optional[List[List[int]]] = [[0, 0]],
75
+ full_shape_list: Optional[List[List[int]]] = None,
76
+ ):
77
+ """
78
+ self._original_predictions is converted to a list of prediction.ObjectPrediction and set to
79
+ self._object_prediction_list_per_image.
80
+ Args:
81
+ shift_amount_list: list of list
82
+ To shift the box and mask predictions from sliced image to full sized image, should
83
+ be in the form of List[[shift_x, shift_y],[shift_x, shift_y],...]
84
+ full_shape_list: list of list
85
+ Size of the full image after shifting, should be in the form of
86
+ List[[height, width],[height, width],...]
87
+ """
88
+ original_predictions = self._original_predictions
89
+
90
+ # compatilibty for sahi v0.8.15
91
+ shift_amount_list = fix_shift_amount_list(shift_amount_list)
92
+ full_shape_list = fix_full_shape_list(full_shape_list)
93
+
94
+ # handle all predictions
95
+ object_prediction_list_per_image = []
96
+ for image_ind, original_prediction in enumerate(original_predictions):
97
+ bboxes = original_prediction[0]
98
+ scores = original_prediction[1]
99
+ class_ids = original_prediction[2]
100
+ shift_amount = shift_amount_list[image_ind]
101
+ full_shape = None if full_shape_list is None else full_shape_list[image_ind]
102
+ object_prediction_list = []
103
+ # process predictions
104
+ for original_bbox, score, category_id in zip(bboxes, scores, class_ids):
105
+ x1 = original_bbox[0]
106
+ y1 = original_bbox[1]
107
+ x2 = original_bbox[2]
108
+ y2 = original_bbox[3]
109
+ bbox = [x1, y1, x2, y2]
110
+ category_name = self.category_mapping[str(category_id)]
111
+
112
+ # fix negative box coords
113
+ bbox[0] = max(0, bbox[0])
114
+ bbox[1] = max(0, bbox[1])
115
+ bbox[2] = max(0, bbox[2])
116
+ bbox[3] = max(0, bbox[3])
117
+
118
+ # fix out of image box coords
119
+ if full_shape is not None:
120
+ bbox[0] = min(full_shape[1], bbox[0])
121
+ bbox[1] = min(full_shape[0], bbox[1])
122
+ bbox[2] = min(full_shape[1], bbox[2])
123
+ bbox[3] = min(full_shape[0], bbox[3])
124
+
125
+ # ignore invalid predictions
126
+ if not (bbox[0] < bbox[2]) or not (bbox[1] < bbox[3]):
127
+ logger.warning(f"ignoring invalid prediction with bbox: {bbox}")
128
+ continue
129
+
130
+ object_prediction = ObjectPrediction(
131
+ bbox=bbox,
132
+ category_id=category_id,
133
+ score=score,
134
+ bool_mask=None,
135
+ category_name=category_name,
136
+ shift_amount=shift_amount,
137
+ full_shape=full_shape,
138
+ )
139
+ object_prediction_list.append(object_prediction)
140
+ object_prediction_list_per_image.append(object_prediction_list)
141
+
142
+ self._object_prediction_list_per_image = object_prediction_list_per_image
src/yolov8_onnx.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import onnxruntime as ort
4
+ import torch
5
+ import copy
6
+
7
+ from ultralytics.utils import ROOT, yaml_load
8
+ from ultralytics.utils.checks import check_requirements, check_yaml
9
+
10
+
11
+ class Yolov8onnx:
12
+
13
+ def __init__(self,
14
+ onnx_model,
15
+ input_width,
16
+ input_height,
17
+ confidence_thres,
18
+ iou_thres,
19
+ device='cpu'):
20
+ """
21
+ Initializes an instance of the Yolov8 class.
22
+
23
+ Args:
24
+ onnx_model: Path to the ONNX model.
25
+ confidence_thres: Confidence threshold for filtering detections.
26
+ iou_thres: IoU (Intersection over Union) threshold for non-maximum suppression.
27
+ """
28
+ self.onnx_model = onnx_model
29
+ self.confidence_thres = confidence_thres
30
+ self.iou_thres = iou_thres
31
+ self.input_width = input_width
32
+ self.input_height = input_height
33
+ if device == 'cpu':
34
+ providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
35
+ else:
36
+ providers = ['CPUExecutionProvider']
37
+
38
+ self.onnx_session = ort.InferenceSession(
39
+ onnx_model,
40
+ providers=providers
41
+ )
42
+
43
+ self.input_name = self.onnx_session.get_inputs()[0].name
44
+ self.output_name = self.onnx_session.get_outputs()[0].name
45
+
46
+
47
+ def preprocess(self, input_image):
48
+ """
49
+ Preprocesses the input image before performing inference.
50
+
51
+ Returns:
52
+ image_data: Preprocessed image data ready for inference.
53
+ """
54
+ # Read the input image using OpenCV
55
+ self.img = input_image
56
+
57
+ # Get the height and width of the input image
58
+ self.img_height, self.img_width = self.img.shape[:2]
59
+
60
+ # Convert the image color space from BGR to RGB
61
+ img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
62
+
63
+ # Resize the image to match the input shape
64
+ img = cv2.resize(img, (self.input_width, self.input_height))
65
+
66
+ # Normalize the image data by dividing it by 255.0
67
+ image_data = np.array(img) / 255.0
68
+
69
+ # Transpose the image to have the channel dimension as the first dimension
70
+ image_data = np.transpose(image_data, (2, 0, 1)) # Channel first
71
+
72
+ # Expand the dimensions of the image data to match the expected input shape
73
+ image_data = np.expand_dims(image_data, axis=0).astype(np.float32)
74
+
75
+ # Return the preprocessed image data
76
+ return image_data
77
+
78
+ def postprocess(self, output):
79
+ """
80
+ Performs post-processing on the model's output to extract bounding boxes, scores, and class IDs.
81
+ """
82
+
83
+ # Transpose and squeeze the output to match the expected shape
84
+ outputs = np.transpose(np.squeeze(output[0]))
85
+
86
+ # Get the number of rows in the outputs array
87
+ rows = outputs.shape[0]
88
+ # Lists to store the bounding boxes, scores, and class IDs of the detections
89
+ boxes = []
90
+ scores = []
91
+ class_ids = []
92
+
93
+ # Calculate the scaling factors for the bounding box coordinates
94
+ x_factor = self.img_width / self.input_width
95
+ y_factor = self.img_height / self.input_height
96
+ # Iterate over each row in the outputs array
97
+ for i in range(rows):
98
+ # Extract the class scores from the current row
99
+ classes_scores = outputs[i][4:]
100
+
101
+ # Find the maximum score among the class scores
102
+ max_score = np.amax(classes_scores)
103
+ # If the maximum score is above the confidence threshold
104
+ if max_score >= self.confidence_thres:
105
+ # Get the class ID with the highest score
106
+ class_id = np.argmax(classes_scores)
107
+
108
+ # Extract the bounding box coordinates from the current row
109
+ x, y, w, h = outputs[i][0], outputs[i][1], outputs[i][2], outputs[i][3]
110
+
111
+ # Calculate the scaled coordinates of the bounding box
112
+ left = int((x - w / 2) * x_factor)
113
+ top = int((y - h / 2) * y_factor)
114
+ width = int(w * x_factor)
115
+ height = int(h * y_factor)
116
+
117
+ # Add the class ID, score, and box coordinates to the respective lists
118
+ class_ids.append(int(class_id))
119
+ scores.append(max_score)
120
+ boxes.append([left, top, left+width, top+height])
121
+ # Apply non-maximum suppression to filter out overlapping bounding boxes
122
+ indices = cv2.dnn.NMSBoxes(boxes, scores, self.confidence_thres, self.iou_thres)
123
+ output_boxes = [boxes[i] for i in indices]
124
+ output_scores = [scores[i] for i in indices]
125
+ output_classes = [class_ids[i] for i in indices]
126
+
127
+ # Return the outputs
128
+ return output_boxes, output_scores, output_classes
129
+
130
+ def inference(self, image):
131
+ """
132
+ Performs inference using an ONNX model and returns the output image with drawn detections.
133
+
134
+ Returns:
135
+ output_img: The output image with drawn detections.
136
+ """
137
+ # Create an inference session using the ONNX model and specify execution providers
138
+ temp_image = copy.deepcopy(image)
139
+ image_height, image_width = image.shape[0], image.shape[1]
140
+
141
+ # Preprocess the image data
142
+ img_data = self.preprocess(temp_image)
143
+ # Run inference using the preprocessed image data
144
+ outputs = self.onnx_session.run(None, {self.input_name: img_data})
145
+ # Perform post-processing on the outputs to obtain output image.
146
+ bboxes, scores, class_ids = self.postprocess(outputs)
147
+ # Return the resulting output image
148
+ return bboxes, scores, class_ids