-
Notifications
You must be signed in to change notification settings - Fork 3
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
Erase features in 3D from 3D mask #35
Open
brisvag
wants to merge
2
commits into
teamtomo:main
Choose a base branch
from
brisvag:feature/3d
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.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import numpy as np | ||
import torch | ||
from scipy.interpolate import LSQBivariateSpline | ||
from torch_cubic_spline_grids.b_spline_grids import CubicBSplineGrid3d | ||
|
||
|
||
def estimate_local_mean( | ||
|
@@ -59,3 +60,71 @@ def estimate_local_mean( | |
x = np.arange(image.shape[-1]) | ||
local_mean = background_model(y, x, grid=True) | ||
return torch.tensor(local_mean, dtype=input_dtype) | ||
|
||
|
||
def estimate_local_mean_3d( | ||
volume: torch.Tensor, | ||
mask: Optional[torch.Tensor] = None, | ||
resolution: Tuple[int, int, int] = (5, 5, 5), | ||
n_samples_for_fit: int = 20000, | ||
): | ||
"""Estimate local mean of an image with a bivariate cubic spline. | ||
|
||
A mask can be provided to | ||
|
||
Parameters | ||
---------- | ||
image: torch.Tensor | ||
`(h, w)` array containing image data. | ||
mask: Optional[torch.Tensor] | ||
`(h, w)` array containing a binary mask specifying foreground | ||
and background pixels for the estimation. | ||
resolution: Tuple[int, int] | ||
Resolution of the local mean estimate in each dimension. | ||
n_samples_for_fit: int | ||
Number of samples taken from foreground pixels for background mean estimation. | ||
The number of background pixels will be used if this number is greater than the | ||
number of background pixels. | ||
|
||
Returns | ||
------- | ||
local_mean: torch.Tensor | ||
`(h, w)` array containing a local estimate of the local mean. | ||
""" | ||
input_dtype = volume.dtype | ||
volume = volume.numpy() | ||
mask = np.ones_like(volume) if mask is None else mask.numpy() | ||
|
||
# get a random set of foreground pixels for the background fit | ||
foreground_sample_idx = np.argwhere(mask == 1) | ||
|
||
n_samples_for_fit = min(n_samples_for_fit, len(foreground_sample_idx)) | ||
selection = np.random.choice( | ||
foreground_sample_idx.shape[0], size=n_samples_for_fit, replace=False | ||
) | ||
foreground_sample_idx = foreground_sample_idx[selection] | ||
z, y, x = foreground_sample_idx[:, 0], foreground_sample_idx[:, 1], foreground_sample_idx[:, 2] | ||
|
||
w = torch.as_tensor(volume[(z, y, x)]) | ||
|
||
grid = CubicBSplineGrid3d(resolution=resolution) | ||
optimiser = torch.optim.Adam(grid.parameters(), lr=0.01) | ||
|
||
for i in range(500): | ||
# what does the model predict for our observations? | ||
prediction = grid(foreground_sample_idx).squeeze() | ||
|
||
# zero gradients and calculate loss between observations and model prediction | ||
optimiser.zero_grad() | ||
loss = torch.sum((prediction - w)**2)**0.5 | ||
|
||
# backpropagate loss and update values at points on grid | ||
loss.backward() | ||
optimiser.step() | ||
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. Not convinced I'm using this right, here... |
||
|
||
tz = torch.tensor(np.linspace(0, 1, volume.shape[0])) | ||
ty = torch.tensor(np.linspace(0, 1, volume.shape[1])) | ||
tx = torch.tensor(np.linspace(0, 1, volume.shape[2])) | ||
zz, yy, xx = torch.meshgrid(tz, ty, tx, indexing='xy') | ||
w = grid(torch.stack((zz, yy, xx), dim=-1)).detach().numpy().reshape(volume.shape) | ||
return torch.tensor(w, dtype=input_dtype) |
This file contains 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.
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.
this looks like it's about right except when sampling the model coordinates should be rescaled to [0, 1] where 0 is the center of the first element in the dimension and 1 is the center of the last element in the dimension
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.
Mhmh... I see, I'll look into it :)
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.
@alisterburt does this make sense now? Though unfortunately it doens't look better @alessiodacapito :/ I think the bulk of the work here is being done by the noise generation, not the local mean...