-
-
Notifications
You must be signed in to change notification settings - Fork 396
extra sampler + docs #1262
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
Merged
Merged
extra sampler + docs #1262
Changes from 4 commits
Commits
Show all changes
6 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 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
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
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
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
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
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,12 +1,20 @@ | ||
| from typing import List, Tuple | ||
| from collections import Counter | ||
| from operator import itemgetter | ||
| import os | ||
| from random import randint, shuffle | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| from torch.utils.data import DataLoader | ||
|
|
||
| from catalyst.data.sampler import BalanceBatchSampler, DynamicBalanceClassSampler | ||
| from catalyst.contrib.datasets import MNIST | ||
| from catalyst.data.sampler import ( | ||
| BalanceBatchSampler, | ||
| BalanceClassSampler, | ||
| BatchBalanceClassSampler, | ||
| DynamicBalanceClassSampler, | ||
| ) | ||
|
|
||
| TLabelsPK = List[Tuple[List[int], int, int]] | ||
|
|
||
|
|
@@ -35,6 +43,44 @@ def generate_valid_labels(num: int) -> TLabelsPK: | |
| return labels_pk | ||
|
|
||
|
|
||
| def test_balance_class_sampler(): | ||
| bs = 32 | ||
| data = MNIST(os.getcwd(), train=False, download=True) | ||
| for mode in ["downsampling", "upsampling", 100, 200, 500]: | ||
| sampler = BalanceClassSampler(data.targets.cpu().numpy().tolist(), mode=mode) | ||
| loader = DataLoader(data, sampler=sampler, batch_size=bs) | ||
| y_list = [] | ||
| for _, y in loader: | ||
| # assert len(x) == len(y) == bs | ||
| y_list.extend(y.cpu().numpy().tolist()) | ||
| # prior | ||
| if mode == "downsampling": | ||
| mode = 892 | ||
| if mode == "upsampling": | ||
| mode = 1135 | ||
| assert all( | ||
| n == mode for n in Counter(y_list).values() | ||
| ), f"Each class shoud contain {mode} instances" | ||
|
|
||
|
|
||
| def test_batch_balance_class_sampler(): | ||
|
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. [pep8] reported by reviewdog 🐶 |
||
| data = MNIST(os.getcwd(), train=False, download=True) | ||
| for num_classes in [2, 3, 5, 10]: | ||
| for num_samples in [2, 5, 10, 50]: | ||
| sampler = BatchBalanceClassSampler( | ||
| data.targets.cpu().numpy().tolist(), | ||
| num_classes=num_classes, | ||
| num_samples=num_samples, | ||
| ) | ||
| loader = DataLoader(data, batch_sampler=sampler) | ||
| for _, y in loader: | ||
| stats = Counter(y.cpu().numpy().tolist()) | ||
| assert len(stats) == num_classes, f"Each batch shoud contain {num_classes} classes" | ||
| assert all( | ||
| n == num_samples for n in stats.values() | ||
| ), f"Each class shoud contain {num_samples} instances" | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def input_for_balance_batch_sampler() -> TLabelsPK: | ||
| """ | ||
|
|
||
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.
[pep8] reported by reviewdog 🐶
D103 Missing docstring in public function