Classifications¶
supervision.classification.core.Classifications
dataclass
¶
Source code in src/supervision/classification/core.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
Methods:¶
__eq__(other: object) -> bool
¶
Compare classifications by value across numpy-backed fields.
Source code in src/supervision/classification/core.py
__len__() -> int
¶
__post_init__() -> None
¶
from_clip(clip_results: torch.Tensor) -> Classifications
classmethod
¶
Creates a Classifications instance from a clip inference result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
The inference result from clip model. |
required |
Returns:
| Type | Description |
|---|---|
Classifications
|
A new Classifications object. |
Example
from PIL import Image
import clip
import supervision as sv
model, preprocess = clip.load('ViT-B/32')
image = cv2.imread(SOURCE_IMAGE_PATH)
image = preprocess(image).unsqueeze(0)
text = clip.tokenize(["a diagram", "a dog", "a cat"])
output, _ = model(image, text)
classifications = sv.Classifications.from_clip(output)
Source code in src/supervision/classification/core.py
from_timm(timm_results: Any) -> Classifications
classmethod
¶
Creates a Classifications instance from a timm inference result.
Note
Returned confidences are softmax-normalized probabilities, so thresholds calibrated against raw logits may need recalibration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
The inference result from timm model. |
required |
Returns:
| Type | Description |
|---|---|
Classifications
|
A new Classifications object. |
Example
from PIL import Image
import timm
from timm.data import resolve_data_config, create_transform
import supervision as sv
model = timm.create_model(
model_name='hf-hub:nateraw/resnet50-oxford-iiit-pet',
pretrained=True
).eval()
config = resolve_data_config({}, model=model)
transform = create_transform(**config)
image = Image.open(SOURCE_IMAGE_PATH).convert('RGB')
x = transform(image).unsqueeze(0)
output = model(x)
classifications = sv.Classifications.from_timm(output)
Source code in src/supervision/classification/core.py
from_ultralytics(ultralytics_results: Any) -> Classifications
classmethod
¶
Creates a Classifications instance from a ultralytics inference result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
The inference result from ultralytics model. |
required |
Returns:
| Type | Description |
|---|---|
Classifications
|
A new Classifications object. |
Example
Source code in src/supervision/classification/core.py
get_top_k(k: int) -> tuple[npt.NDArray[np.int_], npt.NDArray[np.floating]]
¶
Retrieve the top k class IDs and confidences, ordered in descending order by confidence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
The number of top class IDs and confidences to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
tuple[NDArray[int_], NDArray[floating]]
|
A tuple containing the top k class IDs and confidences. |
Example
>>> import numpy as np
>>> import supervision as sv
>>> classifications = sv.Classifications(
... class_id=np.array([0, 1, 2]),
... confidence=np.array([0.3, 0.9, 0.5])
... )
>>> classifications.get_top_k(1)
(array([1]), array([0.9]))