示例#1
0
def test():
    ## dataset
    trans = T.Compose([
        T.Resize(opt.imageSize),
        Style(2),
        T.ToTensor(),
        T.Normalize((0.5), (0.5))
    ])
    test_dataset = load_dataset(opt.dataroot,
                                opt.dataset,
                                opt.imageSize,
                                trans=trans,
                                train=False)
    test_dataloader = DataLoader(test_dataset,
                                 batch_size=opt.batchSize,
                                 shuffle=False)
    opt.dataSize = test_dataset.__len__()

    ## model
    siamese = SiameseNetwork(opt.imageSize).to(device)
    assert siamese.load_state_dict(torch.load(opt.siamese_pth))
    print("Pretrained models have been loaded.")
    print([torch.max(i).item() for i in siamese.state_dict().values()])
    print([torch.min(i).item() for i in siamese.state_dict().values()])

    # loss
    siamese_criteria = ContrastiveLoss()
    siamese.eval()
    siamese_loss = []
    labels = []
    predictions = []
    evaluation = Evaluate(opt.experiment)
    tqdm_loader = tqdm.tqdm(test_dataloader)
    for i, (test_inputs0, test_inputs1, targets) in enumerate(tqdm_loader):
        tqdm_loader.set_description(f"Test Sample {i+1} / {opt.dataSize}")
        ## inference
        test_inputs0, test_inputs1, targets = test_inputs0.to(
            device), test_inputs1.to(device), targets.to(device)
        with torch.no_grad():
            outputs0, outputs1 = siamese(test_inputs0, test_inputs1)
        euclidean_distance = F.pairwise_distance(outputs0, outputs1)
        predictions.extend(list(euclidean_distance.detach().cpu().numpy()))
        labels.extend(list(torch.abs(targets).detach().cpu().numpy()))

        vutils.save_image(torch.cat(
            [test_inputs0[:, :, :, :], test_inputs1[:, :, :, :]], dim=2),
                          '{0}/{1}-0.png'.format(opt.experiment, i),
                          pad_value=2)
        # print(targets.detach().cpu().numpy().reshape([-1, 8]))
        # print(euclidean_distance.detach().cpu().numpy().reshape([-1, 8]))

    # print(np.array(labels).reshape((-1, 8)))
    # print(np.array(predictions).reshape((-1, 8)))

    evaluation.labels = labels
    predictions = np.array(predictions)
    predictions = (predictions - np.min(predictions)) / (np.max(predictions) -
                                                         np.min(predictions))
    evaluation.scores = predictions
    return evaluation.run()
示例#2
0
def test():
    ## dataset
    trans = T.Compose([
        T.Resize((opt.imageSize, opt.imageSize)),
        AddSaltPepperNoise(0.1),
        T.ToTensor(),
    ])
    test_dataset = load_dataset(opt.dataroot,
                                opt.dataset,
                                opt.imageSize,
                                trans=trans,
                                train=False)
    test_dataloader = DataLoader(test_dataset,
                                 batch_size=opt.batchSize,
                                 shuffle=False)
    opt.dataSize = test_dataset.__len__()

    ## model
    cnn = CNN(opt.imageSize).to(device)
    assert cnn.load_state_dict(torch.load(opt.cnn_pth))
    print("Pretrained models have been loaded.")
    print([torch.max(i).item() for i in cnn.state_dict().values()])
    print([torch.min(i).item() for i in cnn.state_dict().values()])

    # loss
    cnn_criteria = nn.MSELoss()
    # cnn_criteria = ContrastiveLoss()
    cnn.eval()
    cnn_loss = []
    labels = []
    predictions = []
    evaluation = Evaluate(opt.experiment)
    tqdm_loader = tqdm.tqdm(test_dataloader)
    for i, (test_inputs0, test_inputs1, targets) in enumerate(tqdm_loader):
        tqdm_loader.set_description(f"Test Sample {i+1} / {opt.dataSize}")
        ## inference
        test_inputs0, test_inputs1, targets = test_inputs0.to(
            device), test_inputs1.to(device), targets.to(device)
        batchsize = test_inputs0.size(0)
        with torch.no_grad():
            outputs, _ = cnn(test_inputs0, test_inputs1)
        outputs = list(torch.abs(outputs).view(-1).detach().cpu().numpy())
        predictions.extend(outputs)
        labels.extend(list(torch.abs(targets).view(-1).detach().cpu().numpy()))

        vutils.save_image(torch.cat(
            [test_inputs0[:, :, :, :], test_inputs1[:, :, :, :]], dim=2),
                          '{0}/{1}-0.png'.format(opt.experiment, i),
                          pad_value=2)

    # print(labels)
    # print(predictions)
    evaluation.labels = labels
    predictions = np.array(predictions)
    # predictions = (predictions - np.min(predictions)) / (np.max(predictions) - np.min(predictions))
    evaluation.scores = predictions
    return evaluation.run()
