Skip to content
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

To tile coords #939

Merged
merged 7 commits into from
Sep 25, 2023
Merged

To tile coords #939

merged 7 commits into from
Sep 25, 2023

Conversation

XinyueLi1012
Copy link
Collaborator

avoid for loop over tiles
-- use sort(), it would be slow when the image is big (eg. 4000* 4000)

@codecov
Copy link

codecov bot commented Sep 16, 2023

Codecov Report

Merging #939 (d4e91c9) into master (0a8b6db) will increase coverage by 0.05%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #939      +/-   ##
==========================================
+ Coverage   95.82%   95.88%   +0.05%     
==========================================
  Files          26       26              
  Lines        3211     3233      +22     
==========================================
+ Hits         3077     3100      +23     
+ Misses        134      133       -1     
Flag Coverage Δ
unittests 95.88% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
bliss/catalog.py 99.66% <100.00%> (+0.39%) ⬆️
bliss/data_augmentation.py 98.43% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@jeff-regier
Copy link
Contributor

jeff-regier commented Sep 17, 2023

Great work @XinyueLi1012! I timed both the old and new versions of to_tile_coords: your new one is more than 10 times faster (0.11 seconds vs 1.6 seconds).

The output from both implementations usually matches, so I think the new implementation is at least nearly right. (Possibly it is perfect and the old implementation is slightly off.) But I'm reluctant to merge until they match exactly or until we're really confident that the new version is correct. Please see my trial below. The difference may have to do with how these two implementations handle negative locations. Should we be filtering sources with out-of-bounds locations (either too high or too low) as part of our data augmentation procedure?

> new_tp = aug_full.to_tile_params(4, 4)
> old_tp = aug_full.old_to_tile_params(4, 4)
> aug_full.n_sources.sum()
tensor(4681, device='cuda:0')
> new_tp.n_sources.sum()
tensor(4681, device='cuda:0')
> old_tp.n_sources.sum()
tensor(4681, device='cuda:0')
> (new_tp.locs < 0).sum()
tensor(0, device='cuda:0')
> (old_tp.locs < 0).sum()
tensor(190, device='cuda:0')
> new_tp.locs.isclose(old_tp.locs).sum()
tensor(204610, device='cuda:0')
> (~new_tp.locs.isclose(old_tp.locs)).sum()
tensor(190, device='cuda:0')

@jeff-regier
Copy link
Contributor

Also, would you add a test to verify that if we rotate a catalog by 90 degrees and next by 270 degree, then we get back the original catalog? (Or do we already have a test like that?)

@XinyueLi1012
Copy link
Collaborator Author

Thanks for the testing results. I didn't do much testing for the new implementation so I'm also a little worry about some special cases. I'll add some tests for the new implementation and data augmentation.

@XinyueLi1012
Copy link
Collaborator Author

@jeff-regier I added some filtering for the negative "locs" in the to_tile_params (easier than filter in data augmentation)

@jeff-regier
Copy link
Contributor

Thanks @XinyueLi1012. Before we merge, can you also verify that our performance on DC2 stays the same with this PR?

_, aug_full270 = aug_rotate270(origin_full, aug_input_images)

_, aug_full90180 = aug_rotate180(aug_full90, aug_image90)
_, aug_full90270 = aug_rotate270(aug_full90, aug_image90)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good test to have, but I realize it doesn't quite test what I thought it would, because there's no conversion btw tile and full catalogs happening.

Would you add an additional test that converts from a tile catalog to a full catalog and then back again to a tile catalog? (And that the first and last tile catalogs are equal?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes make sense

bliss/catalog.py Outdated
x0_mask = (plocs_ii[:, 0] > 0) & (plocs_ii[:, 0] < self.height)
x1_mask = (plocs_ii[:, 1] > 0) & (plocs_ii[:, 1] < self.width)
x_mask = x0_mask * x1_mask
n_filter_sources = x_mask.sum()
Copy link
Contributor

@jeff-regier jeff-regier Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better to do the filtering only on demand: better to give users an error if they try to convert catalog formats without first removing out-of-bounds locations. Would you a new method called something like filter_out_of_bounds? And then call that method following data augmentation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be tricky. Let me have a try first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an alternative that may be less tricky: add a keyword argument to to_tile_coords called filter_oob which is false by default. Only if it's true will we filter light sources that are out of bounds (either too negative or too large).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's clever

bliss/catalog.py Outdated
@@ -504,7 +510,9 @@ def to_tile_params(
mask_sources = mask_sources.scatter_(1, top_indices, 1) & source_mask

# get n_sources for each tile
tile_n_sources[ii] = mask_sources.reshape(n_tiles_h, n_tiles_w, n_sources).sum(-1)
tile_n_sources[ii] = mask_sources.reshape(n_tiles_h, n_tiles_w, n_filter_sources).sum(
-1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please shorten a variable name here or somehow rewrite this assignment so there isn't a stray -1 alone on a line.

@XinyueLi1012
Copy link
Collaborator Author

Thanks @XinyueLi1012. Before we merge, can you also verify that our performance on DC2 stays the same with this PR?

Some times when I run new to_tile_coords on full dc2 catalog (image size 4000*4000) in a jupyter notebook, the kernel dead, I'm digging into it to find if there is any bug. The performance using new to_tile_coords should be similar with the results we got previously.

@jeff-regier jeff-regier merged commit bd59bd1 into master Sep 25, 2023
3 checks passed
@jeff-regier jeff-regier deleted the to-tile-coords branch September 25, 2023 16:17
@XinyueLi1012 XinyueLi1012 mentioned this pull request Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

to_tile_params is too slow to be used for data augmentation
2 participants