示例#1
0
    def construct_DQN(self):
        input_layer = tfkl.Input(shape=(self.observation_size *
                                        self.num_frames, ))
        layer1 = tfkl.Dense(self.observation_size *
                            self.num_frames)(input_layer)
        layer1 = tfka.relu(layer1, alpha=0.01)  #Leaky ReLU

        layer2 = tfkl.Dense(self.observation_size * 2)(layer1)
        layer2 = tfka.relu(layer2, alpha=0.01)

        layer3 = tfkl.Dense(self.observation_size)(layer2)
        layer3 = tfka.relu(layer3, alpha=0.01)

        layer4 = tfkl.Dense(self.action_size * 3)(layer3)
        layer4 = tfka.relu(layer4, alpha=0.01)

        layer5 = tfkl.Dense(self.action_size * 2)(layer4)
        layer5 = tfka.relu(layer5, alpha=0.01)

        output = tfkl.Dense(self.action_size)(layer5)

        self.model = tfk.Model(inputs=[input_layer], outputs=[output])

        self.schedule = tfko.schedules.InverseTimeDecay(
            self.lr, self.lr_decay_steps, self.lr_decay_rate)

        self.optimizer = tfko.Adam(learning_rate=self.schedule)

        self.target_model = tfk.Model(inputs=[input_layer], outputs=[output])
        self.target_model.set_weights(self.model.get_weights())
        return
示例#2
0
    def construct_q_network(self):
        input_layer = tfk.Input(shape = (self.observation_size * self.num_frames,), name="input_obs")
        lay1 = tfkl.Dense(self.observation_size * 2, name="fc_1")(input_layer)
        lay1 = tfka.relu(lay1, alpha=0.01) #leaky_relu

        lay2 = tfkl.Dense(self.observation_size, name="fc_2")(lay1)
        lay2 = tfka.relu(lay2, alpha=0.01) #leaky_relu

        lay3 = tfkl.Dense(self.action_size * 3, name="fc_3")(lay2)
        lay3 = tfka.relu(lay3, alpha=0.01) #leaky_relu

        advantage = tfkl.Dense(self.action_size * 2, name="fc_adv")(lay3)
        advantage = tfkl.Dense(self.action_size, name="adv")(advantage)

        value = tfkl.Dense(self.action_size * 2, name="fc_val")(lay3)
        value = tfkl.Dense(1, name="val")(value)

        advantage_mean = tf.math.reduce_mean(advantage, axis=1, keepdims=True, name="adv_mean")
        advantage = tfkl.subtract([advantage, advantage_mean], name="adv_subtract")
        Q = tf.math.add(value, advantage, name="Qout")

        self.model = tfk.Model(inputs=[input_layer], outputs=[Q],
                               name=self.__class__.__name__)

        # Backwards pass
        self.schedule = tfko.schedules.InverseTimeDecay(self.lr, self.lr_decay_steps, self.lr_decay_rate)
        self.optimizer = tfko.Adam(learning_rate=self.schedule)
