Пример #1
0
    def decoder(self, side):
        """ Decoder Network """
        kwargs = dict(kernel_initializer=self.kernel_initializer)
        decoder_shape = self.input_shape[0] // 8
        input_ = Input(shape=(decoder_shape, decoder_shape, 512))

        var_x = input_
        var_x = UpscaleBlock(512, activation=None, **kwargs)(var_x)
        var_x = LeakyReLU(alpha=0.2)(var_x)
        var_x = ResidualBlock(512, **kwargs)(var_x)
        var_x = UpscaleBlock(256, activation=None, **kwargs)(var_x)
        var_x = LeakyReLU(alpha=0.2)(var_x)
        var_x = ResidualBlock(256, **kwargs)(var_x)
        var_x = UpscaleBlock(self.input_shape[0], activation=None, **kwargs)(var_x)
        var_x = LeakyReLU(alpha=0.2)(var_x)
        var_x = ResidualBlock(self.input_shape[0], **kwargs)(var_x)
        var_x = Conv2DOutput(3, 5, name=f"face_out_{side}")(var_x)
        outputs = [var_x]

        if self.config.get("learn_mask", False):
            var_y = input_
            var_y = UpscaleBlock(512, activation="leakyrelu")(var_y)
            var_y = UpscaleBlock(256, activation="leakyrelu")(var_y)
            var_y = UpscaleBlock(self.input_shape[0], activation="leakyrelu")(var_y)
            var_y = Conv2DOutput(1, 5, name=f"mask_out_{side}")(var_y)
            outputs.append(var_y)
        return KerasModel(input_, outputs=outputs, name=f"decoder_{side}")
Пример #2
0
    def decoder(self, side):
        """ Decoder Network """
        kwargs = dict(kernel_initializer=self.kernel_initializer)
        decoder_shape = self.input_shape[0] // 8
        input_ = Input(shape=(decoder_shape, decoder_shape, 512))

        var_x = input_
        var_x = UpscaleBlock(512, res_block_follows=True, **kwargs)(var_x)
        var_x = ResidualBlock(512, **kwargs)(var_x)
        var_x = UpscaleBlock(256, res_block_follows=True, **kwargs)(var_x)
        var_x = ResidualBlock(256, **kwargs)(var_x)
        var_x = UpscaleBlock(self.input_shape[0],
                             res_block_follows=True,
                             **kwargs)(var_x)
        var_x = ResidualBlock(self.input_shape[0], **kwargs)(var_x)
        var_x = Conv2DOutput(3, 5, name="face_out_{}".format(side))(var_x)
        outputs = [var_x]

        if self.config.get("learn_mask", False):
            var_y = input_
            var_y = UpscaleBlock(512)(var_y)
            var_y = UpscaleBlock(256)(var_y)
            var_y = UpscaleBlock(self.input_shape[0])(var_y)
            var_y = Conv2DOutput(1, 5, name="mask_out_{}".format(side))(var_y)
            outputs.append(var_y)
        return KerasModel(input_,
                          outputs=outputs,
                          name="decoder_{}".format(side))
Пример #3
0
    def decoder(self, side):
        """ Decoder Network """
        input_ = Input(shape=(8, 8, 512))
        var_x = input_

        if self._output_size == 256:
            var_x = UpscaleBlock(1024, res_block_follows=True)(var_x)
            var_x = ResidualBlock(
                1024, kernel_initializer=self.kernel_initializer)(var_x)
        var_x = UpscaleBlock(512, res_block_follows=True)(var_x)
        var_x = ResidualBlock(
            512, kernel_initializer=self.kernel_initializer)(var_x)
        var_x = UpscaleBlock(256, res_block_follows=True)(var_x)
        var_x = ResidualBlock(
            256, kernel_initializer=self.kernel_initializer)(var_x)
        var_x = UpscaleBlock(128, res_block_follows=True)(var_x)
        var_x = ResidualBlock(
            128, kernel_initializer=self.kernel_initializer)(var_x)
        var_x = UpscaleBlock(64)(var_x)
        var_x = Conv2DOutput(3, 5, name="face_out_{}".format(side))(var_x)
        outputs = [var_x]

        if self.config.get("learn_mask", False):
            var_y = input_
            if self._output_size == 256:
                var_y = UpscaleBlock(1024)(var_y)
            var_y = UpscaleBlock(512)(var_y)
            var_y = UpscaleBlock(256)(var_y)
            var_y = UpscaleBlock(128)(var_y)
            var_y = UpscaleBlock(64)(var_y)
            var_y = Conv2DOutput(1, 5, name="mask_out_{}".format(side))(var_y)
            outputs.append(var_y)
        return KerasModel([input_],
                          outputs=outputs,
                          name="decoder_{}".format(side))
