Exemple #1
0
def get_model_result():
    ckpt_path = './FPN_resnet34_sigmoid_valdice_0.9431.pth'
    model = FPN_Resnet34(nclass=4)
    model.load_state_dict(torch.load(ckpt_path))

    train_loader, val_loader, test_loader, nclass = \
        make_data_loader(dataset='steel', data_dir='/data2/hangli_data/kaggle/steel/data',
                         batch_size=36, shuffle=True, num_workers=12,
                         training=True, base_size=[256, 1600], crop_size=[256, 1600], NUM_CLASSES=4)

    pred_ret = []
    target_ret = []
    # tbar = val_loader
    tbar = tqdm(val_loader, ascii=True)
    model = model.cuda()
    model.load_state_dict(torch.load(ckpt_path))
    model.eval()
    for batch_idx, sample in enumerate(tbar):
        data, target = sample['image'], sample['label']
        with torch.no_grad():
            data = data.cuda()
            pred = model(data)
            # pred = torch.sigmoid(pred)

        pred = pred.cpu().numpy()
        target = target.cpu().numpy()
        pred_ret.append(pred)
        target_ret.append(target)

    pred_ret = np.concatenate(pred_ret, axis=0)
    target_ret = np.concatenate(target_ret, axis=0)
    return pred_ret, target_ret
    def __init__(self, args):
        self.args = args

        # define Dataloader
        self.train_loader, self.val_loader, self.test_loader, self.nclass = make_data_loader(args)

        # define network
        model = DeepLab(num_classes=self.nclass, output_stride=args.out_stride)

        optimizer = torch.optim.Adam(model.parameters(), lr=args.base_lr, weight_decay=args.weight_decay)

        self.criterion = SegmentationLoss(weight=None, cuda=args.cuda).build_loss(mode=args.loss_type)
        self.model, self.optimizer = model, optimizer

        self.evaluator = Evaluator(self.nclass)
        self.best_pred = 0.0

        self.trainloss_history = []
        self.valloss_history = []

        self.train_plot = []
        self.val_plot = []
        # every 10 epochs the lr will multiply 0.1
        self.scheduler = LR_Scheduler(args.lr_scheduler, args.base_lr, args.epochs, len(self.train_loader), lr_step=20)

        if args.cuda:
            self.model = self.model.cuda()
    def __init__(self, args):
        self.args = args

        # define Dataloader
        self.train_loader, self.val_loader, self.test_loader, self.nclass = make_data_loader(
            args)

        # define network
        model = InstanceBranch()

        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=args.lr,
                                     weight_decay=args.weight_decay)

        self.criterion = SegmentationLoss(
            weight=None, cuda=args.cuda).build_loss(mode=args.loss_type)
        self.model, self.optimizer = model, optimizer

        self.trainloss_history = []
        self.valloss_history = []

        self.train_plot = []
        self.val_plot = []

        if args.cuda:
            self.model = self.model.cuda()
Exemple #4
0
def main(mode):
    model = Model(mode)
    model_func = model.build_fn()

    if mode == Model.TRAIN_MODE:
        train_loader = make_data_loader("train", Config.train.batch_size)
        model_func(train_loader)
    elif mode == Model.PREDICT_MODE:
        model_func(Config.predict.batch_size)
    else:
        raise ValueError(f"unknown mode: {self.mode}")
Exemple #5
0
def main(data_dir, models_dir, choose_16):
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    planes = ['axial', 'coronal', 'sagittal']
    conditions = ['abnormal', 'acl', 'meniscus']

    models = []

    print(f'Loading best CNN models from {models_dir}...')

    for condition in conditions:
        models_per_condition = []
        for plane in planes:
            checkpoint_pattern = glob(f'{models_dir}/*{plane}*{condition}*.pt')
            checkpoint_path = sorted(checkpoint_pattern)[-1]
            checkpoint = torch.load(checkpoint_path, map_location=device)

            model = MRNet().to(device)
            model.load_state_dict(checkpoint['state_dict'])
            models_per_condition.append(model)

        models.append(models_per_condition)

    print(f'Creating data loaders...')

    axial_loader = make_data_loader(data_dir, 'train', 'axial', choose_16)
    coronal_loader = make_data_loader(data_dir, 'train', 'coronal', choose_16)
    sagittal_loader = make_data_loader(data_dir, 'train', 'sagittal',
                                       choose_16)

    print(f'Collecting predictions on train dataset from the models...')

    ys = []
    Xs = [[], [], []]  # Abnormal, ACL, Meniscus

    with tqdm(total=len(axial_loader)) as pbar:
        for (axial_inputs, labels), (coronal_inputs, _), (sagittal_inputs, _) in \
                zip(axial_loader, coronal_loader, sagittal_loader):

            axial_inputs, coronal_inputs, sagittal_inputs = \
                axial_inputs.to(device), coronal_inputs.to(device), sagittal_inputs.to(device)

            ys.append(labels[0].cpu().tolist())

            for i, model in enumerate(models):
                axial_pred = model[0](axial_inputs).detach().cpu().item()
                coronal_pred = model[1](coronal_inputs).detach().cpu().item()
                sagittal_pred = model[2](sagittal_inputs).detach().cpu().item()

                X = [axial_pred, coronal_pred, sagittal_pred]
                Xs[i].append(X)

            pbar.update(1)

    ys = np.asarray(ys).transpose()
    Xs = np.asarray(Xs)

    print(f'Training logistic regression models for each condition...')

    clfs = []

    for X, y in zip(Xs, ys):
        clf = LogisticRegressionCV(cv=5, random_state=0).fit(X, y)
        clfs.append(clf)

    for i, clf in enumerate(clfs):
        print(
            f'Cross validation score for {conditions[i]}: {clf.score(X, y):.3f}'
        )
        clf_path = f'{models_dir}/lr_{conditions[i]}.pkl'
        joblib.dump(clf, clf_path)

    print(f'Logistic regression models saved to {models_dir}')
