Пример #1
0
    def scale_by(self, f):
        image = to_image(self.image).scale_by(f)
        sem_style = to_image(self.semantic_style_image).scale_by(
            f, resample=NEAREST)
        sem_cont = to_image(self.semantic_content_image).scale_by(
            f, resample=NEAREST)

        return SemanticStyle(image=self.image,
                             layer_ids=self.layer_ids,
                             layer_weights=self.layer_weights,
                             lambda_loss=self.lambda_loss,
                             k=self.k,
                             s=self.s,
                             semantic_style_image=sem_style,
                             semantic_content_image=sem_cont,
                             gamma=self.gamma,
                             gamma_scale=self.gamma_scale)
Пример #2
0
    def generate(self,
                 style,
                 content=None,
                 seed=None,
                 lambda_tv=5e-5,
                 lr=1e-2,
                 niter=200,
                 yield_every=0,
                 disable_progress=False,
                 plugins=None,
                 alpha=1.0):
        '''Generate an image by minimizing style and content losses.
        
        Params
        ------
        style : style.losses.LossProvider
            A method that provides style losses. Currently one can choose
            between global GramStyle, PatchStyle and SemanticStyle.
        
        Kwargs
        ------
        content : style.losses.LossProvider
            Provides content loss. If None does not compute a content loss.
        seed : image
            Initial values of image to be generated. If None, initializes with
            a white noise image of size equal to content. If content is not available,
            defaults to white noise of size (256,256,3)
        lambda_tv : scalar
            Strengthness of total variation regularization used to avoid noise artefacts
            in generated image.
        lr : scalar
            Learning rate
        niter : number
            Number of iterations
        yield_every : number
            Yields intermediate results every so often.
        disable_progress : boolean
            Disables progress messages.
        plugins : list, None
            A optional list of optimization plugins to be invoked during various steps of optimization.
        '''

        content = content or Content(layer_id=8)
        plugins = plugins or []

        x = self._get_or_create_seed(content, style, seed)

        opt = optim.Adam([x], lr=lr)
        scheduler = sched.ReduceLROnPlateau(opt,
                                            'min',
                                            threshold=1e-3,
                                            patience=20,
                                            cooldown=50,
                                            min_lr=1e-4)

        net = self.backbone.trimmed_net(
            max(content.layer_ids + style.layer_ids))

        with content.create_loss(net,
                                 self.backbone.dev) as cl, style.create_loss(
                                     net, self.backbone.dev) as sl:

            [plugin.prepare(cl, sl, x, niter=niter) for plugin in plugins]

            losses = None
            with tqdm(total=niter, disable=disable_progress) as t:
                for idx in range(niter):

                    opt.zero_grad()

                    net(x)
                    closs = cl() * content.lambda_loss
                    sloss = sl() * style.lambda_loss
                    tvloss = priors.tv_prior(x) * lambda_tv
                    loss = closs + alpha * (sloss + tvloss)

                    for plugin in plugins:
                        loss = plugin.after_loss(x, loss)

                    loss.backward()

                    [plugin.after_backward(x) for plugin in plugins]

                    opt.step()

                    [plugin.after_step(x) for plugin in plugins]

                    losses = np.array((loss.item(), closs.item(), sloss.item(),
                                       tvloss.item()))
                    t.set_postfix(loss=np.array_str(losses, precision=3),
                                  lr=self._max_lr(opt))
                    t.update()

                    scheduler.step(loss)

                    # Projected gradient descent
                    x.data.clamp_(0, 1)

                    if yield_every > 0 and idx % yield_every == 0:
                        yield image.to_image(x)

        yield image.to_image(x)
Пример #3
0
    def scale_by(self, f):
        image = None
        if self.image is not None:
            image = to_image(self.image).scale_by(f)

        return Content(image, self.layer_ids[0], self.lambda_loss)
Пример #4
0
 def scale_by(self, f):
     image = to_image(self.image).scale_by(f)
     return PatchStyle(image, self.layer_ids, self.layer_weights,
                       self.lambda_loss, self.k, self.s)