Exemplo n.º 1
0
    def build_model(self):
        """Build an actor (policy) network that maps states -> actions."""

        # Set random uniform initialization with [-3e-3, 3e-3] to have all weights set around 0.
        initSeed = 0
        initMin = -3e-3
        initMax = 3e-3

        # Define input layer (states)
        states = layers.Input(shape=(self.state_size, ), name='states')

        # Add hidden layers according to network architecture.
        for i, nUnits in enumerate(self.architecture):
            net = layers.Dense(units=nUnits)(net if i > 0 else states)
            if self.batchNormalization:
                net = layers.BatchNormalization()(net)
            if self.activation == 'leakyRelu':
                net = layers.LeakyReLU(alpha=.001)(net)
            else:
                net = layers.Activation(activation=self.activation)(net)
            if self.dropout:
                net = layers.Dropout(self.dropoutRate)(net)

        # Add final output layer with sigmoid activation
        net = layers.Dense(
            units=self.action_size,
            kernel_initializer=initializers.RandomUniform(minval=initMin,
                                                          maxval=initMax,
                                                          seed=initSeed))(net)
        if self.batchNormalization:
            net = layers.BatchNormalization()(net)
        raw_actions = layers.Activation(activation='sigmoid',
                                        name='raw_actions')(net)

        # Note that the raw actions produced by the output layer are in
        # a [0.0, 1.0] range (using a sigmoid activation function).
        # So, we add another layer that scales each output to the desired range
        # for each action dimension. This produces a deterministic action for
        # any given state vector. A noise will be added later to this action
        # to produce some exploratory behavior.
        # Scale [0, 1] output for each action dimension to proper range
        actions = layers.Lambda(lambda x:
                                (x * self.action_range) + self.action_low,
                                name='actions')(raw_actions)

        # Create Keras model
        self.model = models.Model(inputs=states, outputs=actions)

        plot_model(self.model, to_file='actorKeras.png', show_shapes=True)

        # Define the loss function using action value (Q value) gradients
        action_gradients = layers.Input(shape=(self.action_size, ))
        loss = K.mean(-action_gradients * actions)

        # Incorporate any additional losses here (e.g. from regularizers)

        # Define optimizer and training function
        optimizer = optimizers.Adam(lr=self.learningRate)
        updates_op = optimizer.get_updates(params=self.model.trainable_weights,
                                           loss=loss)

        # The Q-value gradient will need to be computed using the critic model, and fed
        # in while training. Hence it is specified here as part of the "inputs" used in the training function.
        self.train_fn = K.function(
            inputs=[self.model.input, action_gradients,
                    K.learning_phase()],  # WHAT IS THIS?
            outputs=[],
            updates=updates_op)
except:
    pass

#%%
import keras
from keras import layers
import numpy as np

latent_dim = 32
height = 32
width = 32
channels = 3

generator_input = keras.Input(shape=(latent_dim, ))
x = layers.Dense(128 * 16 * 16)(generator_input)
x = layers.LeakyReLU()(x)
x = layers.Reshape((16, 16, 128))(x)

x = layers.Conv2D(256, 5, padding='same')(x)
x = layers.LeakyReLU()(x)

x = layers.Conv2DTranspose(256, 4, strides=2, padding='same')(x)
x = layers.LeakyReLU()(x)

