예제 #1
0
def eval(cfg):
    # Setup seeds
    torch.manual_seed(cfg.get("seed", 1337))
    torch.cuda.manual_seed(cfg.get("seed", 1337))
    np.random.seed(cfg.get("seed", 1337))
    random.seed(cfg.get("seed", 1337))

    # Setup device
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # Setup evaluation data
    loader_source = get_loader(cfg, "train")
    # data_eval_labels = utils.recursive_glob(os.path.join(cfg["data"]["path"], 'labels'))

    # Setup model
    model = Unet(cfg).to(device)
    checkpoint = torch.load(cfg["training"]["checkpoint"])
    model.load_state_dict(checkpoint["model_state"])
    stats = None
    model.eval()
    for images, labels in tqdm.tqdm(loader_source):
        model.set_input(images, labels)
        model.forward()
        if stats is None:
            stats = [StatsRecorder() for i in range(len(model.hooks))]
        for i, hook in enumerate(model.hooks):
            activation = hook.output
            b, c, h, w = activation.shape
            activation = activation.transpose(0,
                                              1).reshape(c,
                                                         -1).transpose(0, 1)
            stats[i].update(activation.cpu().data.numpy())

    print([s.mean for s in stats])
    print([s.std for s in stats])
예제 #2
0
def main(args):
    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    d = dcrf.DenseCRF2D(512, 512, 2)

    down_method = args.down_method
    up_method = args.up_method
    separable = args.separable

    ds = DataSetWrapper(args.batch_size, args.num_workers, 0.2)
    test_dl = ds.get_data_loaders(train=False)

    model = Unet(input_dim=1,
                 separable=True,
                 down_method='conv',
                 up_method='transpose')
    model = nn.DataParallel(model).to(device)

    load_state = torch.load(f'./checkpoint/conv_transpose_True.ckpt')

    model.load_state_dict(load_state['model_state_dict'])

    model.eval()
    name = 0
    with torch.no_grad():
        for (img, label) in test_dl:
            imgs, labels = img.to(device), label.to(device)
            preds = model(img)
            name += 1
            for i in range(args.batch_size):
                img, label, pred = imgs[i, :], labels[i, :], preds[i, :]

                probs = torch.stack([1 - pred, pred], dim=0).cpu().numpy()
                img, label = img.cpu().numpy(), label.cpu().numpy()
                pairwise_energy = create_pairwise_bilateral(sdims=(10, 10),
                                                            schan=(0.01, ),
                                                            img=img,
                                                            chdim=0)
                U = unary_from_softmax(probs)
                d = dcrf.DenseCRF2D(512, 512, 2)
                d.setUnaryEnergy(U)
                d.addPairwiseEnergy(pairwise_energy, compat=10)

                Q = d.inference(100)
                map = np.argmax(Q, axis=0).reshape((512, 512))
                print(map.shape)

                img = (255. / img.max() * (img - img.min())).astype(np.uint8)
                label = (255. / label.max() * (label - label.min())).astype(
                    np.uint8)
                pred = (255. / map.max() * (map - map.min())).astype(np.uint8)

                img = Image.fromarray(img[0, :], mode='L')
                label = Image.fromarray(label[0, :], mode='L')
                pred = Image.fromarray(pred, mode='L')

                img.save(f'./results/{name}_{i}_i.png')
                label.save(f'./results/{name}_{i}_l.png')
                pred.save(f'./results/{name}_{i}_p.png')
