Ejemplo n.º 1
0
    def __init__(self, filters=1, kernel_size=80, rank=1, strides=1, padding='valid',
                 data_format='channels_last', dilation_rate=1, activation=None, use_bias=True,
                 fsHz=1000.,
                 fc_initializer=initializers.RandomUniform(minval=10, maxval=400),
                 n_order_initializer=initializers.constant(4.),
                 amp_initializer=initializers.constant(10 ** 5),
                 beta_initializer=initializers.RandomNormal(mean=30, stddev=6),
                 bias_initializer='zeros',
                 **kwargs):
        super(Conv1D_gammatone, self).__init__(**kwargs)
        self.rank = rank
        self.filters = filters
        self.kernel_size_ = kernel_size
        self.kernel_size = conv_utils.normalize_tuple(kernel_size, rank, 'kernel_size')
        self.strides = conv_utils.normalize_tuple(strides, rank, 'strides')
        self.padding = conv_utils.normalize_padding(padding)
        self.data_format = conv_utils.normalize_data_format(data_format)
        self.dilation_rate = conv_utils.normalize_tuple(dilation_rate, rank, 'dilation_rate')
        self.activation = activations.get(activation)
        self.use_bias = use_bias
        self.bias_initializer = initializers.get(bias_initializer)
        self.fc_initializer = initializers.get(fc_initializer)
        self.n_order_initializer = initializers.get(n_order_initializer)
        self.amp_initializer = initializers.get(amp_initializer)
        self.beta_initializer = initializers.get(beta_initializer)
        self.input_spec = InputSpec(ndim=self.rank + 2)

        self.fsHz = fsHz
        self.t = tf.range(start=0, limit=kernel_size / float(fsHz),
                          delta=1 / float(fsHz), dtype=K.floatx())
        self.t = tf.expand_dims(input=self.t, axis=-1)
Ejemplo n.º 2
0
    def __init__(self,
                 step_dim,
                 get_alpha=False,
                 return_sequence=False,
                 W_regularizer=None,
                 b_regularizer=None,
                 L_regularizer=None,
                 W_constraint=None,
                 b_constraint=None,
                 L_constraint=None,
                 bias=False,
                 **kwargs):
        self.supports_masking = True
        self.init = initializers.get('glorot_uniform')
        self.l_init = initializers.constant(value=0.5)
        self.return_sequence = return_sequence
        self.W_regularizer = regularizers.get(W_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)
        self.L_regularizer = regularizers.get(L_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)
        self.L_constraint = constraints.get(L_constraint)
        self.bias = bias
        self.step_dim = step_dim
        self.get_alpha = get_alpha
        self.features_dim = 0
        super(ISA, self).__init__(**kwargs)
Ejemplo n.º 3
0
def discriminator_z_model(size_z, num_Dz_channels):
    kernel_initializer = initializers.random_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    inputs = Input(shape=(size_z, ))

    # fully connection layer
    current = inputs
    for i in range(len(num_Dz_channels)):
        name = 'D_z_fc' + str(i)
        current = Dense(units=num_Dz_channels[i],
                        kernel_initializer=kernel_initializer,
                        bias_initializer=bias_initializer,
                        name=name)(current)
        current = Lambda(lrelu, output_shape=(num_Dz_channels[i], ))(current)

    return Model(inputs=inputs, outputs=current)
