Coverage for src/bioimageio/core/_prediction_pipeline.py: 81%

255 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 09:46 +0000

1from __future__ import annotations 

2 

3import warnings 

4from abc import ABC, abstractmethod 

5from collections.abc import Iterable, Mapping, Sequence 

6from types import MappingProxyType 

7from typing import ( 

8 Literal, 

9) 

10 

11from loguru import logger 

12from typing_extensions import assert_never 

13 

14from bioimageio.spec import load_model_description 

15from bioimageio.spec.model import AnyModelDescr, v0_4, v0_5 

16 

17from ._model_adapter import ModelAdapter 

18from ._op_base import BlockwiseOperator, SamplewiseOperator 

19from ._settings import settings 

20from .axis import AxisId, AxisSize, PerAxis 

21from .backends import create_model_adapter 

22from .block import apply_blockwise 

23from .common import ( 

24 BlocksizeParameter, 

25 Halo, 

26 MemberId, 

27 PerMember, 

28 SupportedWeightsFormat, 

29) 

30from .digest_spec import ( 

31 TensorSource, 

32 get_input_halo, 

33 get_member_ids, 

34 get_output_tensor_metadata, 

35 get_tensor, 

36 transpose_sample_for_model, 

37) 

38from .proc_ops import Processing 

39from .proc_setup import setup_pre_and_postprocessing 

40from .sample import Sample 

41from .stat_measures import Measure, MeasureValue, Stat 

42from .tensor import Tensor 

43 

44 

45class _PredictionPipelineBase(ABC): 

46 def __init__( 

47 self, 

48 model_descr: AnyModelDescr, 

49 *, 

50 default_blocksize_parameter: BlocksizeParameter, 

51 default_batch_size: int, 

52 preceding_prediction_pipelines: Sequence[ 

53 PredictionPipeline | RemotePredictionPipeline 

54 ] 

55 | None, 

56 ) -> None: 

57 super().__init__() 

58 self._model_descr = model_descr 

59 self._default_blocksize_parameter = default_blocksize_parameter 

60 self._default_batch_size = default_batch_size 

61 # TODO: Improve parallelization of blockwise predictions with preceding prediction pipelines. 

62 self._preceding_prediction_pipelines = preceding_prediction_pipelines 

63 if isinstance(model_descr, v0_4.ModelDescr): 

64 self._default_output_halo: PerMember[PerAxis[Halo]] = {} 

65 self._default_input_halo: PerMember[PerAxis[Halo]] = {} 

66 # self._block_transform = None 

67 else: 

68 self._default_output_halo = { 

69 t.id: { 

70 a.id: Halo(a.halo, a.halo) 

71 for a in t.axes 

72 if isinstance(a, v0_5.WithHalo) 

73 } 

74 for t in model_descr.outputs 

75 } 

76 self._default_input_halo = get_input_halo( 

77 model_descr, self._default_output_halo 

78 ) 

79 # self._block_transform = get_block_transform(model_descr) 

80 

81 self._pad_mode = ( 

82 {} 

83 if isinstance(model_descr, v0_4.ModelDescr) 

84 else { 

85 ipt.id: ipt.pad or v0_5.SymmetricPadding() for ipt in model_descr.inputs 

86 } 

87 ) 

88 

89 self._input_ids = tuple(get_member_ids(model_descr.inputs)) 

90 self._output_ids = tuple(get_member_ids(model_descr.outputs)) 

91 

92 def __enter__(self): 

93 self.load() 

94 return self 

95 

96 def __exit__(self, exc_type, exc_val, exc_tb): # type: ignore 

97 self.unload() 

98 return False 

99 

100 @abstractmethod 

101 def load(self): ... 

102 

103 @abstractmethod 

104 def unload(self): ... 

105 

106 @property 

107 def input_ids(self) -> Sequence[MemberId]: 

108 return self._input_ids 

109 

110 @property 

111 def output_ids(self) -> Sequence[MemberId]: 

112 return self._output_ids 

113 

114 @property 

115 def model_descr(self) -> AnyModelDescr: 

116 return self._model_descr 

117 

118 @property 

119 def model_description(self) -> AnyModelDescr: 

120 return self._model_descr 

121 

122 def predict_without_blocking( 

123 self, 

124 sample: Sample, 

125 skip_input_padding: bool = False, 

126 skip_preprocessing: bool = False, 

127 skip_postprocessing: bool = False, 

128 skip_output_cropping: bool = False, 

129 ) -> Sample: 

