kyolo 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. kyolo-0.1.0/LICENSE +201 -0
  2. kyolo-0.1.0/PKG-INFO +254 -0
  3. kyolo-0.1.0/README.md +210 -0
  4. kyolo-0.1.0/kyolo/__init__.py +55 -0
  5. kyolo-0.1.0/kyolo/conversion/__init__.py +46 -0
  6. kyolo-0.1.0/kyolo/conversion/cli.py +117 -0
  7. kyolo-0.1.0/kyolo/conversion/convert.py +429 -0
  8. kyolo-0.1.0/kyolo/conversion/exceptions.py +51 -0
  9. kyolo-0.1.0/kyolo/conversion/mappings.py +89 -0
  10. kyolo-0.1.0/kyolo/heads/__init__.py +5 -0
  11. kyolo-0.1.0/kyolo/heads/detect.py +91 -0
  12. kyolo-0.1.0/kyolo/layers/__init__.py +79 -0
  13. kyolo-0.1.0/kyolo/layers/attention.py +264 -0
  14. kyolo-0.1.0/kyolo/layers/blocks.py +253 -0
  15. kyolo-0.1.0/kyolo/layers/common.py +173 -0
  16. kyolo-0.1.0/kyolo/layers/dfl.py +60 -0
  17. kyolo-0.1.0/kyolo/layers/letterbox.py +151 -0
  18. kyolo-0.1.0/kyolo/layers/reparam.py +256 -0
  19. kyolo-0.1.0/kyolo/losses/__init__.py +5 -0
  20. kyolo-0.1.0/kyolo/losses/detection_loss.py +155 -0
  21. kyolo-0.1.0/kyolo/models/__init__.py +213 -0
  22. kyolo-0.1.0/kyolo/models/base.py +112 -0
  23. kyolo-0.1.0/kyolo/models/yolo11/__init__.py +93 -0
  24. kyolo-0.1.0/kyolo/models/yolo11/config.py +14 -0
  25. kyolo-0.1.0/kyolo/models/yolo11/convert_yolo11_torch_to_keras.py +57 -0
  26. kyolo-0.1.0/kyolo/models/yolo11/yolo11_model.py +90 -0
  27. kyolo-0.1.0/kyolo/models/yolo12/__init__.py +93 -0
  28. kyolo-0.1.0/kyolo/models/yolo12/config.py +14 -0
  29. kyolo-0.1.0/kyolo/models/yolo12/convert_yolo12_torch_to_keras.py +57 -0
  30. kyolo-0.1.0/kyolo/models/yolo12/yolo12_model.py +93 -0
  31. kyolo-0.1.0/kyolo/models/yolo26/__init__.py +93 -0
  32. kyolo-0.1.0/kyolo/models/yolo26/config.py +14 -0
  33. kyolo-0.1.0/kyolo/models/yolo26/convert_yolo26_torch_to_keras.py +57 -0
  34. kyolo-0.1.0/kyolo/models/yolo26/yolo26_model.py +104 -0
  35. kyolo-0.1.0/kyolo/models/yolov10/__init__.py +109 -0
  36. kyolo-0.1.0/kyolo/models/yolov10/config.py +15 -0
  37. kyolo-0.1.0/kyolo/models/yolov10/convert_yolov10_torch_to_keras.py +57 -0
  38. kyolo-0.1.0/kyolo/models/yolov10/yolov10_model.py +100 -0
  39. kyolo-0.1.0/kyolo/models/yolov5/__init__.py +93 -0
  40. kyolo-0.1.0/kyolo/models/yolov5/config.py +14 -0
  41. kyolo-0.1.0/kyolo/models/yolov5/convert_yolov5_torch_to_keras.py +57 -0
  42. kyolo-0.1.0/kyolo/models/yolov5/yolov5_model.py +95 -0
  43. kyolo-0.1.0/kyolo/models/yolov6/__init__.py +77 -0
  44. kyolo-0.1.0/kyolo/models/yolov6/config.py +13 -0
  45. kyolo-0.1.0/kyolo/models/yolov6/convert_yolov6_torch_to_keras.py +57 -0
  46. kyolo-0.1.0/kyolo/models/yolov6/yolov6_model.py +101 -0
  47. kyolo-0.1.0/kyolo/models/yolov7/__init__.py +63 -0
  48. kyolo-0.1.0/kyolo/models/yolov7/config.py +13 -0
  49. kyolo-0.1.0/kyolo/models/yolov7/convert_yolov7_torch_to_keras.py +57 -0
  50. kyolo-0.1.0/kyolo/models/yolov7/yolov7_model.py +106 -0
  51. kyolo-0.1.0/kyolo/models/yolov8/__init__.py +93 -0
  52. kyolo-0.1.0/kyolo/models/yolov8/config.py +14 -0
  53. kyolo-0.1.0/kyolo/models/yolov8/convert_yolov8_torch_to_keras.py +57 -0
  54. kyolo-0.1.0/kyolo/models/yolov8/yolov8_model.py +88 -0
  55. kyolo-0.1.0/kyolo/models/yolov9/__init__.py +93 -0
  56. kyolo-0.1.0/kyolo/models/yolov9/config.py +14 -0
  57. kyolo-0.1.0/kyolo/models/yolov9/convert_yolov9_torch_to_keras.py +57 -0
  58. kyolo-0.1.0/kyolo/models/yolov9/yolov9_model.py +95 -0
  59. kyolo-0.1.0/kyolo/ops/__init__.py +24 -0
  60. kyolo-0.1.0/kyolo/ops/anchors.py +123 -0
  61. kyolo-0.1.0/kyolo/ops/boxes.py +114 -0
  62. kyolo-0.1.0/kyolo/ops/tal.py +152 -0
  63. kyolo-0.1.0/kyolo/postprocessing/__init__.py +17 -0
  64. kyolo-0.1.0/kyolo/postprocessing/nms.py +171 -0
  65. kyolo-0.1.0/kyolo/postprocessing/postprocessor.py +90 -0
  66. kyolo-0.1.0/kyolo/preprocessing/__init__.py +5 -0
  67. kyolo-0.1.0/kyolo/preprocessing/preprocessor.py +131 -0
  68. kyolo-0.1.0/kyolo/training/__init__.py +5 -0
  69. kyolo-0.1.0/kyolo/training/detector.py +120 -0
  70. kyolo-0.1.0/kyolo/utils/__init__.py +20 -0
  71. kyolo-0.1.0/kyolo/utils/coco.py +127 -0
  72. kyolo-0.1.0/kyolo/utils/visualization.py +209 -0
  73. kyolo-0.1.0/kyolo/version.py +5 -0
  74. kyolo-0.1.0/kyolo.egg-info/PKG-INFO +254 -0
  75. kyolo-0.1.0/kyolo.egg-info/SOURCES.txt +79 -0
  76. kyolo-0.1.0/kyolo.egg-info/dependency_links.txt +1 -0
  77. kyolo-0.1.0/kyolo.egg-info/entry_points.txt +2 -0
  78. kyolo-0.1.0/kyolo.egg-info/requires.txt +24 -0
  79. kyolo-0.1.0/kyolo.egg-info/top_level.txt +1 -0
  80. kyolo-0.1.0/pyproject.toml +91 -0
  81. kyolo-0.1.0/setup.cfg +4 -0