def main(data_dir, plane, epochs, lr, weight_decay, device=None):
    diagnoses = ['abnormal', 'acl', 'meniscus']

    exp = f'{datetime.now():%Y-%m-%d_%H-%M}'
    out_dir, losses_path = create_output_dir(exp, plane)

    if device is None:
        device = 'cuda' if torch.cuda.is_available() else 'cpu'

    print('Creating data loaders...')

    train_loader = make_data_loader(data_dir,
                                    'train',
                                    plane,
                                    device,
                                    shuffle=True)
    valid_loader = make_data_loader(data_dir, 'valid', plane, device)

    print(f'Creating models...')

    # Create a model for each diagnosis

    models = [MRNet().to(device), MRNet().to(device), MRNet().to(device)]

    # Calculate loss weights based on the prevalences in train set

    pos_weights = calculate_weights(data_dir, 'train', device)
    criterions = [nn.BCEWithLogitsLoss(pos_weight=weight) \
                  for weight in pos_weights]

    optimizers = [make_adam_optimizer(model, lr, weight_decay) \
                  for model in models]

    lr_schedulers = [make_lr_scheduler(optimizer) for optimizer in optimizers]

    min_valid_losses = [np.inf, np.inf, np.inf]

    print(f'Training a model using {plane} series...')
    print(f'Checkpoints and losses will be save to {out_dir}')

    for epoch, _ in enumerate(range(epochs), 1):
        print(f'=== Epoch {epoch}/{epochs} ===')

        batch_train_losses = np.array([0.0, 0.0, 0.0])
        batch_valid_losses = np.array([0.0, 0.0, 0.0])

        for inputs, labels in train_loader:
            inputs, labels = inputs.to(device), labels.to(device)

            batch_loss = batch_forward_backprop(models, inputs, labels,
                                                criterions, optimizers)
            batch_train_losses += batch_loss

        valid_preds = []
        valid_labels = []

        for inputs, labels in valid_loader:
            inputs, labels = inputs.to(device), labels.to(device)

            batch_preds, batch_loss = \
                batch_forward(models, inputs, labels, criterions)
            batch_valid_losses += batch_loss

            valid_labels.append(labels.detach().cpu().numpy().squeeze())
            valid_preds.append(batch_preds)

        batch_train_losses /= len(train_loader)
        batch_valid_losses /= len(valid_loader)

        print_stats(batch_train_losses, batch_valid_losses, valid_labels,
                    valid_preds)
        save_losses(batch_train_losses, batch_valid_losses, losses_path)

        update_lr_schedulers(lr_schedulers, batch_valid_losses)

        for i, (batch_v_loss, min_v_loss) in \
                enumerate(zip(batch_valid_losses, min_valid_losses)):

            if batch_v_loss < min_v_loss:
                save_checkpoint(epoch, plane, diagnoses[i], models[i],
                                optimizers[i], out_dir)

                min_valid_losses[i] = batch_v_loss
def focal_loss(output, target):
    gamma = 2
    loss = target * torch.log(output) * torch.pow(1 - output, gamma) + (
        1 - target) * torch.log(1 - output) * torch.pow(output, gamma)
    loss = -torch.sum(loss)

    return loss


cell_lines = ["E003"]
for cell_line in cell_lines:
    # 4-fold cross validation
    for seed in range(4):
        print("cell line:", cell_line, "seed:", seed)
        train_loader, val_loader, test_loader = data_loader.make_data_loader(
            cell_line, seed)

        model = Model().to(device)

        if os.path.isfile(cell_line + '_' + str(seed) + '.ckpt'):
            model.load_state_dict(
                torch.load(cell_line + '_' + str(seed) + '.ckpt'))
            print("model loaded")

        # criterion = nn.BCELoss()
        criterion = focal_loss
        optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

        best_val_AUC = 0
        best_test_AUC = 0
        best_val_rank_corr = 0
Exemple #8
0
    def __init__(self, args):
        self.args = args

        # Make DataLoader
        kwargs = {'num_workers': args.workers, 'pin_memory': True}
        self.train_loader, self.test_loader = make_data_loader(args, **kwargs)