130 """Predict a whole sample at once. 

131 

132 Note: 

133 The sample's tensor shapes have to match the model's input tensor description. 

134 If that is not the case, consider `predict_sample_with_blocking` 

135 

136 Args: 

137 sample: input sample 

138 skip_input_padding: if `True`, skip padding the input sample according to the model's (optional) output halos. 

139 skip_preprocessing: if `True`, skip all preprocessing steps (except for any preceding prediction pipeline). 

140 skip_postprocessing: if `True`, skip all postprocessing steps (except for any preceding prediction pipeline). 

141 skip_output_cropping: if `True`, skip cropping any output halos from the model output. 

142 """ 

143 sample = sample.copy() 

144 for pp in self._get_preceding_prediction_pipelines_for_sample(sample): 144 ↛ 145line 144 didn't jump to line 145 because the loop on line 144 never started

145 sample = pp.predict_without_blocking( 

146 sample, 

147 skip_input_padding=skip_input_padding, 

148 skip_preprocessing=False, 

149 skip_postprocessing=False, 

150 skip_output_cropping=skip_output_cropping, 

151 ) 

152 

153 return self._predict_without_blocking_impl( 

154 sample, 

155 skip_input_padding=skip_input_padding, 

156 skip_preprocessing=skip_preprocessing, 

157 skip_postprocessing=skip_postprocessing, 

158 skip_output_cropping=skip_output_cropping, 

159 ) 

160 

161 @abstractmethod 

162 def _predict_without_blocking_impl( 

163 self, 

164 sample: Sample, 

165 *, 

166 skip_input_padding: bool, 

167 skip_preprocessing: bool, 

168 skip_postprocessing: bool, 

169 skip_output_cropping: bool, 

170 ) -> Sample: 

171 """Predict a whole sample at once. 

172 

173 Note: 

174 The sample's tensor shapes have to match the model's input tensor description. 

175 If that is not the case, consider `predict_sample_with_blocking` 

176 

177 Args: 

178 sample: input sample 

179 batch_size: if provided, use this batch size for the model prediction. Otherwise, use the default batch size of the pipeline. 

180 skip_input_padding: if `True`, skip padding the input sample according to the model's (optional) output halos. 

181 skip_preprocessing: if `True`, skip all preprocessing steps. 

182 skip_postprocessing: if `True`, skip all postprocessing steps. 

183 skip_output_cropping: if `True`, skip cropping any output halos from the model output. 

184 """ 

185 

186 def predict_with_blocking( 

187 self, 

188 sample: Sample, 

189 ns: v0_5.ParameterizedSize_N 

190 | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] 

191 | None = None, 

192 *, 

193 batch_size: int | None = None, 

194 skip_input_padding: bool = False, 

195 skip_preprocessing: bool = False, 

196 skip_postprocessing: bool = False, 

197 skip_output_cropping: bool = False, 

198 ) -> Sample: 

199 sample = sample.copy() 

200 for pp in self._get_preceding_prediction_pipelines_for_sample(sample): 200 ↛ 201line 200 didn't jump to line 201 because the loop on line 200 never started

201 sample = pp._predict_with_blocking_impl( 

202 sample, 

203 ns=ns, 

204 skip_preprocessing=False, 

205 skip_postprocessing=False, 

206 batch_size=batch_size, 

207 skip_input_padding=skip_input_padding, 

208 skip_output_cropping=skip_output_cropping, 

209 ) 

210 

211 return self._predict_with_blocking_impl( 

212 sample, 

213 ns=ns, 

214 skip_input_padding=skip_input_padding, 

215 batch_size=batch_size, 

216 skip_preprocessing=skip_preprocessing, 

217 skip_postprocessing=skip_postprocessing, 

218 skip_output_cropping=skip_output_cropping, 

219 ) 

220 

221 def _predict_with_blocking_impl( 

222 self, 

223 sample: Sample, 

224 ns: v0_5.ParameterizedSize_N 

225 | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] 

226 | None, 

227 *, 

228 batch_size: int | None, 

229 skip_input_padding: bool, 

230 skip_preprocessing: bool, 

231 skip_postprocessing: bool, 

232 skip_output_cropping: bool, 

233 ) -> Sample: 

234 if isinstance(self._model_descr, v0_4.ModelDescr): 

235 raise NotImplementedError( 

236 "`predict_sample_with_blocking` not implemented for v0_4.ModelDescr" 

237 + f" {self._model_descr.name}." 

238 + " Consider using `predict_sample_with_fixed_blocking`" 

239 + " or load_model_description(..., format_version='0.5')." 

240 ) 

241 

242 ns = ns if ns is not None else self._default_blocksize_parameter 

243 if isinstance(ns, int): 

244 ns = { 

245 (ipt.id, a.id): ns 

246 for ipt in self._model_descr.inputs 

247 for a in ipt.axes 

248 if isinstance(a.size, v0_5.ParameterizedSize) 

249 } 

