pixano_inference.models.sam2
Inference model for the SAM2 model.
Sam2Model(name, provider, predictor, torch_dtype='bfloat16', config={})
Bases: BaseInferenceModel
Inference model for the SAM2 model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the model. |
required |
provider
|
str
|
Provider of the model. |
required |
predictor
|
Any
|
The SAM2 image predictor. |
required |
torch_dtype
|
Literal['float32', 'float16', 'bfloat16']
|
The torch data type to use for inference. |
'bfloat16'
|
config
|
dict[str, Any]
|
Configuration for the model. |
{}
|
Source code in pixano_inference/models/sam2.py
metadata
property
Return the metadata of the model.
delete()
image_mask_generation(image, points, labels, boxes, multimask_output=True, num_multimask_outputs=3, return_image_embedding=False, **kwargs)
Generate masks from the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
ndarray | Image
|
Image for the generation. |
required |
points
|
list[list[list[int]]] | None
|
Points for the mask generation. The first dimension is the number of prompts, the second the number of points per mask and the third the coordinates of the points. |
required |
labels
|
list[list[int]] | None
|
Labels for the mask generation. The first dimension is the number of prompts, the second the number of labels per mask. |
required |
boxes
|
list[list[int]] | None
|
Boxes for the mask generation. The first dimension is the number of prompts, the second the coordinates of the boxes. |
required |
multimask_output
|
bool
|
Whether to generate multiple masks per prediction. |
True
|
num_multimask_outputs
|
int
|
Number of masks to generate per prediction. |
3
|
return_image_embedding
|
bool
|
Whether to return the image embedding and high-resolution features. |
False
|
kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Source code in pixano_inference/models/sam2.py
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
init_video_state(video, offload_video_to_cpu=False, offload_state_to_cpu=False)
Initialize an inference state.
Source code in pixano_inference/models/sam2.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
|
load_video_frames_from_images(frames, image_size, offload_video_to_cpu, compute_device, images_mean=(0.485, 0.456, 0.406), images_std=(0.229, 0.224, 0.225))
Load the video frames from a directory of JPEG files ("
The frames are resized to image_size x image_size and are loaded to GPU if
offload_video_to_cpu
is False
and to CPU if offload_video_to_cpu
is True
.
Source code in pixano_inference/models/sam2.py
set_image_embeddings(image, image_embedding, high_resolution_features)
Calculates the image embeddings for the provided image.
Adapted from https://github.com/facebookresearch/sam2/blob/main/sam2/sam2_image_predictor.py (Apache-2.0 License).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
ndarray | 'Tensor' | Image
|
The input image to embed in RGB format. The image should be in HWC format if np.ndarray, or WHC format if PIL Image with or CHW format if torch.Tensor. |
required |
image_embedding
|
Tensor
|
The image embedding tensor. |
required |
high_resolution_features
|
Tensor
|
The high-resolution features tensor. |
required |
Source code in pixano_inference/models/sam2.py
video_mask_generation(video, objects_ids, frame_indexes, points=None, labels=None, boxes=None, propagate=False, **kwargs)
Generate masks from the video.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
video
|
bytes | Path | list[str] | list[Path]
|
Video data as a video file or a list of frames files. |
required |
objects_ids
|
list[int]
|
IDs of the objects to generate masks for. |
required |
frame_indexes
|
list[int]
|
Indexes of the frames where the objects are located. |
required |
points
|
list[list[list[int]]] | None
|
Points for the mask generation. The first fimension is the number of objects, the second the number of points for each object and the third the coordinates of the points. |
None
|
labels
|
list[list[int]] | None
|
Labels for the mask generation. The first fimension is the number of objects, the second the number of labels for each object. |
None
|
boxes
|
list[list[int]] | None
|
Boxes for the mask generation. The first fimension is the number of objects, the second the coordinates of the boxes. |
None
|
propagate
|
bool
|
Whether to propagate the masks in the video. |
False
|
kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
VideoMaskGenerationOutput
|
Output of the generation. |
Source code in pixano_inference/models/sam2.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
|