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
| model = mt_models.Unet(drop_rate=0.4, bn_momentum=0.1) | |
| model.cuda() | |
| num_epochs = 10 | |
| initial_lr = 0.001 | |
| optimizer = optim.Adam(model.parameters(), lr=initial_lr) | |
| scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, num_epochs) |
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
| train_loader = DataLoader(gmdataset_train, batch_size=16, | |
| shuffle=True, pin_memory=True, | |
| collate_fn=mt_datasets.mt_collate, | |
| num_workers=1) | |
| val_loader = DataLoader(gmdataset_val, batch_size=16, | |
| shuffle=True, pin_memory=True, | |
| collate_fn=mt_datasets.mt_collate, | |
| num_workers=1) |
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
| ### training dataset with 80/20 split | |
| TRAIN_ROOT_DIR_GMCHALLENGE = "/gdrive/My Drive/DAIR RESOURCES/PyTorch/medical_imaging/train/" | |
| gmdataset_train = mt_datasets.SCGMChallenge2DTrain(root_dir=TRAIN_ROOT_DIR_GMCHALLENGE, | |
| subj_ids=range(1, 9), | |
| transform=train_transform, | |
| slice_filter_fn=mt_filters.SliceFilter()) | |
| gmdataset_val = mt_datasets.SCGMChallenge2DTrain(root_dir=TRAIN_ROOT_DIR_GMCHALLENGE, | |
| subj_ids=range(9, 11), |
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
| # training dataset | |
| train_transform = transforms.Compose([ | |
| mt_transforms.Resample(0.25, 0.25), | |
| mt_transforms.CenterCrop2D((200, 200)), | |
| mt_transforms.ElasticTransform(alpha_range=(28.0, 30.0), | |
| sigma_range=(3.5, 4.0), | |
| p=0.3), | |
| mt_transforms.RandomAffine(degrees=4.6, | |
| scale=(0.98, 1.02), | |
| translate=(0.03, 0.03)), |
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
| def threshold_predictions(predictions, thr=0.999): | |
| thresholded_preds = predictions[:] | |
| low_values_indices = thresholded_preds < thr | |
| thresholded_preds[low_values_indices] = 0 | |
| low_values_indices = thresholded_preds >= thr | |
| thresholded_preds[low_values_indices] = 1 | |
| return thresholded_preds |
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
| # transformer | |
| composed_transform = transforms.Compose([ | |
| mt_transforms.Resample(0.25, 0.25), | |
| mt_transforms.CenterCrop2D((200, 200)), | |
| mt_transforms.ToTensor(), | |
| ]) | |
| # load data | |
| train_dataset = mt_datasets.SCGMChallenge2DTrain(root_dir=ROOT_DIR_GMCHALLENGE, transform=composed_transform) |
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
| ROOT_DIR_GMCHALLENGE = "/gdrive/My Drive/DAIR RESOURCES/PyTorch/medical_imaging/train/" | |
| mri_input_filename = os.path.join(ROOT_DIR_GMCHALLENGE, | |
| 'site1-sc01-image.nii.gz') | |
| mri_gt_filename = os.path.join(ROOT_DIR_GMCHALLENGE, | |
| 'site1-sc01-mask-r1.nii.gz') | |
| pair = mt_datasets.SegmentationPair2D(mri_input_filename, mri_gt_filename) | |
| slice_pair = pair.get_pair_slice(0) | |
| input_slice = slice_pair["input"] | |
| gt_slice = slice_pair["gt"] |
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
| !pip3 install http://download.pytorch.org/whl/cu80/torch-0.4.0-cp36-cp36m-linux_x86_64.whl | |
| !pip3 install torchvision | |
| !pip install medicaltorch | |
| !pip3 install numpy==1.14.1 |
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
| from collections import defaultdict | |
| import time | |
| import os | |
| import numpy as np | |
| from tqdm import tqdm | |
| from tensorboardX import SummaryWriter |
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
| pip install medicaltorch |