250 max_block_shape = self._model_descr.get_tensor_sizes( 

251 ns, batch_size or self._default_batch_size, sample.shape 

252 ).inputs 

253 block_shape: dict[MemberId, dict[AxisId, int | AxisSize]] = {} 

254 for ipt in self._model_descr.inputs: 

255 block_shape[ipt.id] = {} 

256 for a in ipt.axes: 

257 if isinstance(a.size, v0_5.ParameterizedSize): 

258 axis_size = AxisSize( 

259 min=a.size.min, 

260 max=max(a.size.min, max_block_shape[ipt.id][a.id]), 

261 step=a.size.step, 

262 ) 

263 else: 

264 axis_size = max_block_shape[ipt.id][a.id] 

265 

266 block_shape[ipt.id][a.id] = axis_size 

267 

268 return self._predict_with_fixed_blocking_impl( 

269 sample, 

270 block_shape=block_shape, 

271 skip_input_padding=skip_input_padding, 

272 skip_preprocessing=skip_preprocessing, 

273 skip_postprocessing=skip_postprocessing, 

274 skip_output_cropping=skip_output_cropping, 

275 ) 

276 

277 def predict_with_fixed_blocking( 

278 self, 

279 sample: Sample, 

280 input_block_shape: PerMember[PerAxis[int | AxisSize]], 

281 *, 

282 skip_input_padding: bool = False, 

283 skip_preprocessing: bool = False, 

284 skip_postprocessing: bool = False, 

285 skip_output_cropping: bool = False, 

286 ) -> Sample: 

287 """Predict `sample` with given `input_block_shape`. 

288 

289 Note: 

290 - `input_block_shape` is expected to be a valid input shape for the model. 

291 - Use `predict_sample_with_blocking` if you want to control block sizes via generic block size parameters rather than fixed block shapes. 

292 

293 Args: 

294 sample: The sample to predict on. 

295 input_block_shape: Mapping of input member id to mapping of axis id to block size for that axis. 

296 skip_input_padding: If `True`, skip input padding that compensates for the model's output halo. 

297 skip_preprocessing: If `True`, skip all preprocessing steps. 

298 skip_postprocessing: If `True`, skip all postprocessing steps. 

299 skip_output_cropping: If `True`, skip output cropping of the model's halo. 

300 """ 

301 sample = sample.copy() 

302 for pp in self._get_preceding_prediction_pipelines_for_sample(sample): 302 ↛ 303line 302 didn't jump to line 303 because the loop on line 302 never started

303 sample = pp._predict_with_fixed_blocking_impl( 

304 sample, 

305 block_shape=input_block_shape, 

306 skip_input_padding=False, 

307 skip_preprocessing=False, 

308 skip_postprocessing=False, 

309 skip_output_cropping=False, 

310 ) 

311 

312 return self._predict_with_fixed_blocking_impl( 

313 sample, 

314 block_shape=input_block_shape, 

315 skip_input_padding=skip_input_padding, 

316 skip_preprocessing=skip_preprocessing, 

317 skip_postprocessing=skip_postprocessing, 

318 skip_output_cropping=skip_output_cropping, 

319 ) 

320 

321 @abstractmethod 

322 def _predict_with_fixed_blocking_impl( 

323 self, 

324 sample: Sample, 

325 block_shape: PerMember[PerAxis[int | AxisSize]], 

326 *, 

327 skip_input_padding: bool, 

328 skip_preprocessing: bool, 

329 skip_postprocessing: bool, 

330 skip_output_cropping: bool, 

331 ) -> Sample: ... 

332 

333 def _get_preceding_prediction_pipelines_for_sample( 

334 self, sample: Sample 

335 ) -> Sequence[PredictionPipeline | RemotePredictionPipeline]: 

336 """Get preceding prediction pipelines for a sample based on the sample's input member ids.""" 

337 if not self._preceding_prediction_pipelines: 337 ↛ 340line 337 didn't jump to line 340 because the condition on line 337 was always true

338 return () 

339 

340 required_inputs = set(self.input_ids) 

341 sample_members = set(sample.members.keys()) 

342 preceding_pipelines: list[PredictionPipeline | RemotePredictionPipeline] = [] 

343 for pp in self._preceding_prediction_pipelines[::-1]: 

344 preceding_pipelines.insert(0, pp) 

345 sample_members.update(pp.output_ids) 

346 required_inputs.update(pp.input_ids) 

347 required_inputs.difference_update(sample_members) 

348 

349 if not required_inputs: 

350 return preceding_pipelines 

351 

352 raise KeyError( 

353 f"Sample is missing required inputs {required_inputs} for the prediction pipeline or its preceding pipelines." 

354 ) 

355 

356 

357class PredictionPipeline(_PredictionPipelineBase): 