예제 #3
0
def main(args):
    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    separable = args.separable
    up_method = args.up_method
    down_method = args.down_method
    img_src = args.s

    ######## Model Setting ########
    model = Unet(input_dim=1,
                 separable=separable,
                 down_method=down_method,
                 up_method=up_method)
    model = nn.DataParallel(model).to(device)
    load_state = torch.load(
        f'./checkpoint/{down_method}_{up_method}_{separable}.ckpt')
    model.load_state_dict(load_state['model_state_dict'])
    model.eval()
    ###############################

    d = dcrf.DenseCRF2D(512, 512, 2)

    img = Image.open(img_src).convert('L')
    img = pad_resize(img, 512)
    img = TF.to_tensor(img)
    img = img.unsqueeze(0)

    with torch.no_grad():
        img = img.to(device)
        pred = model(img)

        probs = torch.stack([1 - pred, pred], dim=0).cpu().numpy()
        img, pred = img.cpu().numpy(), pred.cpu().numpy()
        pairwise_energy = create_pairwise_bilateral(sdims=(10, 10),
                                                    schan=(0.01, ),
                                                    img=img,
                                                    chdim=0)
        U = unary_from_softmax(probs)
        d.setUnaryEnergy(U)
        d.addPairwiseEnergy(pairwise_energy, compat=10)

        Q = d.inference(100)
        map = np.argmax(Q, axis=0).reshape((512, 512))

        img = (255. / img.max() * (img - img.min())).astype(np.uint8)
        pred = (255. / map.max() * (map - map.min())).astype(np.uint8)

        img = Image.fromarray(np.squeeze(img), mode='L')
        pred = Image.fromarray(pred, mode='L')

        img.save(f'../similarity/{img_src[:-4]}_o.png')
        pred.save(f'../similarity/{img_src[:-4]}_p.png')
예제 #4
0
def train():
    save_dir = "/home/FuDawei/NLP/SQUAD/unet/data/"
    train_examples, dev_examples, opt = prepare_train(save_dir)
    epoch = 30
    batch_size = 32
    model = Unet(opt=opt).to(device)
    parameters = filter(lambda p: p.requires_grad, model.parameters())
    optimizer = torch.optim.Adamax(parameters, lr=opt["lr"])
    best_score, exact_scores, f1_scores = 0, [], []

    count = 0
    total_loss = 0
    for ep in range(epoch):
        model.train()
        for batch_data in get_batch_data(train_examples, batch_size):
            data = model.get_data(batch_data)
            loss = model(data)
            model.zero_grad()
            optimizer.zero_grad()
            loss.backward()
            nn.utils.clip_grad_norm_(parameters, 10)
            optimizer.step()
            model.reset_parameters()
            count += 1
            # print(loss.item())
            # Evaluate(dev_examples, model)

            total_loss += loss.item()
            if count % 100 == 0:
                print(total_loss / 100)
                total_loss = 0
                # model.eval()
                # Evaluate(dev_examples, model, opt)
            if not opt["fix_word_embedding"]:
                model.reset_parameters()
        print(ep)
        model.eval()
        exact, f1 = Evaluate(dev_examples, model, opt)
        exact_scores.append(exact)
        f1_scores.append(f1)
        if f1 > best_score:
            best_score = f1
            torch.save(model.state_dict(), save_dir + "best_model")
    with open(save_dir + '_f1_scores.pkl', 'wb') as f:
        pkl.dump(f1_scores, f)
    with open(save_dir + '_exact_scores.pkl', 'wb') as f:
        pkl.dump(exact_scores, f)
예제 #5
0
def test(args):
    my_dataset = MyDataset("../data/val", transform=x_transforms, target_transform=y_transforms)
    dataloaders = DataLoader(my_dataset, batch_size=1)
    model = Unet(3, 1)
    model.load_state_dict(torch.load(args.model_path, map_location='cpu'))
    model.eval()

    plt.ion()
    with torch.no_grad():
        count = 0
        for x, _ in dataloaders:
            y = model(x)
            img_y = torch.squeeze(y).numpy()
            predict_path = os.path.join("../data/predict/", "%03d_predict.png" % count)
            plt.imsave(predict_path, img_y)
            plt.imshow(img_y)
            plt.pause(0.1)
            count += 1
        plt.show()
예제 #6
0
std = (0.229, 0.224, 0.225)
df = pd.read_csv(sample_submission_path)
testset = DataLoader(
    TestDataset(test_data_folder, df, mean, std),
    batch_size=batch_size,
    shuffle=False,
    num_workers=num_workers,
    pin_memory=True
)