示例#3
0
def test():
    ## dataset
    trans = T.Compose([T.Resize((opt.imageSize, opt.imageSize)), Style(2), T.ToTensor(), T.Normalize(0.5, 0.5)])
    test_dataset = load_dataset(opt.dataroot, opt.dataset, opt.imageSize, trans=trans, train=False)
    test_dataloader = DataLoader(test_dataset, batch_size=opt.batchSize, shuffle=False)
    opt.dataSize = test_dataset.__len__()

    ## model
    lenet = LeNet().to(device)
    assert lenet.load_state_dict(torch.load(opt.cnn_pth))
    print("Pretrained models have been loaded.")
    print([torch.max(i).item() for i in lenet.state_dict().values()])
    print([torch.min(i).item() for i in lenet.state_dict().values()])


    # loss
    lenet_criteria = nn.CrossEntropyLoss()
    lenet.eval()
    lenet_loss = []
    labels = []
    predictions = []
    evaluation = Evaluate(opt.experiment)
    tqdm_loader = tqdm.tqdm(test_dataloader)
    for i, (test_inputs, targets) in enumerate(tqdm_loader):
        tqdm_loader.set_description(f"Test Sample {i+1} / {opt.dataSize}")
        ## inference
        test_inputs, targets = test_inputs.to(device), targets.to(device)
        batchsize = test_inputs.size(0)
        with torch.no_grad():
            outputs = lenet(test_inputs)
        outputs = list(torch.softmax(outputs, 1)[:, 1].cpu().numpy())
        predictions.extend(outputs)
        labels.extend(list(torch.abs(targets).view(-1).detach().cpu().numpy()))


        vutils.save_image(test_inputs, '{0}/{1}-0.png'.format(opt.experiment, i), pad_value=2)

    print(labels)
    print(predictions)
    evaluation.labels = labels
    predictions = np.array(predictions)
    predictions = (predictions - np.min(predictions)) / (np.max(predictions) - np.min(predictions))
    evaluation.scores = predictions
    return evaluation.run()