358 """ 

359 Represents model computation including preprocessing and postprocessing 

360 Note: Ideally use the `PredictionPipeline` in a with statement 

361 (as a context manager). 

362 """ 

363 

364 def __init__( 

365 self, 

366 *, 

367 name: str, 

368 model_description: AnyModelDescr, 

369 preprocessing: list[Processing], 

370 postprocessing: list[Processing], 

371 model_adapter: ModelAdapter, 

372 default_blocksize_parameter: BlocksizeParameter = 10, 

373 default_batch_size: int = 1, 

374 preceding_prediction_pipelines: Sequence[ 

375 PredictionPipeline | RemotePredictionPipeline 

376 ] 

377 | None = None, 

378 ) -> None: 

379 """Consider using `create_prediction_pipeline` to create a `PredictionPipeline` with sensible defaults.""" 

380 super().__init__( 

381 model_descr=model_description, 

382 default_blocksize_parameter=default_blocksize_parameter, 

383 default_batch_size=default_batch_size, 

384 preceding_prediction_pipelines=preceding_prediction_pipelines, 

385 ) 

386 

387 if model_description.run_mode: 387 ↛ 388line 387 didn't jump to line 388 because the condition on line 387 was never true

388 warnings.warn( 

389 f"Not yet implemented inference for run mode '{model_description.run_mode.name}'" 

390 ) 

391 

392 self.name = name 

393 # split preprocessing into samplewise and blockwise. samplewise preprocessing is all preprocessing up to including the last samplewise operator, blockwise preprocessing are the remaining blockwise operators. 

394 # I.e. some samplewise preprocessing may be a blockwise op (at some point followed by a samplewise op). 

395 self._samplewise_preprocessing: list[ 

396 SamplewiseOperator | BlockwiseOperator 

397 ] = [] 

398 self._blockwise_preprocessing: list[BlockwiseOperator] = [] 

399 for op in preprocessing[::-1]: 

400 if isinstance(op, BlockwiseOperator) and not self._samplewise_preprocessing: 

401 self._blockwise_preprocessing.insert(0, op) 

402 else: 

403 self._samplewise_preprocessing.insert(0, op) 

404 # split postprocessing analougly, but here we start blockwise and switch to samplewise at the first samplewise operator. 

405 self._blockwise_postprocessing: list[BlockwiseOperator] = [] 

406 self._samplewise_postprocessing: list[ 

407 BlockwiseOperator | SamplewiseOperator 

408 ] = [] 

409 for op in postprocessing: 

410 if ( 

411 isinstance(op, BlockwiseOperator) 

412 and not self._samplewise_postprocessing 

413 ): 

414 self._blockwise_postprocessing.append(op) 

415 else: 

416 self._samplewise_postprocessing.append(op) 

417 

418 self._adapter = model_adapter 

419 

420 @property 

421 def has_samplewise_preprocessing(self) -> bool: 

422 """`True` if any preprocessing operators in the pipeline are not applicable blockwise.""" 

423 return bool(self._samplewise_preprocessing) 

424 

425 @property 

426 def has_samplewise_postprocessing(self) -> bool: 

427 """`True` if any postprocessing operators in the pipeline are not applicable blockwise.""" 

428 return bool(self._samplewise_postprocessing) 

429 

430 def apply_input_padding(self, sample: Sample) -> None: 

431 padded_sample = sample.pad( 

432 pad_width=self._default_input_halo, mode=self._pad_mode 

433 ) 

434 sample.members = padded_sample.members 

435 

436 def apply_preprocessing(self, sample: Sample) -> None: 

437 """Apply all preprocessing in-place, also may updates sample stats""" 

438 

439 self.apply_samplewise_preprocessing(sample) 

440 self.apply_blockwise_preprocessing(sample) 

441 

442 def apply_samplewise_preprocessing(self, sample: Sample, /) -> None: 

443 """Apply preprocessing operators up to and including the last samplewise operator in-place. 

444 

445 Note: This skips all blockwise preprocessing steps after the last samplewise operator. 

446 """ 

447 for op in self._samplewise_preprocessing: 

448 op(sample) 

449 

450 def apply_blockwise_preprocessing(self, sample: Sample, /) -> None: 

451 """Apply blockwise preprocessing operators that follow the last samplewise preprocessing operator in-place. 

452 

453 Note: This skips all preprocessing operators up to and including the last samplewise one. 

454 """ 

455 for op in self._blockwise_preprocessing: 

456 op(sample) 

457 

458 def apply_postprocessing(self, sample: Sample) -> None: 

459 """Apply all postprocessing in-place, also may updates samples stats""" 

460 self.apply_blockwise_postprocessing(sample) 

461 self.apply_samplewise_postprocessing(sample) 

462 