示例#3
0
def build_encoder_cnn_16_32_32_norm_pool(frame):
    
    inputs = Input(shape=frame.input_shape, name='encoder_input')
    x = inputs

    x = Conv2D(filters=16, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

    x = Conv2D(filters=32, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

    x = Conv2D(filters=32, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

    frame.shape = K.int_shape(x)
    x = Flatten()(x)
    latent = Dense(frame.latent_dim, name='latent_vector')(x)

    frame.encoder = Model(inputs, latent, name='encoder')
    frame.inputs = inputs
示例#4
0
def residual_block(in_shape, out_dim, kernel=5, reg=0.01):
    """Construct a residual block using convolutions and skip connections
    
    Arguments:
    in_shape    -- shape of the input tensor
    out_dim     -- size of the channel dimension in the output (i.e. number of Conv1D filters)
    kernel      -- size of the kernel in the Conv1D layers (default 5)
    reg         -- regularization parameter (default 0.01)
    
    """

    in_dim = in_shape[-1]
    inpt = Input(in_shape)
    skip = inpt
    
    # If the number of output channels is different than input channels, we need to transform the 
    # input dim to the same size so that they can be added together in the skip connection. 
    if in_dim != out_dim:
        skip = Conv1D(out_dim, activation=None, kernel_size=1, strides=1, use_bias=False, 
                      kernel_regularizer=tf.keras.regularizers.l2(l=reg))(skip)
    x = BatchNormalization()(inpt)
    x = relu(x)
    x = Conv1D(out_dim, activation=None, kernel_size=kernel, padding='same', use_bias=False, 
               kernel_regularizer=tf.keras.regularizers.l2(l=reg))(x)
    x = BatchNormalization()(x)
    x = relu(x)
    x = Conv1D(out_dim, activation=None, kernel_size=kernel, padding='same', 
               kernel_regularizer=tf.keras.regularizers.l2(l=reg))(x)
    x = x + skip

    model = Model(inputs=inpt, outputs=x)
    return model
示例#5
0
    def call(self, inputs):
        # layer 1 with image input
        c_1 = self.conv1(inputs['image'])
        c_1 = BatchNormalization()(c_1)
        c_1 = relu(c_1)

        # layer 2
        c_2 = self.conv2(c_1)
        c_2 = BatchNormalization()(c_2)
        c_2 = relu(c_2)

        # layer 3
        c_3 = self.conv3(c_2)
        c_3 = BatchNormalization()(c_3)
        c_3 = relu(c_3)
        c_3 = Flatten()(c_3)

        # connect layer 3 output to d4_image layer
        d_4_image = self.dense4_image(c_3)
        d_4_image = BatchNormalization()(d_4_image)

        # connect state input to d4_image layer
        d_4_state = self.dense4_state(inputs['robot_state'])
        d_4_state = BatchNormalization()(d_4_state)

        # add the outputs of two layers and activate
        d_4 = tf.add(d_4_image, d_4_state)
        d_4 = relu(d_4)

        return d_4
示例#6
0
    def construct_q_network(self):
        input_layer = tfk.Input(shape=(self.observation_size *
                                       self.num_frames, ))
        lay1 = tfkl.Dense(self.observation_size * self.num_frames)(input_layer)

        lay2 = tfkl.Dense(512)(lay1)
        lay2 = tfka.relu(lay2, alpha=0.01)  #leaky_relu

        lay3 = tfkl.Dense(256)(lay2)
        lay3 = tfka.relu(lay3, alpha=0.01)  #leaky_relu

        lay4 = tfkl.Dense(128)(lay3)
        lay4 = tfka.relu(lay4, alpha=0.01)  #leaky_relu

        advantage = tfkl.Dense(64)(lay4)
        advantage = tfka.relu(advantage, alpha=0.01)  #leaky_relu
        advantage = tfkl.Dense(self.action_size)(advantage)

        value = tfkl.Dense(64)(lay4)
        value = tfka.relu(value, alpha=0.01)  #leaky_relu
        value = tfkl.Dense(1)(value)

        advantage_mean = tf.math.reduce_mean(advantage, axis=1, keepdims=True)
        advantage = tfkl.subtract([advantage, advantage_mean])
        Q = tf.math.add(value, advantage)

        self.model = tfk.Model(inputs=[input_layer], outputs=[Q])
        self.model.compile(loss='mse', optimizer=tfko.Adam(lr=self.lr))
def inverted_res_block(inputs, expansion, stride, alpha, filters, block_id, skip_connection, rate=1):
    in_channels = inputs.shape[-1] 
    pointwise_conv_filters = int(filters * alpha)
    pointwise_filters = _make_divisible(pointwise_conv_filters, 8)
    x = inputs
    prefix = 'expanded_conv_{}_'.format(block_id)
    if block_id:
        # Expand
        x = Conv2D(expansion * in_channels, kernel_size=1, padding='same', use_bias=False, activation=None, name=prefix + 'expand')(x)
        x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'expand_BN')(x)
        #x = Activation(tf.nn.relu6, name=prefix + 'expand_relu')(x)
        x = Lambda(lambda x: relu(x, max_value=6.), name=prefix + 'expand_relu')(x)
    else:
        prefix = 'expanded_conv_'
    # Depthwise
    x = DepthwiseConv2D(kernel_size=3, strides=stride, activation=None,
                        use_bias=False, padding='same', dilation_rate=(rate, rate), name=prefix + 'depthwise')(x)
    x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'depthwise_BN')(x)

    #x = Activation(tf.nn.relu6, name=prefix + 'depthwise_relu')(x)
    x = Lambda(lambda x: relu(x, max_value=6.), name=prefix + 'depthwise_relu')(x)

    # Project
    x = Conv2D(pointwise_filters, kernel_size=1, padding='same', use_bias=False, activation=None, name=prefix + 'project')(x)
    x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'project_BN')(x)

    if skip_connection:
        return Add(name=prefix + 'add')([inputs, x])

    return x