Ejemplo n.º 4
0
def encoder_model(size_image, size_age_label, size_name_label,
                  size_gender_label, num_input_channels, size_kernel, size_z,
                  num_encoder_channels):
    # map the label of age + gender to size (size_image)

    kernel_initializer = initializers.truncated_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    # input of input images
    input_images = Input(shape=(size_image, size_image, num_input_channels))

    # input of age labels (use {function dupilicate_conv} to time the age_labels to match with images, then concatenate )
    input_ages_conv = Input(shape=(1, 1,
                                   size_age_label))  # (1, 1, 10*tile_ratio)
    input_ages_conv_repeat = Lambda(
        duplicate_conv,
        output_shape=(size_image, size_image, size_age_label),
        arguments={'times':
                   size_image})(input_ages_conv)  #(128, 128, 10*tile_ratio)

    input_names_conv = Input(shape=(1, 1, size_name_label))
    input_names_conv_repeat = Lambda(duplicate_conv,
                                     output_shape=(size_image, size_image,
                                                   size_name_label),
                                     arguments={'times':
                                                size_image})(input_names_conv)
    input_genders_conv = Input(shape=(1, 1, size_gender_label))
    input_genders_conv_repeat = Lambda(duplicate_conv,
                                       output_shape=(size_image, size_image,
                                                     size_gender_label),
                                       arguments={'times': size_image
                                                  })(input_genders_conv)

    current = Concatenate(axis=-1)([
        input_images, input_ages_conv_repeat, input_names_conv_repeat,
        input_genders_conv_repeat
    ])

    # E_conv layer + Batch Normalization
    num_layers = len(num_encoder_channels)

    for i in range(num_layers):
        name = 'E_conv' + str(i)
        current = Conv2D(filters=num_encoder_channels[i],
                         kernel_size=(size_kernel, size_kernel),
                         strides=(2, 2),
                         padding='same',
                         kernel_initializer=kernel_initializer,
                         bias_initializer=bias_initializer,
                         name=name)(current)
        size_image = int(size_image / 2)
        current = Lambda(lrelu,
                         output_shape=(size_image, size_image,
                                       int(current.shape[3])))(current)
        # current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_image, size_image, int(current.shape[3])),
        #                  arguments={'decay':0.9, 'epsilon': 1e-5, 'scale':True})(current)

    # reshape
    current = Flatten()(current)

    # fully connection layer
    kernel_initializer = initializers.random_normal(stddev=0.02)
    name = 'E_fc'
    current = Dense(units=size_z,
                    activation='tanh',
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer,
                    name=name)(current)

    # output
    return Model(inputs=[
        input_images, input_ages_conv, input_names_conv, input_genders_conv
    ],
                 outputs=current)