463 def apply_blockwise_postprocessing(self, sample: Sample, /) -> None: 

464 """Apply blockwise postprocessing operators that precede the first samplewise postprocessing operator in-place. 

465 

466 Note: This does not apply all postprocessing operators from the first samplewise one onwards. 

467 """ 

468 for op in self._blockwise_postprocessing: 

469 op(sample) 

470 

471 def apply_samplewise_postprocessing(self, sample: Sample, /) -> None: 

472 """Apply postprocessing operators starting from and including the first samplewise operator in-place. 

473 

474 Note: This skips all blockwise postprocessing steps before the first samplewise one. 

475 """ 

476 for op in self._samplewise_postprocessing: 

477 op(sample) 

478 

479 def apply_output_cropping(self, sample: Sample) -> None: 

480 sample.members = { 

481 m: t 

482 if m not in self._default_output_halo 

483 else t[ 

484 { 

485 a: slice(h.left, t.tagged_shape[a] + -h.right) 

486 for a, h in self._default_output_halo[m].items() 

487 } 

488 ] 

489 for m, t in sample.members.items() 

490 } 

491 

492 def raise_for_non_blockwise_preprocessing(self): 

493 """ 

494 Raises: 

495 NotImplementedError: if there are any non-blockwise preprocessing operators in the pipeline 

496 """ 

497 self._raise_for_non_blockwise_processing("preprocessing") 

498 

499 def raise_for_non_blockwise_postprocessing(self): 

500 """ 

501 Raises: 

502 NotImplementedError: if there are any non-blockwise postprocessing operators in the pipeline 

503 """ 

504 self._raise_for_non_blockwise_processing("postprocessing") 

505 

506 def _raise_for_non_blockwise_processing( 

507 self, proc_type: Literal["preprocessing", "postprocessing"] 

508 ): 

509 ops = ( 

510 self._samplewise_preprocessing 

511 if proc_type == "preprocessing" 

512 else self._samplewise_postprocessing 

513 ) 

514 non_blockwise = [ 

515 op.__class__.__name__ for op in ops if not isinstance(op, BlockwiseOperator) 

516 ] 

517 if non_blockwise: 

518 raise NotImplementedError( 

519 f"Blockwise {proc_type} for {non_blockwise} not implemented." 

520 ) 

521 

522 def _predict_without_blocking_impl( 

523 self, 

524 sample: Sample, 

525 *, 

526 skip_input_padding: bool, 

527 skip_preprocessing: bool, 

528 skip_postprocessing: bool, 

529 skip_output_cropping: bool, 

530 ) -> Sample: 

531 sample = transpose_sample_for_model( 

532 sample, self._model_descr, extra_dims="stack" 

533 ) 

534 if not skip_input_padding: 

535 self.apply_input_padding(sample) 

536 

537 if not skip_preprocessing: 

538 self.apply_preprocessing(sample) 

539 

540 output_members = { 

541 k: v for k, v in self._forward(sample.members).items() if v is not None 

542 } 

543 

544 output = Sample(members=output_members, stat=sample.stat, id=sample.id) 

545 if not skip_postprocessing: 

546 self.apply_postprocessing(output) 

547 

548 if not skip_output_cropping: 

549 self.apply_output_cropping(output) 

550 

551 return output 

552 

553 def _predict_with_fixed_blocking_impl( 

554 self, 

555 sample: Sample, 

556 block_shape: PerMember[PerAxis[int | AxisSize]], 

557 *, 

558 skip_input_padding: bool, 

559 skip_preprocessing: bool, 

560 skip_postprocessing: bool, 

561 skip_output_cropping: bool, 

562 ) -> Sample: 

563 """Predict `sample` with given `input_block_shape`. 

564 

565 Note: 

566 - `input_block_shape` is expected to be a valid input shape for the model. 

567 - Use `predict_sample_with_blocking` if you want to control block sizes via generic block size parameters 

568 rather than fixed block shapes. 

569 - Postprocessing may only be complete for the final sample (if samplewise postprocessing steps are included 

570 in the pipeline), intermediate samples may have some (blockwise applicable) postprocessing steps applied. 

571 

572 Args: 

573 sample: The sample to predict on. 

574 input_block_shape: Mapping of input member id to mapping of axis id to block size for that axis. 

575 skip_input_padding: If `True`, skip input padding that compensates for the model's output halo. 

576 skip_preprocessing: If `True`, skip all preprocessing steps. 

577 skip_postprocessing: If `True`, skip all postprocessing steps. 

578 skip_output_cropping: If `True`, skip output cropping of the model's halo. 

579 

580 """ 

581 if not skip_input_padding: 

582 self.apply_input_padding(sample) 

583 

584 if not skip_preprocessing: 

585 self.apply_preprocessing(sample) 