示例#8
0
def build_encoder_cnn_16_32_64_512(frame):
    '''
    scored 0.24 RMSE with descent loss history for reconstruction autoencoder
    '''

    inputs = Input(shape=frame.input_shape, name='encoder_input')
    x = inputs

    x = Conv2D(filters=16, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    #x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

    x = Conv2D(filters=32, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    #x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

    x = Conv2D(filters=64, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

    
    frame.shape = K.int_shape(x)
    x = Flatten()(x)
    latent = Dense(frame.latent_dim, name='latent_vector')(x)

    frame.encoder = Model(inputs, latent, name='encoder')
    frame.inputs = inputs
示例#9
0
    def call(self, inputs, condition):
        """Computes a forward pass of the decoder

    Args:
      inputs (tf.Tensor): input data, shape=(batch size, sample length, 1)
      condition (tf.Tensor): conditioning data from encoder,
        should be shape (batch size, encoding length, encoding channels)
        This tensor will be upsampled so that the second dimensions (lengths)
        between the input and conditions match
    """
        c = self.upsample(inputs, condition)

        resid = self.causal_conv(inputs)
        skip = self.skip_conv(resid)

        for wavenet_layer in self.dilated_layers:
            resid, s = wavenet_layer(resid, c)
            skip += s

        skip = relu(skip)
        skip = self.final_conv(skip)
        skip = skip + self.condition_conv(c)
        skip = relu(skip)
        out = self.class_conv(skip)

        return out
示例#10
0
def build_decoder_cnn_16_32_32_norm_pool(frame):

    latent_inputs = Input(shape=(frame.latent_dim,), name='decoder_input')
    x = Dense(frame.shape[1] * frame.shape[2] * frame.shape[3])(latent_inputs)
    x = Reshape((frame.shape[1], frame.shape[2], frame.shape[3]))(x)


    x = Conv2DTranspose(filters=32, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = UpSampling2D((2, 2))(x)

    x = Conv2DTranspose(filters=32, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = UpSampling2D((2, 2))(x)

    x = Conv2DTranspose(filters=16, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = UpSampling2D((2, 2))(x)

    x = Conv2DTranspose(filters=3, kernel_size=frame.kernel_size, padding='same')(x)

    outputs = Activation('sigmoid', name='decoder_output')(x)

    frame.decoder = Model(latent_inputs, outputs, name='decoder')
示例#11
0
def build_encoder_cnn_16_32_16(frame):
    '''best autoencoder performance for RMSE or Binary

    '''
    inputs = Input(shape=frame.input_shape, name='encoder_input')
    x = inputs

    x = Conv2D(filters=16, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

    x = Conv2D(filters=32, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

    x = Conv2D(filters=16, kernel_size=frame.kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization(trainable=False)(x)
    x = relu(x)
    x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

    
    # Shape info needed to build Decoder Model
    frame.shape = K.int_shape(x)
    # Generate the latent vector
    x = Flatten()(x)
    latent = Dense(frame.latent_dim, name='latent_vector')(x)

    # Instantiate Encoder Model
    frame.encoder = Model(inputs, latent, name='encoder')
    frame.inputs = inputs
示例#12
0
def identity_block(input_tensor, kernel_size, filters, stage, block,
                   width_increment):
    """The identity block is the block that has no conv layer at shortcut.
    This block has grouped convolution implementation.
    # Arguments
        input_tensor: input tensor
        kernel_size: default 3, the kernel size of
            middle conv layer at main path
        filters: list of integers, the filters of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names
    # Returns
        Output tensor for the block.
    """
    filters1, filters2, filters3 = filters
    if backend.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    grouped_convolution = []
    for i in range(CARDINALITY):
        x = layers.BatchNormalization(axis=bn_axis,
                                      name=bn_name_base + '2a' +
                                      f'_{i}')(input_tensor)
        x = activations.relu(x)
        x = layers.Conv2D(int(round(filters1 / CARDINALITY)) * 2, (1, 1),
                          kernel_initializer='he_normal',
                          name=conv_name_base + '2a' + f'_{i}')(x)

        x = layers.BatchNormalization(axis=bn_axis,
                                      name=bn_name_base + '2b' + f'_{i}')(x)
        x = activations.relu(x)
        x = layers.Conv2D(int(round(filters1 / CARDINALITY)) * 2,
                          kernel_size,
                          padding='same',
                          kernel_initializer='he_normal',
                          name=conv_name_base + '2b' + f'_{i}')(x)

        grouped_convolution.append(x)
    x = layers.concatenate(grouped_convolution, axis=-1)

    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)
    x = activations.relu(x)
    x = layers.Conv2D(filters3, (1, 1),
                      kernel_initializer='he_normal',
                      name=conv_name_base + '2c')(x)

    # Split Input tensor to its residual and dense components
    residual_input = layers.Lambda(lambda x: x[:, :, :, :filters3])(
        input_tensor)
    dense_input = layers.Lambda(lambda x: x[:, :, :, filters3:])(input_tensor)
    # Extract the grwoth increment from the last convolution
    dense_increment = layers.Lambda(lambda x: x[:, :, :, :width_increment])(x)

    x = layers.add([residual_input, x])
    x = layers.concatenate([x, dense_input, dense_increment], axis=bn_axis)
    return x
示例#13
0
  def call(self, inputs):
    x = inputs
    x = relu(x)
    x = self.dilated_conv(x)
    x = relu(x) # default will be using the ReLU activation, but can change if necessary
    x = self.conv_1x1(x)

    return inputs + x
    def call(self, x, training=False):

        inputs = x
        x = relu(self.bn1(self.conv1(x), training=training))
        x = self.bn2(self.conv2(x), training=training)
        x = x + inputs  #: skip connection
        x = relu(x)

        return x
def get_xception():
    """Returns a Xception pretrained neural net.

        The function returns a partially pretrained Xception neural net.

        Returns:
            A modified Xception semi-pre-trained NN instance
    """

    model = Xception(include_top=False)

    model.trainable = False

    core_output = model.layers[45].output

    # Weighting Xception output via channel and spatial Attention

    channel_attention_map = channel_attention(core_output)
    channel_weighted = core_output * channel_attention_map
    spatial_attention_map = spatial_attention(channel_weighted)
    core_output = channel_weighted * spatial_attention_map

    for _ in range(5):

        output = relu(core_output)
        output = SeparableConvolution2D(728, (3, 3),
                                        padding='same',
                                        depthwise_regularizer=L2(0.2),
                                        pointwise_regularizer=L2(0.03))(output)
        output = BatchNormalization()(output)
        output = Dropout(0.3)(output)

        output = relu(output)
        output = SeparableConvolution2D(728, (3, 3),
                                        padding='same',
                                        depthwise_regularizer=L2(0.2),
                                        pointwise_regularizer=L2(0.03))(output)
        output = BatchNormalization()(output)
        output = Dropout(0.3)(output)

        core_output = Add()[output, core_output]

        # Output Weighting via Attention

        channel_attention_map = channel_attention(core_output)
        channel_weighted = core_output * channel_attention_map
        spatial_attention_map = spatial_attention(channel_weighted)
        core_output = channel_weighted * spatial_attention_map

    model_output = GlobalAvgPool2D()(core_output)

    model_output = Dense(1, activation='sigmoid')(model_output)

    model = Model(inputs=model.input, outputs=model_output)

    return model
 def call(self, inputs, training=False):
     """ Implements the feed forward part of the network """
     x = inputs
     for layer in self.layers_[:-1]:
         x = layer(x)
         x = relu(x, alpha=self.leaky_alpha)
         x = self.dropout(x, training)
     x = self.layers_[-1](x)
     x = relu(x, alpha=self.leaky_alpha)
     return tf.reshape(x, [-1, self.ps, self.ps, self.shape_out])
示例#17
0
文件: ffm.py 项目: jeffin07/m2det-tf
 def v1(self):
     out1 = Conv2D(kernel_size=(1, 1), filters=256)(self.features1)
     out1 = relu(BatchNormalization()(out1))
     #   output 2
     out2 = Conv2D(kernel_size=(1, 1), filters=512)(self.features2)
     out2 = relu(BatchNormalization()(out2))
     out2 = UpSampling2D(size=(2, 2))(out2)
     #   output
     out = tf.concat([out1, out2], -1)
     return out
示例#18
0
 def call(self, inputs, states):
     # h(t) = activation(
     #     Wh x(t) + Uh h(t-1) + bh)
     h = states[-1]
     hh = K.dot(inputs, self.Wh) + K.dot(h, self.Uh)
     hh = activations.relu(K.bias_add(hh, self.bh))
     # y(t) = activate(Wy h(t) + by)
     y = K.dot(hh, self.Wy)
     y = K.bias_add(y, self.by)
     return activations.relu(y), [hh]
示例#19
0
    def call(self, x, inter=None):

        # Encoding
        if self.in_channels == 3:
            x1 = relu(self.bne1(self.conv1(x)))
        else:
            x1 = relu(self.bne1(self.conv1_clean(x)))
        x2 = relu(self.bne2(self.conv2(x1)))
        x3 = relu(self.bne3(self.conv3(x2)))
        x3 = self.flatten(x3)
        x4 = relu(self.bne4(self.fce1(x3)))
        x5 = self.fce2(x4)
        means = self.mean_params(x5)
        stddev = tf.math.exp(0.5 * self.stddev_params(x5))
        eps = random_normal(tf.shape(stddev))

        # Decoding
        z = means + eps * stddev
        if inter is not None:
            z = tf.keras.layers.add([z, inter])
        x6 = relu(self.bnd1(self.fcd1(z)))
        x7 = relu(self.bnd2(self.fcd2(x6)))
        x7 = self.reshape(x7)
        x8 = relu(self.bnd3(self.deconv1(x7)))
        x9 = relu(self.bnd4(self.deconv2(x8)))
        if self.out_channels == 3:
            x10 = self.deconv3(x9)
        else:
            x10 = self.deconv3_clean(x9)

        return x10, means, stddev, z
示例#20
0
    def call(self, inputs):
        """
        :param inputs: [batch_size, 256, 256, 3]
        :return: probabilities of shape [batch_size, num_classes]
        """
        output1 = self.pool1(relu(self.batch_norm1(self.conv1(inputs))))
        output2 = self.pool2(relu(self.batch_norm2(self.conv2(output1))))
        output3 = self.pool3(relu(self.batch_norm3(self.conv3(output2))))

        output3 = self.flatten(output3)
        return self.dense2(self.dense1(output3))
示例#21
0
    def construct_q_network(self):
        # Defines input tensors and scalars
        self.trace_length = tf.Variable(1, dtype=tf.int32)
        self.dropout_rate = tf.Variable(0.0, dtype=tf.float32, trainable=False)
        input_mem_state = tfk.Input(dtype=tf.float32,
                                    shape=(self.h_size),
                                    name='input_mem_state')
        input_carry_state = tfk.Input(dtype=tf.float32,
                                      shape=(self.h_size),
                                      name='input_carry_state')
        input_layer = tfk.Input(dtype=tf.float32,
                                shape=(None, self.observation_size),
                                name='input_obs')

        # Forward pass
        lay1 = tfkl.Dense(512)(input_layer)
        # Bayesian NN simulate
        lay1 = tfkl.Dropout(self.dropout_rate)(lay1)

        lay2 = tfkl.Dense(256)(lay1)
        lay2 = tfka.relu(lay2, alpha=0.01)  #leaky_relu

        lay3 = tfkl.Dense(128)(lay2)
        lay3 = tfka.relu(lay3, alpha=0.01)  #leaky_relu

        lay4 = tfkl.Dense(self.h_size)(lay3)

        # Recurring part
        lstm_layer = tfkl.LSTM(self.h_size, return_state=True)
        lstm_input = lay4
        lstm_state = [input_mem_state, input_carry_state]
        lay5, mem_s, carry_s = lstm_layer(lstm_input, initial_state=lstm_state)
        lstm_output = lay5

        # Advantage and Value streams
        advantage = tfkl.Dense(64)(lstm_output)
        advantage = tfka.relu(advantage, alpha=0.01)  #leaky_relu
        advantage = tfkl.Dense(self.action_size)(advantage)

        value = tfkl.Dense(64)(lstm_output)
        value = tfka.relu(value, alpha=0.01)  #leaky_relu
        value = tfkl.Dense(1)(value)

        advantage_mean = tf.math.reduce_mean(advantage, axis=1, keepdims=True)
        advantage = tfkl.subtract([advantage, advantage_mean])
        Q = tf.math.add(value, advantage)

        # Backwards pass
        self.model = tfk.Model(
            inputs=[input_mem_state, input_carry_state, input_layer],
            outputs=[Q, mem_s, carry_s])
        losses = ['mse', self.no_loss, self.no_loss]
        self.model.compile(loss=losses, optimizer=tfko.Adam(lr=self.lr))
        self.model.summary()
示例#22
0
 def dist(self, x):
     """
     Get distribution for atoms
     """
     feature = self.feature_layer(x)
     adv_hid = relu(self.advantage_hidden_layer(feature))
     val_hid = relu(self.value_hidden_layer(feature))
     advantage = tf.reshape(self.advantage_layer(adv_hid), [-1, self.out_dim, self.atom_size])
     value = tf.reshape(self.value_layer(val_hid), [-1, 1, self.atom_size])
     q_atoms = value + advantage - tf.math.reduce_mean(advantage, axis=1, keepdims=True)
     dist = softmax(q_atoms, axis=-1)
     return tf.clip_by_value(dist, clip_value_min=1e-3, clip_value_max=float('inf')) # for avoiding NaNs
示例#23
0
文件: tum.py 项目: jeffin07/m2det-tf
    def forward(self):
        # encoder
        encoder_outs = []
        for i in range(self.config["model"]["scales"]):
            if i == 0:
                conv_out = Conv2D(kernel_size=(3, 3),
                                  strides=(2, 2),
                                  filters=256,
                                  padding='same')(self.features)
            elif i == self.config["model"]["scales"] - 1:
                conv_out = Conv2D(kernel_size=(3, 3),
                                  strides=(2, 2),
                                  filters=256)(encoder_outs[i - 1])
            else:
                conv_out = Conv2D(kernel_size=(3, 3),
                                  strides=(2, 2),
                                  filters=256,
                                  padding='same')(encoder_outs[i - 1])
            encoder_outs.append(relu(BatchNormalization()(conv_out)))

        # decoder
        decoder_outs = []
        bs_outs = []
        for i in range(self.config["model"]["scales"] + 1):
            if i == 0:
                dec_out = Conv2D(kernel_size=(1, 1),
                                 strides=(1, 1),
                                 filters=128)(encoder_outs[-1])
                decoder_outs.append(relu(BatchNormalization()(dec_out)))
                bs_outs.append(
                    bilinear_upsampler(encoder_outs[-1],
                                       encoder_outs[-2].shape[1:3]))
            else:
                conv_out = Conv2D(kernel_size=(3, 3),
                                  strides=(1, 1),
                                  filters=256)(bs_outs[i - 1])
                conv_out = relu(BatchNormalization()(conv_out))
                if i != (self.config["model"]["scales"]):
                    bs_out = encoder_outs[-i - 1] + bilinear_upsampler(
                        conv_out, encoder_outs[-i - 1].shape[1:3])
                else:
                    bs_out = self.features + bilinear_upsampler(
                        conv_out, self.features.shape[1:3])

                dec_out = Conv2D(kernel_size=(1, 1),
                                 strides=(1, 1),
                                 filters=128)(bs_out)
                decoder_outs.append(relu(BatchNormalization()(dec_out)))
                bs_outs.append(bs_out)
        return decoder_outs
        """
    def call(self, hidden_states, training=False):
        x1 = relu(self.bn_p(self.conv_p(hidden_states), training=training))
        x1 = self.flat_p(x1)
        logits_policy = self.logits_policy(x1)
        policy = tf.nn.softmax(logits_policy)

        x2 = relu(self.bn_v(self.conv_v(hidden_states), training=training))
        x2 = self.flat_v(x2)

        logits_value = self.logits_value(x2)

        categorical_values = tf.nn.softmax(logits_value)

        return policy, categorical_values
示例#25
0
 def call(self, x):
     x = self.conv1(x)
     x = self.batchnorm1(x)
     x = activations.relu(x, alpha=0.01)
     x = self.pool1(x)
     x = self.conv2(x)
     x = self.batchnorm2(x)
     x = activations.relu(x, alpha=0.01)
     x = self.pool2(x)
     x = self.flatten(x)
     x = self.dense1(x)
     x = self.batchnorm3(x)
     x = activations.relu(x, alpha=0.01)
     return self.dense2(x)
    def call(self, x, inter=None):

        # Encoding
        x = relu(self.bne1(self.conv1(x)))
        x = relu(self.bne2(self.conv2(x)))
        if self.res == 64:
            x = relu(self.bne3(self.conv3(x)))
        x = self.flatten(x)
        x = relu(self.bne4(self.fce1(x)))
        x = self.fce2(x)
        means = self.mean_params(x)
        stddev = tf.math.exp(0.5 * self.stddev_params(x))
        eps = random_normal(tf.shape(stddev))

        # Decoding
        z = means + eps * stddev
        if inter is not None:
            z = tf.keras.layers.add([z, inter])
        x = relu(self.bnd1(self.fcd1(z)))
        x = relu(self.bnd2(self.fcd2(x)))
        x = self.reshape(x)
        if self.res == 64:
            x = relu(self.bnd3(self.deconv1(x)))
        x = relu(self.bnd4(self.deconv2(x)))
        x = self.deconv3(x)

        return x, means, stddev, z
示例#27
0
 def call(self, x, dropout=True):
     x = self.conv1(x)
     x = self.batchnorm1(x)
     x = activations.relu(x, alpha=0.01)
     x = self.pool1(x)
     x = self.conv2(x)
     x = self.batchnorm2(x)
     x = activations.relu(x, alpha=0.01)
     x = self.pool2(x)
     x = self.flatten(x)
     x = self.dense1(x)
     x = self.batchnorm3(x)
     x = self.dropout1(x, training=dropout)
     x = activations.relu(x, alpha=0.01)
     return self.dense2(x)
示例#28
0
    def call(self, x, sample_action=True):

        if self.conv:
            # [96, 96] --> [24, 24]
            x = self.conv1(x)
            x = activations.relu(x)
            # [24, 24] --> [12, 12]
            x = self.conv2(x)
            x = activations.relu(x)
        x = self.flatten(x)
        if self.dense_hidden_units > 0:
            x = self.dense(x)
            x = activations.relu(x)

        return x
 def __init__(
     self: tf_Conv,
     in_channels: int,
     out_channels: int,  # filters
     kernel_size: int = 1,
     strides: int = 1,
     padding: Optional[int] = None,
     groups: int = 1,
     activate: bool = True,
     module: Optional[nn.Module] = None
 ) -> None:
     super().__init__()
     assert module is not None
     # TF v2.2 Conv2D does not support 'groups' argument"
     assert groups == 1
     # Convolute multi kernels are not allowed
     assert isinstance(kernel_size, int)
     # Convolution
     # TensorFlow convolution padding is inconsistent
     # with PyTorch (e.g. k=3 s=2 'SAME' padding)
     # see https://stackoverflow.com/questions/52975843
     # /comparing-conv2d-with-padding-between-tensorflow-and-pytorch
     if strides == 1:
         padding_tf = 'SAME'
     else:
         padding_tf = 'VALID'
     kernel_initializer = Constant(
         module.conv.weight.permute(2, 3, 1, 0).numpy()
     )
     conv = Conv2D(
         filters=out_channels,
         kernel_size=kernel_size,
         strides=strides,
         padding=padding_tf,
         use_bias=False,
         kernel_initializer=kernel_initializer
     )
     if strides == 1:
         self.conv = conv
     else:
         self.conv = Sequential([
             tf_Pad(autopad(kernel_size, padding)),
             conv
         ])
     # batch normalization
     if hasattr(module, 'bn'):
         self.bn = tf_BN(module=module.bn)
     else:
         self.bn = tf.indentity
     # activation
     if not activate:
         self.act = tf.identity
     elif isinstance(module.act, nn.LeakyReLU):
         self.act = (lambda x: relu(x, alpha=0.1))
     elif isinstance(module.act, nn.Hardswish):
         self.act = (lambda x: x * tf.nn.relu6(x + 3) * 0.166666667)
     elif isinstance(module.act, nn.SiLU):
         # self.act = (lambda x: x * keras.activations.sigmoid(x))
         self.act = (lambda x: swish(x))
         return
示例#30
0
def single_input_model():
    # activation function: leaky ReLU
    leakyrelu = lambda x: relu(x, alpha=0.01, max_value=None, threshold=0)

    # creation du réseau de neurones
    model = Sequential([

        # hidden layer
        Dense(units=105, activation=leakyrelu, input_shape=(105, )),
        Dropout(rate=0.2),
        Dense(units=210, activation=leakyrelu),
        Dense(units=400, activation=leakyrelu),
        Dropout(rate=0.2),
        Dense(units=210, activation=leakyrelu),
        Dense(units=100, activation=leakyrelu),
        Dense(units=75, activation=leakyrelu),
        Dense(units=53, activation=leakyrelu),

        # final layer
        Dense(units=22, activation=softmax)
    ])

    model.compile(optimizer=Adam(),
                  loss=sparse_categorical_crossentropy,
                  metrics=['accuracy'])

    # Restoring
    single_input_checkpoint_path = join(
        path, "models/Single input model/checkpoints/checkpoint.ckpt")
    single_input_checkpoint_dir = dirname(single_input_checkpoint_path)
    single_input_latest = latest_checkpoint(single_input_checkpoint_dir)
    model.load_weights(single_input_latest)

    return model
示例#31
0
文件: m.py 项目: cottrell/notebooks
 def call(self, inputs):
     X, Z = inputs
     X_layer = kl.Dense(16, activation='linear')(X)
     Z_dense = kl.Dense(16, activation='linear')
     combined = list()
     for i in range(Z.shape[0]):
         z = Z_dense(Z[:,i])
         l = X_layer + z
         l = K.expand_dims(l, axis=1)
         combined.append(l)
     combined = kl.concatenate(combined, axis=1)
     # combined is now shape (batch_size, z_size, 16)
     l = ka.relu(combined)
     l = kl.Dense(16, activation='relu')(l)
     l = kl.Dense(16, activation='relu')(l)
     l = kl.Dense(16, activation='linear')(l)
     return l