-
Notifications
You must be signed in to change notification settings - Fork 1
Enhance evaluation suite to support visualization of multi-input and target #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wli51
wants to merge
1
commit into
WayScience:main
Choose a base branch
from
wli51:viz-multi-chan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+218
−67
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| from typing import List, Optional, Tuple, Union | ||
| from typing import List, Optional, Tuple, Union, Any | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
|
|
@@ -10,13 +10,25 @@ | |
| from virtual_stain_flow.datasets.base_wrapper_dataset import BaseWrapperDataset | ||
|
|
||
|
|
||
| def _to_numpy_image(value: Any) -> np.ndarray: | ||
| if isinstance(value, torch.Tensor): | ||
| return value.detach().cpu().numpy() | ||
| return np.asarray(value) | ||
|
|
||
|
|
||
| def _normalize_to_list(sample: Any) -> List[np.ndarray]: | ||
| if isinstance(sample, (list, tuple)): | ||
| return [_to_numpy_image(item) for item in sample] | ||
| return [_to_numpy_image(sample)] | ||
|
|
||
|
|
||
| def extract_samples_from_dataset( | ||
| dataset: Union[BaseImageDataset, CropImageDataset, BaseWrapperDataset], | ||
| indices: List[int], | ||
| ) -> Tuple[ | ||
| List[np.ndarray], | ||
| List[np.ndarray], | ||
| Optional[List[np.ndarray]], | ||
| List[Union[np.ndarray, List[np.ndarray]]], | ||
| List[Union[np.ndarray, List[np.ndarray]]], | ||
| Optional[List[Union[np.ndarray, List[np.ndarray]]]], | ||
| Optional[List[Tuple[int, int]]], | ||
| ]: | ||
| """ | ||
|
|
@@ -26,13 +38,15 @@ def extract_samples_from_dataset( | |
| (x, y) coordinates of each crop for visualization with bounding boxes. | ||
|
|
||
| :param dataset: A BaseImageDataset or CropImageDataset instance. | ||
| :param indices: List of dataset indices to extract. | ||
| :return: Tuple of (inputs, targets, raw_images, patch_coords). | ||
| - inputs: List of numpy arrays, each with shape (C, H, W) or (H, W). | ||
| - targets: List of numpy arrays, each with shape (C, H, W) or (H, W). | ||
| - raw_images: List of numpy arrays for CropImageDataset (original uncropped images), | ||
| or None for BaseImageDataset. | ||
| - patch_coords: List of (x, y) tuples for CropImageDataset, or None for BaseImageDataset. | ||
| :param indices: List of dataset indices to extract. | ||
| :return: Tuple of (inputs, targets, raw_images, patch_coords). | ||
| - inputs: List of numpy arrays, each with shape (C, H, W) or (H, W). | ||
| Multi-input samples can be provided as a list of arrays per sample. | ||
| - targets: List of numpy arrays, each with shape (C, H, W) or (H, W). | ||
| Multi-target samples can be provided as a list of arrays per sample. | ||
| - raw_images: List of numpy arrays for CropImageDataset (original uncropped images), | ||
| or None for BaseImageDataset. | ||
| - patch_coords: List of (x, y) tuples for CropImageDataset, or None for BaseImageDataset. | ||
| """ | ||
| is_wrapper_dataset = False | ||
| if isinstance(dataset, BaseWrapperDataset): | ||
|
|
@@ -55,25 +69,21 @@ def extract_samples_from_dataset( | |
| f"max index requested: {max(indices)}" | ||
| ) | ||
|
|
||
| inputs: List[np.ndarray] = [] | ||
| targets: List[np.ndarray] = [] | ||
| raw_images: Optional[List[np.ndarray]] = [] if is_crop_dataset else None | ||
| inputs: List[Union[np.ndarray, List[np.ndarray]]] = [] | ||
| targets: List[Union[np.ndarray, List[np.ndarray]]] = [] | ||
| raw_images: Optional[List[Union[np.ndarray, List[np.ndarray]]]] = [] if is_crop_dataset else None | ||
| patch_coords: Optional[List[Tuple[int, int]]] = [] if is_crop_dataset else None | ||
|
|
||
| for idx in indices: | ||
| # Access dataset item to trigger lazy loading and state update | ||
| input_tensor, target_tensor = dataset[idx] | ||
|
|
||
| # Convert to numpy - handle both Tensor and ndarray inputs | ||
| if isinstance(input_tensor, torch.Tensor): | ||
| inputs.append(input_tensor.numpy()) | ||
| else: | ||
| inputs.append(np.asarray(input_tensor)) | ||
|
|
||
| if isinstance(target_tensor, torch.Tensor): | ||
| targets.append(target_tensor.numpy()) | ||
| else: | ||
| targets.append(np.asarray(target_tensor)) | ||
| input_list = _normalize_to_list(input_tensor) | ||
| target_list = _normalize_to_list(target_tensor) | ||
|
|
||
| inputs.append(input_list[0] if len(input_list) == 1 else input_list) | ||
| targets.append(target_list[0] if len(target_list) == 1 else target_list) | ||
|
Comment on lines
+85
to
+86
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the normalization step do this? |
||
|
|
||
| if is_crop_dataset: | ||
| # Access the original uncropped image and crop coordinates | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider docstrings providing information as to what these do and why.