586 

587 output_members = self._forward_with_fixed_blocking( 

588 sample.members, 

589 chunk_shape=settings.processing_chunk_min_bytes, 

590 block_shape=block_shape, 

591 ) 

592 sample = Sample(id=sample.id, members=output_members, stat=sample.stat) 

593 

594 if not skip_postprocessing: 

595 self.apply_postprocessing(sample) 

596 

597 if not skip_output_cropping: 

598 self.apply_output_cropping(sample) 

599 

600 return sample 

601 

602 def _forward(self, inputs: PerMember[Tensor]) -> PerMember[Tensor | None]: 

603 outputs = self._adapter.forward(inputs) 

604 for m, metadata in get_output_tensor_metadata( 

605 self._model_descr, inputs 

606 ).items(): 

607 if (tensor := outputs.get(m)) is not None: 607 ↛ 604line 607 didn't jump to line 604 because the condition on line 607 was always true

608 tensor.set_metadata(metadata) 

609 return outputs 

610 

611 def _forward_with_fixed_blocking( 

612 self, 

613 sample_members: PerMember[Tensor], 

614 chunk_shape: PerMember[PerAxis[int]] | int, 

615 block_shape: PerMember[PerAxis[int | AxisSize]], 

616 ) -> dict[MemberId, Tensor]: 

617 

618 # fill block_shape to set treat extra dims unknown to the model to 1 

619 if isinstance(self._model_descr, v0_4.ModelDescr): 619 ↛ 620line 619 didn't jump to line 620 because the condition on line 619 was never true

620 model_axes = { 

621 AxisId(ax) for ipt in self._model_descr.inputs for ax in ipt.axes 

622 } 

623 else: 

624 model_axes = {ax.id for ipt in self._model_descr.inputs for ax in ipt.axes} 

625 block_shape = { 

626 m: { 

627 a: block_shape[m][a] 

628 if m in block_shape and a in block_shape[m] 

629 else s 

630 if a in model_axes 

631 else 1 

632 for a, s in t.tagged_shape.items() 

633 } 

634 for m, t in sample_members.items() 

635 } 

636 

637 return apply_blockwise( 

638 self._forward, 

639 sample_members, 

640 chunk_shape=chunk_shape, 

641 block_shape=block_shape, 

642 pad_mode=self._pad_mode, 

643 pad_boundary="allow", 

644 input_halo=self._default_input_halo, 

645 output_halo=self._default_output_halo, 

646 get_output_shape=self._adapter.get_output_shape, 

647 ) 

648 

649 @property 

650 def is_loaded(self) -> bool: 

651 """`True` if the model adapter is loaded and ready for inference.""" 

652 return self._adapter.is_loaded 

653 

654 def load(self): 

655 """Prepare prediction pipeline for use. 

656 

657 Reusable model adapters may be loaded and unloaded multiple times, but currently not all model adapters 

658 cleanly unload and reload. 

659 

660 Note: 

661 For some model adapters loading is currently part of the constructor making them unusable after unloading. 

662 """ 

663 self._adapter.load() 

664 

665 def unload(self): 

666 """Free any device memory in use. 

667 

668 Note: 

669 Currently prediction pipeline becomes unusable after unloading.""" 

670 self._adapter.unload() 

671 

672 def close(self): 

673 """Permanently close the prediction pipeline and free any device memory in use. 

674 This makes the prediction pipeline unusable afterwards.""" 

675 self.unload() 

676 

677 

678class RemotePredictionPipeline(_PredictionPipelineBase): 

679 """Abstract base class for fully remote prediction pipelines. 

680 

681 Note: A ("local") `PredictionPipeline` may also use a `RemoteModelAdapter` for remote model inference, but it may 

682 still apply local preprocessing and postprocessing steps. 

683 In contrast, a `RemotePredictionPipeline` is designed for the case where all steps including preprocessing and 

684 postprocessing are performed remotely. 

685 """ 

686 

687 def __init__( 

688 self, 

689 model_descr: AnyModelDescr, 

690 *, 

691 server: str, 

692 default_blocksize_parameter: BlocksizeParameter, 

693 default_batch_size: int, 

694 preceding_prediction_pipelines: Sequence[ 

695 PredictionPipeline | RemotePredictionPipeline 

696 ] 

697 | None = None, 

698 ) -> None: 

699 super().__init__( 

700 model_descr, 

701 default_blocksize_parameter=default_blocksize_parameter, 

702 default_batch_size=default_batch_size, 

703 preceding_prediction_pipelines=preceding_prediction_pipelines, 

704 ) 

705 self._server = server 

706 

707 @property 

708 def server(self) -> str: 

709 return self._server 

710 

711 