# Initialize mode and load trained weights
ckpt_path = "../input/res18ex6/model26.pth"
device = torch.device("cuda")
model = Unet("resnet18", encoder_weights=None, classes=4, activation=None)
model.to(device)
model.eval()
state = torch.load(ckpt_path, map_location=lambda storage, loc: storage)
model.load_state_dict(state["state_dict"])

# start prediction
predictions = []
for i, batch in enumerate(tqdm(testset)):
    fnames, images = batch
    batch_preds = torch.sigmoid(model(images.to(device)))
    batch_preds = batch_preds.detach().cpu().numpy()
    for fname, preds in zip(fnames, batch_preds):
        for cls, pred in enumerate(preds):
            pred, num = post_process(pred, best_threshold, min_size)
            rle = mask2rle(pred)
            name = fname + f"_{cls+1}"
            predictions.append([name, rle])
예제 #7
0
                     validation_augmentation_kaggle())
 test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False)
 print('testing data loaded')
 net = Unet(3, 4).float()
 if is_gpu:
     net.cuda()
 net.load_state_dict(torch.load(model_id, map_location=device))
 print('model loaded')
 # prepare
 ans_dict = global_dict()
 image_uid = 0
 answer_label = []
 answer_index = ['Image_Label', 'EncodedPixels']
 # calculation
 t_bar = tq(test_loader)
 net.eval()
 with torch.no_grad():
     for img, masks in t_bar:
         if is_gpu:
             img = img.cuda()
         masks_pr = net(img).cpu().detach().numpy()
         for batch in masks_pr:
             for i, mask in enumerate(batch):
                 # image_uid, i
                 answer_label.append(get_answer(mask, thresholds[i]))
             image_uid += 1
 # submission
 submit_file = model_id + '.csv'
 sub = test_set.df
 sub['EncodedPixels'] = answer_label
 sub.to_csv(submit_file, columns=answer_index, index=False)
예제 #8
0
def main(args):
    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    ### Hyperparameters Setting ###
    epochs = args.epochs
    batch_size = args.batch_size
    num_workers = args.num_workers
    valid_ratio = args.valid_ratio
    threshold = args.threshold
    separable = args.separable
    down_method = args.down_method
    up_method = args.up_method
    ### DataLoader ###
    dataset = DataSetWrapper(batch_size, num_workers, valid_ratio)
    train_dl, valid_dl = dataset.get_data_loaders(train=True)

    ### Model: U-Net ###
    model = Unet(input_dim=1,
                 separable=separable,
                 down_method=down_method,
                 up_method=up_method)
    model.summary()
    model = nn.DataParallel(model).to(device)

    optimizer = optim.Adam(model.parameters(),
                           lr=args.lr,
                           weight_decay=args.weight_decay)
    scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer,
                                                     T_max=len(train_dl),
                                                     eta_min=0,
                                                     last_epoch=-1)
    criterion = nn.BCEWithLogitsLoss()
    train_losses = []
    val_losses = []

    ###Train & Validation start ###
    mIOU_list = []
    best_mIOU = 0.
    step = 0

    for epoch in range(epochs):

        ### train ###
        pbar = tqdm(train_dl)
        model.train()
        losses = []

        for (img, label) in pbar:
            optimizer.zero_grad()
            img, label = img.to(device), label.to(device)
            pred = model(img)
            # pred = Padding()(pred, label.size(3))
            loss = criterion(pred, label)
            loss.backward()
            optimizer.step()
            losses.append(loss.item())
            pbar.set_description(
                f'E: {epoch + 1} | L: {loss.item():.4f} | lr: {scheduler.get_lr()[0]:.7f}'
            )
        scheduler.step()
        if (epoch + 1) % 10:
            losses = sum(losses) / len(losses)
            train_losses.append(losses)

        ### validation ###
        with torch.no_grad():
            model.eval()
            mIOU = []
            losses = []
            pbar = tqdm(valid_dl)
            for (img, label) in pbar:
                img, label = img.to(device), label.to(device)
                pred = model(img)

                loss = criterion(pred, label)

                mIOU.append(get_IOU(pred, label, threshold=threshold))
                losses.append(loss.item())

            mIOU = sum(mIOU) / len(mIOU)
            mIOU_list.append(mIOU)
            if (epoch + 1) % 10:
                losses = sum(losses) / len(losses)
                val_losses.append(losses)

            print(
                f'VL: {loss.item():.4f} | mIOU: {100 * mIOU:.1f}% | best mIOU: {100 * best_mIOU:.1f}'
            )

        ### Early Stopping ###
        if mIOU > best_mIOU:
            best_mIOU = mIOU
            save_state = {
                'epoch': epoch,
                'model_state_dict': model.state_dict(),
                'optimizer_state_dict': optimizer.state_dict(),
                'scheduler_state_dict': scheduler.state_dict(),
                'train_losses': train_losses,
                'val_losses': val_losses,
                'best_mIOU': best_mIOU
            }
            torch.save(
                save_state,
                f'./checkpoint/{down_method}_{up_method}_{separable}.ckpt')
            step = 0
        else:
            step += 1
            if step > args.patience:
                print('Early stopped...')
                return
