(model, device, wsi, filtered_tiles, workers, out_size, batch_size, n_last_blocks, avgpool_patchtokens, depths)
| 553 | |
| 554 | @torch.no_grad() |
| 555 | def extract_features(model, device, wsi, filtered_tiles, workers, out_size, batch_size, n_last_blocks, avgpool_patchtokens, depths): |
| 556 | # Use multiple workers if running on the GPU, otherwise we'll need all workers for evaluating the model. |
| 557 | kwargs = ( |
| 558 | {"num_workers": workers, "pin_memory": True} if device.type == "cuda" else {} |
| 559 | ) |
| 560 | loader = DataLoader( |
| 561 | dataset=BagOfTiles(wsi, filtered_tiles, resize_to=out_size), |
| 562 | batch_size=batch_size, |
| 563 | collate_fn=collate_features, |
| 564 | **kwargs, |
| 565 | ) |
| 566 | features_ = [] |
| 567 | coords_ = [] |
| 568 | for batch, coords in loader: |
| 569 | batch = batch.to(device, non_blocking=True) |
| 570 | # NOTE: Example using EsVIT. You may want to call your own feature extractor otherwise. |
| 571 | features = model.forward_return_n_last_blocks(batch, n_last_blocks, avgpool_patchtokens, depths).cpu().numpy() |
| 572 | features_.extend(features) |
| 573 | coords_.extend(coords) |
| 574 | return np.asarray(features_), np.asarray(coords_) |
| 575 | |
| 576 | def extract_save_features(args): |
| 577 | # Derive the slide ID from its name. |
no test coverage detected