Ejemplo n.º 1
0
    def test_set_regional_image_or_guide(self, method, desired_attr):
        regions = [str(idx) for idx in range(3)]
        torch.manual_seed(0)
        regional_images_or_guides = [
            (region, torch.rand(1, 3, 128, 128)) for region in regions
        ]

        multi_region_operator = ops.MultiRegionOperator(regions, self.get_op)

        for region, image_or_guide in regional_images_or_guides:
            getattr(multi_region_operator, method)(region, image_or_guide)

        for region, image_or_guide in regional_images_or_guides:
            actual = getattr(getattr(multi_region_operator, region), desired_attr)
            desired = image_or_guide
            ptu.assert_allclose(actual, desired)
Ejemplo n.º 2
0
 def get_guided_perceptual_loss():
     content_loss = ops.FeatureReconstructionOperator(
         enc.SequentialEncoder((nn.Conv2d(1, 1, 1), )))
     style_loss = ops.MultiRegionOperator(regions, get_op)
     return loss.GuidedPerceptualLoss(content_loss, style_loss)
Ejemplo n.º 3
0
 def get_multi_region_operator():
     return ops.MultiRegionOperator(regions, get_op)
# definition honors the position of the content during the optimization. Thus, the
# previously defined ``content_loss`` is combined with the new regional ``style_loss``
# in a :class:`~pystiche.loss.GuidedPerceptualLoss` as optimization ``criterion``.


def get_region_op(region, region_weight):
    return ops.MultiLayerEncodingOperator(
        multi_layer_encoder,
        style_layers,
        get_style_op,
        score_weight=region_weight,
    )


style_loss = ops.MultiRegionOperator(regions,
                                     get_region_op,
                                     score_weight=style_weight)

criterion = loss.GuidedPerceptualLoss(content_loss, style_loss).to(device)
print(criterion)

########################################################################################
# The ``content_loss`` is unguided and thus the content image can be set as we did
# before. For the ``style_loss`` we use the same ``style_image`` for all regions and
# only vary the guides.

criterion.set_content_image(content_image)

for region in regions:
    criterion.set_style_guide(region, style_guides[region])
    criterion.set_style_image(region, style_image)