Beispiel #1
0
def test_optimiser_output(tmpdir, simple_net):
    """Save image per iteration of the optimiser."""
    model, objective, image = setup_optimiser(simple_net)
    n_iterations = 10

    opt = sumie.Optimiser(image, model, objective)
    opt.run(iterations=10, output=Path(tmpdir))

    search = str(tmpdir.join('*.jpg'))
    assert len(glob(search)) == 11
Beispiel #2
0
def test_optimiser_output_str(tmpdir, simple_net):
    """Save image to new folder of type string."""
    model, objective, image = setup_optimiser(simple_net)
    n_iterations = 10
    new_folder = 'tmp'

    opt = sumie.Optimiser(image, model, objective)
    opt.run(iterations=10, output=new_folder)

    search = new_folder + '/*.jpg'
    assert len(glob(search)) == 11
Beispiel #3
0
def test_optimiser(simple_net):
    """Optimiser class optimises an image given an objective."""
    model, objective, image = setup_optimiser(simple_net)
    original_image, start_value = get_state(model, image, objective)

    opt = sumie.Optimiser(image, model, objective)
    opt.run(iterations=10)

    new_image, end_value = get_state(model, image, objective)
    assert torch.any(new_image != original_image)
    assert end_value > start_value
Beispiel #4
0
def test_optimiser_history(simple_net):
    """Optimiser stores objective history."""
    model, objective, image = setup_optimiser(simple_net)
    n_iterations = 10

    opt = sumie.Optimiser(image, model, objective)
    opt.run(iterations=10)
    new_image, end_value = get_state(model, image, objective)

    # +1 as we get the initial and final losses
    assert len(opt.history) == n_iterations + 1
    assert opt.history[-1] == end_value
Beispiel #5
0
def run(target, selected_module, average=True):
    device = 'cuda'

    im = sumie.Image(imsize, param='fft', transforms=[
                        sumie.transforms.PositionJitter(8),
                        sumie.transforms.ScaleJitter(1.01),
                        sumie.transforms.RotationJitter(0.1),
                        sumie.transforms.PositionJitter(8),
                        sumie.transforms.Interpolate(0.1),
                        sumie.transforms.Normalise(),
                  ])

    im.to(device)

    content = Direction(selected_module, target.detach())

    opt = sumie.Optimiser(im, model, content)
    opt.add_callback(change_scale)
    opt.run(iterations=512, lr=0.05, progress=True)
    return im.get_image()
Beispiel #6
0
    all_target = monitor.values

    values = all_target.detach().squeeze(0).cpu().numpy()
    values = values.reshape((values.shape[0], -1)).T
    n_clusters = 5
    kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(values)
    print(kmeans.cluster_centers_.shape)

    for i in range(n_clusters):
        cluster = torch.Tensor(kmeans.cluster_centers_[i, :])
        cluster = cluster[None, :, None, None]
        im = sumie.Image((1000, 300),
                         param='fft',
                         transforms=[
                             sumie.transforms.PositionJitter(8),
                             sumie.transforms.ScaleJitter(1.01),
                             sumie.transforms.RotationJitter(0.1),
                             sumie.transforms.PositionJitter(8),
                             sumie.transforms.Interpolate(0.1),
                             sumie.transforms.Normalise(),
                         ])

        im.to(device)

        content = Direction(selected_module, cluster.to(device))

        opt = sumie.Optimiser(im, model, content)
        opt.add_callback(change_scale)
        opt.run(iterations=512, lr=0.05, progress=True)
        sumie.io.save(im.get_image(), f'tmp/output_{i:03}.png')