segment-everything 0.1.0__py3-none-any.whl

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 (145) hide show
  1. segment_everything/__init__.py +5 -0
  2. segment_everything/augmentation/albumentations_helper.py +0 -0
  3. segment_everything/detect_and_segment.py +131 -0
  4. segment_everything/napari_helper.py +15 -0
  5. segment_everything/prompt_generator.py +188 -0
  6. segment_everything/py.typed +5 -0
  7. segment_everything/stacked_label_dataset.py +113 -0
  8. segment_everything/stacked_labels.py +428 -0
  9. segment_everything/vendored/PromptGuidedDecoder/Prompt_guided_Mask_Decoder.pt +0 -0
  10. segment_everything/vendored/__init__.py +5 -0
  11. segment_everything/vendored/dice.py +158 -0
  12. segment_everything/vendored/efficientvit/__init__.py +0 -0
  13. segment_everything/vendored/efficientvit/apps/__init__.py +0 -0
  14. segment_everything/vendored/efficientvit/apps/data_provider/__init__.py +7 -0
  15. segment_everything/vendored/efficientvit/apps/data_provider/augment/__init__.py +6 -0
  16. segment_everything/vendored/efficientvit/apps/data_provider/augment/bbox.py +30 -0
  17. segment_everything/vendored/efficientvit/apps/data_provider/augment/color_aug.py +78 -0
  18. segment_everything/vendored/efficientvit/apps/data_provider/base.py +254 -0
  19. segment_everything/vendored/efficientvit/apps/data_provider/random_resolution/__init__.py +6 -0
  20. segment_everything/vendored/efficientvit/apps/data_provider/random_resolution/_data_loader.py +1538 -0
  21. segment_everything/vendored/efficientvit/apps/data_provider/random_resolution/_data_worker.py +357 -0
  22. segment_everything/vendored/efficientvit/apps/data_provider/random_resolution/controller.py +100 -0
  23. segment_everything/vendored/efficientvit/apps/setup.py +150 -0
  24. segment_everything/vendored/efficientvit/apps/trainer/__init__.py +6 -0
  25. segment_everything/vendored/efficientvit/apps/trainer/base.py +318 -0
  26. segment_everything/vendored/efficientvit/apps/trainer/run_config.py +129 -0
  27. segment_everything/vendored/efficientvit/apps/utils/__init__.py +12 -0
  28. segment_everything/vendored/efficientvit/apps/utils/dist.py +32 -0
  29. segment_everything/vendored/efficientvit/apps/utils/ema.py +52 -0
  30. segment_everything/vendored/efficientvit/apps/utils/export.py +45 -0
  31. segment_everything/vendored/efficientvit/apps/utils/init.py +66 -0
  32. segment_everything/vendored/efficientvit/apps/utils/lr.py +52 -0
  33. segment_everything/vendored/efficientvit/apps/utils/metric.py +43 -0
  34. segment_everything/vendored/efficientvit/apps/utils/misc.py +101 -0
  35. segment_everything/vendored/efficientvit/apps/utils/opt.py +28 -0
  36. segment_everything/vendored/efficientvit/cls_model_zoo.py +79 -0
  37. segment_everything/vendored/efficientvit/clscore/__init__.py +0 -0
  38. segment_everything/vendored/efficientvit/clscore/data_provider/__init__.py +5 -0
  39. segment_everything/vendored/efficientvit/clscore/data_provider/imagenet.py +142 -0
  40. segment_everything/vendored/efficientvit/clscore/trainer/__init__.py +6 -0
  41. segment_everything/vendored/efficientvit/clscore/trainer/cls_run_config.py +18 -0
  42. segment_everything/vendored/efficientvit/clscore/trainer/cls_trainer.py +265 -0
  43. segment_everything/vendored/efficientvit/clscore/trainer/utils/__init__.py +7 -0
  44. segment_everything/vendored/efficientvit/clscore/trainer/utils/label_smooth.py +18 -0
  45. segment_everything/vendored/efficientvit/clscore/trainer/utils/metric.py +23 -0
  46. segment_everything/vendored/efficientvit/clscore/trainer/utils/mixup.py +67 -0
  47. segment_everything/vendored/efficientvit/models/__init__.py +0 -0
  48. segment_everything/vendored/efficientvit/models/efficientvit/__init__.py +8 -0
  49. segment_everything/vendored/efficientvit/models/efficientvit/backbone.py +380 -0
  50. segment_everything/vendored/efficientvit/models/efficientvit/cls.py +188 -0
  51. segment_everything/vendored/efficientvit/models/efficientvit/sam.py +181 -0
  52. segment_everything/vendored/efficientvit/models/efficientvit/seg.py +373 -0
  53. segment_everything/vendored/efficientvit/models/nn/__init__.py +8 -0
  54. segment_everything/vendored/efficientvit/models/nn/act.py +30 -0
  55. segment_everything/vendored/efficientvit/models/nn/drop.py +104 -0
  56. segment_everything/vendored/efficientvit/models/nn/norm.py +164 -0
  57. segment_everything/vendored/efficientvit/models/nn/ops.py +597 -0
  58. segment_everything/vendored/efficientvit/models/utils/__init__.py +7 -0
  59. segment_everything/vendored/efficientvit/models/utils/list.py +53 -0
  60. segment_everything/vendored/efficientvit/models/utils/network.py +73 -0
  61. segment_everything/vendored/efficientvit/models/utils/random.py +65 -0
  62. segment_everything/vendored/efficientvit/sam_model_zoo.py +45 -0
  63. segment_everything/vendored/efficientvit/seg_model_zoo.py +70 -0
  64. segment_everything/vendored/get_object_aware.py +26 -0
  65. segment_everything/vendored/mobilesamv2/__init__.py +16 -0
  66. segment_everything/vendored/mobilesamv2/automatic_mask_generator.py +415 -0
  67. segment_everything/vendored/mobilesamv2/build_sam.py +246 -0
  68. segment_everything/vendored/mobilesamv2/modeling/__init__.py +11 -0
  69. segment_everything/vendored/mobilesamv2/modeling/common.py +43 -0
  70. segment_everything/vendored/mobilesamv2/modeling/image_encoder.py +394 -0
  71. segment_everything/vendored/mobilesamv2/modeling/mask_decoder.py +213 -0
  72. segment_everything/vendored/mobilesamv2/modeling/prompt_encoder.py +217 -0
  73. segment_everything/vendored/mobilesamv2/modeling/sam.py +203 -0
  74. segment_everything/vendored/mobilesamv2/modeling/transformer.py +240 -0
  75. segment_everything/vendored/mobilesamv2/predictor.py +384 -0
  76. segment_everything/vendored/mobilesamv2/utils/__init__.py +5 -0
  77. segment_everything/vendored/mobilesamv2/utils/amg.py +347 -0
  78. segment_everything/vendored/mobilesamv2/utils/onnx.py +144 -0
  79. segment_everything/vendored/mobilesamv2/utils/transforms.py +103 -0
  80. segment_everything/vendored/object_detection/__init__.py +0 -0
  81. segment_everything/vendored/object_detection/ultralytics/__init__.py +5 -0
  82. segment_everything/vendored/object_detection/ultralytics/nn/__init__.py +9 -0
  83. segment_everything/vendored/object_detection/ultralytics/nn/autobackend.py +658 -0
  84. segment_everything/vendored/object_detection/ultralytics/nn/autoshape.py +397 -0
  85. segment_everything/vendored/object_detection/ultralytics/nn/modules/__init__.py +110 -0
  86. segment_everything/vendored/object_detection/ultralytics/nn/modules/block.py +304 -0
  87. segment_everything/vendored/object_detection/ultralytics/nn/modules/conv.py +297 -0
  88. segment_everything/vendored/object_detection/ultralytics/nn/modules/head.py +468 -0
  89. segment_everything/vendored/object_detection/ultralytics/nn/modules/transformer.py +378 -0
  90. segment_everything/vendored/object_detection/ultralytics/nn/modules/utils.py +78 -0
  91. segment_everything/vendored/object_detection/ultralytics/nn/tasks.py +1049 -0
  92. segment_everything/vendored/object_detection/ultralytics/prompt_mobilesamv2/__init__.py +6 -0
  93. segment_everything/vendored/object_detection/ultralytics/prompt_mobilesamv2/model.py +104 -0
  94. segment_everything/vendored/object_detection/ultralytics/prompt_mobilesamv2/predict.py +95 -0
  95. segment_everything/vendored/object_detection/ultralytics/yolo/__init__.py +5 -0
  96. segment_everything/vendored/object_detection/ultralytics/yolo/cfg/__init__.py +588 -0
  97. segment_everything/vendored/object_detection/ultralytics/yolo/cfg/default.yaml +117 -0
  98. segment_everything/vendored/object_detection/ultralytics/yolo/data/__init__.py +9 -0
  99. segment_everything/vendored/object_detection/ultralytics/yolo/data/annotator.py +53 -0
  100. segment_everything/vendored/object_detection/ultralytics/yolo/data/augment.py +899 -0
  101. segment_everything/vendored/object_detection/ultralytics/yolo/data/base.py +286 -0
  102. segment_everything/vendored/object_detection/ultralytics/yolo/data/build.py +213 -0
  103. segment_everything/vendored/object_detection/ultralytics/yolo/data/converter.py +358 -0
  104. segment_everything/vendored/object_detection/ultralytics/yolo/data/dataloaders/__init__.py +0 -0
  105. segment_everything/vendored/object_detection/ultralytics/yolo/data/dataloaders/stream_loaders.py +459 -0
  106. segment_everything/vendored/object_detection/ultralytics/yolo/data/dataset.py +274 -0
  107. segment_everything/vendored/object_detection/ultralytics/yolo/data/dataset_wrappers.py +53 -0
  108. segment_everything/vendored/object_detection/ultralytics/yolo/data/scripts/download_weights.sh +18 -0
  109. segment_everything/vendored/object_detection/ultralytics/yolo/data/scripts/get_coco.sh +60 -0
  110. segment_everything/vendored/object_detection/ultralytics/yolo/data/scripts/get_coco128.sh +17 -0
  111. segment_everything/vendored/object_detection/ultralytics/yolo/data/scripts/get_imagenet.sh +51 -0
  112. segment_everything/vendored/object_detection/ultralytics/yolo/data/utils.py +716 -0
  113. segment_everything/vendored/object_detection/ultralytics/yolo/engine/__init__.py +0 -0
  114. segment_everything/vendored/object_detection/ultralytics/yolo/engine/exporter.py +1214 -0
  115. segment_everything/vendored/object_detection/ultralytics/yolo/engine/model.py +641 -0
  116. segment_everything/vendored/object_detection/ultralytics/yolo/engine/predictor.py +461 -0
  117. segment_everything/vendored/object_detection/ultralytics/yolo/engine/results.py +741 -0
  118. segment_everything/vendored/object_detection/ultralytics/yolo/utils/__init__.py +893 -0
  119. segment_everything/vendored/object_detection/ultralytics/yolo/utils/autobatch.py +108 -0
  120. segment_everything/vendored/object_detection/ultralytics/yolo/utils/callbacks/__init__.py +5 -0
  121. segment_everything/vendored/object_detection/ultralytics/yolo/utils/callbacks/base.py +212 -0
  122. segment_everything/vendored/object_detection/ultralytics/yolo/utils/checks.py +547 -0
  123. segment_everything/vendored/object_detection/ultralytics/yolo/utils/dist.py +67 -0
  124. segment_everything/vendored/object_detection/ultralytics/yolo/utils/downloads.py +353 -0
  125. segment_everything/vendored/object_detection/ultralytics/yolo/utils/errors.py +12 -0
  126. segment_everything/vendored/object_detection/ultralytics/yolo/utils/files.py +100 -0
  127. segment_everything/vendored/object_detection/ultralytics/yolo/utils/instance.py +391 -0
  128. segment_everything/vendored/object_detection/ultralytics/yolo/utils/loss.py +579 -0
  129. segment_everything/vendored/object_detection/ultralytics/yolo/utils/metrics.py +1189 -0
  130. segment_everything/vendored/object_detection/ultralytics/yolo/utils/ops.py +870 -0
  131. segment_everything/vendored/object_detection/ultralytics/yolo/utils/patches.py +45 -0
  132. segment_everything/vendored/object_detection/ultralytics/yolo/utils/plotting.py +767 -0
  133. segment_everything/vendored/object_detection/ultralytics/yolo/utils/tal.py +276 -0
  134. segment_everything/vendored/object_detection/ultralytics/yolo/utils/torch_utils.py +684 -0
  135. segment_everything/vendored/object_detection/ultralytics/yolo/utils/tuner.py +54 -0
  136. segment_everything/vendored/object_detection/ultralytics/yolo/v8/__init__.py +5 -0
  137. segment_everything/vendored/object_detection/ultralytics/yolo/v8/detect/__init__.py +5 -0
  138. segment_everything/vendored/object_detection/ultralytics/yolo/v8/detect/predict.py +69 -0
  139. segment_everything/vendored/tinyvit/__init__.py +2 -0
  140. segment_everything/vendored/tinyvit/tiny_vit.py +867 -0
  141. segment_everything/weights_helper.py +124 -0
  142. segment_everything-0.1.0.dist-info/METADATA +53 -0
  143. segment_everything-0.1.0.dist-info/RECORD +145 -0
  144. segment_everything-0.1.0.dist-info/WHEEL +4 -0
  145. segment_everything-0.1.0.dist-info/licenses/LICENSE +28 -0
