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

added sampler logic to mean_teacher #385

Merged
merged 2 commits into from
Oct 17, 2024
Merged
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
8 changes: 8 additions & 0 deletions torch_em/self_training/mean_teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ def __init__(
logger=SelfTrainingTensorboardLogger,
momentum=0.999,
reinit_teacher=None,
sampler=None,
**kwargs
):
self.sampler = sampler
# Do we have supervised data or not?
if supervised_train_loader is None:
# No. -> We use the unsupervised training logic.
Expand Down Expand Up @@ -221,6 +223,12 @@ def _train_epoch_unsupervised(self, progress, forward_context, backprop):
# Compute the pseudo labels.
pseudo_labels, label_filter = self.pseudo_labeler(self.teacher, teacher_input)

# If we have a sampler then check if the current batch matches the condition for inclusion in training.
if self.sampler is not None:
keep_batch = self.sampler(pseudo_labels, label_filter)
if not keep_batch:
continue

self.optimizer.zero_grad()
# Perform unsupervised training
with forward_context():
Expand Down