Ejemplo n.º 5
0
def generator_model():
    kernel_initializer = initializers.random_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    inputs_z = Input(shape=(100, ))
    # inputs_y = Input(shape=(10, ))
    # input_y_conv = Input(shape=(1, 1, 10))

    current = Dense(64 * 8 * 4 * 4,
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer)(inputs_z)
    current = Reshape(target_shape=(4, 4, 64 * 8))(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(4, 4, 64 * 8),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Activation(activation='relu')(current)

    current = Conv2DTranspose(filters=64 * 4,
                              kernel_size=(5, 5),
                              padding='same',
                              strides=(2, 2),
                              kernel_initializer=kernel_initializer,
                              bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(8, 8, 64*4),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(8, 8, 64 * 4),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Activation(activation='relu')(current)

    current = Conv2DTranspose(filters=64 * 2,
                              kernel_size=(5, 5),
                              padding='same',
                              strides=(2, 2),
                              kernel_initializer=kernel_initializer,
                              bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(16, 16, 64*2),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(16, 16, 64 * 2),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Activation(activation='relu')(current)

    current = Conv2DTranspose(filters=64 * 1,
                              kernel_size=(5, 5),
                              padding='same',
                              strides=(2, 2),
                              kernel_initializer=kernel_initializer,
                              bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(32, 32, 64*1),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(32, 32, 64 * 1),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Activation(activation='relu')(current)

    current = Conv2DTranspose(filters=3,
                              kernel_size=(5, 5),
                              padding='same',
                              strides=(2, 2),
                              kernel_initializer=kernel_initializer,
                              bias_initializer=bias_initializer)(current)
    current = Activation('tanh')(current)

    return Model(inputs=inputs_z, outputs=current)
Ejemplo n.º 6
0
def discriminator_model():
    kernel_initializer = initializers.truncated_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    inputs_img = Input(shape=(64, 64, 3))

    current = Conv2D(filters=64,
                     kernel_size=(5, 5),
                     padding='same',
                     strides=(2, 2),
                     kernel_initializer=kernel_initializer,
                     bias_initializer=bias_initializer)(inputs_img)
    current = Lambda(lrelu,
                     output_shape=(32, 32, int(current.shape[3])))(current)

    current = Conv2D(filters=64 * 2,
                     kernel_size=(5, 5),
                     padding='same',
                     strides=(2, 2),
                     kernel_initializer=kernel_initializer,
                     bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(16, 16, int(current.shape[3])),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(16, 16, int(current.shape[3])),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Lambda(lrelu,
                     output_shape=(16, 16, int(current.shape[3])))(current)

    current = Conv2D(filters=64 * 4,
                     kernel_size=(5, 5),
                     padding='same',
                     strides=(2, 2),
                     kernel_initializer=kernel_initializer,
                     bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(8, 8, int(current.shape[3])),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(8, 8, int(current.shape[3])),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Lambda(lrelu,
                     output_shape=(8, 8, int(current.shape[3])))(current)

    current = Conv2D(filters=64 * 8,
                     kernel_size=(5, 5),
                     padding='same',
                     strides=(2, 2),
                     kernel_initializer=kernel_initializer,
                     bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(4, 4, int(current.shape[3])),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(4, 4, int(current.shape[3])),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Lambda(lrelu,
                     output_shape=(4, 4, int(current.shape[3])))(current)

    kernel_initializer = initializers.random_normal(stddev=0.02)
    current = Reshape(target_shape=(4 * 4 * 512, ))(current)
    current = Dense(1,
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer)(current)
    current = Activation('sigmoid')(current)

    return Model(inputs=inputs_img, outputs=current)
Ejemplo n.º 7
0
def generator_model(size_z, size_age_label, size_name_label, size_gender_label,
                    size_mini_map, size_kernel, size_gen, num_input_channels,
                    num_gen_channels):

    kernel_initializer = initializers.random_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    #Input layer
    input_z = Input(shape=(size_z, ))
    input_age_label = Input(shape=(size_age_label, ))
    input_name_label = Input(shape=(size_name_label, ))
    input_gender_label = Input(shape=(size_gender_label, ))
    current = Concatenate(axis=-1)(
        [input_z, input_age_label, input_name_label, input_gender_label])

    # fc layer
    name = 'G_fc'
    current = Dense(units=size_mini_map * size_mini_map * size_gen,
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer,
                    name=name)(current)
    # Reshape
    current = Reshape(target_shape=(size_mini_map, size_mini_map,
                                    size_gen))(current)
    # BatchNormalization
    #current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_mini_map, size_mini_map, size_gen),
    #arguments={'decay': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    # Activation
    current = Activation(activation='relu')(current)

    # deconv layers with stride 2
    num_layers = len(num_gen_channels)
    size_image = size_mini_map
    for i in range(num_layers - 1):
        name = 'G_deconv' + str(i)
        current = Conv2DTranspose(filters=num_gen_channels[i],
                                  kernel_size=(size_kernel, size_kernel),
                                  padding='same',
                                  strides=(2, 2),
                                  kernel_initializer=kernel_initializer,
                                  bias_initializer=bias_initializer,
                                  name=name)(current)
        # size_image = size_image * 2
        # current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_image, size_image, int(current.shape[3])),
        #                  arguments={'decay':0.9, 'epsilon': 1e-5, 'scale':True})(current)
        current = Activation(activation='relu')(current)

    # final layer of generator---> activation: tanh
    name = 'G_deconv' + str(i + 1)
    current = Conv2DTranspose(filters=num_gen_channels[-1],
                              kernel_size=(size_kernel, size_kernel),
                              padding='same',
                              strides=(2, 2),
                              kernel_initializer=kernel_initializer,
                              bias_initializer=bias_initializer,
                              name=name)(current)
    current = Activation('tanh')(current)

    # output
    return Model(inputs=[
        input_z, input_age_label, input_name_label, input_gender_label
    ],
                 outputs=current)
Ejemplo n.º 8
0
def generator_pix2pix_model(size_image, size_age_label, size_name_label,
                            size_gender_label, size_kernel, num_input_channels,
                            num_encoder_channels, num_gen_channels, G_net):

    from ops import duplicate_conv, lrelu
    kernel_initializer = initializers.random_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    # Encoder Input layer
    input_images = Input(shape=(size_image, size_image, num_input_channels))
    input_ages_conv = Input(shape=(1, 1, size_age_label))
    input_names_conv = Input(shape=(1, 1, size_name_label))
    input_genders_conv = Input(shape=(1, 1, size_gender_label))
    input_ages_conv_repeat = Lambda(duplicate_conv,
                                    output_shape=(size_image, size_image,
                                                  size_age_label),
                                    arguments={'times':
                                               size_image})(input_ages_conv)
    input_names_conv_repeat = Lambda(duplicate_conv,
                                     output_shape=(size_image, size_image,
                                                   size_name_label),
                                     arguments={'times':
                                                size_image})(input_names_conv)
    input_genders_conv_repeat = Lambda(duplicate_conv,
                                       output_shape=(size_image, size_image,
                                                     size_gender_label),
                                       arguments={'times': size_image
                                                  })(input_genders_conv)

    current = Concatenate(axis=-1)([
        input_images, input_ages_conv_repeat, input_names_conv_repeat,
        input_genders_conv_repeat
    ])

    if G_net == 'Unet':
        # E_conv layer + lrelu + Batch Normalization
        res_list = []
        size_current = current.shape[1].value
        for i in range(len(num_encoder_channels)):
            name = 'E_conv' + str(i)
            kernel_size_change = max(size_kernel - i, 2)
            current = Conv2D(filters=num_encoder_channels[i],
                             kernel_size=(kernel_size_change,
                                          kernel_size_change),
                             strides=(2, 2),
                             padding='same',
                             kernel_initializer=kernel_initializer,
                             bias_initializer=bias_initializer,
                             name=name)(current)
            size_current = int(size_current / 2)
            current = Lambda(lrelu,
                             output_shape=(size_current, size_current,
                                           int(current.shape[3])))(current)
            current = Lambda(tf.contrib.layers.batch_norm,
                             output_shape=(size_current, size_current,
                                           int(current.shape[3])),
                             arguments={
                                 'decay': 0.9,
                                 'epsilon': 1e-5,
                                 'scale': True
                             })(current)
            res_list.append(current)

            # if size_current > 1:
            #     input_ages_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_age_label),
            #                                     arguments={'times': size_current})(input_ages_conv)
            #     input_names_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_name_label),
            #                                      arguments={'times': size_current})(input_names_conv)
            #     input_genders_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_gender_label),
            #                                        arguments={'times': size_current})(input_genders_conv)
            #     current = Concatenate(axis=-1)([current, input_ages_conv_repeat, input_names_conv_repeat, input_genders_conv_repeat])
            # else:
            #     current = Concatenate(axis=-1)([current, input_ages_conv, input_names_conv, input_genders_conv])

        # G_deconv layer + Batch Normalization + relu/tanh
        size_current = current.shape[1].value
        for i, (filter, dropout) in enumerate(num_gen_channels):

            # Residual Block ---------------> every E_conv layer has 3 mini layers(E_conv + lambda:lrelu + lambda:BN)
            if i > 0:
                current = Concatenate(axis=-1)([current, res_list[-1 - i]])

            name = 'G_deconv' + str(i)
            kernel_size_change = max(
                size_kernel - (len(num_gen_channels) - 1 - i), 2)
            current = Conv2DTranspose(filters=filter,
                                      kernel_size=(kernel_size_change,
                                                   kernel_size_change),
                                      padding='same',
                                      strides=(2, 2),
                                      kernel_initializer=kernel_initializer,
                                      bias_initializer=bias_initializer,
                                      name=name)(current)
            size_current = size_current * 2
            current = Lambda(tf.contrib.layers.batch_norm,
                             output_shape=(size_current, size_current,
                                           int(current.shape[3])),
                             arguments={
                                 'decay': 0.9,
                                 'epsilon': 1e-5,
                                 'scale': True
                             })(current)

            if dropout > 0.0:
                current = Dropout(rate=dropout)(current)

            if i == len(num_gen_channels) - 1:
                current = Activation(activation='tanh')(current)
            else:
                current = Activation(activation='relu')(current)

            # if size_current > 1 and size_current < size_image:
            #     input_ages_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_age_label),
            #                                     arguments={'times': size_current})(input_ages_conv)
            #     input_names_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_name_label),
            #                                      arguments={'times': size_current})(input_names_conv)
            #     input_genders_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_gender_label),
            #                                        arguments={'times': size_current})(input_genders_conv)
            #     current = Concatenate(axis=-1)([current, input_ages_conv_repeat, input_names_conv_repeat, input_genders_conv_repeat])
            # elif size_current == 1:
            #     current = Concatenate(axis=-1)([current, input_ages_conv, input_names_conv, input_genders_conv])

    elif G_net == 'Resnet':
        # E_conv layer + lrelu + Batch Normalization
        for i in range(len(num_encoder_channels)):
            name = 'E_conv' + str(i)
            kernel_size_change = max(size_kernel - i, 2)
            current = Conv2D(filters=num_encoder_channels[i],
                             kernel_size=(kernel_size_change,
                                          kernel_size_change),
                             strides=(2, 2),
                             padding='same',
                             kernel_initializer=kernel_initializer,
                             bias_initializer=bias_initializer,
                             name=name)(current)
            size_image = int(size_image / 2)
            current = Lambda(lrelu,
                             output_shape=(size_image, size_image,
                                           int(current.shape[3])))(current)
            current = Lambda(tf.contrib.layers.batch_norm,
                             output_shape=(size_image, size_image,
                                           int(current.shape[3])),
                             arguments={
                                 'decay': 0.9,
                                 'epsilon': 1e-5,
                                 'scale': True
                             })(current)

        # G_deconv layer + Batch Normalization + relu/tanh
        res_list = []
        size_current = current.shape[1].value
        for i, (filter, dropout) in enumerate(num_gen_channels):

            # Residual Block ---------------> every E_conv layer has 3 mini layers(E_conv + lambda:lrelu + lambda:BN)
            if i > 1 and i < len(num_gen_channels) - 1:
                # res_output = res_list[-1 - i](inputs=[input_images, input_ages_conv, input_names_conv, input_genders_conv])
                # current = Concatenate(axis=-1)([current, res_output])
                num_padding = int(2**(i - 1) / 2)
                res_block = ZeroPadding2D(padding=(num_padding, num_padding))(
                    res_list[i - 2])
                current = Concatenate(axis=-1)([current, res_block])

            name = 'G_deconv' + str(i)
            kernel_size_change = max(
                size_kernel - (len(num_gen_channels) - 1 - i), 2)
            current = Conv2DTranspose(filters=filter,
                                      kernel_size=(kernel_size_change,
                                                   kernel_size_change),
                                      padding='same',
                                      strides=(2, 2),
                                      kernel_initializer=kernel_initializer,
                                      bias_initializer=bias_initializer,
                                      name=name)(current)
            size_current = size_current * 2
            current = Lambda(tf.contrib.layers.batch_norm,
                             output_shape=(size_current, size_current,
                                           int(current.shape[3])),
                             arguments={
                                 'decay': 0.9,
                                 'epsilon': 1e-5,
                                 'scale': True
                             })(current)
            if dropout > 0.0:
                current = Dropout(rate=dropout)(current)

            if i == len(num_gen_channels) - 1:
                current = Activation(activation='tanh')(current)
            else:
                current = Activation(activation='relu')(current)

            res_list.append(current)

    # output
    return Model(inputs=[
        input_images, input_ages_conv, input_names_conv, input_genders_conv
    ],
                 outputs=current)
Ejemplo n.º 9
0
def discriminator_model():
    kernel_initializer = initializers.truncated_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    inputs_img = Input(shape=(28, 28, 1))
    inputs_y = Input(shape=(10, ))
    input_y_conv = Input(shape=(1, 1, 10))

    # current = Reshape((28, 28, 1))(inputs_img)
    inputs_y_repeat = Lambda(concatenate,
                             output_shape=(28, 28, 10),
                             arguments={'times': 28})(input_y_conv)
    current = Concatenate(axis=-1)([inputs_img, inputs_y_repeat])

    current = Conv2D(filters=1 + 10,
                     kernel_size=(5, 5),
                     padding='same',
                     strides=(2, 2),
                     kernel_initializer=kernel_initializer,
                     bias_initializer=bias_initializer)(current)
    current = Lambda(lrelu,
                     output_shape=(14, 14, int(current.shape[3])))(current)

    inputs_y_repeat = Lambda(concatenate,
                             output_shape=(14, 14, 10),
                             arguments={'times': 14})(input_y_conv)
    current = Concatenate(axis=-1)([current, inputs_y_repeat])

    current = Conv2D(filters=64 + 10,
                     kernel_size=(5, 5),
                     padding='same',
                     strides=(2, 2),
                     kernel_initializer=kernel_initializer,
                     bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(7, 7, int(current.shape[3])),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(7, 7, int(current.shape[3])),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Lambda(lrelu,
                     output_shape=(7, 7, int(current.shape[3])))(current)

    kernel_initializer = initializers.random_normal(stddev=0.02)

    current = Reshape(target_shape=(7 * 7 * 74, ))(current)
    current = Concatenate(axis=-1)([current, inputs_y])
    current = Dense(1024,
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(int(current.shape[1]),),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(int(current.shape[1]), ),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Lambda(lrelu, output_shape=(int(current.shape[1]), ))(current)

    current = Concatenate(axis=-1)([current, inputs_y])
    current = Dense(1,
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer)(current)
    current = Activation('sigmoid')(current)
    # conv1 = Conv2D(filters=64, kernel_size=(5, 5), padding="same")(x)
    # conv1 = LeakyReLU(alpha=0.2)(conv1)
    # Convolution2D is another name of Conv2D
    # conv1 = Convolution2D(filters=64, kernel_size=(5, 5), padding="same", activation="relu")(x)
    # max1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    # conv2 = Conv2D(filters=128, kernel_size=(5, 5), padding="same")(max1)
    # conv2 = LeakyReLU(alpha=0.2)(conv2)
    # conv2 = Convolution2D(filters=128, kernel_size=(5, 5), padding="same")(max1)
    # max2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    # flat = Flatten()(max2)
    # dense1 = Dense(units=1024, activation="relu")(flat)
    # # dense1 = LeakyReLU(alpha=0.2)(dense1)
    # dense2 = Dense(units=1, activation="sigmoid")(dense1)

    return Model(inputs=[inputs_img, inputs_y, input_y_conv], outputs=current)
Ejemplo n.º 10
0
def generator_model():
    kernel_initializer = initializers.random_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    inputs_img = Input(shape=(100, ))
    inputs_y = Input(shape=(10, ))
    input_y_conv = Input(shape=(1, 1, 10))

    # current = tf.concat([current, inputs_y], axis=-1)
    # current = Merge(mode='concat', concat_axis=-1)([inputs_img, inputs_y])
    current = Concatenate(axis=-1)([inputs_img, inputs_y])
    current = Dense(1024,
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization,output_shape=(int(current.shape[1]),),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(int(current.shape[1]), ),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    # current = tf.layers.batch_normalization(inputs=current)
    current = Activation(activation='relu')(current)

    current = Concatenate(axis=-1)([current, inputs_y])
    current = Dense(128 * 7 * 7,
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(int(current.shape[1]),),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(int(current.shape[1]), ),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Activation(activation='relu')(current)

    current = Reshape(target_shape=(7, 7, 128))(current)
    inputs_y_repeat = Lambda(concatenate,
                             output_shape=(7, 7, 10),
                             arguments={'times': 7})(input_y_conv)
    current = Concatenate(axis=-1)([current, inputs_y_repeat])

    current = Conv2DTranspose(filters=64,
                              kernel_size=(5, 5),
                              padding='same',
                              strides=(2, 2),
                              kernel_initializer=kernel_initializer,
                              bias_initializer=bias_initializer)(current)
    # current = Lambda(tf.layers.batch_normalization, output_shape=(14, 14, int(current.shape[3])),
    #                  arguments={'momentum': 0.9, 'epsilon': 1e-5, 'scale': True})(current)
    current = Lambda(tf.contrib.layers.batch_norm,
                     output_shape=(14, 14, int(current.shape[3])),
                     arguments={
                         'decay': 0.9,
                         'epsilon': 1e-5,
                         'scale': True
                     })(current)
    current = Activation(activation='relu')(current)

    current = Conv2DTranspose(filters=1,
                              kernel_size=(5, 5),
                              padding='same',
                              strides=(2, 2),
                              kernel_initializer=kernel_initializer,
                              bias_initializer=bias_initializer)(current)
    current = Activation('sigmoid')(current)

    return Model(inputs=[inputs_img, inputs_y, input_y_conv], outputs=current)
Ejemplo n.º 11
0
def discriminator_img_model(size_image, size_kernel, size_age_label,
                            size_name_label, size_gender_label,
                            num_input_channels, num_Dimg_channels,
                            num_Dimg_fc_channels):

    kernel_initializer = initializers.truncated_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    # Dimg model
    input_images = Input(shape=(size_image, size_image, num_input_channels))
    # the label of age + gender
    input_ages_conv = Input(shape=(1, 1,
                                   size_age_label))  # (1, 1, 10*tile_ratio)
    input_ages_conv_repeat = Lambda(
        duplicate_conv,
        output_shape=(size_image, size_image, size_age_label),
        arguments={'times':
                   size_image})(input_ages_conv)  #(128, 128, 10*tile_ratio)

    input_names_conv = Input(shape=(1, 1, size_name_label))
    input_names_conv_repeat = Lambda(duplicate_conv,
                                     output_shape=(size_image, size_image,
                                                   size_name_label),
                                     arguments={'times':
                                                size_image})(input_names_conv)
    input_genders_conv = Input(shape=(1, 1, size_gender_label))
    input_genders_conv_repeat = Lambda(duplicate_conv,
                                       output_shape=(size_image, size_image,
                                                     size_gender_label),
                                       arguments={'times': size_image
                                                  })(input_genders_conv)

    # concatenate
    current = Concatenate(axis=-1)([
        input_images, input_ages_conv_repeat, input_names_conv_repeat,
        input_genders_conv_repeat
    ])

    num_layers = len(num_Dimg_channels)

    # name = 'D_img_conv0'
    # current = Conv2D(
    #     filters=num_Dimg_channels[0],
    #     kernel_size=(size_kernel, size_kernel),
    #     strides=(2, 2),
    #     padding='same',
    #     kernel_initializer=kernel_initializer,
    #     bias_initializer=bias_initializer,
    #     name=name)(current)
    # size_image = int(size_image / 2)
    # current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_image, size_image, int(current.shape[3])),
    #                  arguments={'decay':0.9, 'epsilon': 1e-5, 'scale':True})(current)
    # current = Lambda(lrelu, output_shape=(size_image, size_image, int(current.shape[3])))(current)

    # conv layers with stride 2
    for i in range(num_layers):
        name = 'D_img_conv' + str(i)
        current = Conv2D(filters=num_Dimg_channels[i],
                         kernel_size=(size_kernel, size_kernel),
                         strides=(2, 2),
                         padding='same',
                         kernel_initializer=kernel_initializer,
                         bias_initializer=bias_initializer,
                         name=name)(current)
        size_image = int(size_image / 2)

        # current = Lambda(tf.contrib.layers.batch_norm, output_shape=(size_image, size_image, int(current.shape[3])),
        #                  arguments={'decay':0.9, 'epsilon': 1e-5, 'scale':True})(current)
        current = Lambda(lrelu,
                         output_shape=(size_image, size_image,
                                       int(current.shape[3])))(current)

    # current = Flatten()(current)
    current = Reshape(target_shape=(size_image * size_image *
                                    int(current.shape[3]), ))(current)

    # fully connection layer
    kernel_initializer = initializers.random_normal(stddev=0.02)
    name = 'D_img_fc1'
    current = Dense(units=num_Dimg_fc_channels,
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer,
                    name=name)(current)
    current = Lambda(lrelu, output_shape=(num_Dimg_fc_channels, ))(current)

    name = 'D_img_fc2'
    current = Dense(units=1,
                    activation='sigmoid',
                    kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer,
                    name=name)(current)

    # output = Activation('sigmoid')(current)

    # output
    return Model(inputs=[
        input_images, input_ages_conv, input_names_conv, input_genders_conv
    ],
                 outputs=current)
Ejemplo n.º 12
0
def discriminator_img_model(size_image, size_kernel, size_age_label,
                            size_name_label, size_gender_label,
                            num_input_channels, num_Dimg_channels,
                            num_Dimg_fc_channels, GANs):

    kernel_initializer = initializers.truncated_normal(stddev=0.02)
    bias_initializer = initializers.constant(value=0.0)

    # Dimg model
    input_images = Input(shape=(size_image, size_image, num_input_channels))
    # the label of age + gender
    input_ages_conv = Input(shape=(1, 1,
                                   size_age_label))  # (1, 1, 10*tile_ratio)
    input_names_conv = Input(shape=(1, 1, size_name_label))
    input_genders_conv = Input(shape=(1, 1, size_gender_label))
    input_ages_conv_repeat = Lambda(
        duplicate_conv,
        output_shape=(size_image, size_image, size_age_label),
        arguments={'times':
                   size_image})(input_ages_conv)  #(128, 128, 10*tile_ratio)
    input_names_conv_repeat = Lambda(duplicate_conv,
                                     output_shape=(size_image, size_image,
                                                   size_name_label),
                                     arguments={'times':
                                                size_image})(input_names_conv)
    input_genders_conv_repeat = Lambda(duplicate_conv,
                                       output_shape=(size_image, size_image,
                                                     size_gender_label),
                                       arguments={'times': size_image
                                                  })(input_genders_conv)
    # concatenate
    current = Concatenate(axis=-1)([
        input_images, input_ages_conv_repeat, input_names_conv_repeat,
        input_genders_conv_repeat
    ])

    num_layers = len(num_Dimg_channels)
    size_current = size_image
    # conv layers with stride 2
    for i in range(num_layers):
        # if i == 0:
        #     strides = 1
        # else:
        #     strides = 2
        #     size_image = int(size_image / 2)
        #
        # name = 'D_img_conv' + str(i)
        # current = Conv2D(
        #     filters=num_Dimg_channels[i],
        #     kernel_size=(size_kernel, size_kernel),
        #     strides=(strides, strides),
        #     padding='same',
        #     kernel_initializer=kernel_initializer,
        #     bias_initializer=bias_initializer,
        #     name=name)(current)

        name = 'D_img_conv' + str(i) + str('_') + str(1)
        current = Conv2D(filters=num_Dimg_channels[i],
                         kernel_size=(size_kernel, size_kernel),
                         strides=(1, 1),
                         padding='same',
                         kernel_initializer=kernel_initializer,
                         bias_initializer=bias_initializer,
                         name=name)(current)
        if i != 0:
            current = Lambda(tf.contrib.layers.batch_norm,
                             output_shape=(size_image, size_image,
                                           int(current.shape[3])),
                             arguments={
                                 'decay': 0.9,
                                 'epsilon': 1e-5,
                                 'scale': True
                             })(current)
        current = Lambda(lrelu,
                         output_shape=(size_image, size_image,
                                       int(current.shape[3])))(current)

        size_current = int(size_current / 2)
        name = 'D_img_conv' + str(i) + str('_') + str(2)
        current = Conv2D(filters=num_Dimg_channels[i],
                         kernel_size=(size_kernel, size_kernel),
                         strides=(2, 2),
                         padding='same',
                         kernel_initializer=kernel_initializer,
                         bias_initializer=bias_initializer,
                         name=name)(current)
        if i != 0:
            current = Lambda(tf.contrib.layers.batch_norm,
                             output_shape=(size_current, size_current,
                                           int(current.shape[3])),
                             arguments={
                                 'decay': 0.9,
                                 'epsilon': 1e-5,
                                 'scale': True
                             })(current)
        current = Lambda(lrelu,
                         output_shape=(size_current, size_current,
                                       int(current.shape[3])))(current)
        # if i < num_layers - 1:
        #     input_ages_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_age_label),
        #                                     arguments={'times': size_current})(input_ages_conv)  # (128, 128, 10*tile_ratio)
        #     input_names_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_name_label),
        #                                      arguments={'times': size_current})(input_names_conv)
        #     input_genders_conv_repeat = Lambda(duplicate_conv, output_shape=(size_current, size_current, size_gender_label),
        #                                        arguments={'times': size_current})(input_genders_conv)
        #     current = Concatenate(axis=-1)([current, input_ages_conv_repeat, input_names_conv_repeat, input_genders_conv_repeat])

    # Patch GAN_D
    # name = 'D_img_conv_final'
    # if GANs == 'LSGAN':
    #     current = Conv2D(
    #         filters=1,
    #         kernel_size=(size_kernel, size_kernel),
    #         strides=(1, 1),
    #         padding='same',
    #         kernel_initializer=kernel_initializer,
    #         bias_initializer=bias_initializer,
    #         name=name)(current)
    # elif GANs == 'cGAN':
    #     current = Conv2D(
    #         filters=1,
    #         kernel_size=(size_kernel, size_kernel),
    #         strides=(1, 1),
    #         padding='same',
    #         kernel_initializer=kernel_initializer,
    #         bias_initializer=bias_initializer,
    #         activation='sigmoid',
    #         name=name)(current)

    name = 'D_img_conv_final_1'
    current = Flatten()(current)
    current = Dense(units=num_Dimg_fc_channels, name=name)(current)
    current = Lambda(lrelu, output_shape=(num_Dimg_fc_channels, ))(current)
    name = 'D_img_conv_final_2'
    if GANs == 'LSGAN':
        current = Dense(units=1, name=name)(current)
    elif GANs == 'cGAN':
        current = Dense(units=1, activation='sigmoid', name=name)(current)

    # output
    return Model(inputs=[
        input_images, input_ages_conv, input_names_conv, input_genders_conv
    ],
                 outputs=current)