712def create_prediction_pipeline( 

713 bioimageio_model: AnyModelDescr, 

714 *, 

715 devices: Sequence[str] | None = None, 

716 weight_format: SupportedWeightsFormat | None = None, 

717 weights_format: SupportedWeightsFormat | None = None, 

718 dataset_for_initial_statistics: Iterable[ 

719 Sample 

720 | TensorSource 

721 | Sequence[TensorSource] 

722 | Mapping[MemberId, Tensor | TensorSource] 

723 ] = (), 

724 dataset_fits_in_memory: bool = False, 

725 keep_updating_initial_dataset_statistics: bool = False, 

726 fixed_dataset_statistics: Mapping[Measure, MeasureValue] = MappingProxyType({}), 

727 model_adapter: ModelAdapter | None = None, 

728 ns: BlocksizeParameter | None = None, 

729 default_blocksize_parameter: BlocksizeParameter = 10, # TODO: default to None and find smart blocksize params per axis to reduce overlap of blocks with large halo 

730 preceding_prediction_pipelines: Sequence[ 

731 PredictionPipeline | RemotePredictionPipeline 

732 ] 

733 | None = None, 

734) -> PredictionPipeline: 

735 """ 

736 Creates prediction pipeline which includes: 

737 * computation of input statistics 

738 * preprocessing 

739 * model prediction 

740 * computation of output statistics 

741 * postprocessing 

742 

743 Args: 

744 bioimageio_model: A bioimageio model description. 

745 devices: (optional) 

746 weight_format: deprecated in favor of **weights_format** 

747 weights_format: (optional) Use a specific **weights_format** rather than 

748 choosing one automatically. 

749 A corresponding `bioimageio.core.model_adapters.ModelAdapter` will be 

750 created to run inference with the **bioimageio_model**. 

751 dataset_for_initial_statistics: (optional) If preprocessing steps require input 

752 dataset statistics, **dataset_for_initial_statistics** allows you to 

753 specify a dataset from which these statistics are computed. Required when 

754 dataset statistics are not supplied by **fixed_dataset_statistics** unless 

755 **keep_updating_initial_dataset_statistics** is explicitly enabled. 

756 dataset_fits_in_memory: (optional) Set to `True` if the dataset for initial statistics fits in memory. 

757 keep_updating_initial_dataset_statistics: (optional) Set to `True` if you want 

758 to update dataset statistics with each processed sample. This also 

759 explicitly allows starting without initial dataset statistics. 

760 fixed_dataset_statistics: (optional) Precomputed dataset (and optionally sample) statistics. 

761 Any included sample statistics will not be calculated on the fly and it is the callers 

762 responsibility to use samples with the corresponding statistics availble in `sample.stat`. 

763 model_adapter: (optional) Allows you to use a custom **model_adapter** instead 

764 of creating one according to the present/selected **weights_format**. 

765 ns: deprecated in favor of **default_blocksize_parameter** 

766 default_blocksize_parameter: Allows to control the default block size for 

767 blockwise predictions, see `BlocksizeParameter`. 

768 preceding_prediction_pipelines: (optional) If the model has inputs that are 

769 outputs of other models (input field 'output_of'), you can provide a sequence 

770 of preceding prediction pipelines. The prediction pipeline will then automatically 

771 use the outputs of those preceding pipelines as inputs for the current model. 

772 If no preceding prediction pipelines for a model are provided, prediction pipelines using the 

773 same devices and weight format as for the current model will be created for any required preceding models. 

774 """ 

775 weights_format = weight_format or weights_format 

776 del weight_format 

777 default_blocksize_parameter = ns or default_blocksize_parameter 

778 del ns 

779 

780 model_adapter = model_adapter or create_model_adapter( 

781 model_description=bioimageio_model, 

782 devices=devices, 

783 weight_format_priority_order=weights_format and (weights_format,), 

784 ) 

785 

786 input_ids = get_member_ids(bioimageio_model.inputs) 

787 input_descrs = dict(zip(input_ids, bioimageio_model.inputs)) 

788 

789 def dataset(): 

790 common_stat: Stat = {} 

791 for i, x in enumerate(dataset_for_initial_statistics): 

792 if isinstance(x, Sample): 

793 yield x 

794 else: 

795 if not isinstance(x, Mapping): 795 ↛ 796line 795 didn't jump to line 796 because the condition on line 795 was never true

796 if isinstance(x, Tensor) or not isinstance(x, Sequence): 

797 x = [x] 

798 

799 x = dict(zip(input_ids, x)) 

800 

801 yield Sample( 

802 members={ 

803 k: v 

804 if isinstance(v, Tensor) 

805 else get_tensor(v, input_descrs[k]) 

806 for k, v in x.items() 

807 }, 

808 stat=common_stat, 

809 id=i, 

810 ) 

