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
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
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
def numeric_score(prediction, groundtruth): | |
FP = np.float(np.sum((prediction == 1) & (groundtruth == 0))) | |
FN = np.float(np.sum((prediction == 0) & (groundtruth == 1))) | |
TP = np.float(np.sum((prediction == 1) & (groundtruth == 1))) | |
TN = np.float(np.sum((prediction == 0) & (groundtruth == 0))) | |
return FP, FN, TP, TN | |
def accuracy(prediction, groundtruth): | |
FP, FN, TP, TN = numeric_score(prediction, groundtruth) | |
N = FP + FN + TP + TN |
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
for epoch in tqdm(range(1, num_epochs+1)): | |
start_time = time.time() | |
scheduler.step() | |
lr = scheduler.get_lr()[0] | |
model.train() | |
train_loss_total = 0.0 | |
num_steps = 0 |
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 torch==1.2.0+cu92 torchvision==0.4.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html |
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
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
import torchvision | |
import torchvision.transforms as transforms |
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
BATCH_SIZE = 32 | |
## transformations | |
transform = transforms.Compose( | |
[transforms.ToTensor()]) | |
## download and load training dataset | |
trainset = torchvision.datasets.MNIST(root='./data', train=True, | |
download=True, transform=transform) | |
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, |
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
import matplotlib.pyplot as plt | |
import numpy as np | |
## functions to show an image | |
def imshow(img): | |
#img = img / 2 + 0.5 # unnormalize | |
npimg = img.numpy() | |
plt.imshow(np.transpose(npimg, (1, 2, 0))) | |
## get some random training images |
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
for images, labels in trainloader: | |
print("Image batch dimensions:", images.shape) | |
print("Image label dimensions:", labels.shape) | |
break |