Example #1
0
    def test_custom_image_param_set_param(self):
        """
        checks if custom_image_param.set_param correctly 
        loads the image without discrepancies with an absolute tolerance of 1e-5 element-wise
        """
        model = models.inception_v3(pretrained=True)

        dreamy_boi = dreamer(model=model, device='cpu', quiet=False)
        param = custom_image_param(image='images/sample_small.jpg',
                                   device='cpu')

        image_param = dreamy_boi.render(image_parameter=param,
                                        layers=[model.Mixed_6a],
                                        iters=5,
                                        lr=2e-4,
                                        grad_clip=0.1,
                                        weight_decay=1e-1)

        image_tensor = image_param.to_nchw_tensor()

        image_param.set_param(tensor=image_tensor)
        # print(torch.abs((image_tensor - image_param.to_nchw_tensor())).mean())

        self.assertTrue(
            torch.allclose(image_tensor,
                           image_param.to_nchw_tensor(),
                           atol=1e-5))
Example #2
0
    def test_custom_image_param(self):

        model = models.inception_v3(pretrained=True)

        dreamy_boi = dreamer(model=model, device='cpu', quiet=False)
        param = custom_image_param(image='images/sample_small.jpg',
                                   device='cpu')

        image_param = dreamy_boi.render(image_parameter=param,
                                        layers=[model.Mixed_6a],
                                        iters=5,
                                        lr=2e-4,
                                        grad_clip=0.1,
                                        weight_decay=1e-1)

        image_param.save(filename='test_custom_image_param.jpg')

        self.assertTrue(os.path.exists('test_custom_image_param.jpg'))
        self.assertTrue(isinstance(image_param, custom_image_param),
                        'should be an instance of auto_image_param')
        self.assertTrue(isinstance(image_param.__array__(), np.ndarray))
        self.assertTrue(isinstance(image_param.to_hwc_tensor(), torch.Tensor),
                        'should be a torch.Tensor')
        self.assertTrue(isinstance(image_param.to_chw_tensor(), torch.Tensor),
                        'should be a torch.Tensor')
        os.remove('test_custom_image_param.jpg')
Example #3
0
def run(config, img, title=None):
    if not dreamy_boi or not model:
        setup_torch_dreams()

    # get config
    assert 'objective' in config, 'No objective to optimize in config'
    c = EasyDict(config)
    width, height = c.size if 'size' in c else (256, 256)
    iters = c.iters if 'iters' in c else 150
    lr = c.lr if 'lr' in c else 9e-3
    rotate_degrees = c.rotate_degrees if 'rotate_degrees' in c else 15
    scale_max = c.scale_max if 'scale_max' in c else 1.2
    scale_min = c.scale_min if 'scale_min' in c else 0.5
    translate = c.translate if 'translate' in c else (0.2, 0.2)
    weight_decay = c.weight_decay if 'weight_decay' in c else 1e-2
    grad_clip = c.grad_clip if 'grad_clip' in c else 1
    layer = get_layer(c.objective['layer'])
    channel = c.objective['channel']

    # set target
    layers_to_use = [layer]
    my_custom_func = make_custom_func(layer_number=0, channel_number=channel)

    if img is None:
        input_param = auto_image_param(height=height,
                                       width=width,
                                       device='cuda',
                                       standard_deviation=0.01)
    else:
        if isinstance(img, str):
            img = image.load_image(img)
        img = image.resize(img, (width, height))
        img = torch.tensor(np.array(img) / 255).permute(-1, 0, 1).unsqueeze(0)
        input_param = custom_image_param(image=img, device='cuda')

    # run torch_dreams
    output_param = dreamy_boi.render(image_parameter=input_param,
                                     layers=layers_to_use,
                                     custom_func=my_custom_func,
                                     width=width,
                                     height=height,
                                     iters=iters,
                                     lr=lr,
                                     rotate_degrees=rotate_degrees,
                                     scale_max=scale_max,
                                     scale_min=scale_min,
                                     translate_x=translate[0],
                                     translate_y=translate[1],
                                     weight_decay=weight_decay,
                                     grad_clip=grad_clip)

    output_image = (255 * np.clip(np.array(output_param), 0, 1)).astype(
        np.uint8)
    return output_image
Example #4
0
    def test_caricature(self):

        model = models.resnet18(pretrained=True)

        dreamy_boi = dreamer(model, device='cpu')
        param = custom_image_param(image='images/sample_small.jpg',
                                   device='cpu')

        image_tensor = param.to_nchw_tensor()

        param = dreamy_boi.caricature(input_tensor=image_tensor,
                                      layers=[model.layer3],
                                      power=1.0,
                                      iters=5)

        self.assertTrue(isinstance(param, auto_image_param),
                        'should be an auto_image_param')