Ejemplo n.º 1
0
def test(hidden_dim=25, out_dir='out', data_dir='data', device=util.device()):
    out_dir, data_dir = map(Path, (out_dir, data_dir))
    vocab = util.Vocab.load(data_dir / 'vocab.txt')
    model = Model(hidden_dim=hidden_dim, vocab=vocab)
    model.load_state_dict(torch.load(out_dir / 'model.pt'))
    loss_fn = nn.CrossEntropyLoss()
    trainer = Trainer(model, loss_fn, vocab, device)

    with data.SentPairStream(data_dir / 'dev.tsv') as dev_data:
        dev_loader = torchdata.DataLoader(dev_data,
                                          shuffle=False,
                                          batch_size=8)
        dev_metrics = trainer.eval_(dev_loader)
        L.info('Dev performance: %s', dev_metrics)

    with data.SentPairStream(data_dir / 'test.tsv') as test_data:
        test_loader = torchdata.DataLoader(test_data,
                                           shuffle=False,
                                           batch_size=8)
        test_metrics = trainer.eval_(test_loader)
        L.info('Test performance: %s', test_metrics)

t_dataset=MelanomaDataset(df=df, imfolder=test,
                          train=False, transforms=test_transform, meta_features=meta_features)

print('Length of test set is {}'.format(len(t_dataset)))

testloader=DataLoader(t_dataset, batch_size=8, shuffle=False, num_workers=8)

"""Testing"""
# model = ResNetModel()()
# model = EfficientModel()
# model = EfficientModel(n_meta_features=len(meta_features))
model = Model(arch='efficientnet-b1')
# model.load_state_dict(torch.load("../checkpoint/fold_1/efficient_256/efficientb0_256_14_0.9212.pth", map_location=torch.device(device)))
model.load_state_dict(torch.load("..//checkpoint/fold_1/efficient_320/efficientb1_320_14_0.9293.pth", map_location=torch.device(device)))
model.to(device)

model.eval()
test_prob_stack = []
img_ids = []
with torch.no_grad():
    for i in range(15):
        test_prob = []
        for img, meta, img_id in tqdm(testloader):
            if train_on_gpu:
                img, meta = img.to(device), meta.to(device)

            logits = model.forward(img)

            pred = logits.sigmoid().detach().cpu()