示例#4
0
def test():
    ## dataset
    trans = T.Compose([
        T.Resize(opt.imageSize),
        Style(2),
        T.ToTensor(),
        T.Normalize(0.5, 0.5)
    ])
    test_dataset = load_dataset(opt.dataroot,
                                opt.dataset,
                                opt.imageSize,
                                trans=trans,
                                train=False)
    test_dataloader = DataLoader(test_dataset,
                                 batch_size=opt.batchSize,
                                 shuffle=False)
    opt.dataSize = test_dataset.__len__()

    ## model
    mlp = MLP(2 * opt.imageSize**2, 1).to(device)
    assert mlp.load_state_dict(torch.load(opt.mlp_pth))
    print("Pretrained models have been loaded.")
    print([torch.max(i).item() for i in mlp.state_dict().values()])
    print([torch.min(i).item() for i in mlp.state_dict().values()])

    # loss
    mlp.eval()
    mlp_loss = []
    labels = []
    predictions = []
    SSIM = []
    evaluation = Evaluate(opt.experiment)
    tqdm_loader = tqdm.tqdm(test_dataloader)
    for i, (test_inputs0, test_inputs1, targets) in enumerate(tqdm_loader):
        tqdm_loader.set_description(f"Test Sample {i+1} / {opt.dataSize}")
        ## inference
        batchsize = test_inputs0.size(0)
        test_inputs = torch.cat([
            test_inputs0.view(batchsize, -1),
            test_inputs1.view(batchsize, -1)
        ],
                                dim=1)
        test_inputs = test_inputs.to(device)
        targets = targets.to(device)
        with torch.no_grad():
            outputs = mlp(test_inputs)
        outputs = list(torch.abs(outputs).view(-1).detach().cpu().numpy())
        # outputs = [j if j<1 else 1 for j in outputs]
        predictions.extend(outputs)
        targets = list(torch.abs(targets).detach().cpu().numpy().flatten())
        labels.extend(targets)

        for k in range(batchsize):
            img1 = test_inputs0[k].detach().cpu().numpy().reshape(
                [opt.imageSize, opt.imageSize])
            img2 = test_inputs1[k].detach().cpu().numpy().reshape(
                [opt.imageSize, opt.imageSize])
            SSIM.append(ssim(img1, img2))

        vutils.save_image(torch.cat(
            [test_inputs0[:, :, :, :], test_inputs1[:, :, :, :]], dim=2),
                          '{0}/{1}-0.png'.format(opt.experiment, i),
                          pad_value=2)
        vutils.save_image(torch.cat(
            [test_inputs0[0:1, 0:1, :, :], test_inputs1[0:1, 0:1, :, :]],
            dim=2),
                          '{0}/{1}-example{2}.png'.format(
                              opt.experiment, i, targets[0].item()),
                          pad_value=2)

    # print(np.array(labels).reshape((-1, 8)))
    # print(np.array(predictions).reshape((-1, 8)))
    ssim_diff = np.sum(np.array(labels) * np.array(SSIM)) / len(
        np.where(np.array(labels) == 1)[0])
    ssim_iden = np.sum((1 - np.array(labels)) * np.array(SSIM)) / len(
        np.where(np.array(labels) == 0)[0])
    print("ssim_iden: ", ssim_iden)
    print("ssim_diff: ", ssim_diff)
    evaluation.labels = labels
    evaluation.scores = np.array(predictions)
    return evaluation.run()
示例#5
0
# torch.manual_seed(opt.seed)
# np.random.seed(opt.seed)

## cudnn
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True

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

## dataset
trans = T.Compose([T.Resize((opt.imageSize, opt.imageSize)), T.ToTensor()])
train_dataset = load_dataset(opt.dataroot,
                             opt.dataset,
                             opt.imageSize,
                             trans=trans,
                             train=True)
train_dataloader = DataLoader(train_dataset,
                              batch_size=opt.batchSize,
                              shuffle=True)
opt.dataSize = train_dataset.__len__()


## model
def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        m.weight.data.normal_(1.0, 0.02)
示例#6
0
                    help="size of image after scaled")
parser.add_argument("--imageSize",
                    type=int,
                    default=28,
                    help="size of each image dimension")
opt = parser.parse_args()
os.makedirs(opt.experiment, exist_ok=True)

##
trans = T.Compose(
    [T.Resize(opt.imageSize),
     T.ToTensor(),
     T.Normalize(0.5, 0.5)])
test_dataset = load_dataset(opt.dataroot,
                            opt.dataset,
                            opt.imageSize,
                            trans=trans,
                            train=False)
test_dataloader = DataLoader(test_dataset,
                             batch_size=opt.batchSize,
                             shuffle=False)
i, (test_inputs0, test_inputs1, targets) = next(enumerate(test_dataloader))
batchsize = test_inputs0.size(0)
test_inputs_raw = torch.cat(
    [test_inputs0.view(batchsize, -1),
     test_inputs1.view(batchsize, -1)],
    dim=1)
test_inputs_raw = test_inputs_raw.detach().numpy()
targets_raw = list(torch.abs(targets).detach().numpy())

##