Пример #4
0
    def decoder(self, side):
        """ Decoder Network """
        input_ = Input(shape=(8, 8, 512))
        var_x = input_

        if self._output_size == 256:
            var_x = UpscaleBlock(1024, activation=None)(var_x)
            var_x = LeakyReLU(alpha=0.2)(var_x)
            var_x = ResidualBlock(1024, kernel_initializer=self.kernel_initializer)(var_x)
        var_x = UpscaleBlock(512, activation=None)(var_x)
        var_x = LeakyReLU(alpha=0.2)(var_x)
        var_x = ResidualBlock(512, kernel_initializer=self.kernel_initializer)(var_x)
        var_x = UpscaleBlock(256, activation=None)(var_x)
        var_x = LeakyReLU(alpha=0.2)(var_x)
        var_x = ResidualBlock(256, kernel_initializer=self.kernel_initializer)(var_x)
        var_x = UpscaleBlock(128, activation=None)(var_x)
        var_x = LeakyReLU(alpha=0.2)(var_x)
        var_x = ResidualBlock(128, kernel_initializer=self.kernel_initializer)(var_x)
        var_x = UpscaleBlock(64, activation="leakyrelu")(var_x)
        var_x = Conv2DOutput(3, 5, name=f"face_out_{side}")(var_x)
        outputs = [var_x]

        if self.config.get("learn_mask", False):
            var_y = input_
            if self._output_size == 256:
                var_y = UpscaleBlock(1024, activation="leakyrelu")(var_y)
            var_y = UpscaleBlock(512, activation="leakyrelu")(var_y)
            var_y = UpscaleBlock(256, activation="leakyrelu")(var_y)
            var_y = UpscaleBlock(128, activation="leakyrelu")(var_y)
            var_y = UpscaleBlock(64, activation="leakyrelu")(var_y)
            var_y = Conv2DOutput(1, 5, name=f"mask_out_{side}")(var_y)
            outputs.append(var_y)
        return KerasModel([input_], outputs=outputs, name=f"decoder_{side}")
Пример #5
0
    def decoder_b(self):
        """ RealFace Decoder Network """
        input_filters = self.config["complexity_encoder"] * 2**(
            self.downscalers_no - 1)
        input_width = self.config["input_size"] // self._downscale_ratio
        input_ = Input(shape=(input_width, input_width, input_filters))

        var_xy = input_

        var_xy = Dense(self.config["dense_nodes"])(Flatten()(var_xy))
        var_xy = Dense(self.dense_width * self.dense_width *
                       self.dense_filters)(var_xy)
        var_xy = Reshape(
            (self.dense_width, self.dense_width, self.dense_filters))(var_xy)
        var_xy = UpscaleBlock(self.dense_filters, activation=None)(var_xy)

        var_x = var_xy
        var_x = LeakyReLU(alpha=0.2)(var_x)
        var_x = ResidualBlock(self.dense_filters, use_bias=False)(var_x)

        decoder_b_complexity = self.config["complexity_decoder"]
        for idx in range(self.upscalers_no - 2):
            var_x = UpscaleBlock(decoder_b_complexity // 2**idx,
                                 activation=None)(var_x)
            var_x = LeakyReLU(alpha=0.2)(var_x)
            var_x = ResidualBlock(decoder_b_complexity // 2**idx,
                                  use_bias=False)(var_x)
            var_x = ResidualBlock(decoder_b_complexity // 2**idx,
                                  use_bias=True)(var_x)
        var_x = UpscaleBlock(decoder_b_complexity // 2**(idx + 1),
                             activation="leakyrelu")(var_x)

        var_x = Conv2DOutput(3, 5, name="face_out_b")(var_x)

        outputs = [var_x]

        if self.config.get("learn_mask", False):
            var_y = var_xy
            var_y = LeakyReLU(alpha=0.1)(var_y)

            mask_b_complexity = 384
            for idx in range(self.upscalers_no - 2):
                var_y = UpscaleBlock(mask_b_complexity // 2**idx,
                                     activation="leakyrelu")(var_y)
            var_y = UpscaleBlock(mask_b_complexity // 2**(idx + 1),
                                 activation="leakyrelu")(var_y)

            var_y = Conv2DOutput(1, 5, name="mask_out_b")(var_y)

            outputs += [var_y]

        return KerasModel(input_, outputs=outputs, name="decoder_b")