x = layers.Conv2D(256, 5, padding='same')(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(256, 5, padding='same')(x)
x = layers.LeakyReLU()(x)

x = layers.Conv2D(channels, 7, activation='tanh', padding='same')(x)
Exemplo n.º 3
0
def back_bone(outputs, num_class=1):
    # print("input", x.shape)
    # base_model = keras.applications.xception.Xception(include_top=False, input_shape=input_size, pooling='avg')(x)

    # Encoder
    adap_encoder_1 = EncoderAdaption(filters=128,
                                     kernel_size=3,
                                     dilation_rate=1)
    adap_encoder_2 = EncoderAdaption(filters=128,
                                     kernel_size=3,
                                     dilation_rate=1)
    adap_encoder_3 = EncoderAdaption(filters=128,
                                     kernel_size=3,
                                     dilation_rate=1)
    adap_encoder_4 = EncoderAdaption(filters=64,
                                     kernel_size=3,
                                     dilation_rate=1)
    adap_encoder_5 = EncoderAdaption(filters=32,
                                     kernel_size=3,
                                     dilation_rate=1)

    # Decoder
    decoder_conv_1 = FeatureGeneration(filters=128,
                                       kernel_size=3,
                                       dilation_rate=1,
                                       blocks=3)
    decoder_conv_2 = FeatureGeneration(filters=64,
                                       kernel_size=3,
                                       dilation_rate=1,
                                       blocks=3)
    decoder_conv_3 = FeatureGeneration(filters=32,
                                       kernel_size=3,
                                       dilation_rate=1,
                                       blocks=3)
    decoder_conv_4 = FeatureGeneration(filters=32,
                                       kernel_size=3,
                                       dilation_rate=1,
                                       blocks=1)
    aspp = ASPP_2(filters=32, kernel_size=3)

    # output
    conv_logits = conv(filters=num_class,
                       kernel_size=1,
                       strides=1,
                       use_bias=True)

    # build the net
    # add activations to the ourputs of the model
    for i in range(len(outputs)):
        # print(outputs[i].shape)
        outputs[i] = layers.LeakyReLU(alpha=0.3)(outputs[i])

    # (outputs[0].shape)
    x = adap_encoder_1(outputs[0])

    x = upsampling(x, scale=2)
    x += reshape_into(adap_encoder_2(outputs[1]), x)  # 512
    x = decoder_conv_1(x)  # 256

    x = upsampling(x, scale=2)
    x += reshape_into(adap_encoder_3(outputs[2]), x)  # 256
    x = decoder_conv_2(x)  # 256
    # print("x1", x.shape)

    x = upsampling(x, scale=2)
    x += reshape_into(adap_encoder_4(outputs[3]), x)  # 128
    x = decoder_conv_3(x)  # 128
    # print("x1", x.shape)

    x = aspp(x, operation='sum')  # 128

    x = upsampling(x, scale=2)
    x += reshape_into(adap_encoder_5(outputs[4]), x)  # 64
    x = decoder_conv_4(x)  # 64
    # print("x1", x.shape)
    x = conv_logits(x)
    x = upsampling(x, scale=2)
    print("output", x.shape)
    aux_loss = False
    if aux_loss:
        return x, x
    else:
        return x
Exemplo n.º 4
0
def build_spectral_ae(input_shape=(513, 256, 1),
                      latent_dim=3,
                      n_filters=[32, 64, 128, 256, 512],
                      lr=0.001):

    f1 = n_filters[0]
    f2 = n_filters[1]
    f3 = n_filters[2]
    f4 = n_filters[3]

    input_spect = layers.Input(input_shape)
    x = layers.Conv2D(f1, (5, 5), padding='same', strides=(2, 2))(input_spect)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(f1, (4, 4), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(f1, (4, 4), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(f2, (4, 4), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(f2, (4, 4), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(f2, (4, 4), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(f3, (4, 4), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(f3, (4, 4), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(f3, (4, 4), padding='same', strides=(2, 1))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(f4, (1, 1), padding='same', strides=(2, 1))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(latent_dim, (1, 1), padding='same', strides=(1, 1))(x)
    z = layers.BatchNormalization()(x)

    input_z = layers.Input(shape=(1, 1, latent_dim))
    x = layers.Conv2DTranspose(f4, (1, 1), padding='same',
                               strides=(1, 1))(input_z)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2DTranspose(f3, (2, 2), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2DTranspose(f3, (2, 2), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2DTranspose(f3, (2, 2), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2DTranspose(f2, (2, 2), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2DTranspose(f2, (2, 2), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2DTranspose(f2, (2, 2), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2DTranspose(f1, (2, 2), padding='same', strides=(2, 2))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2DTranspose(f1, (2, 2), padding='same', strides=(2, 1))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2DTranspose(f1, (3, 1), padding='valid', strides=(2, 1))(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.BatchNormalization()(x)
    output_spect = layers.Conv2DTranspose(1, (1, 1),
                                          padding='same',
                                          strides=(1, 1))(x)

    encoder = Model(input_spect, z)
    encoder.summary()

    decoder = Model(input_z, output_spect)
    decoder.summary()

    outputs = decoder(encoder(input_spect))
    autoencoder = Model(input_spect, outputs)
    autoencoder.compile(optimizer=optimizers.Adam(lr=lr),
                        loss='mean_squared_error')
    autoencoder.summary()

    return encoder, decoder, autoencoder
Exemplo n.º 5
0
    def build(self):
        # Exogenous data Input data input
        input_layer_1 = layers.Input(shape=(self.time_window, self.exog_dim))

        # input is an image of time_window x 1
        # Endogenous data Input
        input_layer_2 = layers.Input(shape=(self.time_window, 1))

        exg_conv_layer_op_1 = layers.Conv1D(
            filters=16,
            kernel_size=2,
            dilation_rate=[1],
            strides=1,
            kernel_regularizer=keras.regularizers.l2(0.01),
            padding='valid')(input_layer_1)

        print exg_conv_layer_op_1
        exg_conv_layer_op_1 = layers.BatchNormalization()(exg_conv_layer_op_1)
        exg_conv_layer_op_1 = layers.LeakyReLU()(exg_conv_layer_op_1)

        end_conv_layer_op_1 = layers.Conv1D(
            filters=4,
            kernel_size=2,
            dilation_rate=[1],
            strides=1,
            kernel_regularizer=keras.regularizers.l2(0.01),
            padding='valid')(input_layer_2)

        print end_conv_layer_op_1

        concat_op = layers.Concatenate(axis=-1)(
            [end_conv_layer_op_1, exg_conv_layer_op_1])
        print 'Added ', concat_op

        concat_op = layers.LeakyReLU()(concat_op)
        print concat_op
        # ---- #
        # inp = input_layer_1
        network_op = None

        inp = concat_op

        for l in range(1, self.num_layers):
            d = math.pow(2, l)
            network_op = self.block(inp=inp,
                                    num_filters=4,
                                    dilation=[d],
                                    kernel_size=2)
            print network_op
            inp = network_op

        network_op = layers.Conv1D(
            filters=1,
            kernel_size=1,
            dilation_rate=[1],
            strides=1,
            kernel_regularizer=keras.regularizers.l2(0.01),
            padding='valid')(inp)

        print 'OP', network_op
        network_op = layers.Flatten()(network_op)
        print 'OP', network_op

        model = models.Model(inputs=[input_layer_1, input_layer_2],
                             outputs=[network_op])

        model.compile(optimizer=keras.optimizers.Adam(),
                      loss=keras.losses.MSE,
                      metrics=[keras.metrics.mae, keras.metrics.mse])

        print model.summary()
        self.model = model
        return
    up9 = Conv2D(16, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal', name='UpConv4')(UpSampling2D(size = (2,2), name='Up4')(conv8_1))
    merge9 = keras.layers.Concatenate(name='Concat4')([conv1,up9])
    conv9 = Residual17(48, 16, merge9)
    conv10 = Residual18(16, 2, conv9)
    conv10 = Residual19(2, 1, conv10)
    conv11 = Conv2D(1, 1, activation = 'sigmoid', name='Output')(conv10)
    
with tf.device('/device:GPU:3'):
    init = initial_conv_block(inputs, weight_decay=5e-4)
    
    #x1 = ResidualR(32, 64, init)    #192x192x64
    #x1 = ResidualR(64, 64, x1)
    #x1 = ResidualR(64, 64, x1)    #192x192x64
    x1 = Conv2D(64, (3,3), padding='same', kernel_initializer='he_normal')(init)
    x1 = BatchNormalization()(x1)
    x1 = layers.LeakyReLU()(x1)
    x1concat = keras.layers.Concatenate()([x1, conv9]) #192x192x80
    x1se = squeeze_excite_block(x1concat)
    x1conv1 = SeparableConv2D(80, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x1se)
    x1conv1 = layers.LeakyReLU()(x1conv1)
    x1conv2 = Conv2D(64, (1,1), padding = 'same', kernel_initializer = 'he_normal')(x1conv1)
    x1conv2 = layers.LeakyReLU()(x1conv2)
    x1pool = MaxPooling2D(pool_size=(2,2))(x1conv2)
    
    #x2 = ResidualR(64, 96, x1pool)   #96x96x96
    #x2 = ResidualR(96, 96, x2)
    #x2 = ResidualR(96, 96, x2)   #96x96x96
    x2 = Conv2D(96, (3,3), padding='same', kernel_initializer='he_normal')(x1pool)
    x2 = BatchNormalization()(x2)
    x2 = layers.LeakyReLU()(x2)
    x2concat = keras.layers.Concatenate()([x2, conv8]) #96x96x128
Exemplo n.º 7
0
 def add_common_layers(y):
     y = layers.BatchNormalization()(y)
     y = layers.LeakyReLU()(y)
     #	y = Dropout(0.2)(y)
     return y
Exemplo n.º 8
0
z = layers.multiply([z, x])
z = layers.Conv2D(128, (1, 1))(z)
z = layers.MaxPooling2D((3, 3))(z)
a_3 = layers.SeparableConv2D(256, (3, 3), activation='tanh', padding='same')(z)
z = layers.SpatialDropout2D(0.2)(a_3)
z = layers.SeparableConv2D(256, (3, 3), activation='tanh', padding='same')(z)
z = layers.SpatialDropout2D(0.2)(z)
z = layers.SeparableConv2D(256, (3, 3), activation='tanh', padding='same')(z)
z = layers.add([a_3, z])
z = layers.SpatialDropout2D(0.1)(z)
z = layers.BatchNormalization()(z)
z = layers.MaxPooling2D((3, 3))(z)
z = layers.Conv2D(128, (1, 1), padding='same')(z)
z = layers.ReLU()(z)
z = layers.Conv2D(64, (1, 1))(z)
z = layers.LeakyReLU(alpha=0.3)(z)
z = layers.Conv2D(32, (1, 1))(z)
z = layers.ReLU()(z)
z = layers.Flatten()(z)
# Von folgendem Layer werden die Gewichtungen erfasst
z = layers.Dense(32, kernel_regularizer=l1(0.001))(z)
z = layers.ReLU()(z)
model_output_1 = layers.Dense(1, activation='sigmoid')(z)
model = Model(input_1, model_output_1)
model.summary()
model.compile(loss=['binary_crossentropy'],
              optimizer=optimizers.Nadam(lr=1e-2),
              metrics=['acc'])
path = os.path.join(os.getcwd(), 'logs/')
callbacks_list = [keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
                                                    factor=0.2,
Exemplo n.º 9
0
# plt.imshow(data[0])
# plt.show()

width = 256
height = 256
channels = 3

input_layer = layers.Input(name='input', shape=(height, width, channels))

# Encoder
x = layers.Conv2D(32, (5, 5),
                  strides=(1, 1),
                  padding='same',
                  name='conv_1',
                  kernel_regularizer='l2')(input_layer)
x = layers.LeakyReLU(name='leaky_1')(x)

x = layers.Conv2D(64, (3, 3),
                  strides=(2, 2),
                  padding='same',
                  name='conv_2',
                  kernel_regularizer='l2')(x)
x = layers.BatchNormalization(name='norm_1')(x)
x = layers.LeakyReLU(name='leaky_2')(x)

x = layers.Conv2D(128, (3, 3),
                  strides=(2, 2),
                  padding='same',
                  name='conv_3',
                  kernel_regularizer='l2')(x)
x = layers.BatchNormalization(name='norm_2')(x)
Exemplo n.º 10
0
def build_encoder(is_discriminator=False, pooling=None, **params):
    thought_vector_size = params['thought_vector_size']
    pretrained_encoder = params['pretrained_encoder']
    img_width = params['img_width']
    cgru_size = params['csr_size']
    cgru_layers = params['csr_layers']

    include_top = False
    LEARNABLE_CNN_LAYERS = 1

    input_shape = (img_width, img_width, IMG_CHANNELS)
    input_img = layers.Input(shape=input_shape)
    if pretrained_encoder:
        if pretrained_encoder == 'vgg16':
            cnn = applications.vgg16.VGG16(input_tensor=input_img,
                                           include_top=include_top)
        elif pretrained_encoder == 'resnet50':
            # Note: This is a hacked version of resnet50 with pooling removed
            cnn = resnet50.ResNet50(include_top=include_top, pooling=pooling)
        for layer in cnn.layers[:-LEARNABLE_CNN_LAYERS]:
            layer.trainable = False
        x = cnn(input_img)
    else:
        x = layers.Conv2D(64, (3, 3), padding='same')(input_img)
        if not is_discriminator:
            x = layers.BatchNormalization()(x)
        x = layers.LeakyReLU()(x)
        x = layers.MaxPooling2D()(x)

        x = layers.Conv2D(128, (3, 3), padding='same')(x)
        if not is_discriminator:
            x = layers.BatchNormalization()(x)
        x = layers.LeakyReLU()(x)

        if cgru_layers >= 1:
            x = QuadCSR(cgru_size)(x)
        else:
            x = layers.Conv2D(cgru_size * 3 / 2, (3, 3), padding='same')(x)
        x = layers.LeakyReLU()(x)
        x = layers.MaxPooling2D()(x)

        x = layers.Conv2D(256, (3, 3), padding='same')(x)
        if not is_discriminator:
            x = layers.BatchNormalization()(x)
        x = layers.LeakyReLU()(x)
        x = layers.MaxPooling2D()(x)

        x = layers.Conv2D(384, (3, 3), padding='same')(x)
        if not is_discriminator:
            x = layers.BatchNormalization()(x)
        x = layers.Activation(LeakyReLU())(x)
        x = layers.Conv2D(384, (3, 3), padding='same')(x)
        if not is_discriminator:
            x = layers.BatchNormalization()(x)
        x = layers.LeakyReLU()(x)
        x = layers.MaxPooling2D()(x)

    if not pooling:
        x = layers.Flatten()(x)
    if is_discriminator:
        x = layers.Dense(1)(x)
        x = layers.Activation('tanh')(x)
    else:
        x = layers.Dense(thought_vector_size)(x)
        x = layers.BatchNormalization()(x)
    return models.Model(inputs=[input_img], outputs=x)
Exemplo n.º 11
0
# fig_format = 'eps'

# Loading the dataset
positions = np.loadtxt('positions.txt')
# The dataset contains the walking cycles, but we will use only the first one for training
expected_output = positions[0:40, :]

# Creating a input vector (0.008 ms is the sample time of the walking algorithm)
input = np.matrix(0.008 * np.arange(0, expected_output.shape[0])).T

# Setting the random seed of numpy's random library for reproducibility reasons
np.random.seed(0)

model = models.Sequential()
model.add(layers.Dense(75, activation=activations.linear, input_shape=(1, )))
model.add(layers.LeakyReLU(0.01))
model.add(layers.Dense(50, activation=activations.linear))
model.add(layers.LeakyReLU(0.01))
model.add(layers.Dense(20, activation=activations.linear))

model.compile(optimizer=optimizers.Adam(), loss=losses.mean_squared_error)

history = model.fit(input,
                    expected_output,
                    batch_size=len(expected_output),
                    epochs=num_epochs)

input_predict = np.matrix(np.arange(0, input[-1] + 0.001, 0.001)).T

# add this line to predict the output from the Neural Network
output = model.predict(input_predict)
Exemplo n.º 12
0
def fpn_orientation_graph(rois,
                          feature_maps,
                          mrcnn_probs,
                          mrcnn_bbox,
                          image_meta,
                          pool_size,
                          train_bn=True):
    """Builds the computation graph of the feature pyramid network orientation
     heads.

    rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized
          coordinates.
    feature_maps: List of feature maps from different layers of the pyramid,
                  [P2, P3, P4, P5]. Each has a different resolution.
    mrcnn_probs: classifier probabilities.
    mrcnn_bbox: Deltas to apply to proposal boxes
    image_meta: [batch, (meta data)] Image details. See compose_image_meta()
    pool_size: The width of the square feature map generated from ROI Pooling.
    train_bn: Boolean. Train or freeze Batch Norm layers

    Returns:
        r_matrices: [batch, 3, 3] rotation matrices
        angles: [batch,, 3] rotation angles in Euler ZYX (radians)
    """

    x = model.PyramidROIAlign(
        [pool_size, pool_size],
        name="roi_align_orientation")([rois, image_meta] + feature_maps)
    # Two 1024 FC layers (implemented with Conv2D for consistency)
    x = KL.TimeDistributed(KL.Conv2D(1024, (pool_size, pool_size),
                                     padding="valid"),
                           name="mrcnn_or_conv1")(x)
    x = KL.TimeDistributed(model.BatchNorm(),
                           name='mrcnn_or_bn1')(x, training=train_bn)
    x = KL.Activation('relu')(x)
    x = KL.TimeDistributed(KL.Conv2D(1024, (1, 1)), name="mrcnn_or_conv2")(x)
    x = KL.TimeDistributed(model.BatchNorm(),
                           name='mrcnn_or_bn2')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.Lambda(lambda x: K.squeeze(K.squeeze(x, 3), 2),
                  name="pool_squeeze_or")(x)

    # Add class probabilities
    x = KL.Concatenate(axis=2)([x, mrcnn_probs])

    # Add detected bounding box
    s = K.int_shape(mrcnn_bbox)
    mrcnn_bbox = KL.Reshape((s[1], s[2] * s[3]))(mrcnn_bbox)
    x = KL.Concatenate(axis=2)([x, mrcnn_bbox])

    x = KL.TimeDistributed(KL.Dense(1024), name='mrcnn_or_d1')(x)
    x = KL.LeakyReLU(alpha=0.2)(x)
    x = KL.TimeDistributed(KL.BatchNormalization(),
                           name='mrcnn_or_bn3')(x, training=train_bn)
    x = KL.TimeDistributed(KL.Dense(1024), name='mrcnn_or_d2')(x)
    x = KL.LeakyReLU(alpha=0.2)(x)
    x = KL.TimeDistributed(KL.BatchNormalization(),
                           name='mrcnn_or_bn4')(x, training=train_bn)
    x = KL.TimeDistributed(KL.Dense(6), name='mrcnn_or_d5')(x)

    #s = K.int_shape(x)
    #x = KL.Lambda(lambda t: tf.reshape(t, (-1, s[2])))(x)

    r_matrices = KL.TimeDistributed(
        KL.Lambda(lambda t: or_tools.compute_rotation_matrix_from_ortho6d(t)))(
            x)
    #r_matrices = KL.TimeDistributed(KL.Reshape((-1, s[1], 3, 3))(r_matrices))
    angles = KL.TimeDistributed(
        KL.Lambda(lambda x: or_tools.
                  compute_euler_angles_from_rotation_matrices(x)))(r_matrices)
    #angles = KL.Reshape((-1, s[1], 3))(angles)

    return r_matrices, angles
Exemplo n.º 13
0
    def build_generator(self):
        noise_shape = (self.latent_space_size, )

        model = Sequential()
        model.add(
            layers.Dense(8 * 8 * 1024,
                         use_bias=False,
                         input_shape=(self.latent_space_size, )))
        model.add(layers.BatchNormalization())
        model.add(layers.LeakyReLU(alpha=0.2))

        model.add(layers.Reshape((8, 8, 1024)))
        assert model.output_shape == (None, 8, 8, 1024
                                      )  # Note: None is the batch size

        model.add(
            layers.Conv2DTranspose(512,
                                   kernel_size=self.kernel_size,
                                   strides=(2, 2),
                                   padding='same',
                                   use_bias=False))
        assert model.output_shape == (None, 16, 16, 512)
        model.add(layers.BatchNormalization())
        model.add(layers.LeakyReLU(alpha=0.2))

        model.add(
            layers.Conv2DTranspose(256,
                                   kernel_size=self.kernel_size,
                                   strides=(2, 2),
                                   padding='same',
                                   use_bias=False))
        assert model.output_shape == (None, 32, 32, 256)
        model.add(layers.BatchNormalization())
        model.add(layers.LeakyReLU(alpha=0.2))

        model.add(
            layers.Conv2DTranspose(128,
                                   kernel_size=self.kernel_size,
                                   strides=(2, 2),
                                   padding='same',
                                   use_bias=False))
        assert model.output_shape == (None, 64, 64, 128)
        model.add(layers.BatchNormalization())
        model.add(layers.LeakyReLU(alpha=0.2))

        model.add(
            layers.Conv2DTranspose(3,
                                   kernel_size=self.kernel_size,
                                   strides=(2, 2),
                                   padding='same',
                                   use_bias=False,
                                   activation='tanh'))
        assert model.output_shape == (None, 128, 128, 3)
        ''' 
        model.add(layers.BatchNormalization())
        model.add(layers.LeakyReLU(alpha = 0.2))

        model.add(layers.Conv2DTranspose(3, kernel_size = (7,7) , strides=(2, 2), padding='same', use_bias=False, activation='tanh'))
        assert model.output_shape == (None, 256, 256, 3)'''

        model.summary()

        noise = Input(shape=noise_shape)
        img = model(noise)

        return Model(noise, img)
Exemplo n.º 14
0
def make_model(input_shape, output_shape):
    nn = models.Sequential()

    # first, second, third layer

    nn.add(layers.Conv2D(32,
        (3, 3),
        activation="linear",
        kernel_regularizer=regularizers.l2(.01),
        input_shape=input_shape,
        kernel_initializer="he_normal"
    ))

    nn.add(layers.LeakyReLU(0.001))

    nn.add(layers.Conv2D(32,
        (3, 3),
        activation="linear",
        kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    ))
    nn.add(layers.LeakyReLU(0.001))

    nn.add(layers.Conv2D(32,
        (3, 3),
        activation="linear",
        #kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    ))
    nn.add(layers.LeakyReLU(0.001))

    nn.add(layers.BatchNormalization())
    nn.add(layers.Dropout(.4))
    nn.add(layers.MaxPooling2D())

    # four, five, sixth layer
    nn.add(layers.Conv2D(64,
        (3, 3),
        activation="linear",
        kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    )) 

    nn.add(layers.LeakyReLU(0.001))

    nn.add(layers.Conv2D(64,
        (3, 3),
        activation="linear",
        kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    ))
    nn.add(layers.LeakyReLU(0.001))

    nn.add(layers.Conv2D(64,
        (3, 3),
        activation="linear",
        #kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    ))
    nn.add(layers.LeakyReLU(0.001))

    nn.add(layers.BatchNormalization()) 
    nn.add(layers.Dropout(.4))
    nn.add(layers.MaxPooling2D())

    # seven, eight, nine, ten layer
    nn.add(layers.Conv2D(64,
        (1, 1),
        activation="linear",
        kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    ))  

    nn.add(layers.LeakyReLU(0.001))

    nn.add(layers.Conv2D(32,
        (1,1),
        activation="linear",
        kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    ))
    nn.add(layers.LeakyReLU(0.001))

    nn.add(layers.Conv2D(16,
        (1,1),
        activation="linear",
        #kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    ))
    nn.add(layers.LeakyReLU(0.001))

    nn.add(layers.Conv2D(16,
        (3,3),
        activation="linear",
        #kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    ))
    nn.add(layers.LeakyReLU(0.001))
    nn.add(layers.BatchNormalization())
    nn.add(layers.MaxPooling2D())
    nn.add(layers.Flatten())

    # eleventh layer

    nn.add(layers.Dense(64,
        activation="relu",
        kernel_regularizer=regularizers.l2(.01),
        kernel_initializer="he_normal"
    ))
    nn.add(layers.Dropout(.3))
    nn.add(layers.Dense(output_shape, activation='softmax'))

    nn.summary()

    nn.compile(
        optimizer=optimizers.Adam(lr=0.0001),
        loss='categorical_crossentropy',
        metrics=['accuracy']
    )

    return nn
Exemplo n.º 15
0
def trainModel(DS, x_train, y_train, x_valid, y_valid, x_test, y_test):
    '''Build and Train (pretrained) VGG16 Model'''
    #Prepare Dataset for training
    x_train, y_train, x_valid, y_valid, x_test, y_test = prepareDataset(
        x_train, y_train, x_valid, y_valid, x_test, y_test)

    # Define the parameters for VGG16 model.
    IMG_HEIGHT = 48
    IMG_WIDTH = 48
    IMG_DEPTH = 3
    BATCH_SIZE = 16
    NB_EPOCHS = 100
    no_of_class = noClass(DS)
    print('Parameters are defined')

    # Preprocessing the input
    x_train = preprocess_input(x_train)
    x_valid = preprocess_input(x_valid)
    x_test = preprocess_input(x_test)
    print('Input is preprocessed')

    #Conv base
    conv_base = VGG16(weights='imagenet',\
                      include_top=False, \
                      input_shape=(IMG_HEIGHT, IMG_WIDTH, IMG_DEPTH))

    print('Conv Base is created')
    conv_base.summary()

    #Model features
    train_features = conv_base.predict(np.array(x_train),
                                       batch_size=BATCH_SIZE,
                                       verbose=1)
    val_features = conv_base.predict(np.array(x_valid),
                                     batch_size=BATCH_SIZE,
                                     verbose=1)
    test_features = conv_base.predict(np.array(x_test),
                                      batch_size=BATCH_SIZE,
                                      verbose=1)

    print('Featurs are created')
    print("train_features shape:", train_features.shape)
    print("test_features shape:", test_features.shape)
    print("val_features shape:", val_features.shape)

    train_features_flat = np.reshape(train_features,
                                     (train_features.shape[0], 1 * 1 * 512))
    val_features_flat = np.reshape(val_features,
                                   (val_features.shape[0], 1 * 1 * 512))
    test_features_flat = np.reshape(test_features,
                                    (test_features.shape[0], 1 * 1 * 512))
    print('Features are flattened')

    # 7.0 Define the densely connected classifier followed by leakyrelu layer and finally dense layer for the number of classes
    NB_TRAIN_SAMPLES = train_features_flat.shape[0]
    NB_VALIDATION_SAMPLES = val_features_flat.shape[0]

    model = models.Sequential()
    model.add(layers.Dense(512, activation='relu', input_dim=(1 * 1 * 512)))
    model.add(layers.LeakyReLU(alpha=0.1))
    model.add(layers.Dense(no_of_class, activation='softmax'))

    print('Model layers are created')

    # Compile the model.
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizers.Adam(),
                  metrics=['acc'])

    print('Model is compiled')

    from keras import callbacks
    #Define callbacks
    reduce_learning = callbacks.ReduceLROnPlateau(monitor='val_loss',
                                                  factor=0.2,
                                                  patience=2,
                                                  verbose=1,
                                                  mode='auto',
                                                  min_delta=0.0001,
                                                  cooldown=2,
                                                  min_lr=0)

    early_stopping = callbacks.EarlyStopping(monitor='val_loss',
                                             min_delta=0,
                                             patience=7,
                                             verbose=1,
                                             mode='auto')

    callbacks = [reduce_learning, early_stopping]

    print('Call backs are defined')

    print('\n\n>>>Model Trainig starts now')

    # Train the the model
    history = model.fit(train_features_flat,
                        y_train,
                        epochs=NB_EPOCHS,
                        validation_data=(val_features_flat, y_valid),
                        callbacks=callbacks)

    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']

    print('')
    print(f'Model training accuracy: {acc[-1]}')
    print(f'Model validation accuracy: {val_loss[-1]}')
    print(f'Model training loss: {acc[-1]}')
    print(f'Model validation loss: {val_loss[-1]}')

    test_acc, test_precision, test_recall, test_fscore, hamming_loss = modelPerformance(
        model, test_features_flat, y_test)

    results = {'train':[acc[-1], val_acc[-1], loss[-1], val_loss[-1]],\
               'test':[test_acc, test_precision, test_recall, test_fscore, hamming_loss]}

    return model, history, results
Exemplo n.º 16
0
def darknet(channels,
            odd_pointwise,
            avg_pool_size,
            cls_activ,
            alpha=0.1,
            in_channels=3,
            in_size=(224, 224),
            classes=1000):
    """
    DarkNet model from 'Darknet: Open source neural networks in c,' https://github.com/pjreddie/darknet.

    Parameters:
    ----------
    channels : list of list of int
        Number of output channels for each unit.
    odd_pointwise : bool
        Whether pointwise convolution layer is used for each odd unit.
    avg_pool_size : int
        Window size of the final average pooling.
    cls_activ : bool
        Whether classification convolution layer uses an activation.
    alpha : float, default 0.1
        Slope coefficient for Leaky ReLU activation.
    in_channels : int, default 3
        Number of input channels.
    in_size : tuple of two ints, default (224, 224)
        Spatial size of the expected input image.
    classes : int, default 1000
        Number of classification classes.
    """
    input_shape = (in_channels, 224,
                   224) if K.image_data_format() == 'channels_first' else (
                       224, 224, in_channels)
    input = nn.Input(shape=input_shape)

    x = input
    for i, channels_per_stage in enumerate(channels):
        for j, out_channels in enumerate(channels_per_stage):
            x = dark_convYxY(x=x,
                             in_channels=in_channels,
                             out_channels=out_channels,
                             alpha=alpha,
                             pointwise=(len(channels_per_stage) > 1)
                             and not (((j + 1) % 2 == 1) ^ odd_pointwise),
                             name="features/stage{}/unit{}".format(
                                 i + 1, j + 1))
            in_channels = out_channels
        if i != len(channels) - 1:
            x = nn.MaxPool2D(pool_size=2,
                             strides=2,
                             name="features/pool{}".format(i + 1))(x)

    x = nn.Conv2D(filters=classes, kernel_size=1, name="output/final_conv")(x)
    if cls_activ:
        x = nn.LeakyReLU(alpha=alpha, name="output/final_activ")(x)
    x = nn.AvgPool2D(pool_size=avg_pool_size,
                     strides=1,
                     name="output/final_pool")(x)
    x = nn.Flatten()(x)

    model = Model(inputs=input, outputs=x)
    model.in_size = in_size
    model.classes = classes
    return model
Exemplo n.º 17
0
    def build(self):

        # input is an image of time_window x 1
        input_exg = layers.Input(shape=(self.time_window, 1, self.exog_dim))
        input_end = layers.Input(shape=(self.time_window, 1))

        op_end = input_end
        op_exg = self.build_exog_cnn(input_exg)

        print '---> Start of parallel layers'
        # build parallel dilated layers
        for l in range(self.parallel_layers):
            print '--- parallel layer  ', l + 1
            d = math.pow(2, l)
            print 'dialtion rate :', d

            op_end = self.res_block(inp=op_end,
                                    num_filters=4,
                                    dilation=[d],
                                    kernel_size=2)

            op_exg = self.res_block(inp=op_exg,
                                    num_filters=4,
                                    dilation=[d],
                                    kernel_size=2)

        # build combined dilated layers
        print '---> Start of combined layers'
        comb_op = layers.add([op_exg, op_end])
        print comb_op

        for l in range(self.parallel_layers,
                       self.parallel_layers + self.comb_layers):

            d = math.pow(2, l)
            print 'layer', l + 1, 'dialtion rate :', d

            comb_op = self.res_block(inp=comb_op,
                                     num_filters=4,
                                     dilation=[d],
                                     kernel_size=2)

        network_op = layers.Conv1D(
            filters=1,
            kernel_size=1,
            dilation_rate=[1],
            strides=1,
            kernel_regularizer=keras.regularizers.l2(0.01),
            padding='valid')(comb_op)
        network_op = layers.LeakyReLU()(network_op)

        model = models.Model(inputs=[input_exg, input_end],
                             outputs=[network_op])

        model.compile(optimizer=keras.optimizers.Adam(),
                      loss=keras.losses.MSE,
                      metrics=[keras.metrics.mae, keras.metrics.mse])

        print model.summary()
        self.model = model
        return
Exemplo n.º 18
0
    def build_model(self):
        """Build a critic (value) network that maps (state, action) pairs -> Q-values."""
        # Define input layers
        """
        它在某些方面比行动者模型简单,但是需要注意几点。首先,行动者模型旨在将状态映射到动作,
        而评论者模型需要将(状态、动作)对映射到它们的 Q 值。这一点体现在了输入层中。"""
        states = layers.Input(shape=(self.state_size, ), name='states')
        actions = layers.Input(shape=(self.action_size, ), name='actions')

        # Add hidden layer(s) for state pathway
        """It took me so, use_bias=True, bias_constraint=regularizers.l2(0.02)"""
        net_states = states
        net_states = layers.Dense(
            units=32,
            kernel_regularizer=regularizers.l2(0.001),
            activity_regularizer=regularizers.l1(0.001))(net_states)
        net_states = layers.LeakyReLU(alpha=0.3)(net_states)
        net_states = layers.BatchNormalization()(net_states)
        net_states = layers.Dropout(0.4)(net_states)

        net_states = layers.Dense(
            units=64,
            kernel_regularizer=regularizers.l2(0.001),
            activity_regularizer=regularizers.l1(0.001))(net_states)
        net_states = layers.LeakyReLU(alpha=0.3)(net_states)
        net_states = layers.BatchNormalization()(net_states)
        net_states = layers.Dropout(0.4)(net_states)

        net_actions = actions
        net_actions = layers.Dense(
            units=32,
            kernel_regularizer=regularizers.l2(0.001),
            activity_regularizer=regularizers.l1(0.001))(net_actions)
        net_actions = layers.LeakyReLU(alpha=0.3)(net_actions)
        net_actions = layers.BatchNormalization()(net_actions)
        net_actions = layers.Dropout(0.4)(net_actions)

        net_actions = layers.Dense(
            units=64,
            kernel_regularizer=regularizers.l2(0.001),
            activity_regularizer=regularizers.l1(0.001))(net_actions)
        net_actions = layers.LeakyReLU(alpha=0.3)(net_actions)
        net_actions = layers.BatchNormalization()(net_actions)
        net_actions = layers.Dropout(0.4)(net_actions)

        # Try different layer sizes, activations, add batch normalization, regularizers, etc.

        # Combine state and action pathways
        """这两个层级首先可以通过单独的“路径”(迷你子网络)处理,但是最终需要结合到一起
        例如,可以通过使用 Keras 中的 Add 层级类型来实现请参阅合并层级):"""
        net = layers.Add()([net_states, net_actions])

        net = layers.Dense(units=256,
                           kernel_regularizer=regularizers.l2(0.001),
                           activity_regularizer=regularizers.l1(0.001))(net)
        net = layers.LeakyReLU(alpha=0.3)(net)
        net = layers.BatchNormalization()(net)
        net = layers.Dropout(0.4)(net)

        net = layers.Dense(units=16,
                           kernel_regularizer=regularizers.l2(0.001),
                           activity_regularizer=regularizers.l1(0.001))(net)
        net = layers.LeakyReLU(alpha=0.3)(net)

        # Add more layers to the combined network if needed

        # Add final output layer to prduce action values (Q values)
        Q_values = layers.Dense(units=1, name='q_values')(net)

        # Create Keras model
        self.model = models.Model(inputs=[states, actions], outputs=Q_values)

        # Define optimizer and compile model for training with built-in loss function
        optimizer = optimizers.Adam(lr=1e-4)
        self.model.compile(optimizer=optimizer, loss='mse')

        # Compute action gradients (derivative of Q values w.r.t. to actions)
        """该模型的最终输出是任何给定(状态、动作)对的 Q 值。但是,我们还需要计算此 Q 值相对于相应动作向量的梯度,以用于训练行动者模型。
        这一步需要明确执行,并且需要定义一个单独的函数来访问这些梯度:"""
        action_gradients = K.gradients(Q_values, actions)

        # Define an additional function to fetch action gradients (to be used by actor model)
        self.get_action_gradients = K.function(
            inputs=[*self.model.input, K.learning_phase()],
            outputs=action_gradients)
Exemplo n.º 19
0
 def add_common_layers(y):
     y = layers.BatchNormalization()(y)
     y = layers.LeakyReLU()(y)
     y = layers.Dropout(dropout_level)(y)
     return y
Exemplo n.º 20
0
import numpy as np
import os
import random
from PIL import Image

noise_dim = 16  # 噪声维度
height = 32
width = 32
channels = 3

# G net

generator_input = keras.Input(shape=(noise_dim, ))

out = layers.Dense(16 * 16 * 128)(generator_input)
out = layers.LeakyReLU()(out)
out = layers.Reshape((16, 16, 128))(out)

out = layers.Conv2D(256, 5, padding='same')(out)
out = layers.LeakyReLU()(out)

out = layers.Conv2DTranspose(256, 4, strides=2, padding='same')(out)
out = layers.LeakyReLU()(out)

out = layers.Conv2D(256, 5, padding='same')(out)
out = layers.LeakyReLU()(out)

out = layers.Conv2D(channels, 7, activation='tanh', padding='same')(out)
g_net = Model(generator_input, out)

# D net
Exemplo n.º 21
0
    def build_model(self):
        """Build a critic (value) network that maps (state, action) pairs -> Q-values."""
        # Define input layers
        states = layers.Input(shape=(self.state_size, ), name='states')
        actions = layers.Input(shape=(self.action_size, ), name='actions')

        # Add hidden layer(s) for state pathway
        net_states = layers.Dense(units=256,
                                  use_bias=False,
                                  kernel_initializer='he_normal',
                                  kernel_regularizer=regularizers.l2(
                                      self.l2_reg))(states)
        net_states = layers.BatchNormalization()(net_states)
        net_states = layers.LeakyReLU(1e-2)(net_states)
        net_states = layers.Dropout(self.dropout)(net_states)

        net_states = layers.Dense(units=128,
                                  use_bias=False,
                                  kernel_initializer='he_normal',
                                  kernel_regularizer=regularizers.l2(
                                      self.l2_reg))(net_states)
        net_states = layers.BatchNormalization()(net_states)
        net_states = layers.LeakyReLU(1e-2)(net_states)
        net_states = layers.Dropout(self.dropout)(net_states)

        # Add hidden layer(s) for action pathway
        net_actions = layers.Dense(units=256,
                                   use_bias=False,
                                   kernel_initializer='he_normal',
                                   kernel_regularizer=regularizers.l2(
                                       self.l2_reg))(actions)
        net_actions = layers.BatchNormalization()(net_actions)
        net_states = layers.LeakyReLU(1e-2)(net_actions)
        net_actions = layers.Dropout(self.dropout)(net_actions)

        net_actions = layers.Dense(units=128,
                                   use_bias=False,
                                   kernel_initializer='he_normal',
                                   kernel_regularizer=regularizers.l2(
                                       self.l2_reg))(net_actions)
        net_actions = layers.BatchNormalization()(net_actions)
        net_states = layers.LeakyReLU(1e-2)(net_actions)
        net_actions = layers.Dropout(self.dropout)(net_actions)

        # Try different layer sizes, activations, add batch normalization, regularizers, etc.

        # Combine state and action pathways
        net = layers.Add()([net_states, net_actions])
        net = layers.LeakyReLU(1e-2)(net)

        # Add more layers to the combined network if needed

        # Add final output layer to prduce action values (Q values)
        Q_values = layers.Dense(units=1, name='q_values')(net)

        # Create Keras model
        self.model = models.Model(inputs=[states, actions], outputs=Q_values)
        plot_model(self.model, "critic.png")

        # Define optimizer and compile model for training with built-in loss function
        optimizer = optimizers.Adam(lr=self.learning_rate)
        self.model.compile(optimizer=optimizer, loss='mse')

        # Compute action gradients (derivative of Q values w.r.t. to actions)
        action_gradients = K.gradients(Q_values, actions)

        # Define an additional function to fetch action gradients (to be used by actor model)
        self.get_action_gradients = K.function(
            inputs=[*self.model.input, K.learning_phase()],
            outputs=action_gradients)
Exemplo n.º 22
0
def get_model():
    model = models.Sequential()
    model.add(
        layers.Conv2D(8,
                      kernel_size,
                      padding='same',
                      input_shape=(INPUT_SHAPE_X, INPUT_SHAPE_Y, 1),
                      activation='relu',
                      kernel_initializer='he_uniform'))
    model.add(layers.BatchNormalization())
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(
        layers.Conv2D(16,
                      kernel_size,
                      padding='same',
                      activation='relu',
                      kernel_initializer='he_uniform'))
    model.add(layers.BatchNormalization())
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(
        layers.Conv2D(16,
                      kernel_size,
                      padding='same',
                      activation='relu',
                      kernel_initializer='he_uniform'))
    model.add(layers.BatchNormalization())
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(
        layers.Conv2D(32,
                      kernel_size,
                      padding='same',
                      activation='relu',
                      kernel_initializer='he_uniform'))
    model.add(layers.BatchNormalization())
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(
        layers.Conv2D(48,
                      kernel_size,
                      padding='same',
                      activation='relu',
                      kernel_initializer='he_uniform'))
    model.add(layers.BatchNormalization())
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(
        layers.Conv2D(56,
                      kernel_size,
                      padding='same',
                      activation='relu',
                      kernel_initializer='he_uniform'))
    model.add(layers.BatchNormalization())
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(layers.Flatten())
    model.add(
        layers.Dense(128,
                     kernel_regularizer=regularizers.l2(regu),
                     kernel_initializer='he_uniform'))
    model.add(layers.LeakyReLU())
    model.add(layers.Dropout(0.5))
    model.add(
        layers.Dense(64,
                     kernel_regularizer=regularizers.l2(regu),
                     kernel_initializer='he_uniform'))
    model.add(layers.LeakyReLU())

    model.add(layers.Dense(OUTPUT_NUM, activation='softmax'))

    model.compile(optimizer=optimizers.Adam(learning_rate=LR),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    return model
Exemplo n.º 23
0
def darknet53_model(channels,
                    init_block_channels,
                    alpha=0.1,
                    in_channels=3,
                    in_size=(224, 224),
                    classes=1000):
    """
    DarkNet-53 model from 'YOLOv3: An Incremental Improvement,' https://arxiv.org/abs/1804.02767.

    Parameters:
    ----------
    channels : list of list of int
        Number of output channels for each unit.
    init_block_channels : int
        Number of output channels for the initial unit.
    alpha : float, default 0.1
        Slope coefficient for Leaky ReLU activation.
    in_channels : int, default 3
        Number of input channels.
    in_size : tuple of two ints, default (224, 224)
        Spatial size of the expected input image.
    classes : int, default 1000
        Number of classification classes.
    """
    input_shape = (in_channels, in_size[0], in_size[1]) if is_channels_first() else\
        (in_size[0], in_size[1], in_channels)
    input = nn.Input(shape=input_shape)

    x = conv3x3_block(x=input,
                      in_channels=in_channels,
                      out_channels=init_block_channels,
                      activation=nn.LeakyReLU(
                          alpha=alpha, name="features/init_block/activ"),
                      name="features/init_block")
    in_channels = init_block_channels
    for i, channels_per_stage in enumerate(channels):
        for j, out_channels in enumerate(channels_per_stage):
            if j == 0:
                x = conv3x3_block(
                    x=x,
                    in_channels=in_channels,
                    out_channels=out_channels,
                    strides=2,
                    activation=nn.LeakyReLU(
                        alpha=alpha,
                        name="features/stage{}/unit{}/active".format(
                            i + 1, j + 1)),
                    name="features/stage{}/unit{}".format(i + 1, j + 1))
            else:
                x = dark_unit(x=x,
                              in_channels=in_channels,
                              out_channels=out_channels,
                              alpha=alpha,
                              name="features/stage{}/unit{}".format(
                                  i + 1, j + 1))
            in_channels = out_channels
    x = nn.AvgPool2D(pool_size=7, strides=1, name="features/final_pool")(x)

    # x = nn.Flatten()(x)
    x = flatten(x)
    x = nn.Dense(units=classes, input_dim=in_channels, name="output")(x)

    model = Model(inputs=input, outputs=x)
    model.in_size = in_size
    model.classes = classes
    return model
Exemplo n.º 24
0
def mk_conv_64(*, channels):

    i = kr.Input((64, 64, channels), name='x0')

    cc_stack = [
        kr.GaussianNoise(0.025),
        kr.Conv2D(10, 1),
        kr.LeakyReLU(alpha=0.5),
        kr.Conv2D(2),
    ]

    h = apply_layers(i, cc_stack)

    conv_stack = [
        kr.GaussianNoise(0.025),
        kr.Conv2D(
            64,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            128,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            256,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            384,
            3,
            strides=2,
            activation='elu',
        ),
    ]

    h = apply_layers(i, conv_stack)
    gp0 = kr.GlobalMaxPool2D()(h)
    ap0 = kr.GlobalAveragePooling2D()(h)

    conv_stack_2 = [
        kr.Conv2D(
            384,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            384,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            384,
            3,
            strides=2,
            activation='elu',
        ),
    ]

    conv_stack_3 = [
        kr.Conv2D(
            198,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            198,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            198,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            198,
            3,
            strides=2,
            activation='elu',
        ),
        kr.Flatten(),
    ]

    h = apply_layers(h, conv_stack_2)
    h = apply_layers(h, conv_stack_3)

    head = [
        kr.Dense(512, activation='elu'),
        kr.Dropout(0.5),
        kr.Dense(256, activation='elu'),
        kr.Dropout(0.5),
        kr.Dense(128, activation='elu'),
        kr.Dense(NL, activation='sigmoid', name='labs'),
    ]

    cat = kr.Concatenate()([h, gp0, ap0])
    y = apply_layers(cat, head)

    m = krm.Model(inputs=[i], outputs=[y], name='base_conv')
    m.compile(loss=f2_crossentropy,
              optimizer='adam',
              metrics=['binary_accuracy'])

    return m
Exemplo n.º 25
0
    def build_model(self):
        """Build a critic (value) network that maps (state, action) pairs -> Q-values."""
        
        # Set random uniform initialization with [-3e-4, 3e-4] to have all weights set around 0.
        # This is according to DDPG paper.
        initSeed = 0
        initMin = -3e-3
        initMax = 3e-3
        l2Lambda = 1e-2
        
        # Define separate input layers for state and action pathways (sub-networks).
        states = layers.Input(shape=(self.state_size,), name='states')
        actions = layers.Input(shape=(self.action_size,), name='actions')

        # Add hidden layer(s) for state pathway
        for i, nUnits in enumerate(self.architecture[0]):
            net_states = layers.Dense(units=nUnits, activity_regularizer=regularizers.l2(l2Lambda))(net_states if i>0 else states)
            # Add batch normalization, activation and dropout.
            if self.batchNormalization:
                net_states = layers.BatchNormalization()(net_states)
            if self.activation == 'leakyRelu':
                net = layers.LeakyReLU(alpha=.001)(net)
            else:
                net_states = layers.Activation(activation=self.activation)(net_states)
            if self.dropout:
                net_states = layers.Dropout(self.dropoutRate)(net_states)

        # Add hidden layer(s) for action pathway
        for i, nUnits in enumerate(self.architecture[1]):
            net_actions = layers.Dense(units=nUnits, activity_regularizer=regularizers.l2(l2Lambda))(net_actions if i>0 else actions)
            # Add batch normalization, activation and dropout.
            if self.batchNormalization:
                net_actions = layers.BatchNormalization()(net_actions)
            if self.activation == 'leakyRelu':
                net = layers.LeakyReLU(alpha=.001)(net)
            else:
                net_actions = layers.Activation(activation=self.activation)(net_actions)
            if self.dropout:
                net_actions = layers.Dropout(self.dropoutRate)(net_actions)

        # Combine state and action pathways
        net = layers.Add()([net_states, net_actions])
        if self.activation == 'leakyRelu':
            net = layers.LeakyReLU(alpha=.001)(net)
        else:
            net = layers.Activation('relu')(net)
        if self.dropout:
            net = layers.Dropout(self.dropoutRate)(net)
        

        # Add more layers to the combined network if needed
        for nUnits in self.architecture[2]:
            net = layers.Dense(units=nUnits, activity_regularizer=regularizers.l2(l2Lambda))(net)
            if self.batchNormalization:
                net = layers.BatchNormalization()(net)
            if self.activation == 'leakyRelu':
                net = layers.LeakyReLU(alpha=.001)(net)
            else:
                net = layers.Activation(activation=self.activation)(net)
            if self.dropout:
                net = layers.Dropout(self.dropoutRate)(net)
                                   
        # Add final output layer to produce action values (Q values)
        Q_values = layers.Dense(units=1, kernel_initializer=initializers.RandomUniform(minval=initMin, maxval=initMax, seed=initSeed), name='q_values')(net)

        # Create Keras model
        self.model = models.Model(inputs=[states, actions], outputs=Q_values)
        
        plot_model(self.model, to_file='criticKeras.png', show_shapes=True)
        
        # Define optimizer and compile model for training with built-in loss function
        optimizer = optimizers.Adam(lr=self.learningRate)
        self.model.compile(optimizer=optimizer, loss='mse')

        # The final output of this model is the Q-value for any given (state, action) pair.
        # However, we also need to compute the gradient of this Q-value with respect
        # to the corresponding action vector, needed for training the actor model.
        # This step needs to be performed explicitly, and a separate function needs
        # to be defined to provide access to these gradients.
        # Compute action gradients (derivative of Q values w.r.t. to actions)
        action_gradients = K.gradients(Q_values, actions)

        # Define an additional function to fetch action gradients (to be used by actor model)
        self.get_action_gradients = K.function(
            inputs=[*self.model.input, K.learning_phase()],
            outputs=action_gradients)
Exemplo n.º 26
0
X_train, X_val, Y_train, Y_val = train_test_split(train_image, train_label, test_size = 0.1, random_state=42)

train_datagen = ImageDataGenerator(rescale = 1./255.,
                                   rotation_range = 10,
                                   width_shift_range = 0.25,
                                   height_shift_range = 0.25,
                                   shear_range = 0.1,
                                   zoom_range = 0.25,
                                   horizontal_flip = False)

test_datagen = ImageDataGenerator(rescale = 1./255.)

model = models.Sequential()
model.add(layers.Conv2D(64, (3,3), padding = 'same', input_shape = (28,28,1)))
model.add(layers.BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.Conv2D(64, (3,3), padding = 'same'))
model.add(layers.BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))

model.add(layers.MaxPool2D(2,2))
model.add(layers.Dropout(0.2))

model.add(layers.Conv2D(128, (3,3), padding = 'same'))
model.add(layers.BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.Conv2D(128, (3,3), padding = 'same'))
model.add(layers.BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))

model.add(layers.MaxPool2D(2,2))
Exemplo n.º 27
0
    def call(self, inputs):
        x = self.conv(inputs)
        x = self.bn(x)
        x = layers.LeakyReLU(alpha=0.3)(x)

        return x
conv10 = Residual19(16, 1, conv10)
conv11 = Conv2D(1, 1, activation='sigmoid', name='Output')(conv10)

conv1r = _conv_bn_relu(filters=64, kernel_size=(7, 7), strides=(1, 1))(input)

block1 = _residual_block(basic_block,
                         filters=64,
                         repetitions=1,
                         is_first_layer=True)(conv1r)
block1concat = keras.layers.Concatenate()([block1, conv9])
block1se = squeeze_excite_block(block1concat)
block1conv1 = Conv2D(64, (1, 1),
                     padding='same',
                     kernel_initializer='he_normal')(block1se)
block1conv1 = BatchNormalization(axis=CHANNEL_AXIS)(block1conv1)
block1conv1 = layers.LeakyReLU()(block1conv1)
block1conv2 = Conv2D(64, (3, 3),
                     padding='same',
                     kernel_initializer='he_normal')(block1conv1)
block1conv2 = BatchNormalization(axis=CHANNEL_AXIS)(block1conv2)
block1conv2 = layers.LeakyReLU()(block1conv2)
block1conv3 = Conv2D(64, (1, 1),
                     padding='same',
                     kernel_initializer='he_normal')(block1conv2)
block1conv3 = BatchNormalization(axis=CHANNEL_AXIS)(block1conv3)
block1conv3 = layers.LeakyReLU()(block1conv3)
block1b = _residual_block(basic_block,
                          filters=64,
                          repetitions=1,
                          is_first_layer=False)(block1conv3)
Exemplo n.º 29
0
    def build_model(self):
        #lrelu = LeakyReLU(alpha=0.1)
        #Define input layers
        states = layers.Input(shape=(self.state_size, ), name="states")
        actions = layers.Input(shape=(self.action_size, ), name="actions")

        #Add hidden layers for state pathway
        net_states = layers.Dense(units=32, use_bias=False)(states)
        net_states = layers.BatchNormalization()(net_states)
        net_states = layers.LeakyReLU(alpha=0.1)(net_states)

        net_states = layers.Dense(units=64)(net_states)
        net_states = layers.BatchNormalization()(net_states)
        net_states = layers.LeakyReLU(alpha=0.1)(net_states)

        #hidden layers for action
        net_actions = layers.Dense(units=32)(actions)
        net_actions = layers.BatchNormalization()(net_actions)
        net_actions = layers.LeakyReLU(alpha=0.1)(net_actions)

        net_actions = layers.Dense(units=64)(net_actions)
        net_actions = layers.BatchNormalization()(net_actions)
        net_actions = layers.LeakyReLU(alpha=0.1)(net_actions)

        #try different layers sizes, activations, add batch normalization, regularizers, etc.

        #Combine state and action pathyways
        net = layers.Add()([net_states, net_actions])
        net = layers.BatchNormalization()(net)
        net = layers.LeakyReLU(alpha=0.1)(net)

        #Add more layers ot the combined network if needed

        #Add final output layer to produce action values (Q-values)
        # array?
        Q_values = layers.Dense(units=1, name='q_values')(net)

        #Create Keras model
        self.model = models.Model(inputs=[states, actions], outputs=Q_values)

        #Define optimizer and compile model for training with built-in loss function
        optimizer = optimizers.Adam(lr=self.critic_local_lr)
        self.model.compile(optimizer=optimizer, loss='mse')

        self.model.summary()

        #Compute action gradients (derivative of Q values w.r.t. to actions)
        action_gradients = K.gradients(Q_values, actions)  #dQ/dA
        """Q_values Tensor("q_values/BiasAdd:0", shape=(?, 1), dtype=float32) 
            actions Tensor("actions:0", shape=(?, 1), dtype=float32)"""
        print("Q_values", Q_values, "actions", actions)  #
        print("action_gradients", action_gradients)  #[None]
        print("action_gradients type", type(action_gradients))  #list
        #print("self.model.input", self.model.input) #states and actions
        """self.get_action_gradients can be called by agent's critic_local in def learn, action-grads used by action grads
        K.function runs the graph to get the Q-value necessary to calculate action_gradients which is the output
        call:
        action_gradients = np.reshape(self.critic_local.get_action_gradients([states, actions, 0]), \
            (-1, self.action_size))
        """
        self.get_action_gradients = K.function(
            inputs=[*self.model.input, K.learning_phase()],
            outputs=action_gradients)
        print("self.get_action_gradients", self.get_action_gradients)
Exemplo n.º 30
0
    # Define model
    logger.info('Constructing model...')

    input_shape = 1, MEL_BANDS, SEGMENT_LENGTH  # , SEGMENT_LENGTH

    # model = keras.models.Sequential()

    print("pre")
    model = keras.models.Sequential([
        layers.Input(shape=input_shape),
        layers.Reshape((100, 80, 1), input_shape=input_shape),
        layers.Conv2D(80, (3, 3),
                      kernel_regularizer=L2(0.001),
                      kernel_initializer='he_uniform',
                      activation="relu"),
        layers.LeakyReLU(),
        layers.MaxPool2D((3, 3), (3, 3)),
        layers.Conv2D(160, (3, 3),
                      kernel_regularizer=L2(0.001),
                      kernel_initializer='he_uniform',
                      activation="relu"),
        layers.LeakyReLU(),
        layers.MaxPool2D((3, 3), (3, 3)),
        layers.Conv2D(240, (3, 3),
                      kernel_regularizer=L2(0.001),
                      kernel_initializer='he_uniform',
                      activation="relu"),
        layers.LeakyReLU(),
        layers.MaxPool2D((3, 3), (3, 3)),
        layers.Flatten(),
        layers.Dropout(rate=0.5),