예제 #9
0
파일: main.py 프로젝트: oosky9/2D-Unet
def train(args, x_train, y_train, x_valid, y_valid):

    writer = SummaryWriter()

    best_dice = 0 

    model = Unet().to(args.device)

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

    bce_loss = torch.nn.BCELoss()

    train_dataloader = load_dataset(x_train, y_train, args.batch_size, True)
    valid_dataloader = load_dataset(x_valid, y_valid, args.batch_size, False)

    result = {}
    result['train/BCE'] = []
    result['train/Dice'] = []
    result['valid/BCE'] = []
    result['valid/Dice'] = []

    for epoch in range(args.epochs):
        print('train step: epoch {}'.format(str(epoch+1).zfill(4)))

        train_bce = []
        train_dice = []

        for inp_im, lab_im in tqdm(train_dataloader):
            inp_im = inp_im.to(args.device)
            lab_im = lab_im.to(args.device)

            pred = model(inp_im)

            bce = bce_loss(pred, lab_im)
            dice = calc_dice(pred, lab_im)

            train_bce.append(bce.item())
            train_dice.append(dice)

            model.zero_grad()
            bce.backward()
            optimizer.step()
        
        result['train/BCE'].append(statistics.mean(train_bce))
        result['train/Dice'].append(statistics.mean(train_dice))

        writer.add_scalar('train/BinaryCrossEntropy', result['train/BCE'][-1], epoch+1)
        writer.add_scalar('train/DiceScore', result['train/Dice'][-1], epoch+1)

        print('BCE: {}, Dice: {}'.format(result['train/BCE'][-1], result['train/Dice'][-1]))

        if (epoch+1) % 10 == 0 or (epoch+1) == 1:

            with torch.no_grad():
                print('valid step: epoch {}'.format(str(epoch+1).zfill(4)))
                model.eval()

                valid_bce = []
                valid_dice = []
                for inp_im, lab_im in tqdm(valid_dataloader):
                    inp_im = inp_im.to(args.device)
                    lab_im = lab_im.to(args.device)

                    pred = model(inp_im)

                    bce = bce_loss(pred, lab_im)
                    dice = calc_dice(pred, lab_im)

                    valid_bce.append(bce.item())
                    valid_dice.append(dice)
                
                result['valid/BCE'].append(statistics.mean(valid_bce))
                result['valid/Dice'].append(statistics.mean(valid_dice))

                writer.add_scalar('valid/BinaryCrossEntropy', result['valid/BCE'][-1], epoch+1)
                writer.add_scalar('valid/DiceScore', result['valid/Dice'][-1], epoch+1)

                print('BCE: {}, Dice: {}'.format(result['valid/BCE'][-1], result['valid/Dice'][-1]))


                if best_dice < result['valid/Dice'][-1]:
                    best_dice = result['valid/Dice'][-1]

                    best_model_name = os.path.join(args.save_model_path, f'best_model_{epoch + 1:04}.pth')
                    print('save model ==>> {}'.format(best_model_name))
                    torch.save(model.state_dict(), best_model_name)