PanSeg CNN Prediction¶
panseg.functionals.prediction.prediction.unet_prediction(raw: np.ndarray, input_layout: ImageLayout, model_name: str | None, model_id: str | None, patch: tuple[int, int, int] | None = None, patch_halo: tuple[int, int, int] | None = None, single_batch_mode: bool = True, device: str = 'cuda', model_update: bool = False, disable_tqdm: bool = False, config_path: Path | None = None, model_weights_path: Path | None = None, tracker=None, amp: AmpSetting = 'auto', channels_last_3d: bool = False) -> np.ndarray
¶
Generate prediction from raw data using a specified 3D U-Net model.
This function handles both single and multi-channel outputs from the model, returning appropriately shaped arrays based on the output channel configuration. The raw volume is reflect-padded up to the aligned domain and the prediction is cropped back, so the returned shape always equals the input shape.
For Bioimage.IO Model Zoo models, weights are downloaded and loaded into UNet3D or UNet2D
in panseg.functionals.training.model, i.e. bioimageio.core is not used. biio_prediction() uses
bioimageio.core for loading and running models.
Parameters:
-
raw(ndarray) –Raw input data.
-
Input_layout(ImageLayout) –The layout of the input data.
-
model_name(str | None) –The name of the model to use.
-
model_id(str | None) –The ID of the model from the BioImage.IO model zoo.
-
patch(tuple[int, int, int] | None, default:None) –Patch size for prediction.
Nonederives a patch aligned togeometry.incrementand sized sopatch + 2 * halofits the probed model-input budget. Defaults to None. -
patch_halo(tuple[int, int, int] | None, default:None) –Halo size around patches.
Noneusesgeometry.halo, the minimal halo that makes an aligned patch exact; a manual halo is used verbatim (no rounding, no alignment check). Defaults to None. -
single_batch_mode(bool, default:True) –Whether to use a single batch for prediction. Defaults to True.
-
device(str, default:'cuda') –The computation device ('cpu', 'cuda', etc.). Defaults to 'cuda'.
-
model_update(bool, default:False) –Whether to update the model to the latest version. Defaults to False.
-
disable_tqdm(bool, default:False) –If True, disables the tqdm progress bar. Defaults to False.
-
config_path(Path | None, default:None) –Path to the model configuration file. Defaults to None.
-
model_weights_path(Path | None, default:None) –Path to the model weights file. Defaults to None.
-
amp(AmpSetting, default:'auto') –precision mode (
"auto": bf16 autocast on supported CUDA devices, else fp32;"off": always fp32; the YAML-boolean spellings of these names also resolve, seevalidate_amp). The memory probes size patches in the same mode. Outputs are fp32 in either mode. Defaults to "auto". -
channels_last_3d(bool, default:False) –opt in to running 3D models on CUDA in the channels_last_3d memory format (kept off on CPU and for 2D models). The memory probes size patches in the same layout. Outputs are unaffected. Defaults to False.
Returns:
-
ndarray–np.ndarray: The predicted boundaries as a 3D (Z, Y, X) or 4D (C, Z, Y, X) array, normalized between 0 and 1.
Raises:
-
ValueError–If neither
model_name,model_id, norconfig_pathare provided. -
ValueError–If the patch or halo shape cannot run on the model.
Source code in panseg/functionals/prediction/prediction.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 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 406 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 | |