From 3946e482413f421b6ec7470c0a46b2d57c2a6947 Mon Sep 17 00:00:00 2001 From: "-T.K.-" Date: Mon, 15 Jul 2024 03:02:51 -0700 Subject: [PATCH] ADD: add yolov8 example --- .gitignore | 3 +- examples/yolov8/README.md | 364 ++++++++++++++++++++++++++++++++++++++ examples/yolov8/test.py | 15 ++ 3 files changed, 380 insertions(+), 2 deletions(-) create mode 100644 examples/yolov8/README.md create mode 100644 examples/yolov8/test.py diff --git a/.gitignore b/.gitignore index 6d819eb..f256620 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,5 @@ share/python-wheels/ MANIFEST examples/mnist/data/ - examples/llama2/checkpoints/stories15M.bin - +examples/yolov8/yolov8n.pt diff --git a/examples/yolov8/README.md b/examples/yolov8/README.md new file mode 100644 index 0000000..0ce82e9 --- /dev/null +++ b/examples/yolov8/README.md @@ -0,0 +1,364 @@ + +# YOLO v8 + + +## Model Architecture + +```python +YOLO( + (model): DetectionModel( + (model): Sequential( + (0): Conv( + (conv): Conv2d(3, 16, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) + (bn): BatchNorm2d(16, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (1): Conv( + (conv): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) + (bn): BatchNorm2d(32, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (2): C2f( + (cv1): Conv( + (conv): Conv2d(32, 32, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(32, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(48, 32, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(32, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (m): ModuleList( + (0): Bottleneck( + (cv1): Conv( + (conv): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(16, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(16, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + ) + ) + ) + (3): Conv( + (conv): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (4): C2f( + (cv1): Conv( + (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(128, 64, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (m): ModuleList( + (0-1): 2 x Bottleneck( + (cv1): Conv( + (conv): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(32, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(32, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + ) + ) + ) + (5): Conv( + (conv): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (6): C2f( + (cv1): Conv( + (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (m): ModuleList( + (0-1): 2 x Bottleneck( + (cv1): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + ) + ) + ) + (7): Conv( + (conv): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) + (bn): BatchNorm2d(256, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (8): C2f( + (cv1): Conv( + (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(256, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(384, 256, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(256, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (m): ModuleList( + (0): Bottleneck( + (cv1): Conv( + (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + ) + ) + ) + (9): SPPF( + (cv1): Conv( + (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(256, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (m): MaxPool2d(kernel_size=5, stride=1, padding=2, dilation=1, ceil_mode=False) + ) + (10): Upsample(scale_factor=2.0, mode='nearest') + (11): Concat() + (12): C2f( + (cv1): Conv( + (conv): Conv2d(384, 128, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(192, 128, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (m): ModuleList( + (0): Bottleneck( + (cv1): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + ) + ) + ) + (13): Upsample(scale_factor=2.0, mode='nearest') + (14): Concat() + (15): C2f( + (cv1): Conv( + (conv): Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(96, 64, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (m): ModuleList( + (0): Bottleneck( + (cv1): Conv( + (conv): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(32, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(32, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + ) + ) + ) + (16): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (17): Concat() + (18): C2f( + (cv1): Conv( + (conv): Conv2d(192, 128, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(192, 128, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (m): ModuleList( + (0): Bottleneck( + (cv1): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + ) + ) + ) + (19): Conv( + (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (20): Concat() + (21): C2f( + (cv1): Conv( + (conv): Conv2d(384, 256, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(256, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(384, 256, kernel_size=(1, 1), stride=(1, 1), bias=False) + (bn): BatchNorm2d(256, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (m): ModuleList( + (0): Bottleneck( + (cv1): Conv( + (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (cv2): Conv( + (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(128, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + ) + ) + ) + (22): Detect( + (cv2): ModuleList( + (0): Sequential( + (0): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (1): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (2): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1)) + ) + (1): Sequential( + (0): Conv( + (conv): Conv2d(128, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (1): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (2): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1)) + ) + (2): Sequential( + (0): Conv( + (conv): Conv2d(256, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (1): Conv( + (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(64, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (2): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1)) + ) + ) + (cv3): ModuleList( + (0): Sequential( + (0): Conv( + (conv): Conv2d(64, 80, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(80, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (1): Conv( + (conv): Conv2d(80, 80, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(80, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (2): Conv2d(80, 80, kernel_size=(1, 1), stride=(1, 1)) + ) + (1): Sequential( + (0): Conv( + (conv): Conv2d(128, 80, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(80, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (1): Conv( + (conv): Conv2d(80, 80, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(80, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (2): Conv2d(80, 80, kernel_size=(1, 1), stride=(1, 1)) + ) + (2): Sequential( + (0): Conv( + (conv): Conv2d(256, 80, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(80, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (1): Conv( + (conv): Conv2d(80, 80, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) + (bn): BatchNorm2d(80, eps=0.001, momentum=0.03, affine=True, track_running_stats=True) + (act): SiLU(inplace=True) + ) + (2): Conv2d(80, 80, kernel_size=(1, 1), stride=(1, 1)) + ) + ) + (dfl): DFL( + (conv): Conv2d(16, 1, kernel_size=(1, 1), stride=(1, 1), bias=False) + ) + ) + ) + ) +) + +``` \ No newline at end of file diff --git a/examples/yolov8/test.py b/examples/yolov8/test.py new file mode 100644 index 0000000..81e368c --- /dev/null +++ b/examples/yolov8/test.py @@ -0,0 +1,15 @@ +import torch +from ultralytics import YOLO + +# Load a model +model = YOLO("yolov8n.yaml") # build a new model from scratch +model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training) + +breakpoint() + +# Use the model +model.train(data="coco128.yaml", epochs=3) # train the model +metrics = model.val() # evaluate model performance on the validation set +results = model("https://ultralytics.com/images/bus.jpg") # predict on an image + +print(results)