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

[fix]: fixes balanced subsampling bug in data/emnist.py #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions text_recognizer/data/emnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ def _process_raw_dataset(filename: str, dirname: Path):
shutil.rmtree("matlab")


def _sample_to_balance(x, y):
def _sample_to_balance(x, y, y_min_element=NUM_SPECIAL_TOKENS):
"""Because the dataset is not balanced, we take at most the mean number of instances per class."""
np.random.seed(42)
num_to_sample = int(np.bincount(y.flatten()).mean())
# np.bincount always starts counting from 0, so only take
# result for elements that actually occur in y;
num_to_sample = int(np.bincount(y.flatten())[y_min_element:].mean())
all_sampled_inds = []
for label in np.unique(y.flatten()):
inds = np.where(y == label)[0]
Expand Down