-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from anwai98/main
Update Mean-Teacher and FixMatch Self-Training Scheme(s)
- Loading branch information
Showing
9 changed files
with
741 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Probabilistic Domain Adaption | ||
|
||
Implemention of [Probabilistic Domain Adaptation for Biomedical Image Segmentation](https://arxiv.org/abs/2303.11790) in `torch_em`. | ||
Please cite the paper if you are using these approaches in your research. | ||
|
||
## Self-Training Approaches | ||
|
||
The subfolders contain the training scripts for both separate and joint training setups: | ||
|
||
- `unet_source.py` (UNet Source Training): | ||
``` | ||
python unet_source.py -p [check / train / evaluate] | ||
-c <CELL-TYPE> | ||
-i <PATH-TO-DATA> | ||
-s <PATH-TO-SAVE-MODEL-WEIGHTS> | ||
-o <PATH-FOR-SAVING-PREDICTIONS> | ||
``` | ||
|
||
- `unet_mean_teacher.py` (UNet Mean-Teacher Separate Training): | ||
``` | ||
python unet_mean_teacher.py -p [check / train / evaluate] | ||
-c <CELL-TYPE> | ||
-i <PATH-TO-DATA> | ||
-s <PATH-TO-SAVE-MODEL-WEIGHTS> | ||
-o <PATH-FOR-SAVING-PREDICTIONS> | ||
[(optional) --confidence_threshold <THRESHOLD-FOR-COMPUTING-FILTER-MASK>] | ||
``` | ||
|
||
- `unet_adamt.py` (UNet Mean-Teacher Joint Training): | ||
``` | ||
python unet_adamt.py -p [check / train / evaluate] | ||
-c <CELL-TYPE> | ||
-i <PATH-TO-DATA> | ||
-s <PATH-TO-SAVE-MODEL-WEIGHTS> | ||
-o <PATH-FOR-SAVING-PREDICTIONS> | ||
[(optional) --confidence_threshold <THRESHOLD-FOR-COMPUTING-FILTER-MASK>] | ||
``` | ||
|
||
- `unet_fixmatch.py` (UNet FixMatch Separate Training): | ||
``` | ||
python unet_fixmatch.py -p [check / train / evaluate] | ||
-c <CELL-TYPE> | ||
-i <PATH-TO-DATA> | ||
-s <PATH-TO-SAVE-MODEL-WEIGHTS> | ||
-o <PATH-FOR-SAVING-PREDICTIONS> | ||
[(optional) --confidence_threshold <THRESHOLD-FOR-COMPUTING-FILTER-MASK>] | ||
[(optional) --distribution_alignment <ACTIVATES-DISTRIBUTION-ALIGNMENT>] | ||
``` | ||
|
||
- `unet_adamatch.py` (UNet FixMatch Joint Training): | ||
``` | ||
python unet_adamatch.py -p [check / train / evaluate] | ||
-c <CELL-TYPE> | ||
-i <PATH-TO-DATA> | ||
-s <PATH-TO-SAVE-MODEL-WEIGHTS> | ||
-o <PATH-FOR-SAVING-PREDICTIONS> | ||
[(optional) --confidence_threshold <THRESHOLD-FOR-COMPUTING-FILTER-MASK>] | ||
[(optional) --distribution_alignment <ACTIVATES-DISTRIBUTION-ALIGNMENT>] | ||
``` |
This file contains 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
128 changes: 128 additions & 0 deletions
128
experiments/probabilistic_domain_adaptation/livecell/unet_adamatch.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import os | ||
|
||
import pandas as pd | ||
import torch | ||
import torch_em.self_training as self_training | ||
|
||
import common | ||
|
||
|
||
def check_loader(args, n_images=5): | ||
from torch_em.util.debug import check_loader | ||
|
||
cell_types = args.cell_types | ||
print("The cell types", cell_types, "were selected.") | ||
print("Checking the unsupervised loader for the first cell type", cell_types[0]) | ||
|
||
loader = common.get_unsupervised_loader( | ||
args, "train", cell_types[0], | ||
teacher_augmentation="weak", student_augmentation="strong", | ||
) | ||
check_loader(loader, n_images) | ||
|
||
|
||
def _train_source_target(args, source_cell_type, target_cell_type): | ||
model = common.get_unet() | ||
optimizer = torch.optim.Adam(model.parameters(), lr=1e-5) | ||
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode="min", factor=0.5, patience=5) | ||
|
||
# self training functionality | ||
thresh = args.confidence_threshold | ||
pseudo_labeler = self_training.DefaultPseudoLabeler(confidence_threshold=thresh) | ||
loss = self_training.DefaultSelfTrainingLoss() | ||
loss_and_metric = self_training.DefaultSelfTrainingLossAndMetric() | ||
|
||
# data loaders | ||
supervised_train_loader = common.get_supervised_loader(args, "train", source_cell_type, args.batch_size) | ||
supervised_val_loader = common.get_supervised_loader(args, "val", source_cell_type, 1) | ||
unsupervised_train_loader = common.get_unsupervised_loader( | ||
args, args.batch_size, "train", target_cell_type, | ||
teacher_augmentation="weak", student_augmentation="strong-joint", | ||
) | ||
unsupervised_val_loader = common.get_unsupervised_loader( | ||
args, 1, "val", target_cell_type, | ||
teacher_augmentation="weak", student_augmentation="strong-joint", | ||
) | ||
|
||
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") | ||
|
||
name = f"unet_adamatch/thresh-{thresh}" | ||
|
||
if args.distribution_alignment: | ||
assert args.output is not None | ||
print(f"Getting scores for Source {source_cell_type} at Targets {target_cell_type}") | ||
pred_folder = args.output + f"unet_source/{source_cell_type}/{target_cell_type}/" | ||
src_dist = common.compute_class_distribution(pred_folder) | ||
name = f"{name}-distro-align" | ||
else: | ||
src_dist = None | ||
|
||
name = name + f"/{source_cell_type}/{target_cell_type}" | ||
|
||
trainer = self_training.FixMatchTrainer( | ||
name=name, | ||
model=model, | ||
optimizer=optimizer, | ||
lr_scheduler=scheduler, | ||
pseudo_labeler=pseudo_labeler, | ||
unsupervised_loss=loss, | ||
unsupervised_loss_and_metric=loss_and_metric, | ||
supervised_train_loader=supervised_train_loader, | ||
unsupervised_train_loader=unsupervised_train_loader, | ||
supervised_val_loader=supervised_val_loader, | ||
unsupervised_val_loader=unsupervised_val_loader, | ||
supervised_loss=loss, | ||
supervised_loss_and_metric=loss_and_metric, | ||
logger=self_training.SelfTrainingTensorboardLogger, | ||
mixed_precision=True, | ||
device=device, | ||
log_image_interval=100, | ||
save_root=args.save_root, | ||
source_distribution=src_dist | ||
) | ||
trainer.fit(args.n_iterations) | ||
|
||
|
||
def _train_source(args, cell_type): | ||
for target_cell_type in common.CELL_TYPES: | ||
if target_cell_type == cell_type: | ||
continue | ||
_train_source_target(args, cell_type, target_cell_type) | ||
|
||
|
||
def run_training(args): | ||
for cell_type in args.cell_types: | ||
print("Start training for cell type:", cell_type) | ||
_train_source(args, cell_type) | ||
|
||
|
||
def run_evaluation(args): | ||
results = [] | ||
for ct in args.cell_types: | ||
res = common.evaluate_transfered_model(args, ct, "unet_adamatch") | ||
results.append(res) | ||
results = pd.concat(results) | ||
print("Evaluation results:") | ||
print(results) | ||
result_folder = "./results" | ||
os.makedirs(result_folder, exist_ok=True) | ||
results.to_csv(os.path.join(result_folder, "unet_adamatch.csv"), index=False) | ||
|
||
|
||
def main(): | ||
parser = common.get_parser(default_iterations=25000, default_batch_size=8) | ||
parser.add_argument("--confidence_threshold", default=None, type=float) | ||
parser.add_argument("--distribution_alignment", action='store_true', help="Activates Distribution Alignment") | ||
args = parser.parse_args() | ||
if args.phase in ("c", "check"): | ||
check_loader(args) | ||
elif args.phase in ("t", "train"): | ||
run_training(args) | ||
elif args.phase in ("e", "evaluate"): | ||
run_evaluation(args) | ||
else: | ||
raise ValueError(f"Got phase={args.phase}, expect one of check, train, evaluate.") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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
Oops, something went wrong.