Ejemplo n.º 1
0
def stest_one_versus_many(model, data_dir, img_size):
    """ """
    data_iterator = iter(
        DataLoader(
            PairDataset(
                data_dir,
                transform=transforms.Compose([
                    transforms.Grayscale(),
                    transforms.Resize(img_size),
                    transforms.ToTensor(),
                ]),
                split=SplitEnum.testing,
            ),
            num_workers=0,
            batch_size=1,
            shuffle=True,
        ))
    x0, *_ = next(data_iterator)
    for i in range(10):
        _, x1, _ = next(data_iterator)
        dis = (torch.pairwise_distance(*model(
            to_tensor(x0, device=global_torch_device()),
            to_tensor(x1, device=global_torch_device()),
        )).cpu().item())
        boxed_text_overlay_plot(
            torchvision.utils.make_grid(torch.cat((x0, x1), 0)),
            f"Dissimilarity: {dis:.2f}",
        )
Ejemplo n.º 2
0
def stest_many_versus_many(model, data_dir, img_size, threshold=0.5):
    """ """
    data_iterator = iter(
        DataLoader(
            PairDataset(
                data_dir,
                transform=transforms.Compose([
                    transforms.Grayscale(),
                    transforms.Resize(img_size),
                    transforms.ToTensor(),
                ]),
            ),
            num_workers=0,
            batch_size=1,
            shuffle=True,
        ))
    for i in range(10):
        x0, x1, is_diff = next(data_iterator)
        distance = (torch.pairwise_distance(*model(
            to_tensor(x0, device=global_torch_device()),
            to_tensor(x1, device=global_torch_device()),
        )).cpu().item())
        boxed_text_overlay_plot(
            torchvision.utils.make_grid(torch.cat((x0, x1), 0)),
            f"Truth: {'Different' if is_diff.cpu().item() else 'Alike'},"
            f" Dissimilarity: {distance:.2f},"
            f" Verdict: {'Different' if distance > threshold else 'Alike'}",
        )
Ejemplo n.º 3
0
def vis(model, data_dir, img_size):
    """ """
    # ## Visualising some of the data
    # The top row and the bottom row of any column is one pair. The 0s and 1s correspond to the column of the
    # image.
    # 0 indicates dissimilar, and 1 indicates similar.

    example_batch = next(
        iter(
            DataLoader(
                PairDataset(
                    data_path=data_dir,
                    transform=transforms.Compose([
                        transforms.Grayscale(),
                        transforms.Resize(img_size),
                        transforms.ToTensor(),
                    ]),
                    split=SplitEnum.validation,
                ),
                shuffle=True,
                num_workers=0,
                batch_size=8,
            )))
    concatenated = torch.cat((example_batch[0], example_batch[1]), 0)
    boxed_text_overlay_plot(torchvision.utils.make_grid(concatenated),
                            str(example_batch[2].numpy()))
Ejemplo n.º 4
0
def stest_many_versus_many2(model: Module,
                            data_dir: Path,
                            img_size: Tuple[int, int],
                            threshold=0.5):
    """

:param model:
:type model:
:param data_dir:
:type data_dir:
:param img_size:
:type img_size:
:param threshold:
:type threshold:
"""
    dataiter = iter(
        DataLoader(
            PairDataset(
                data_dir,
                transform=transforms.Compose([
                    transforms.Grayscale(),
                    transforms.Resize(img_size),
                    transforms.ToTensor(),
                ]),
            ),
            num_workers=4,
            batch_size=1,
            shuffle=True,
        ))
    for i in range(10):
        x0, x1, is_diff = next(dataiter)
        distance = (model(
            to_tensor(x0, device=global_torch_device()),
            to_tensor(x1, device=global_torch_device()),
        ).cpu().item())
        boxed_text_overlay_plot(
            torchvision.utils.make_grid(torch.cat((x0, x1), 0)),
            f"Truth: {'Different' if is_diff.cpu().item() else 'Alike'},"
            f" Dissimilarity: {distance:.2f},"
            f" Verdict: {'Different' if distance > threshold else 'Alike'}",
        )