811 

812 preprocessing, postprocessing = setup_pre_and_postprocessing( 

813 bioimageio_model, 

814 dataset(), 

815 keep_updating_initial_dataset_stats=keep_updating_initial_dataset_statistics, 

816 fixed_dataset_stats=fixed_dataset_statistics, 

817 dataset_fits_in_memory=dataset_fits_in_memory, 

818 ) 

819 

820 def _get_preceding_model_ids(model: AnyModelDescr) -> set[v0_5.ModelId]: 

821 return { 

822 input_descr.output_of 

823 for input_descr in model.inputs 

824 if isinstance(input_descr, v0_5.InputTensorDescr) 

825 and input_descr.output_of is not None 

826 } 

827 

828 preceding_model_ids = _get_preceding_model_ids(bioimageio_model) 

829 if preceding_prediction_pipelines is None: 829 ↛ 832line 829 didn't jump to line 832 because the condition on line 829 was always true

830 preceding_prediction_pipelines = [] 

831 else: 

832 preceding_prediction_pipelines = list(preceding_prediction_pipelines) 

833 

834 for preceding_model_id in preceding_model_ids: 834 ↛ 835line 834 didn't jump to line 835 because the loop on line 834 never started

835 if preceding_model_id in { 

836 pp.model_description.id for pp in preceding_prediction_pipelines 

837 }: 

838 continue 

839 

840 preceding_model = load_model_description(preceding_model_id) 

841 preceding_prediction_pipelines.insert( 

842 0, 

843 create_prediction_pipeline( 

844 preceding_model, 

845 devices=devices, 

846 weights_format=weights_format, 

847 default_blocksize_parameter=default_blocksize_parameter, 

848 dataset_for_initial_statistics=dataset_for_initial_statistics, 

849 keep_updating_initial_dataset_statistics=keep_updating_initial_dataset_statistics, 

850 fixed_dataset_statistics=fixed_dataset_statistics, 

851 ), 

852 ) 

853 

854 pp = PredictionPipeline( 

855 name=bioimageio_model.name, 

856 model_description=bioimageio_model, 

857 model_adapter=model_adapter, 

858 preprocessing=preprocessing, 

859 postprocessing=postprocessing, 

860 default_blocksize_parameter=default_blocksize_parameter, 

861 preceding_prediction_pipelines=preceding_prediction_pipelines, 

862 ) 

863 logger.info( 

864 "Created prediction pipeline for '{}' with {} adapter", 

865 bioimageio_model.name, 

866 model_adapter.__class__.__name__, 

867 ) 

868 return pp 

869 

870 

871def create_remote_prediction_pipeline( 

872 model_description: AnyModelDescr, 

873 *, 

874 server: str | None = None, 

875 server_type: Literal["gradio"] | None = "gradio", 

876 precomputed_statistics: Mapping[Measure, MeasureValue] = MappingProxyType({}), 

877 default_blocksize_parameter: BlocksizeParameter = 10, # TODO: default to None and find smart blocksize params per axis to reduce overlap of blocks with large halo 

878 default_batch_size: int = 1, 

879) -> RemotePredictionPipeline: 

880 """Create a `RemotePredictionPipeline` for the given `model_description`. 

881 

882 Args: 

883 model_description: The model to run inference with. 

884 server: The URL or Hugging Face space name of a running bioimageio server instance 

885 server_type: The type of the remote server to connect to. Currently only "gradio" is supported. 

886 precomputed_statistics: Precomputed dataset (and optionally sample) statistics. 

887 Any included sample statistics will not be calculated on the fly and it is the callers 

888 responsibility to use samples with the corresponding statistics availble in `sample.stat`. 

889 default_blocksize_parameter: Allows to control the default block size with a single parameter for blockwise predictions. (not all models support this) 

890 default_batch_size: Default batch size to use 

891 """ 

892 

893 if server_type is None: 

894 server_type = "gradio" 

895 

896 try: 

897 if server_type == "gradio": 

898 from .remote_backends.gradio.client import ( 

899 GradioPredictionPipeline as RemotePredictionPipelineImpl, 

900 ) 

901 else: 

902 assert_never(server_type) 

903 except ImportError as e: 

904 raise ImportError( 

905 f"Failed to import {server_type.capitalize()}PredictionPipeline. Make sure to install the '{server_type}-client' extra," 

906 + f" e.g. with `pip install bioimageio.core[{server_type}-client]`." 

907 ) from e 

908 

909 return RemotePredictionPipelineImpl( 

910 model_description, 

911 server=server, 

912 precomputed_statistics=precomputed_statistics, 

913 default_blocksize_parameter=default_blocksize_parameter, 

914 default_batch_size=default_batch_size, 

915 )