-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Tutorial on imbalanced datasets #236
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
Conversation
############################################################################### | ||
# Let's have a look at the class distribution in the datasets. | ||
|
||
# Get all training targets and count the number of class instances |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
# The last 5 classes will keep their samples. | ||
|
||
# Create class proportions | ||
imbal_class_prop = imbal_class_prop = np.hstack(([0.1] * 5, [1.0] * 5)) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
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.
I have roughly gone through half of the tutorial.
I also have some minor comments with formatting etc. But I'll create a PR to your branch later with those changes rather than commenting.
# The last 5 classes will keep their samples. | ||
|
||
# Create class proportions | ||
imbal_class_prop = imbal_class_prop = np.hstack(([0.1] * 5, [1.0] * 5)) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
@chsasank Thank you for the review! I've added your suggestions. Let me know, what you think about the changes. |
# Let's have a look at the class distribution in the datasets. | ||
|
||
|
||
def get_labels_and_class_counts(labels_list): |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(15, 6)) | ||
ax1.bar(class_names, train_class_counts) | ||
ax1.set_title('Training dataset distribution') | ||
ax1.set_xlabel('Classes') |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
optimizer = optim.SGD(model.parameters(), lr=1e-3, momentum=0.9) | ||
|
||
|
||
def train(epoch): |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Thanks, I think this looks good. Let me add some small formatting changes to your branch. |
# Let's have a look at the class distribution in the datasets. | ||
|
||
# Get all training targets and count the number of class instances | ||
train_targets = np.array(train_dataset.train_labels) |
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.
Getting the following error on this line:
AttributeError: 'CIFAR10' object has no attribute 'train_labels'
Could you please help with this
Hi @ptrblck! Thank you for your pull request. We require contributors to sign our Contributor License Agreement, and yours needs attention. You currently have a record in our system, but we do not have a signature on file. In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
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.
Updated variables because in recent versions, the variable for the labels for the training samples in torchvision.datasets.CIFAR10
has been changed to targets
|
||
# Get all training targets and count the number of class instances | ||
train_targets, train_class_counts = get_labels_and_class_counts( | ||
train_dataset.train_labels) |
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.
train_dataset.train_labels) | |
train_dataset.targets) |
''' | ||
if self.train: | ||
targets, class_counts = get_labels_and_class_counts( | ||
self.dataset.train_labels) |
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.
self.dataset.train_labels) | |
self.dataset.targets) |
Closing this as it's been quite some time since it was created and no longer relevant. |
This tutorial deals with the problem of an imbalanced dataset and how to train a classifier on it.
After training a CNN on the original CIFAR10 dataset, we resample it to create an artificially imbalanced dataset. Since the CNN performs quite poorly on this new dataset, we use the
WeightedRandomSampler
in the first step and a weighted criterion afterwards to tackle the problem.I've created the tutorial in the intermediate section, but I'm not sure if it's the right place.
Feedback regarding the text and code is very welcome!