import numpy as np
from torchvision import transforms
import pandas as pd

from fcn_naive_model import fcn_model
from data_loader import Melanoma_Train_Validation_DataLoader
from helper import jaccard, dice, load_model

use_cuda = torch.cuda.is_available()

# Hyperparameters
thrs_list = np.linspace(0.1, 0.9, 400) 
batch_size = 10
num_workers = 10

_, validation_loader = Melanoma_Train_Validation_DataLoader(batch_size = batch_size,  data_transforms=transforms.Compose([transforms.Resize([512, 512]), transforms.ToTensor()]), num_workers=num_workers)

model = fcn_model().cuda() if use_cuda else fcn_model()
model = load_model(model, model_dir="fcn_15epch_interpol.pt", map_location_device="gpu") if use_cuda else load_model(model, model_dir="fcn_15epch_interpol.pt")
columns = ["Threshold", "Accuracy"]
thrs_df = pd.DataFrame(columns = columns)
thrs_df["Threshold"] = thrs_list

for thrs in thrs_list:
    jaccard_acc = 0.0
    for input_img, label_img in validation_loader:
        input_img = input_img.cuda() if use_cuda else input_img
        label_img = label_img.cuda() if use_cuda else label_img
        outputs = model(input_img)
        preds = torch.sigmoid(outputs)
        jaccard_acc += jaccard(label_img, (preds > thrs).float())
    transforms.RandomVerticalFlip(p=1.0),
    transforms.RandomRotation(205),
    transforms.RandomRotation(45),
    transforms.RandomRotation(145),
    transforms.RandomRotation(300),
    transforms.ColorJitter(brightness=1.3),
    transforms.ColorJitter(contrast=1.2),
    transforms.ColorJitter(saturation=1.2),
    transforms.ColorJitter(saturation=0.7),
    transforms.ColorJitter(hue=0.3),
    transforms.ColorJitter(hue=0.1),
    transforms.Resize([img_size, img_size]),
    transforms.ToTensor()
]
# Note: FIX AUGMENTATIONS
train_loader, validation_loader = Melanoma_Train_Validation_DataLoader(
    batch_size=batch_size, data_transforms=transforms.Compose(mul_transform))

optimizer = optim.SGD(segm_model.parameters(),
                      lr=learning_rate,
                      momentum=momentum)
criterion = nn.BCEWithLogitsLoss().cuda(
) if use_cuda else nn.BCEWithLogitsLoss()

dataloader_dict = {"train": train_loader, "valid": validation_loader}


def train_model(cust_model,
                dataloaders,
                criterion,
                optimizer,
                num_epochs=10,
Exemple #3
0
mul_transf = [
    transforms.Resize(size=(img_size, img_size)),
    transforms.ToTensor()
]

optimizerSGD = optim.SGD(segm_model.parameters(),
                         lr=lr_rate,
                         momentum=momentum)
criterion = nn.BCELoss().cuda() if use_cuda else nn.BCELoss()
scheduler = optim.lr_scheduler.MultiStepLR(optimizerSGD,
                                           milestones=milestones,
                                           gamma=gamma)

train_loader, valid_loader = Melanoma_Train_Validation_DataLoader(
    batch_size=batch_size,
    validation_split=0.1,
    num_workers=8,
    data_transforms=transforms.Compose(mul_transf))

dict_loaders = {"train": train_loader, "valid": valid_loader}


def train_model(cust_model,
                dataloaders,
                criterion,
                optimizer,
                num_epochs,
                scheduler=None):
    start_time = time.time()
    val_acc_history = []
    best_acc = 0.0