@@ -0,0 +1,741 @@
1
+ # Ultralytics YOLO 🚀, AGPL-3.0 license
2
+ """
3
+ Ultralytics Results, Boxes and Masks classes for handling inference results
4
+
5
+ Usage: See https://docs.ultralytics.com/modes/predict/
6
+ """
7
+
8
+ from copy import deepcopy
9
+ from functools import lru_cache
10
+ from pathlib import Path
11
+
12
+ import numpy as np
13
+ import torch
14
+
15
+ from ..data.augment import LetterBox
16
+ from ..utils import LOGGER, SimpleClass, deprecation_warn, ops
17
+ from ..utils.plotting import Annotator, colors, save_one_box
18
+
19
+
20
+ class BaseTensor(SimpleClass):
21
+ """
22
+ Base tensor class with additional methods for easy manipulation and device handling.
23
+ """
24
+
25
+ def __init__(self, data, orig_shape) -> None:
26
+ """Initialize BaseTensor with data and original shape.
27
+
28
+ Args:
29
+ data (torch.Tensor | np.ndarray): Predictions, such as bboxes, masks and keypoints.
30
+ orig_shape (tuple): Original shape of image.
31
+ """
32
+ assert isinstance(data, (torch.Tensor, np.ndarray))
33
+ self.data = data
34
+ self.orig_shape = orig_shape
35
+
36
+ @property
37
+ def shape(self):
38
+ """Return the shape of the data tensor."""
39
+ return self.data.shape
40
+
41
+ def cpu(self):
42
+ """Return a copy of the tensor on CPU memory."""
43
+ return (
44
+ self
45
+ if isinstance(self.data, np.ndarray)
46
+ else self.__class__(self.data.cpu(), self.orig_shape)
47
+ )
48
+
49
+ def numpy(self):
50
+ """Return a copy of the tensor as a numpy array."""
51
+ return (
52
+ self
53
+ if isinstance(self.data, np.ndarray)
54
+ else self.__class__(self.data.numpy(), self.orig_shape)
55
+ )
56
+
57
+ def cuda(self):
58
+ """Return a copy of the tensor on GPU memory."""
59
+ return self.__class__(
60
+ torch.as_tensor(self.data).cuda(), self.orig_shape
61
+ )
62
+
63
+ def to(self, *args, **kwargs):
64
+ """Return a copy of the tensor with the specified device and dtype."""
65
+ return self.__class__(
66
+ torch.as_tensor(self.data).to(*args, **kwargs), self.orig_shape
67
+ )
68
+
69
+ def __len__(self): # override len(results)
70
+ """Return the length of the data tensor."""
71
+ return len(self.data)
72
+
73
+ def __getitem__(self, idx):
74
+ """Return a BaseTensor with the specified index of the data tensor."""
75
+ return self.__class__(self.data[idx], self.orig_shape)
76
+
77
+
78
+ class Results(SimpleClass):
79
+ """
80
+ A class for storing and manipulating inference results.
81
+
82
+ Args:
83
+ orig_img (numpy.ndarray): The original image as a numpy array.
84
+ path (str): The path to the image file.
85
+ names (dict): A dictionary of class names.
86
+ boxes (torch.tensor, optional): A 2D tensor of bounding box coordinates for each detection.
87
+ masks (torch.tensor, optional): A 3D tensor of detection masks, where each mask is a binary image.
88
+ probs (torch.tensor, optional): A 1D tensor of probabilities of each class for classification task.
89
+ keypoints (List[List[float]], optional): A list of detected keypoints for each object.
90
+
91
+
92
+ Attributes:
93
+ orig_img (numpy.ndarray): The original image as a numpy array.
94
+ orig_shape (tuple): The original image shape in (height, width) format.
95
+ boxes (Boxes, optional): A Boxes object containing the detection bounding boxes.
96
+ masks (Masks, optional): A Masks object containing the detection masks.
97
+ probs (Probs, optional): A Probs object containing probabilities of each class for classification task.
98
+ names (dict): A dictionary of class names.
99
+ path (str): The path to the image file.
100
+ keypoints (Keypoints, optional): A Keypoints object containing detected keypoints for each object.
101
+ speed (dict): A dictionary of preprocess, inference and postprocess speeds in milliseconds per image.
102
+ _keys (tuple): A tuple of attribute names for non-empty attributes.
103
+ """
104
+
105
+ def __init__(
106
+ self,
107
+ orig_img,
108
+ path,
109
+ names,
110
+ boxes=None,
111
+ masks=None,
112
+ probs=None,
113
+ keypoints=None,
114
+ ) -> None:
115
+ """Initialize the Results class."""
116
+ self.orig_img = orig_img
117
+ self.orig_shape = orig_img.shape[:2]
118
+ self.boxes = (
119
+ Boxes(boxes, self.orig_shape) if boxes is not None else None
120
+ ) # native size boxes
121
+ self.masks = (
122
+ Masks(masks, self.orig_shape) if masks is not None else None
123
+ ) # native size or imgsz masks
124
+ self.probs = Probs(probs) if probs is not None else None
125
+ self.keypoints = (
126
+ Keypoints(keypoints, self.orig_shape)
127
+ if keypoints is not None
128
+ else None
129
+ )
130
+ self.speed = {
131
+ "preprocess": None,
132
+ "inference": None,
133
+ "postprocess": None,
134
+ } # milliseconds per image
135
+ self.names = names
136
+ self.path = path
137
+ self.save_dir = None
138
+ self._keys = ("boxes", "masks", "probs", "keypoints")
139
+
140
+ def __getitem__(self, idx):
141
+ """Return a Results object for the specified index."""
142
+ r = self.new()
143
+ for k in self.keys:
144
+ setattr(r, k, getattr(self, k)[idx])
145
+ return r
146
+
147
+ def update(self, boxes=None, masks=None, probs=None):
148
+ """Update the boxes, masks, and probs attributes of the Results object."""
149
+ if boxes is not None:
150
+ self.boxes = Boxes(boxes, self.orig_shape)
151
+ if masks is not None:
152
+ self.masks = Masks(masks, self.orig_shape)
153
+ if probs is not None:
154
+ self.probs = probs
155
+
156
+ def cpu(self):
157
+ """Return a copy of the Results object with all tensors on CPU memory."""
158
+ r = self.new()
159
+ for k in self.keys:
160
+ setattr(r, k, getattr(self, k).cpu())
161
+ return r
162
+
163
+ def numpy(self):
164
+ """Return a copy of the Results object with all tensors as numpy arrays."""
165
+ r = self.new()
166
+ for k in self.keys:
167
+ setattr(r, k, getattr(self, k).numpy())
168
+ return r
169
+
170
+ def cuda(self):
171
+ """Return a copy of the Results object with all tensors on GPU memory."""
172
+ r = self.new()
173
+ for k in self.keys:
174
+ setattr(r, k, getattr(self, k).cuda())
175
+ return r
176
+
177
+ def to(self, *args, **kwargs):
178
+ """Return a copy of the Results object with tensors on the specified device and dtype."""
179
+ r = self.new()
180
+ for k in self.keys:
181
+ setattr(r, k, getattr(self, k).to(*args, **kwargs))
182
+ return r
183
+
184
+ def __len__(self):
185
+ """Return the number of detections in the Results object."""
186
+ for k in self.keys:
187
+ return len(getattr(self, k))
188
+
189
+ def new(self):
190
+ """Return a new Results object with the same image, path, and names."""
191
+ return Results(
192
+ orig_img=self.orig_img, path=self.path, names=self.names
193
+ )
194
+
195
+ @property
196
+ def keys(self):
197
+ """Return a list of non-empty attribute names."""
198
+ return [k for k in self._keys if getattr(self, k) is not None]
199
+
200
+ def plot(
201
+ self,
202
+ conf=True,
203
+ line_width=None,
204
+ font_size=None,
205
+ font="Arial.ttf",
206
+ pil=False,
207
+ img=None,
208
+ img_gpu=None,
209
+ kpt_line=True,
210
+ labels=True,
211
+ boxes=True,
212
+ masks=True,
213
+ probs=True,
214
+ **kwargs, # deprecated args TODO: remove support in 8.2
215
+ ):
216
+ """
217
+ Plots the detection results on an input RGB image. Accepts a numpy array (cv2) or a PIL Image.
218
+
219
+ Args:
220
+ conf (bool): Whether to plot the detection confidence score.
221
+ line_width (float, optional): The line width of the bounding boxes. If None, it is scaled to the image size.
222
+ font_size (float, optional): The font size of the text. If None, it is scaled to the image size.
223
+ font (str): The font to use for the text.
224
+ pil (bool): Whether to return the image as a PIL Image.
225
+ img (numpy.ndarray): Plot to another image. if not, plot to original image.
226
+ img_gpu (torch.Tensor): Normalized image in gpu with shape (1, 3, 640, 640), for faster mask plotting.
227
+ kpt_line (bool): Whether to draw lines connecting keypoints.
228
+ labels (bool): Whether to plot the label of bounding boxes.
229
+ boxes (bool): Whether to plot the bounding boxes.
230
+ masks (bool): Whether to plot the masks.
231
+ probs (bool): Whether to plot classification probability
232
+
233
+ Returns:
234
+ (numpy.ndarray): A numpy array of the annotated image.
235
+ """
236
+ # Deprecation warn TODO: remove in 8.2
237
+ if "show_conf" in kwargs:
238
+ deprecation_warn("show_conf", "conf")
239
+ conf = kwargs["show_conf"]
240
+ assert (
241
+ type(conf) == bool
242
+ ), "`show_conf` should be of boolean type, i.e, show_conf=True/False"
243
+
244
+ if "line_thickness" in kwargs:
245
+ deprecation_warn("line_thickness", "line_width")
246
+ line_width = kwargs["line_thickness"]
247
+ assert (
248
+ type(line_width) == int
249
+ ), "`line_width` should be of int type, i.e, line_width=3"
250
+
251
+ names = self.names
252
+ annotator = Annotator(
253
+ deepcopy(self.orig_img if img is None else img),
254
+ line_width,
255
+ font_size,
256
+ font,
257
+ pil,
258
+ example=names,
259
+ )
260
+ pred_boxes, show_boxes = self.boxes, boxes
261
+ pred_masks, show_masks = self.masks, masks
262
+ pred_probs, show_probs = self.probs, probs
263
+ keypoints = self.keypoints
264
+ if pred_masks and show_masks:
265
+ if img_gpu is None:
266
+ img = LetterBox(pred_masks.shape[1:])(image=annotator.result())
267
+ img_gpu = (
268
+ torch.as_tensor(
269
+ img, dtype=torch.float16, device=pred_masks.data.device
270
+ )
271
+ .permute(2, 0, 1)
272
+ .flip(0)
273
+ .contiguous()
274
+ / 255
275
+ )
276
+ idx = pred_boxes.cls if pred_boxes else range(len(pred_masks))
277
+ annotator.masks(
278
+ pred_masks.data,
279
+ colors=[colors(x, True) for x in idx],
280
+ im_gpu=img_gpu,
281
+ )
282
+
283
+ if pred_boxes and show_boxes:
284
+ for d in reversed(pred_boxes):
285
+ c, conf, id = (
286
+ int(d.cls),
287
+ float(d.conf) if conf else None,
288
+ None if d.id is None else int(d.id.item()),
289
+ )
290
+ name = ("" if id is None else f"id:{id} ") + names[c]
291
+ label = (
292
+ (f"{name} {conf:.2f}" if conf else name)
293
+ if labels
294
+ else None
295
+ )
296
+ annotator.box_label(
297
+ d.xyxy.squeeze(), label, color=colors(c, True)
298
+ )
299
+
300
+ if pred_probs is not None and show_probs:
301
+ text = f"{', '.join(f'{names[j] if names else j} {pred_probs.data[j]:.2f}' for j in pred_probs.top5)}, "
302
+ annotator.text(
303
+ (32, 32), text, txt_color=(255, 255, 255)
304
+ ) # TODO: allow setting colors
305
+
306
+ if keypoints is not None:
307
+ for k in reversed(keypoints.data):
308
+ annotator.kpts(k, self.orig_shape, kpt_line=kpt_line)
309
+
310
+ return annotator.result()
311
+
312
+ def verbose(self):
313
+ """
314
+ Return log string for each task.
315
+ """
316
+ log_string = ""
317
+ probs = self.probs
318
+ boxes = self.boxes
319
+ if len(self) == 0:
320
+ return (
321
+ log_string
322
+ if probs is not None
323
+ else f"{log_string}(no detections), "
324
+ )
325
+ if probs is not None:
326
+ log_string += f"{', '.join(f'{self.names[j]} {probs.data[j]:.2f}' for j in probs.top5)}, "
327
+ if boxes:
328
+ for c in boxes.cls.unique():
329
+ n = (boxes.cls == c).sum() # detections per class
330
+ log_string += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, "
331
+ return log_string
332
+
333
+ def save_txt(self, txt_file, save_conf=False):
334
+ """
335
+ Save predictions into txt file.
336
+
337
+ Args:
338
+ txt_file (str): txt file path.
339
+ save_conf (bool): save confidence score or not.
340
+ """
341
+ boxes = self.boxes
342
+ masks = self.masks
343
+ probs = self.probs
344
+ kpts = self.keypoints
345
+ texts = []
346
+ if probs is not None:
347
+ # Classify
348
+ [
349
+ texts.append(f"{probs.data[j]:.2f} {self.names[j]}")
350
+ for j in probs.top5
351
+ ]
352
+ elif boxes:
353
+ # Detect/segment/pose
354
+ for j, d in enumerate(boxes):
355
+ c, conf, id = (
356
+ int(d.cls),
357
+ float(d.conf),
358
+ None if d.id is None else int(d.id.item()),
359
+ )
360
+ line = (c, *d.xywhn.view(-1))
361
+ if masks:
362
+ seg = (
363
+ masks[j].xyn[0].copy().reshape(-1)
364
+ ) # reversed mask.xyn, (n,2) to (n*2)
365
+ line = (c, *seg)
366
+ if kpts is not None:
367
+ kpt = kpts[j].xyn.reshape(-1).tolist()
368
+ line += (*kpt,)
369
+ line += (conf,) * save_conf + (() if id is None else (id,))
370
+ texts.append(("%g " * len(line)).rstrip() % line)
371
+
372
+ if texts:
373
+ with open(txt_file, "a") as f:
374
+ f.writelines(text + "\n" for text in texts)
375
+
376
+ def save_crop(self, save_dir, file_name=Path("im.jpg")):
377
+ """
378
+ Save cropped predictions to `save_dir/cls/file_name.jpg`.
379
+
380
+ Args:
381
+ save_dir (str | pathlib.Path): Save path.
382
+ file_name (str | pathlib.Path): File name.
383
+ """
384
+ if self.probs is not None:
385
+ LOGGER.warning(
386
+ "Warning: Classify task do not support `save_crop`."
387
+ )
388
+ return
389
+ if isinstance(save_dir, str):
390
+ save_dir = Path(save_dir)
391
+ if isinstance(file_name, str):
392
+ file_name = Path(file_name)
393
+ for d in self.boxes:
394
+ save_one_box(
395
+ d.xyxy,
396
+ self.orig_img.copy(),
397
+ file=save_dir
398
+ / self.names[int(d.cls)]
399
+ / f"{file_name.stem}.jpg",
400
+ BGR=True,
401
+ )
402
+
403
+ def pandas(self):
404
+ """Convert the object to a pandas DataFrame (not yet implemented)."""
405
+ LOGGER.warning(
406
+ "WARNING ⚠️ 'Results.pandas' method is not yet implemented."
407
+ )
408
+
409
+ def tojson(self, normalize=False):
410
+ """Convert the object to JSON format."""
411
+ if self.probs is not None:
412
+ LOGGER.warning(
413
+ "Warning: Classify task do not support `tojson` yet."
414
+ )
415
+ return
416
+
417
+ import json
418
+
419
+ # Create list of detection dictionaries
420
+ results = []
421
+ data = self.boxes.data.cpu().tolist()
422
+ h, w = self.orig_shape if normalize else (1, 1)
423
+ for i, row in enumerate(data):
424
+ box = {
425
+ "x1": row[0] / w,
426
+ "y1": row[1] / h,
427
+ "x2": row[2] / w,
428
+ "y2": row[3] / h,
429
+ }
430
+ conf = row[4]
431
+ id = int(row[5])
432
+ name = self.names[id]
433
+ result = {
434
+ "name": name,
435
+ "class": id,
436
+ "confidence": conf,
437
+ "box": box,
438
+ }
439
+ if self.masks:
440
+ x, y = (
441
+ self.masks.xy[i][:, 0],
442
+ self.masks.xy[i][:, 1],
443
+ ) # numpy array
444
+ result["segments"] = {
445
+ "x": (x / w).tolist(),
446
+ "y": (y / h).tolist(),
447
+ }
448
+ if self.keypoints is not None:
449
+ x, y, visible = (
450
+ self.keypoints[i].data[0].cpu().unbind(dim=1)
451
+ ) # torch Tensor
452
+ result["keypoints"] = {
453
+ "x": (x / w).tolist(),
454
+ "y": (y / h).tolist(),
455
+ "visible": visible.tolist(),
456
+ }
457
+ results.append(result)
458
+
459
+ # Convert detections to JSON
460
+ return json.dumps(results, indent=2)
461
+
462
+
463
+ class Boxes(BaseTensor):
464
+ """
465
+ A class for storing and manipulating detection boxes.
466
+
467
+ Args:
468
+ boxes (torch.Tensor | numpy.ndarray): A tensor or numpy array containing the detection boxes,
469
+ with shape (num_boxes, 6). The last two columns should contain confidence and class values.
470
+ orig_shape (tuple): Original image size, in the format (height, width).
471
+
472
+ Attributes:
473
+ boxes (torch.Tensor | numpy.ndarray): The detection boxes with shape (num_boxes, 6).
474
+ orig_shape (torch.Tensor | numpy.ndarray): Original image size, in the format (height, width).
475
+ is_track (bool): True if the boxes also include track IDs, False otherwise.
476
+
477
+ Properties:
478
+ xyxy (torch.Tensor | numpy.ndarray): The boxes in xyxy format.
479
+ conf (torch.Tensor | numpy.ndarray): The confidence values of the boxes.
480
+ cls (torch.Tensor | numpy.ndarray): The class values of the boxes.
481
+ id (torch.Tensor | numpy.ndarray): The track IDs of the boxes (if available).
482
+ xywh (torch.Tensor | numpy.ndarray): The boxes in xywh format.
483
+ xyxyn (torch.Tensor | numpy.ndarray): The boxes in xyxy format normalized by original image size.
484
+ xywhn (torch.Tensor | numpy.ndarray): The boxes in xywh format normalized by original image size.
485
+ data (torch.Tensor): The raw bboxes tensor
486
+
487
+ Methods:
488
+ cpu(): Move the object to CPU memory.
489
+ numpy(): Convert the object to a numpy array.
490
+ cuda(): Move the object to CUDA memory.
491
+ to(*args, **kwargs): Move the object to the specified device.
492
+ pandas(): Convert the object to a pandas DataFrame (not yet implemented).
493
+ """
494
+
495
+ def __init__(self, boxes, orig_shape) -> None:
496
+ """Initialize the Boxes class."""
497
+ if boxes.ndim == 1:
498
+ boxes = boxes[None, :]
499
+ n = boxes.shape[-1]
500
+ assert n in (
501
+ 6,
502
+ 7,
503
+ ), f"expected `n` in [6, 7], but got {n}" # xyxy, (track_id), conf, cls
504
+ super().__init__(boxes, orig_shape)
505
+ self.is_track = n == 7
506
+ self.orig_shape = orig_shape
507
+
508
+ @property
509
+ def xyxy(self):
510
+ """Return the boxes in xyxy format."""
511
+ return self.data[:, :4]
512
+
513
+ @property
514
+ def conf(self):
515
+ """Return the confidence values of the boxes."""
516
+ return self.data[:, -2]
517
+
518
+ @property
519
+ def cls(self):
520
+ """Return the class values of the boxes."""
521
+ return self.data[:, -1]
522
+
523
+ @property
524
+ def id(self):
525
+ """Return the track IDs of the boxes (if available)."""
526
+ return self.data[:, -3] if self.is_track else None
527
+
528
+ @property
529
+ @lru_cache(maxsize=2) # maxsize 1 should suffice
530
+ def xywh(self):
531
+ """Return the boxes in xywh format."""
532
+ return ops.xyxy2xywh(self.xyxy)
533
+
534
+ @property
535
+ @lru_cache(maxsize=2)
536
+ def xyxyn(self):
537
+ """Return the boxes in xyxy format normalized by original image size."""
538
+ xyxy = (
539
+ self.xyxy.clone()
540
+ if isinstance(self.xyxy, torch.Tensor)
541
+ else np.copy(self.xyxy)
542
+ )
543
+ xyxy[..., [0, 2]] /= self.orig_shape[1]
544
+ xyxy[..., [1, 3]] /= self.orig_shape[0]
545
+ return xyxy
546
+
547
+ @property
548
+ @lru_cache(maxsize=2)
549
+ def xywhn(self):
550
+ """Return the boxes in xywh format normalized by original image size."""
551
+ xywh = ops.xyxy2xywh(self.xyxy)
552
+ xywh[..., [0, 2]] /= self.orig_shape[1]
553
+ xywh[..., [1, 3]] /= self.orig_shape[0]
554
+ return xywh
555
+
556
+ @property
557
+ def boxes(self):
558
+ """Return the raw bboxes tensor (deprecated)."""
559
+ LOGGER.warning(
560
+ "WARNING ⚠️ 'Boxes.boxes' is deprecated. Use 'Boxes.data' instead."
561
+ )
562
+ return self.data
563
+
564
+
565
+ class Masks(BaseTensor):
566
+ """
567
+ A class for storing and manipulating detection masks.
568
+
569
+ Args:
570
+ masks (torch.Tensor | np.ndarray): A tensor containing the detection masks, with shape (num_masks, height, width).
571
+ orig_shape (tuple): Original image size, in the format (height, width).
572
+
573
+ Attributes:
574
+ masks (torch.Tensor | np.ndarray): A tensor containing the detection masks, with shape (num_masks, height, width).
575
+ orig_shape (tuple): Original image size, in the format (height, width).
576
+
577
+ Properties:
578
+ xy (list): A list of segments (pixels) which includes x, y segments of each detection.
579
+ xyn (list): A list of segments (normalized) which includes x, y segments of each detection.
580
+
581
+ Methods:
582
+ cpu(): Returns a copy of the masks tensor on CPU memory.
583
+ numpy(): Returns a copy of the masks tensor as a numpy array.
584
+ cuda(): Returns a copy of the masks tensor on GPU memory.
585
+ to(): Returns a copy of the masks tensor with the specified device and dtype.
586
+ """
587
+
588
+ def __init__(self, masks, orig_shape) -> None:
589
+ """Initialize the Masks class."""
590
+ if masks.ndim == 2:
591
+ masks = masks[None, :]
592
+ super().__init__(masks, orig_shape)
593
+
594
+ @property
595
+ @lru_cache(maxsize=1)
596
+ def segments(self):
597
+ """Return segments (deprecated; normalized)."""
598
+ LOGGER.warning(
599
+ "WARNING ⚠️ 'Masks.segments' is deprecated. Use 'Masks.xyn' for segments (normalized) and "
600
+ "'Masks.xy' for segments (pixels) instead."
601
+ )
602
+ return self.xyn
603
+
604
+ @property
605
+ @lru_cache(maxsize=1)
606
+ def xyn(self):
607
+ """Return segments (normalized)."""
608
+ return [
609
+ ops.scale_coords(
610
+ self.data.shape[1:], x, self.orig_shape, normalize=True
611
+ )
612
+ for x in ops.masks2segments(self.data)
613
+ ]
614
+
615
+ @property
616
+ @lru_cache(maxsize=1)
617
+ def xy(self):
618
+ """Return segments (pixels)."""
619
+ return [
620
+ ops.scale_coords(
621
+ self.data.shape[1:], x, self.orig_shape, normalize=False
622
+ )
623
+ for x in ops.masks2segments(self.data)
624
+ ]
625
+
626
+ @property
627
+ def masks(self):
628
+ """Return the raw masks tensor (deprecated)."""
629
+ LOGGER.warning(
630
+ "WARNING ⚠️ 'Masks.masks' is deprecated. Use 'Masks.data' instead."
631
+ )
632
+ return self.data
633
+
634
+ def pandas(self):
635
+ """Convert the object to a pandas DataFrame (not yet implemented)."""
636
+ LOGGER.warning(
637
+ "WARNING ⚠️ 'Masks.pandas' method is not yet implemented."
638
+ )
639
+
640
+
641
+ class Keypoints(BaseTensor):
642
+ """
643
+ A class for storing and manipulating detection keypoints.
644
+
645
+ Args:
646
+ keypoints (torch.Tensor | np.ndarray): A tensor containing the detection keypoints, with shape (num_dets, num_kpts, 2/3).
647
+ orig_shape (tuple): Original image size, in the format (height, width).
648
+
649
+ Attributes:
650
+ keypoints (torch.Tensor | np.ndarray): A tensor containing the detection keypoints, with shape (num_dets, num_kpts, 2/3).
651
+ orig_shape (tuple): Original image size, in the format (height, width).
652
+
653
+ Properties:
654
+ xy (list): A list of keypoints (pixels) which includes x, y keypoints of each detection.
655
+ xyn (list): A list of keypoints (normalized) which includes x, y keypoints of each detection.
656
+
657
+ Methods:
658
+ cpu(): Returns a copy of the keypoints tensor on CPU memory.
659
+ numpy(): Returns a copy of the keypoints tensor as a numpy array.
660
+ cuda(): Returns a copy of the keypoints tensor on GPU memory.
661
+ to(): Returns a copy of the keypoints tensor with the specified device and dtype.
662
+ """
663
+
664
+ def __init__(self, keypoints, orig_shape) -> None:
665
+ if keypoints.ndim == 2:
666
+ keypoints = keypoints[None, :]
667
+ super().__init__(keypoints, orig_shape)
668
+ self.has_visible = self.data.shape[-1] == 3
669
+
670
+ @property
671
+ @lru_cache(maxsize=1)
672
+ def xy(self):
673
+ return self.data[..., :2]
674
+
675
+ @property
676
+ @lru_cache(maxsize=1)
677
+ def xyn(self):
678
+ xy = (
679
+ self.xy.clone()
680
+ if isinstance(self.xy, torch.Tensor)
681
+ else np.copy(self.xy)
682
+ )
683
+ xy[..., 0] /= self.orig_shape[1]
684
+ xy[..., 1] /= self.orig_shape[0]
685
+ return xy
686
+
687
+ @property
688
+ @lru_cache(maxsize=1)
689
+ def conf(self):
690
+ return self.data[..., 2] if self.has_visible else None
691
+
692
+
693
+ class Probs(BaseTensor):
694
+ """
695
+ A class for storing and manipulating classify predictions.
696
+
697
+ Args:
698
+ probs (torch.Tensor | np.ndarray): A tensor containing the detection keypoints, with shape (num_class, ).
699
+
700
+ Attributes:
701
+ probs (torch.Tensor | np.ndarray): A tensor containing the detection keypoints, with shape (num_class).
702
+
703
+ Properties:
704
+ top5 (list[int]): Top 1 indice.
705
+ top1 (int): Top 5 indices.
706
+
707
+ Methods:
708
+ cpu(): Returns a copy of the probs tensor on CPU memory.
709
+ numpy(): Returns a copy of the probs tensor as a numpy array.
710
+ cuda(): Returns a copy of the probs tensor on GPU memory.
711
+ to(): Returns a copy of the probs tensor with the specified device and dtype.
712
+ """
713
+
714
+ def __init__(self, probs, orig_shape=None) -> None:
715
+ super().__init__(probs, orig_shape)
716
+
717
+ @property
718
+ @lru_cache(maxsize=1)
719
+ def top5(self):
720
+ """Return the indices of top 5."""
721
+ return (
722
+ (-self.data).argsort(0)[:5].tolist()
723
+ ) # this way works with both torch and numpy.
724
+
725
+ @property
726
+ @lru_cache(maxsize=1)
727
+ def top1(self):
728
+ """Return the indices of top 1."""
729
+ return int(self.data.argmax())
730
+
731
+ @property
732
+ @lru_cache(maxsize=1)
733
+ def top5conf(self):
734
+ """Return the confidences of top 5."""
735
+ return self.data[self.top5]
736
+
737
+ @property
738
+ @lru_cache(maxsize=1)
739
+ def top1conf(self):
740
+ """Return the confidences of top 1."""
741
+ return self.data[self.top1]