kyolo-0.1.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
kyolo-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,254 @@
1
+ Metadata-Version: 2.4
2
+ Name: kyolo
3
+ Version: 0.1.0
4
+ Summary: kyolo: the YOLO object-detection family (v5-v26) in pure Keras 3, with PyTorch weight conversion.
5
+ Author-email: Gitesh Chawda <gitesh.ch.0912@gmail.com>
6
+ License: Apache License 2.0
7
+ Project-URL: homepage, https://github.com/IMvision12/kyolo
8
+ Project-URL: documentation, https://github.com/IMvision12/kyolo
9
+ Project-URL: repository, https://github.com/IMvision12/kyolo.git
10
+ Keywords: keras,keras3,yolo,object-detection,computer-vision,deep-learning,jax,tensorflow,pytorch,neural-networks,pretrained-weights,transfer-learning
11
+ Classifier: Intended Audience :: Education
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Scientific/Engineering
19
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
+ Classifier: Topic :: Software Development :: Libraries
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Requires-Python: >=3.9
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: keras>=3.5.0
26
+ Requires-Dist: numpy>=1.24
27
+ Provides-Extra: tensorflow
28
+ Requires-Dist: tensorflow>=2.16; extra == "tensorflow"
29
+ Provides-Extra: jax
30
+ Requires-Dist: jax>=0.4.28; extra == "jax"
31
+ Provides-Extra: torch
32
+ Requires-Dist: torch>=2.2; extra == "torch"
33
+ Requires-Dist: torchvision>=0.17; extra == "torch"
34
+ Provides-Extra: conversion
35
+ Requires-Dist: torch>=2.2; extra == "conversion"
36
+ Requires-Dist: ultralytics>=8.2.0; extra == "conversion"
37
+ Provides-Extra: viz
38
+ Requires-Dist: matplotlib>=3.7; extra == "viz"
39
+ Requires-Dist: pillow>=10.0; extra == "viz"
40
+ Provides-Extra: tests
41
+ Requires-Dist: pytest; extra == "tests"
42
+ Requires-Dist: pytest-cov; extra == "tests"
43
+ Dynamic: license-file
44
+
45
+ # kyolo — YOLO in pure Keras 3
46
+
47
+ `kyolo` is a pure [Keras 3](https://keras.io) implementation of the YOLO
48
+ object-detection family (YOLOv5, v6, v7, v8, v9, v10, YOLO11, YOLO12, YOLO26).
49
+ Every layer, loss, and pre/post-processing step is written entirely in
50
+ `keras.ops`, so the exact same code runs unchanged on the **TensorFlow**,
51
+ **JAX**, and **PyTorch** backends. It ships with best-effort utilities for
52
+ converting the official PyTorch checkpoints into Keras `.weights.h5` files.
53
+
54
+ ## Supported models
55
+
56
+ | Family | Variants | Detection head |
57
+ | -------- | ---------------------------- | -------------------------------------- |
58
+ | YOLOv5 | `n` `s` `m` `l` `x` | Classic anchor-based (NMS) |
59
+ | YOLOv6 | `n` `s` `m` `l` | Anchor-free, DFL (NMS) |
60
+ | YOLOv7 | `yolov7` `yolov7-tiny` `yolov7-x` | Classic anchor-based (NMS) |
61
+ | YOLOv8 | `n` `s` `m` `l` `x` | Anchor-free, DFL (NMS) |
62
+ | YOLOv9 | `t` `s` `m` `c` `e` | Anchor-free, DFL (NMS) |
63
+ | YOLOv10 | `n` `s` `m` `b` `l` `x` | Anchor-free, DFL, **end-to-end (NMS-free)** |
64
+ | YOLO11 | `n` `s` `m` `l` `x` | Anchor-free, DFL (NMS) |
65
+ | YOLO12 | `n` `s` `m` `l` `x` | Anchor-free, DFL (NMS) |
66
+ | YOLO26 | `n` `s` `m` `l` `x` | Anchor-free, DFL, **end-to-end (NMS-free)** |
67
+
68
+ Regardless of family, a model's forward pass returns a **list of 3 raw feature
69
+ maps** `[P3, P4, P5]`, each of shape `(B, Hi, Wi, 4 * reg_max + nc)` in
70
+ channels-last layout, with `reg_max = 16` and strides `(8, 16, 32)`. The
71
+ anchor-free families (v6/v8/v9/11/12/26) decode boxes with a Distribution Focal
72
+ Loss (DFL) regression head; the end-to-end families (v10, v26) are trained to be
73
+ NMS-free but a standard NMS postprocessor is still available for the rest.
74
+
75
+ ## Installation
76
+
77
+ `kyolo` needs Keras 3 plus **exactly one** backend. Install the package in
78
+ editable mode with the backend extra you want:
79
+
80
+ ```bash
81
+ # pick ONE backend
82
+ pip install -e ".[tensorflow]"
83
+ pip install -e ".[jax]"
84
+ pip install -e ".[torch]"
85
+
86
+ # optional extras
87
+ pip install -e ".[viz]" # matplotlib + pillow, for the examples
88
+ pip install -e ".[conversion]" # torch + ultralytics, for weight conversion
89
+ pip install -e ".[all]" # tensorflow + convert + viz + dev
90
+ ```
91
+
92
+ Select the active backend with the `KERAS_BACKEND` environment variable before
93
+ importing anything:
94
+
95
+ ```bash
96
+ export KERAS_BACKEND=tensorflow # or "jax" / "torch"
97
+ ```
98
+
99
+ ```python
100
+ import os
101
+ os.environ["KERAS_BACKEND"] = "tensorflow" # must be set before `import keras`
102
+ ```
103
+
104
+ ## Quickstart: inference
105
+
106
+ ```python
107
+ import keras
108
+
109
+ from kyolo.models import yolov8n # every variant is a factory: yolov5n, yolo11s, ...
110
+ from kyolo.preprocessing import YOLOPreprocessor
111
+ from kyolo.postprocessing import YOLOPostprocessor
112
+ from kyolo.utils import visualize_detections, COCO_CLASS_NAMES
113
+
114
+ # 1. Build a model (deploy=True fuses reparameterizable branches for inference).
115
+ model = yolov8n(nc=80, input_shape=(640, 640, 3), deploy=True)
116
+ # model.load_weights("yolov8n.weights.h5") # a converted checkpoint (see below)
117
+
118
+ # 2. Preprocess: letterbox to a square and (optionally) normalize to [0, 1].
119
+ preprocessor = YOLOPreprocessor(image_size=640, normalize=True, letterbox=True)
120
+ batch = preprocessor.from_files("assets/bird.png")
121
+ # batch == {"images": (B, 640, 640, 3), "ratio": (B, 2), "pad": (B, 2)}
122
+
123
+ # 3. Forward pass -> list of 3 raw feature maps [P3, P4, P5].
124
+ raw_feats = model(batch["images"])
125
+
126
+ # 4. Postprocess -> (B, max_detections, 6) = [x1, y1, x2, y2, score, class_id].
127
+ postprocessor = YOLOPostprocessor(
128
+ nc=80, reg_max=16, strides=(8, 16, 32),
129
+ conf_threshold=0.25, iou_threshold=0.7, max_detections=300,
130
+ )
131
+ detections = postprocessor(raw_feats)
132
+
133
+ # 5. Visualize the first image in the batch.
134
+ image = keras.ops.convert_to_numpy(batch["images"])[0]
135
+ dets = keras.ops.convert_to_numpy(detections)[0]
136
+ visualize_detections(image, dets, class_names=COCO_CLASS_NAMES,
137
+ save_path="assets/inference_result.png")
138
+ ```
139
+
140
+ A runnable version lives in [`examples/inference.py`](examples/inference.py).
141
+
142
+ ## Training / fine-tuning
143
+
144
+ `kyolo.training.YOLODetector` is a `keras.Model` subclass that plugs into
145
+ `.fit()`. Datasets yield `(images, targets)` tuples where `targets` is
146
+ `{"boxes", "labels", "mask"}` (boxes are xyxy pixels, labels are ints, mask
147
+ marks real vs. padded boxes). `detect.detect(...)` runs the full decode + NMS
148
+ pipeline in one call:
149
+
150
+ ```python
151
+ import keras
152
+
153
+ from kyolo.models import yolov8n
154
+ from kyolo.losses import YOLODetectionLoss
155
+ from kyolo.training import YOLODetector
156
+
157
+ nc = 80
158
+ model = yolov8n(nc=nc)
159
+
160
+ # Fine-tuning: load converted weights onto the bare model BEFORE wrapping it.
161
+ # model.load_weights("yolov8n.weights.h5") # AGPL-3.0 converted checkpoint
162
+
163
+ # Freeze the backbone so only the neck + detection head train:
164
+ for layer in model.layers:
165
+ if layer.name.startswith("backbone"):
166
+ layer.trainable = False
167
+
168
+ loss = YOLODetectionLoss(nc=nc, reg_max=16, strides=(8, 16, 32))
169
+ detector = YOLODetector(model, loss=loss, nc=nc, reg_max=16)
170
+ detector.compile(optimizer=keras.optimizers.Adam(1e-3))
171
+
172
+ # `dataset` yields (images, {"boxes", "labels", "mask"}) tuples.
173
+ detector.fit(dataset, epochs=1, steps_per_epoch=2)
174
+
175
+ # Convenience: run the full detect -> NMS pipeline in one call.
176
+ detections = detector.detect(images, conf=0.25, iou=0.7)
177
+ ```
178
+
179
+ A runnable synthetic-data version lives in [`examples/train.py`](examples/train.py).
180
+
181
+ ## Weight conversion
182
+
183
+ The official YOLO checkpoints are **AGPL-3.0 licensed** and are **not**
184
+ redistributed with this project. To use pretrained weights you must convert an
185
+ official `.pt` file yourself. Each model ships a co-located converter:
186
+
187
+ ```bash
188
+ # per-model converter (co-located under kyolo/models/<name>/)
189
+ python -m kyolo.models.yolov8.convert_yolov8_torch_to_keras \
190
+ --weights yolov8n.pt --output yolov8n.weights.h5 --variant n
191
+
192
+ # or the unified CLI (installed as `kyolo-convert`)
193
+ kyolo-convert --model yolov8n --weights yolov8n.pt --output yolov8n.weights.h5
194
+ ```
195
+
196
+ Conversion requires the `conversion` extra (`pip install -e ".[conversion]"`,
197
+ which pulls in `torch` and `ultralytics`). The programmatic entry point is
198
+ `from kyolo.conversion import convert_weights`.
199
+
200
+ > Conversion is **best-effort**: layer-name and tensor-layout mappings are
201
+ > maintained by hand, so always validate a converted model's outputs against the
202
+ > reference PyTorch implementation before trusting it.
203
+
204
+ ## Package layout
205
+
206
+ The repo follows the [KerasFormers](https://github.com/IMvision12/KerasFormers)
207
+ layout: a flat top-level package with one self-contained folder per model.
208
+
209
+ ```
210
+ kyolo/ # flat package (no src/)
211
+ ├── __init__.py
212
+ ├── version.py # single source of truth for the version → releases
213
+ ├── models/
214
+ │ ├── __init__.py # per-variant factories (yolov8n, yolo11s, ...)
215
+ │ ├── base.py # shared assembly (image_input, finalize_detector)
216
+ │ └── yolov8/ # one folder per model (v5, v6, v7, v8, v9, v10, 11, 12, 26)
217
+ │ ├── __init__.py
218
+ │ ├── config.py # scale variants
219
+ │ ├── yolov8_model.py # architecture
220
+ │ └── convert_yolov8_torch_to_keras.py # co-located weight converter
221
+ ├── heads/ # detection head
222
+ ├── layers/ # conv/bn, CSP/ELAN/GELAN/attention blocks, DFL, letterbox
223
+ ├── ops/ # anchors, box math, task-aligned assigner (pure keras.ops)
224
+ ├── losses/ # YOLODetectionLoss
225
+ ├── preprocessing/ # YOLOPreprocessor
226
+ ├── postprocessing/ # YOLOPostprocessor + pure-ops NMS
227
+ ├── training/ # YOLODetector (keras.Model subclass)
228
+ ├── conversion/ # convert_weights, name mappings, exceptions, CLI
229
+ └── utils/ # COCO metadata + visualization
230
+
231
+ .github/workflows/ # release.yml (auto-release) + test_code.yml (CI)
232
+ docs/ # one page per model
233
+ tests/integration/ # build + processing smoke tests
234
+ examples/ # inference.py, train.py
235
+ ```
236
+
237
+ ## Releasing
238
+
239
+ Releases are automated. Bump `__version__` in
240
+ [`kyolo/version.py`](kyolo/version.py) and merge to `main`:
241
+ [`.github/workflows/release.yml`](.github/workflows/release.yml) detects the
242
+ change, creates the `v<version>` GitHub release, and publishes the wheel + sdist
243
+ to PyPI (via Trusted Publishing — no API token). The package version is read
244
+ dynamically from `kyolo.__version__`, so `version.py` is the single source of
245
+ truth.
246
+
247
+ ## License
248
+
249
+ - The **code** in this repository is licensed under the
250
+ [Apache License 2.0](LICENSE).
251
+ - The official YOLO **weights** are licensed under
252
+ [AGPL-3.0](https://github.com/ultralytics/ultralytics/blob/main/LICENSE).
253
+ They are not shipped here; anything you convert from them inherits AGPL-3.0,
254
+ and using such weights subjects your project to the AGPL-3.0 requirements.
kyolo-0.1.0/README.md ADDED
@@ -0,0 +1,210 @@
1
+ # kyolo — YOLO in pure Keras 3
2
+
3
+ `kyolo` is a pure [Keras 3](https://keras.io) implementation of the YOLO
4
+ object-detection family (YOLOv5, v6, v7, v8, v9, v10, YOLO11, YOLO12, YOLO26).
5
+ Every layer, loss, and pre/post-processing step is written entirely in
6
+ `keras.ops`, so the exact same code runs unchanged on the **TensorFlow**,
7
+ **JAX**, and **PyTorch** backends. It ships with best-effort utilities for
8
+ converting the official PyTorch checkpoints into Keras `.weights.h5` files.
9
+
10
+ ## Supported models
11
+
12
+ | Family | Variants | Detection head |
13
+ | -------- | ---------------------------- | -------------------------------------- |
14
+ | YOLOv5 | `n` `s` `m` `l` `x` | Classic anchor-based (NMS) |
15
+ | YOLOv6 | `n` `s` `m` `l` | Anchor-free, DFL (NMS) |
16
+ | YOLOv7 | `yolov7` `yolov7-tiny` `yolov7-x` | Classic anchor-based (NMS) |
17
+ | YOLOv8 | `n` `s` `m` `l` `x` | Anchor-free, DFL (NMS) |
18
+ | YOLOv9 | `t` `s` `m` `c` `e` | Anchor-free, DFL (NMS) |
19
+ | YOLOv10 | `n` `s` `m` `b` `l` `x` | Anchor-free, DFL, **end-to-end (NMS-free)** |
20
+ | YOLO11 | `n` `s` `m` `l` `x` | Anchor-free, DFL (NMS) |
21
+ | YOLO12 | `n` `s` `m` `l` `x` | Anchor-free, DFL (NMS) |
22
+ | YOLO26 | `n` `s` `m` `l` `x` | Anchor-free, DFL, **end-to-end (NMS-free)** |
23
+
24
+ Regardless of family, a model's forward pass returns a **list of 3 raw feature
25
+ maps** `[P3, P4, P5]`, each of shape `(B, Hi, Wi, 4 * reg_max + nc)` in
26
+ channels-last layout, with `reg_max = 16` and strides `(8, 16, 32)`. The
27
+ anchor-free families (v6/v8/v9/11/12/26) decode boxes with a Distribution Focal
28
+ Loss (DFL) regression head; the end-to-end families (v10, v26) are trained to be
29
+ NMS-free but a standard NMS postprocessor is still available for the rest.
30
+
31
+ ## Installation
32
+
33
+ `kyolo` needs Keras 3 plus **exactly one** backend. Install the package in
34
+ editable mode with the backend extra you want:
35
+
36
+ ```bash
37
+ # pick ONE backend
38
+ pip install -e ".[tensorflow]"
39
+ pip install -e ".[jax]"
40
+ pip install -e ".[torch]"
41
+
42
+ # optional extras
43
+ pip install -e ".[viz]" # matplotlib + pillow, for the examples
44
+ pip install -e ".[conversion]" # torch + ultralytics, for weight conversion
45
+ pip install -e ".[all]" # tensorflow + convert + viz + dev
46
+ ```
47
+
48
+ Select the active backend with the `KERAS_BACKEND` environment variable before
49
+ importing anything:
50
+
51
+ ```bash
52
+ export KERAS_BACKEND=tensorflow # or "jax" / "torch"
53
+ ```
54
+
55
+ ```python
56
+ import os
57
+ os.environ["KERAS_BACKEND"] = "tensorflow" # must be set before `import keras`
58
+ ```
59
+
60
+ ## Quickstart: inference
61
+
62
+ ```python
63
+ import keras
64
+
65
+ from kyolo.models import yolov8n # every variant is a factory: yolov5n, yolo11s, ...
66
+ from kyolo.preprocessing import YOLOPreprocessor
67
+ from kyolo.postprocessing import YOLOPostprocessor
68
+ from kyolo.utils import visualize_detections, COCO_CLASS_NAMES
69
+
70
+ # 1. Build a model (deploy=True fuses reparameterizable branches for inference).
71
+ model = yolov8n(nc=80, input_shape=(640, 640, 3), deploy=True)
72
+ # model.load_weights("yolov8n.weights.h5") # a converted checkpoint (see below)
73
+
74
+ # 2. Preprocess: letterbox to a square and (optionally) normalize to [0, 1].
75
+ preprocessor = YOLOPreprocessor(image_size=640, normalize=True, letterbox=True)
76
+ batch = preprocessor.from_files("assets/bird.png")
77
+ # batch == {"images": (B, 640, 640, 3), "ratio": (B, 2), "pad": (B, 2)}
78
+
79
+ # 3. Forward pass -> list of 3 raw feature maps [P3, P4, P5].
80
+ raw_feats = model(batch["images"])
81
+
82
+ # 4. Postprocess -> (B, max_detections, 6) = [x1, y1, x2, y2, score, class_id].
83
+ postprocessor = YOLOPostprocessor(
84
+ nc=80, reg_max=16, strides=(8, 16, 32),
85
+ conf_threshold=0.25, iou_threshold=0.7, max_detections=300,
86
+ )
87
+ detections = postprocessor(raw_feats)
88
+
89
+ # 5. Visualize the first image in the batch.
90
+ image = keras.ops.convert_to_numpy(batch["images"])[0]
91
+ dets = keras.ops.convert_to_numpy(detections)[0]
92
+ visualize_detections(image, dets, class_names=COCO_CLASS_NAMES,
93
+ save_path="assets/inference_result.png")
94
+ ```
95
+
96
+ A runnable version lives in [`examples/inference.py`](examples/inference.py).
97
+
98
+ ## Training / fine-tuning
99
+
100
+ `kyolo.training.YOLODetector` is a `keras.Model` subclass that plugs into
101
+ `.fit()`. Datasets yield `(images, targets)` tuples where `targets` is
102
+ `{"boxes", "labels", "mask"}` (boxes are xyxy pixels, labels are ints, mask
103
+ marks real vs. padded boxes). `detect.detect(...)` runs the full decode + NMS
104
+ pipeline in one call:
105
+
106
+ ```python
107
+ import keras
108
+
109
+ from kyolo.models import yolov8n
110
+ from kyolo.losses import YOLODetectionLoss
111
+ from kyolo.training import YOLODetector
112
+
113
+ nc = 80
114
+ model = yolov8n(nc=nc)
115
+
116
+ # Fine-tuning: load converted weights onto the bare model BEFORE wrapping it.
117
+ # model.load_weights("yolov8n.weights.h5") # AGPL-3.0 converted checkpoint
118
+
119
+ # Freeze the backbone so only the neck + detection head train:
120
+ for layer in model.layers:
121
+ if layer.name.startswith("backbone"):
122
+ layer.trainable = False
123
+
124
+ loss = YOLODetectionLoss(nc=nc, reg_max=16, strides=(8, 16, 32))
125
+ detector = YOLODetector(model, loss=loss, nc=nc, reg_max=16)
126
+ detector.compile(optimizer=keras.optimizers.Adam(1e-3))
127
+
128
+ # `dataset` yields (images, {"boxes", "labels", "mask"}) tuples.
129
+ detector.fit(dataset, epochs=1, steps_per_epoch=2)
130
+
131
+ # Convenience: run the full detect -> NMS pipeline in one call.
132
+ detections = detector.detect(images, conf=0.25, iou=0.7)
133
+ ```
134
+
135
+ A runnable synthetic-data version lives in [`examples/train.py`](examples/train.py).
136
+
137
+ ## Weight conversion
138
+
139
+ The official YOLO checkpoints are **AGPL-3.0 licensed** and are **not**
140
+ redistributed with this project. To use pretrained weights you must convert an
141
+ official `.pt` file yourself. Each model ships a co-located converter:
142
+
143
+ ```bash
144
+ # per-model converter (co-located under kyolo/models/<name>/)
145
+ python -m kyolo.models.yolov8.convert_yolov8_torch_to_keras \
146
+ --weights yolov8n.pt --output yolov8n.weights.h5 --variant n
147
+
148
+ # or the unified CLI (installed as `kyolo-convert`)
149
+ kyolo-convert --model yolov8n --weights yolov8n.pt --output yolov8n.weights.h5
150
+ ```
151
+
152
+ Conversion requires the `conversion` extra (`pip install -e ".[conversion]"`,
153
+ which pulls in `torch` and `ultralytics`). The programmatic entry point is
154
+ `from kyolo.conversion import convert_weights`.
155
+
156
+ > Conversion is **best-effort**: layer-name and tensor-layout mappings are
157
+ > maintained by hand, so always validate a converted model's outputs against the
158
+ > reference PyTorch implementation before trusting it.
159
+
160
+ ## Package layout
161
+
162
+ The repo follows the [KerasFormers](https://github.com/IMvision12/KerasFormers)
163
+ layout: a flat top-level package with one self-contained folder per model.
164
+
165
+ ```
166
+ kyolo/ # flat package (no src/)
167
+ ├── __init__.py
168
+ ├── version.py # single source of truth for the version → releases
169
+ ├── models/
170
+ │ ├── __init__.py # per-variant factories (yolov8n, yolo11s, ...)
171
+ │ ├── base.py # shared assembly (image_input, finalize_detector)
172
+ │ └── yolov8/ # one folder per model (v5, v6, v7, v8, v9, v10, 11, 12, 26)
173
+ │ ├── __init__.py
174
+ │ ├── config.py # scale variants
175
+ │ ├── yolov8_model.py # architecture
176
+ │ └── convert_yolov8_torch_to_keras.py # co-located weight converter
177
+ ├── heads/ # detection head
178
+ ├── layers/ # conv/bn, CSP/ELAN/GELAN/attention blocks, DFL, letterbox
179
+ ├── ops/ # anchors, box math, task-aligned assigner (pure keras.ops)
180
+ ├── losses/ # YOLODetectionLoss
181
+ ├── preprocessing/ # YOLOPreprocessor
182
+ ├── postprocessing/ # YOLOPostprocessor + pure-ops NMS
183
+ ├── training/ # YOLODetector (keras.Model subclass)
184
+ ├── conversion/ # convert_weights, name mappings, exceptions, CLI
185
+ └── utils/ # COCO metadata + visualization
186
+
187
+ .github/workflows/ # release.yml (auto-release) + test_code.yml (CI)
188
+ docs/ # one page per model
189
+ tests/integration/ # build + processing smoke tests
190
+ examples/ # inference.py, train.py
191
+ ```
192
+
193
+ ## Releasing
194
+
195
+ Releases are automated. Bump `__version__` in
196
+ [`kyolo/version.py`](kyolo/version.py) and merge to `main`:
197
+ [`.github/workflows/release.yml`](.github/workflows/release.yml) detects the
198
+ change, creates the `v<version>` GitHub release, and publishes the wheel + sdist
199
+ to PyPI (via Trusted Publishing — no API token). The package version is read
200
+ dynamically from `kyolo.__version__`, so `version.py` is the single source of
201
+ truth.
202
+
203
+ ## License
204
+
205
+ - The **code** in this repository is licensed under the
206
+ [Apache License 2.0](LICENSE).
207
+ - The official YOLO **weights** are licensed under
208
+ [AGPL-3.0](https://github.com/ultralytics/ultralytics/blob/main/LICENSE).
209
+ They are not shipped here; anything you convert from them inherits AGPL-3.0,
210
+ and using such weights subjects your project to the AGPL-3.0 requirements.