Пример #6
0
    def decoder_b(self):
        """ Decoder for side B """
        kwargs = dict(kernel_size=5,
                      kernel_initializer=self.kernel_initializer)
        dense_dim = 384 if self.low_mem else self.config["complexity_decoder_b"]
        decoder_complexity = 384 if self.low_mem else 512
        decoder_shape = self.input_shape[0] // 16
        input_ = Input(shape=(decoder_shape, decoder_shape, dense_dim))

        var_x = input_
        if self.low_mem:
            var_x = UpscaleBlock(decoder_complexity, **kwargs)(var_x)
            var_x = UpscaleBlock(decoder_complexity // 2, **kwargs)(var_x)
            var_x = UpscaleBlock(decoder_complexity // 4, **kwargs)(var_x)
            var_x = UpscaleBlock(decoder_complexity // 8, **kwargs)(var_x)
        else:
            var_x = UpscaleBlock(decoder_complexity,
                                 res_block_follows=True,
                                 **kwargs)(var_x)
            var_x = ResidualBlock(
                decoder_complexity,
                kernel_initializer=self.kernel_initializer)(var_x)
            var_x = UpscaleBlock(decoder_complexity,
                                 res_block_follows=True,
                                 **kwargs)(var_x)
            var_x = ResidualBlock(
                decoder_complexity,
                kernel_initializer=self.kernel_initializer)(var_x)
            var_x = UpscaleBlock(decoder_complexity // 2,
                                 res_block_follows=True,
                                 **kwargs)(var_x)
            var_x = ResidualBlock(
                decoder_complexity // 2,
                kernel_initializer=self.kernel_initializer)(var_x)
            var_x = UpscaleBlock(decoder_complexity // 4, **kwargs)(var_x)
        var_x = Conv2DOutput(3, 5, name="face_out_b")(var_x)
        outputs = [var_x]

        if self.config.get("learn_mask", False):
            var_y = input_
            var_y = UpscaleBlock(decoder_complexity)(var_y)
            if not self.low_mem:
                var_y = UpscaleBlock(decoder_complexity)(var_y)
            var_y = UpscaleBlock(decoder_complexity // 2)(var_y)
            var_y = UpscaleBlock(decoder_complexity // 4)(var_y)
            if self.low_mem:
                var_y = UpscaleBlock(decoder_complexity // 8)(var_y)
            var_y = Conv2DOutput(1, 5, name="mask_out_b")(var_y)
            outputs.append(var_y)
        return KerasModel(input_, outputs=outputs, name="decoder_b")
Пример #7
0
    def encoder(self):
        """ Encoder Network """
        kwargs = dict(kernel_initializer=self.kernel_initializer)
        input_ = Input(shape=self.input_shape)
        in_conv_filters = self.input_shape[0]
        if self.input_shape[0] > 128:
            in_conv_filters = 128 + (self.input_shape[0] - 128) // 4
        dense_shape = self.input_shape[0] // 16

        var_x = Conv2DBlock(in_conv_filters, activation=None, **kwargs)(input_)
        tmp_x = var_x

        var_x = LeakyReLU(alpha=0.2)(var_x)
        res_cycles = 8 if self.config.get("lowmem", False) else 16
        for _ in range(res_cycles):
            nn_x = ResidualBlock(in_conv_filters, **kwargs)(var_x)
            var_x = nn_x
        # consider adding scale before this layer to scale the residual chain
        tmp_x = LeakyReLU(alpha=0.1)(tmp_x)
        var_x = add([var_x, tmp_x])
        var_x = Conv2DBlock(128, activation="leakyrelu", **kwargs)(var_x)
        var_x = PixelShuffler()(var_x)
        var_x = Conv2DBlock(128, activation="leakyrelu", **kwargs)(var_x)
        var_x = PixelShuffler()(var_x)
        var_x = Conv2DBlock(128, activation="leakyrelu", **kwargs)(var_x)
        var_x = SeparableConv2DBlock(256, **kwargs)(var_x)
        var_x = Conv2DBlock(512, activation="leakyrelu", **kwargs)(var_x)
        if not self.config.get("lowmem", False):
            var_x = SeparableConv2DBlock(1024, **kwargs)(var_x)

        var_x = Dense(self.encoder_dim, **kwargs)(Flatten()(var_x))
        var_x = Dense(dense_shape * dense_shape * 1024, **kwargs)(var_x)
        var_x = Reshape((dense_shape, dense_shape, 1024))(var_x)
        var_x = UpscaleBlock(512, activation="leakyrelu", **kwargs)(var_x)
        return KerasModel(input_, var_x, name="encoder")
Пример #8
0
    def encoder(self):
        """ RealFace Encoder Network """
        input_ = Input(shape=self.input_shape)
        var_x = input_

        encoder_complexity = self.config["complexity_encoder"]

        for idx in range(self.downscalers_no - 1):
            var_x = Conv2DBlock(encoder_complexity * 2**idx, activation=None)(var_x)
            var_x = LeakyReLU(alpha=0.2)(var_x)
            var_x = ResidualBlock(encoder_complexity * 2**idx, use_bias=True)(var_x)
            var_x = ResidualBlock(encoder_complexity * 2**idx, use_bias=True)(var_x)

        var_x = Conv2DBlock(encoder_complexity * 2**(idx + 1), activation="leakyrelu")(var_x)

        return KerasModel(input_, var_x, name="encoder")
Пример #9
0
    def decoder_a(self):
        """ RealFace Decoder (A) Network """
        input_filters = self.config["complexity_encoder"] * 2**(
            self.downscalers_no - 1)
        input_width = self.config["input_size"] // self._downscale_ratio
        input_ = Input(shape=(input_width, input_width, input_filters))

        var_xy = input_

        dense_nodes = int(self.config["dense_nodes"] / 1.5)
        dense_filters = int(self.dense_filters / 1.5)

        var_xy = Dense(dense_nodes)(Flatten()(var_xy))
        var_xy = Dense(self.dense_width * self.dense_width *
                       dense_filters)(var_xy)
        var_xy = Reshape(
            (self.dense_width, self.dense_width, dense_filters))(var_xy)

        var_xy = UpscaleBlock(dense_filters)(var_xy)

        var_x = var_xy
        var_x = ResidualBlock(dense_filters, use_bias=False)(var_x)

        decoder_a_complexity = int(self.config["complexity_decoder"] / 1.5)
        for idx in range(self.upscalers_no - 2):
            var_x = UpscaleBlock(decoder_a_complexity // 2**idx)(var_x)
        var_x = UpscaleBlock(decoder_a_complexity // 2**(idx + 1))(var_x)

        var_x = Conv2DOutput(3, 5, name="face_out_a")(var_x)

        outputs = [var_x]

        if self.config.get("learn_mask", False):
            var_y = var_xy
            mask_a_complexity = 384
            for idx in range(self.upscalers_no - 2):
                var_y = UpscaleBlock(mask_a_complexity // 2**idx)(var_y)
            var_y = UpscaleBlock(mask_a_complexity // 2**(idx + 1))(var_y)

            var_y = Conv2DOutput(1, 5, name="mask_out_a")(var_y)

            outputs += [var_y]

        return KerasModel(input_, outputs=outputs, name="decoder_a")
Пример #10
0
    def decoder_b(self):
        """ DeLight Decoder B(new face) Network  """
        input_ = Input(shape=(4, 4, 1024))

        decoder_b_complexity = 512
        mask_complexity = 128

        var_xy = input_

        var_xy = Upscale2xBlock(512,
                                scale_factor=self.upscale_ratio,
                                fast=False)(var_xy)

        var_x = var_xy

        var_x = ResidualBlock(512, use_bias=True)(var_x)
        var_x = ResidualBlock(512, use_bias=False)(var_x)
        var_x = ResidualBlock(512, use_bias=False)(var_x)
        var_x = Upscale2xBlock(decoder_b_complexity, fast=False)(var_x)
        var_x = ResidualBlock(decoder_b_complexity, use_bias=True)(var_x)
        var_x = ResidualBlock(decoder_b_complexity, use_bias=False)(var_x)
        var_x = BatchNormalization()(var_x)
        var_x = Upscale2xBlock(decoder_b_complexity // 2, fast=False)(var_x)
        var_x = ResidualBlock(decoder_b_complexity // 2, use_bias=True)(var_x)
        var_x = Upscale2xBlock(decoder_b_complexity // 4, fast=False)(var_x)
        var_x = ResidualBlock(decoder_b_complexity // 4, use_bias=False)(var_x)
        var_x = BatchNormalization()(var_x)
        var_x = Upscale2xBlock(decoder_b_complexity // 8, fast=False)(var_x)

        var_x = Conv2DOutput(3, 5, name="face_out")(var_x)

        outputs = [var_x]

        if self.config.get("learn_mask", False):
            var_y = var_xy  # mask decoder

            var_y = Upscale2xBlock(mask_complexity, fast=False)(var_y)
            var_y = Upscale2xBlock(mask_complexity // 2, fast=False)(var_y)
            var_y = Upscale2xBlock(mask_complexity // 4, fast=False)(var_y)
            var_y = Upscale2xBlock(mask_complexity // 8, fast=False)(var_y)

            var_y = Conv2DOutput(1, 5, name="mask_out")(var_y)

            outputs.append(var_y)

        return KerasModel([input_], outputs=outputs, name="decoder_b")
Пример #11
0
    def _upscale_block(self,
                       inputs,
                       filters,
                       skip_residual=False,
                       is_mask=False):
        """ Upscale block for Phaze-A Decoder.

        Uses requested upscale method, adds requested regularization and activation function.

        Parameters
        ----------
        inputs: tensor
            The input tensor for the upscale block
        filters: int
            The number of filters to use for the upscale
        skip_residual: bool, optional
            ``True`` if a residual block should not be placed in the upscale block, otherwise
            ``False``. Default ``False``
        is_mask: bool, optional
            ``True`` if the input is a mask. ``False`` if the input is a face. Default: ``False``

        Returns
        -------
        tensor
            The output tensor from the upscale block
        """
        upscaler = _get_upscale_layer(
            self._config["dec_upscale_method"].lower(), filters)

        var_x = upscaler(inputs)
        if not is_mask and self._config["dec_gaussian"]:
            var_x = GaussianNoise(1.0)(var_x)
        if not is_mask and self._config["dec_res_blocks"] and not skip_residual:
            var_x = self._normalization(var_x)
            var_x = LeakyReLU(alpha=0.2)(var_x)
            for _ in range(self._config["dec_res_blocks"]):
                var_x = ResidualBlock(filters)(var_x)
        else:
            var_x = self._normalization(var_x)
            var_x = LeakyReLU(alpha=0.1)(var_x)
        return var_x
Пример #12
0
    def decoder(self, side, input_shape):
        """ DFL SAE Decoder Network"""
        input_ = Input(shape=input_shape)
        outputs = []

        dims = self.input_shape[-1] * self.decoder_dim
        var_x = input_

        var_x1 = UpscaleBlock(dims * 8, activation=None)(var_x)
        var_x1 = LeakyReLU(alpha=0.2)(var_x1)
        var_x1 = ResidualBlock(dims * 8)(var_x1)
        var_x1 = ResidualBlock(dims * 8)(var_x1)
        if self.multiscale_count >= 3:
            outputs.append(
                Conv2DOutput(3, 5, name="face_out_32_{}".format(side))(var_x1))

        var_x2 = UpscaleBlock(dims * 4, activation=None)(var_x1)
        var_x2 = LeakyReLU(alpha=0.2)(var_x2)
        var_x2 = ResidualBlock(dims * 4)(var_x2)
        var_x2 = ResidualBlock(dims * 4)(var_x2)
        if self.multiscale_count >= 2:
            outputs.append(
                Conv2DOutput(3, 5, name="face_out_64_{}".format(side))(var_x2))

        var_x3 = UpscaleBlock(dims * 2, activation=None)(var_x2)
        var_x3 = LeakyReLU(alpha=0.2)(var_x3)
        var_x3 = ResidualBlock(dims * 2)(var_x3)
        var_x3 = ResidualBlock(dims * 2)(var_x3)

        outputs.append(
            Conv2DOutput(3, 5, name="face_out_128_{}".format(side))(var_x3))

        if self.use_mask:
            var_y = input_
            var_y = UpscaleBlock(self.decoder_dim * 8,
                                 activation="leakyrelu")(var_y)
            var_y = UpscaleBlock(self.decoder_dim * 4,
                                 activation="leakyrelu")(var_y)
            var_y = UpscaleBlock(self.decoder_dim * 2,
                                 activation="leakyrelu")(var_y)
            var_y = Conv2DOutput(1, 5, name="mask_out_{}".format(side))(var_y)
            outputs.append(var_y)
        return KerasModel(input_,
                          outputs=outputs,
                          name="decoder_{}".format(side))