-
Notifications
You must be signed in to change notification settings - Fork 0
model weights and config modification #2
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
kiryteo
wants to merge
7
commits into
master
Choose a base branch
from
bioimage
base: master
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a5c6ff
model weights and config modification
kiryteo 14f7b05
axes modification in the config
kiryteo f4a8e90
source distance idea
kiryteo bf1ada6
source displacement routine changes
kiryteo 2d15769
Revert "source displacement routine changes"
kiryteo b5ddc38
new pairwise distance idea
kiryteo 1bf5b55
check model script added
kiryteo 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
Binary file not shown.
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # File based on https://github.com/mobie/platybrowser-datasets/blob/master/segmentation/cells/UNet3DPlatyCellProbs.model/check_model.py | ||
| # specific to Kinetochores use case | ||
|
|
||
| import os | ||
| import numpy as np | ||
| import torch | ||
|
|
||
| from pybio.spec.utils.transformers import load_and_resolve_spec | ||
| from pybio.spec.utils import get_instance | ||
|
|
||
|
|
||
| # TODO this is missing the normalization (preprocessing) | ||
| def check_model(path): | ||
| """ Convert model weights from format 'pytorch_state_dict' to 'torchscript'. | ||
| """ | ||
| spec = load_and_resolve_spec(path) | ||
|
|
||
| with torch.no_grad(): | ||
| print("Loading inputs and outputs:") | ||
| # load input and expected output data | ||
| input_data = np.load(spec.test_inputs[0]).astype('float32') | ||
| input_data = torch.from_numpy(input_data) | ||
| expected_output_data = np.load(spec.test_outputs[0]).astype(np.float32) | ||
| print(input_data.shape) | ||
|
|
||
| # instantiate and trace the model | ||
| print("Predicting model") | ||
| model = get_instance(spec) | ||
| state = torch.load(spec.weights['pytorch_state_dict'].source) | ||
| model.load_state_dict(state) | ||
|
|
||
| # check the scripted model | ||
| output_data = model(input_data).numpy() | ||
| assert output_data.shape == expected_output_data.shape | ||
| assert np.allclose(expected_output_data, output_data) | ||
| print("Check passed") | ||
|
|
||
|
|
||
| # TODO this is missing the normalization (preprocessing) | ||
| def generate_output(path): | ||
| spec = load_and_resolve_spec(path) | ||
|
|
||
| with torch.no_grad(): | ||
| print("Loading inputs and outputs:") | ||
| # load input and expected output data | ||
| input_data = np.load(spec.test_inputs[0]).astype('float32') | ||
| input_data = torch.from_numpy(input_data) | ||
|
|
||
| # instantiate and trace the model | ||
| print("Predicting model") | ||
| model = get_instance(spec) | ||
| state = torch.load(spec.weights['pytorch_state_dict'].source) | ||
| model.load_state_dict(state) | ||
|
|
||
| # check the scripted model | ||
| output_data = model(input_data).numpy() | ||
| assert output_data.shape == input_data.shape | ||
| np.save('./test_output.npy', output_data) | ||
|
|
||
|
|
||
| def resave_data(): | ||
| halo = [32, 48, 48] | ||
| x = np.load('./test_input.npz')['arr_0'] | ||
| shape = x.shape[2:] | ||
| bb = tuple(slice(sh // 2 - ha, sh // 2 + ha) for sh, ha in zip(shape, halo)) | ||
| bb = (slice(None), slice(None)) + bb | ||
| x = x[bb] | ||
| print(x.shape) | ||
| np.save('./test_input.npy', x) | ||
|
|
||
| y = np.load('./test_output.npz')['arr_0'] | ||
| y = y[bb] | ||
| print(y.shape) | ||
| np.save('./test_output.npy', y) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| # resave and crop the older test data | ||
| # resave_data() | ||
|
|
||
| path = os.path.abspath('./UNet3DKinetochores.model.yaml') | ||
|
|
||
| # generate expected output again | ||
| # generate_output(path) | ||
|
|
||
| # check model predictions against the output | ||
| check_model(path) |
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
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.
is really so much of the returned output affected by edge artefacts?
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 is based on the settings in Pytorch-3DUNet, which worked well for Kinetochores use case (training as well as inference). But do you suggest to go for smaller values? (I can try that)
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.
could this be the halo within the network? You have your slicer, etc. to go over the whole volume. Just make sure that this is actually specifying the final output and not an intermediate step within your algorithm. If that's the case leave it as is and let's get this working before we start tweaking things.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, I checked for this and it is part of the predictor config and the routine.
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.
my understanding was that as this slicer is only used within your model it has no direct influence over the overall in- and output of the whole bioimage.io model. Let's take a closer look at this when we have a more or less running example.