Ejemplo n.º 1
0
    def squeeze_excite_block(self, input_tensor, ratio=16):
        """ Create a channel-wise squeeze-excite block
        Args:
            input_tensor: input Keras tensor
            ratio: number of output filters
        Returns: a Keras tensor
        References
        -   [Squeeze and Excitation Networks](https://arxiv.org/abs/1709.01507)
        """
        init = input_tensor
        se_shape = (1, 1, self.depth)

        se = GlobalAveragePooling2D()(init)
        se = Reshape(se_shape)(se)
        se = Dense(self.depth // ratio,
                   activation='relu',
                   kernel_initializer='he_normal',
                   use_bias=False)(se)
        se = Dense(self.depth,
                   activation='sigmoid',
                   kernel_initializer='he_normal',
                   use_bias=False)(se)

        x = K.layers.multiply([init, se])
        return x
Ejemplo n.º 2
0
 def Generator(self):
     gmodel = tf.keras.models.Sequential(name="Generator")
     gmodel.add(
         Dense(8 * 8 * 2 * self.Ndeconvfilters[0],
               input_shape=(self.noise_vect, ),
               kernel_initializer=tf.keras.initializers.RandomNormal(
                   mean=0.0, stddev=0.02)))
     gmodel.add(BatchNormalization(epsilon=1e-5, momentum=0.9))
     gmodel.add(ReLU())
     gmodel.add(Reshape((8, 8, 2 * self.Ndeconvfilters[0])))
     for lyrIdx in range(4):
         gmodel.add(
             Conv2DTranspose(
                 self.Ndeconvfilters[lyrIdx],
                 5,
                 strides=2,
                 padding='same',
                 kernel_initializer=tf.keras.initializers.RandomNormal(
                     mean=0.0, stddev=0.02)))
         if lyrIdx == 3:
             gmodel.add(
                 Activation('tanh'))  # last layer has tanh activation
         else:
             gmodel.add(BatchNormalization(epsilon=1e-5, momentum=0.9))
             gmodel.add(ReLU())
     gmodel.summary()
     return gmodel
def Unet(img_height, img_width, nclasses=3, filters=64):
    # down
    input_layer = Input(shape=(img_height, img_width, 3), name='image_input')
    conv1 = conv_block(input_layer, nfilters=filters)
    conv1_out = MaxPooling2D(pool_size=(2, 2))(conv1)
    conv2 = conv_block(conv1_out, nfilters=filters * 2)
    conv2_out = MaxPooling2D(pool_size=(2, 2))(conv2)
    conv3 = conv_block(conv2_out, nfilters=filters * 4)
    conv3_out = MaxPooling2D(pool_size=(2, 2))(conv3)
    conv4 = conv_block(conv3_out, nfilters=filters * 8)
    conv4_out = MaxPooling2D(pool_size=(2, 2))(conv4)
    conv4_out = Dropout(0.5)(conv4_out)
    conv5 = conv_block(conv4_out, nfilters=filters * 16)
    conv5 = Dropout(0.5)(conv5)
    # up
    deconv6 = deconv_block(conv5, residual=conv4, nfilters=filters * 8)
    deconv6 = Dropout(0.5)(deconv6)
    deconv7 = deconv_block(deconv6, residual=conv3, nfilters=filters * 4)
    deconv7 = Dropout(0.5)(deconv7)
    deconv8 = deconv_block(deconv7, residual=conv2, nfilters=filters * 2)
    deconv9 = deconv_block(deconv8, residual=conv1, nfilters=filters)
    # output
    output_layer = Conv2D(filters=nclasses, kernel_size=(1, 1))(deconv9)
    output_layer = BatchNormalization()(output_layer)
    output_layer = Reshape(
        (img_height * img_width, nclasses),
        input_shape=(img_height, img_width, nclasses))(output_layer)
    output_layer = Activation('softmax')(output_layer)

    model = Model(inputs=input_layer, outputs=output_layer, name='Unet')
    print('Created and loaded model')
    return model
Ejemplo n.º 4
0
    def __init__(self):
        #self.inception = InceptionResNetV2(weights=None, include_top=True)
        #self.inception.load_weights('/data/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5')
        #inception.graph = tf.get_default_graph()

        embed_input = Input(shape=(1000,))
        encoder_input = Input(shape=(256, 256, 1,))
        #encoder_output=GaussianNoise(0.1)(encoder_input)
        encoder_output = Conv2D(64, (3, 3), activation='relu', padding='same', strides=2)(encoder_input)
        encoder_output = Conv2D(128, (3, 3), activation='relu', padding='same')(encoder_output)
        encoder_output = Conv2D(128, (3, 3), activation='relu', padding='same', strides=2)(encoder_output)
        encoder_output = Conv2D(256, (3, 3), activation='relu', padding='same')(encoder_output)
        encoder_output = Conv2D(256, (3, 3), activation='relu', padding='same', strides=2)(encoder_output)
        encoder_output = Conv2D(512, (3, 3), activation='relu', padding='same')(encoder_output)
        encoder_output = Conv2D(512, (3, 3), activation='relu', padding='same')(encoder_output)
        encoder_output = Conv2D(256, (3, 3), activation='relu', padding='same')(encoder_output)  # Fusion

        fusion_output = RepeatVector(32 * 32)(embed_input)
        fusion_output = Reshape(([32, 32, 1000]))(fusion_output)
        fusion_output = concatenate([encoder_output, fusion_output], axis=3)
        fusion_output = Conv2D(256, (1, 1), activation='relu', padding='same')(fusion_output)  # Decoder
        decoder_output = Conv2D(128, (3, 3), activation='relu', padding='same')(fusion_output)

        decoder_output = UpSampling2D((2, 2))(decoder_output)
        decoder_output = Conv2D(64, (3, 3), activation='relu', padding='same')(decoder_output)
        decoder_output = UpSampling2D((2, 2))(decoder_output)
        decoder_output = Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_output)
        decoder_output = Conv2D(16, (3, 3), activation='relu', padding='same')(decoder_output)
        decoder_output = Conv2D(2, (3, 3), activation='tanh', padding='same')(decoder_output)
        decoder_output = UpSampling2D((2, 2))(decoder_output)

        model = Model(inputs=[encoder_input, embed_input], outputs=decoder_output)
        model.compile(optimizer="adagrad", loss='mse')
        self.model = model
Ejemplo n.º 5
0
def mnist(data_format):

    # pylint: disable=no-member

    if data_format == 'channels_first':
        input_shape = [1, 28, 28]
    else:
        assert data_format == 'channels_last'
        input_shape = [28, 28, 1]

    return Sequential([
        Reshape(target_shape=input_shape, input_shape=(28 * 28, )),
        Conv2D(32,
               5,
               padding='same',
               data_format=data_format,
               activation=tf.nn.relu,
               kernel_initializer='random_uniform'),
        MaxPool2D((2, 2), (2, 2), padding='same', data_format=data_format),
        Conv2D(64,
               5,
               padding='same',
               data_format=data_format,
               activation=tf.nn.relu,
               kernel_initializer='random_uniform'),
        MaxPool2D((2, 2), (2, 2), padding='same', data_format=data_format),
        Flatten(),
        Dense(1024, activation=tf.nn.relu,
              kernel_initializer='random_uniform'),
        Dense(10, kernel_initializer='random_uniform')
    ])
Ejemplo n.º 6
0
	def build_forward_model(self, image_size, autoencoder, input_dim=2, output_channels=1, max_pool_size = 2, conv_size = 3, channels =1, code_size = 4):
		print ('building forward model...')
		#forward_model = Sequential()
		enc_layer_idx = self.getLayerIndexByName(autoencoder, 'encoded')

		# create fwd model layers, partly from autoencoder (decoder part
		cmd_fwd_inp = Input(shape=(input_dim,), name = 'fwd_input')
		x = Dense(code_size, name = 'fwd_dense_1', activation='relu')(cmd_fwd_inp)


		ims = 8
		first = True
		x = Dense(int(ims*ims), activation='relu')(x)
		x = Reshape(target_shape=(ims, ims, 1))(x) #-12
		while ims!=image_size:
			x = Conv2D(int(ims*ims*max_pool_size), (conv_size, conv_size), activation='relu', padding='same')(x)
			x = UpSampling2D((max_pool_size, max_pool_size))(x)
			ims = ims*max_pool_size
		decoded = Conv2D(channels, (conv_size, conv_size), padding='same', name= 'decoded')(x)
		fwd_model = Model(cmd_fwd_inp, decoded)
		fwd_model.compile(optimizer='adam', loss='mse')

		print ('forward model')
		fwd_model.summary()
		return fwd_model
Ejemplo n.º 7
0
def generator(input_dim=INPUT_DIM_START, units=START_NN_NEURONCOUNT, activation='relu'):
    
    model = Sequential()
    model.add(Dense(input_dim=input_dim, units=units))
    model.add(BatchNormalization())
    model.add(Activation(activation))
    denselayerneuroncount = CHANNEL_OF_IMAGE*CHANNEL_COEFFICIENT*2*(WIDTH_OF_IMAGE//4)*(HEIGHT_OF_IMAGE//4)
    model.add(Dense(denselayerneuroncount))
    
    model.add(BatchNormalization())
    model.add(Activation(activation))
    model.add(Reshape(((WIDTH_OF_IMAGE//4), (HEIGHT_OF_IMAGE//4), CHANNEL_OF_IMAGE*CHANNEL_COEFFICIENT*2), input_shape=(denselayerneuroncount,)))
    '''
    burada artık elimizde küçük bir matris var çünkü 7*7*128(kanal sayısı) lik bir matris var 
    bunu büyütmek için ise upsampling yapmak gerekli
    
    '''
    model.add(UpSampling2D((UPSAMPLINGRANGE, UPSAMPLINGRANGE)))
    #bu aşamadan sonra matris artık 14*14*128 olacak
    model.add(Conv2D(CHANNEL_COEFFICIENT*CHANNEL_OF_IMAGE, (CONVOLUTIONRANGE, CONVOLUTIONRANGE), padding='same'))
    #bu aşama derinliği yani kanal sayısını etkiler artık matrisin boyutu 14*14*64 oldu
    model.add(BatchNormalization())
    model.add(Activation(activation))
    model.add(UpSampling2D((UPSAMPLINGRANGE, UPSAMPLINGRANGE)))
    #bu aşamadan sonra matrisin boyutu 28*28*64 oldu 
    model.add(Conv2D(CHANNEL_OF_IMAGE, (CONVOLUTIONRANGE, CONVOLUTIONRANGE), padding='same'))
    #bu aşamadan sonra ise matrisin boyutu 28*28*1 oldu
    model.add(Activation('tanh'))
    #tanh olmasının sebebi de 0 ile 1 arasında çıkmasını sağlamak içindir
    print(model.summary())
    return model
Ejemplo n.º 8
0
Archivo: ACGAN.py Proyecto: fernadn/GAN
def build_generator(channels, num_classes, latent_dim):

    model = Sequential()

    model.add(Dense(128 * 7 * 7, activation="relu", input_dim=latent_dim))
    model.add(Reshape((7, 7, 128)))
    #7x7x128
    model.add(BatchNormalization(momentum=0.8))
    model.add(UpSampling2D())
    #14x14x128
    model.add(Conv2D(128, kernel_size=3, padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(momentum=0.8))
    model.add(UpSampling2D())
    #28x28x128
    model.add(Conv2D(64, kernel_size=3, padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(momentum=0.8))
    #28x28x64
    model.add(Conv2D(channels, kernel_size=3, padding='same'))
    model.add(Activation("tanh"))
    #28x28x3

    model.summary()

    noise = Input(shape=(latent_dim, ))
    label = Input(shape=(1, ), dtype='int32')
    label_embedding = Flatten()(Embedding(num_classes, latent_dim)(label))

    model_input = multiply([noise, label_embedding])
    img = model(model_input)

    return Model([noise, label], img)
Ejemplo n.º 9
0
    def build_discriminator(self):
        '''
        This function is used to define the discriminator of model(DCGAN).
        '''
        model = Sequential()
        model.add(Reshape((self.img_size, self.img_size, self.img_channels)))
        model.add(Conv2D(32, kernel_size = 5, strides = 2, padding = 'same', use_bias = True, kernel_initializer = RandomNormal(stddev = 0.02)))
        model.add(LeakyReLU(alpha = 0.2))
        model.add(Dropout(rate = 0.25))
        model.add(Conv2D(64, kernel_size = 5, strides = 2, padding = 'same', use_bias = True, kernel_initializer = RandomNormal(stddev = 0.02)))
        #model.add(ZeroPadding2D(padding = ((0,1),(0,1))))
        model.add(BatchNormalization(momentum = 0.99))
        model.add(LeakyReLU(alpha = 0.2))
        model.add(Dropout(rate = 0.25))
        model.add(Conv2D(128, kernel_size = 5, strides = 2, padding = 'same', use_bias = True, kernel_initializer = RandomNormal(stddev = 0.02)))
        #model.add(ZeroPadding2D(padding = ((0,1),(0,1))))
        model.add(BatchNormalization(momentum = 0.99))
        model.add(LeakyReLU(alpha = 0.2))
        model.add(Dropout(rate = 0.25))
        model.add(Conv2D(128, kernel_size = 5, strides = 2, padding = 'same', use_bias = True, kernel_initializer = RandomNormal(stddev = 0.02)))
        #model.add(ZeroPadding2D(padding = ((0,1),(0,1))))
        model.add(BatchNormalization(momentum = 0.99))
        model.add(LeakyReLU(alpha = 0.2))
        model.add(Dropout(rate = 0.25))
        model.add(Flatten())
        model.add(Dense(1, activation = 'sigmoid'))



        img = Input(shape = (self.img_size, self.img_size, self.img_channels))
        output_ = model(img)

        return Model(img, output_)
Ejemplo n.º 10
0
def create_generator(input_shape):
    input_vec = Input(input_shape, name='input')
    x = Dense(128 * 4 * 4, activation='tanh', name='dense')(input_vec)
    x = BatchNormalization()(x)
    x = Reshape((4, 4, 128))(x)
    x = Conv2DTranspose(512, (3, 3),
                        strides=2,
                        activation=tf.nn.leaky_relu,
                        padding='same',
                        name='de_conv_1')(x)
    x = Conv2DTranspose(256, (3, 3),
                        strides=2,
                        activation=tf.nn.leaky_relu,
                        padding='same',
                        name='de_conv_2')(x)
    x = Conv2DTranspose(128, (3, 3),
                        strides=2,
                        activation=tf.nn.leaky_relu,
                        padding='same',
                        name='de_conv_3')(x)
    output = Conv2D(3, (3, 3),
                    activation='tanh',
                    padding='same',
                    name='conv_2')(x)

    model = Model(input_vec, outputs=output)

    return model
Ejemplo n.º 11
0
Archivo: dcgan.py Proyecto: Lynchez/GAN
def generator(input_dim=100,
              units=1024,
              activation='relu'
              ):  ##iput dim ilk başlangıç gürültü units ilk nöron sayısı
    model = Sequential()  ## ardışık model
    model.add(Dense(input_dim=input_dim, units=units))  ## layer oluşturma
    model.add(BatchNormalization())
    model.add(Activation(activation))
    model.add(Dense(128 * 7 * 7))  ## nöron sayısı veriyoruz
    model.add(BatchNormalization())
    model.add(Activation(activation))
    model.add(Reshape((7, 7, 128), input_shape=(128 * 7 * 7, )))
    model.add(UpSampling2D(
        (2, 2)))  ## en boy u arttırdık bunlar derinliği etkilemez
    model.add(Conv2D(64, (5, 5), padding='same')
              )  ## filtre olulturduk ve 14 14 128 lik matris üstünde gezdirdik
    model.add(BatchNormalization())
    model.add(Activation(activation))
    model.add(UpSampling2D((2, 2)))
    model.add(Conv2D(1, (5, 5),
                     padding='same'))  ## derinliği tekrar 1 eindirdik
    model.add(Activation('tanh'))  ## değerleri -1 1 arasına çeker
    print(model.summary())
    " generator ile bilgisayara görüntü üretmeyi öğretiyoruz  "
    return model
Ejemplo n.º 12
0
    def _build_decoder(img_shape, config):
        latent_vector = Input(config['latent_dim'])
        init_shape = tuple([
            get_initial_size(d, config['num_conv_blocks'])
            for d in img_shape[:-1]
        ] + [config['init_filters']])

        # CNN part
        #x = Dense(1024)(latent_vector)
        #x = LeakyReLU()(x)

        x = Dense(np.prod(init_shape))(latent_vector)
        x = BatchNormalization()(x)
        x = LeakyReLU()(x)
        x = Reshape(init_shape)(x)

        for i in range(config['num_conv_blocks']):
            x = decoder_deconv_block(filters=config['init_filters'] // (2**i),
                                     block_input=x,
                                     kernel_size=config['kernel_size'],
                                     strides=config['strides'])

        x = Conv2D(img_shape[-1], (2, 2), padding='same',
                   activation='sigmoid')(x)

        # ??why not passing as input the actual features tensor, output of the encoder??
        return Model(inputs=latent_vector, outputs=x)
Ejemplo n.º 13
0
    def build_models(self):

        input_state = Input(shape=self.state_shape, name='state_input')
        input_action = Input(shape=(self.action_size, ), name='action_input')
        model_inputs = [input_state, input_action]

        input_size = self.get_input_size(self.state_shape)
        input_reshaped = Reshape((input_size, ))(input_state)

        with tf.variable_scope(self.scope):

            states = Lambda(lambda x: self.discretize_states(x))(input_reshaped)
            actions = Lambda(lambda x: self.discretize_actions(x))(input_action)

            tt_shape = (self.part_size, ) * (self.state_shape[1] + 1)

            qt_layer = QTLayer(self.state_shape, tt_shape, self.part_size, self.tt_rank)

            q_values_sa = qt_layer([states, actions], mode='q_sa')
            q_values_s = qt_layer(states, mode='q_s')

            best_actions = Lambda(lambda x: tf.argmax(x, axis=1))(q_values_s)
            best_actions_cont = Lambda(lambda x: self.discretize_actions_back(x))(best_actions)

            model_critic = keras.models.Model(inputs=[input_state, input_action], outputs=q_values_sa)
            model_actor = keras.models.Model(inputs=[input_state], outputs=best_actions_cont)
            model_critic.summary()
            model_actor.summary()

        return model_critic, model_actor
Ejemplo n.º 14
0
def build(input_size, channels, latent_dim):
    layer_units = [512, 256]
    input_shape = (input_size, channels)
    drop_rate = 0.8
    inputs = Input(shape=input_shape)
    x = inputs
    x = Dropout(0.4, input_shape=(None, 978, 1))(x)
    for f in layer_units:
        x = Dense(f)(x)
        x = LeakyReLU(alpha=0.2)(x)

    x = Dropout(drop_rate, input_shape=(None, input_size, layer_units[1]))(x)
    shape = K.int_shape(x)
    x = Flatten()(x)
    latent = Dense(latent_dim, kernel_regularizer=regularizers.l2(1e-5),
                   activity_regularizer=regularizers.l1(1e-5))(x)
    #, kernel_regularizer=regularizers.l2(1e-5),
    #               activity_regularizer=regularizers.l1(1e-5)
    encoder = Model(inputs, latent, name="encoder")
    latent_inputs = Input(shape=(latent_dim,))
    x = Dense(shape[1] * shape[2])(latent_inputs)
    x = Reshape((shape[1], shape[2]))(x)
    for f in layer_units[::-1]:
        x = Dense(f)(x)
        x = LeakyReLU(alpha=0.2)(x)

    x = Dropout(drop_rate, input_shape=(None, input_size, layer_units[0]))(x)
    x = Dense(1)(x)
    outputs = Activation("tanh")(x)
    decoder = Model(latent_inputs, outputs, name="decoder")
    autoencoder = Model(inputs, decoder(encoder(inputs)), name="autoencoder")
    return autoencoder
Ejemplo n.º 15
0
def LeNetBuild(kernal=5,stride=1,filter=16):

    # Create an input layer which is similar to a feed_dict in TensorFlow.
    # Note that the input-shape must be a tuple containing the image-size.
    inputs = Input(shape=(img_size_flat,))

    # Variable used for building the Neural Network.
    net = inputs

    # The input is an image as a flattened array with 784 elements.
    # But the convolutional layers expect images with shape (28, 28, 1)
    net = Reshape(img_shape_full)(net)

    # First convolutional layer with ReLU-activation and max-pooling.
    net = Conv2D(kernel_size=kernal, strides=stride, filters=16, padding='same',
                 activation='relu', name='layer_conv1')(net)
    net = MaxPooling2D(pool_size=2, strides=2)(net)

    # Second convolutional layer with ReLU-activation and max-pooling.
    net = Conv2D(kernel_size=kernal, strides=stride, filters=36, padding='same',
                 activation='relu', name='layer_conv2')(net)
    net = MaxPooling2D(pool_size=2, strides=2)(net)

    # Flatten the output of the conv-layer from 4-dim to 2-dim.
    net = Flatten()(net)

    # First fully-connected / dense layer with ReLU-activation.
    net = Dense(128, activation='relu')(net)

    # Last fully-connected / dense layer with softmax-activation
    # so it can be used for classification.
    net = Dense(num_classes, activation='softmax')(net)

    # Output of the Neural Network.
    outputs = net



    #####################################################

    model2 = Model(inputs=inputs, outputs=outputs)
    ###################################################

    # COMPILE THE MODEL BY PASSING THE DATA TO THE BUILT MODEL

    model2.compile(optimizer='rmsprop',
                   loss='categorical_crossentropy',
                   metrics=['accuracy'])

    #TRAINING THE MODEL






    # EVALUATE THE MODEL

    return model2
Ejemplo n.º 16
0
def default_classification_model(num_classes,
                                 num_anchors,
                                 pyramid_feature_size=256,
                                 prior_probability=0.01,
                                 classification_feature_size=256,
                                 name='classification_submodel'):
    """Creates the default regression submodel.

    Args:
        num_classes: Number of classes to predict a score for at each feature level.
        num_anchors: Number of anchors to predict classification
            scores for at each feature level.
        pyramid_feature_size: The number of filters to expect from the
            feature pyramid levels.
        classification_feature_size: The number of filters to use in the layers
            in the classification submodel.
        name: The name of the submodel.

    Returns:
        A keras.models.Model that predicts classes for each anchor.
    """
    options = {
        'kernel_size': 3,
        'strides': 1,
        'padding': 'same',
    }

    if K.image_data_format() == 'channels_first':
        inputs = Input(shape=(pyramid_feature_size, None, None, None))
    else:
        inputs = Input(shape=(None, None, None, pyramid_feature_size))
    outputs = inputs
    for i in range(4):
        outputs = Conv3D(filters=classification_feature_size,
                         activation='relu',
                         name='pyramid_classification_{}'.format(i),
                         kernel_initializer=RandomNormal(mean=0.0,
                                                         stddev=0.01,
                                                         seed=None),
                         bias_initializer='zeros',
                         **options)(outputs)

    outputs = Conv3D(
        filters=num_classes * num_anchors,
        kernel_initializer=RandomNormal(mean=0.0, stddev=0.01, seed=None),
        bias_initializer=PriorProbability(probability=prior_probability),
        name='pyramid_classification',
        **options)(outputs)

    # reshape output and apply sigmoid
    if K.image_data_format() == 'channels_first':
        outputs = Permute((2, 3, 1),
                          name='pyramid_classification_permute')(outputs)
    outputs = Reshape((-1, num_classes),
                      name='pyramid_classification_reshape')(outputs)
    outputs = Activation('sigmoid',
                         name='pyramid_classification_sigmoid')(outputs)

    return Model(inputs=inputs, outputs=outputs, name=name)
Ejemplo n.º 17
0
def get_pooling_embed_dict(input_layer_dict,
                           sparse_feat_cls_list,
                           embedding_init_name='truncate_normal',
                           embedding_init_stddev=0.05,
                           embedding_size=16,
                           embedding_l1_reg=0.0,
                           embedding_l2_reg=0.0,
                           mask_zero=False,
                           pooling=None,
                           embedding_matrix=None,
                           suffix_name='default'):

    raw_embedding_dict = get_raw_embed_dict(
        input_layer_dict=input_layer_dict,
        sparse_feat_cls_list=sparse_feat_cls_list,
        embedding_init_name=embedding_init_name,
        embedding_init_stddev=embedding_init_stddev,
        embedding_size=embedding_size,
        embedding_l1_reg=embedding_l1_reg,
        embedding_l2_reg=embedding_l2_reg,
        mask_zero=mask_zero,
        suffix_name=suffix_name,
        embedding_matrix=embedding_matrix)

    pooling_embedding = []

    for index, feature_cls in enumerate(sparse_feat_cls_list):
        # only process the given sparse feature
        if feature_cls.name not in raw_embedding_dict.keys():
            continue
        if isinstance(feature_cls, SequenceFeature):
            if feature_cls.pooling in ('mean', 'sum', 'max', 'min'):
                if pooling is None:
                    pooling_output = Pooling(feature_cls.pooling)(
                        raw_embedding_dict[feature_cls.name])
                else:
                    pooling_output = Pooling(pooling)(
                        raw_embedding_dict[feature_cls.name])
            elif feature_cls.pooling == 'inner':
                query = feature_cls.query
                pooling_output = AttentionPooling()([
                    raw_embedding_dict[query],
                    raw_embedding_dict[feature_cls.name]
                ])
            elif feature_cls.pooling == 'mlp':
                query = feature_cls.query
                pooling_output = AttentionPooling(attention_type='mlp')([
                    raw_embedding_dict[query],
                    raw_embedding_dict[feature_cls.name]
                ])
        else:
            pooling_output = Pooling('sum')(
                raw_embedding_dict[feature_cls.name])
        pooling_embedding.append(
            Reshape((1, int(pooling_output.shape[-1])))(pooling_output))

    feature_name = list(raw_embedding_dict.keys())

    return OrderedDict(zip(feature_name, pooling_embedding))
Ejemplo n.º 18
0
def linear_dropout(shape=(120, 2 * 160)):
    drop = 0.1

    img_in = Input(shape=shape, name='img_in')
    x = img_in

    x = Reshape(target_shape=shape + (1, ))(x)

    x = BatchNormalization()(x)

    x = Conv2D(filters=24,
               kernel_size=(5, 5),
               strides=(2, 2),
               activation='relu')(x)
    x = Dropout(drop)(x)
    x = Conv2D(filters=32,
               kernel_size=(5, 5),
               strides=(2, 2),
               activation='relu')(x)
    x = Dropout(drop)(x)
    x = Conv2D(filters=64,
               kernel_size=(5, 5),
               strides=(2, 2),
               activation='relu')(x)
    x = Dropout(drop)(x)
    x = Conv2D(filters=64,
               kernel_size=(3, 3),
               strides=(2, 2),
               activation='relu')(x)
    x = Dropout(drop)(x)
    x = Conv2D(filters=64,
               kernel_size=(3, 3),
               strides=(1, 1),
               activation='relu')(x)
    x = Dropout(drop)(x)

    x = Flatten(name='flattened')(x)
    x = Dense(units=100, activation='linear')(x)
    x = Dropout(rate=.1)(x)
    x = Dense(units=50, activation='linear')(x)
    x = Dropout(rate=.1)(x)

    angle_out = Dense(units=1, activation='linear', name='angle_out')(x)

    throttle_out = Dense(units=1, activation='linear', name='throttle_out')(x)

    model = Model(inputs=[img_in], outputs=[angle_out, throttle_out])

    model.compile(optimizer='adam',
                  loss={
                      'angle_out': 'mean_squared_error',
                      'throttle_out': 'mean_squared_error'
                  },
                  loss_weights={
                      'angle_out': 0.5,
                      'throttle_out': 0.5
                  })

    return model
    def decode_embedding_input(latent, shape, name, large=False):
        conv1_size = 128 if large else 64

        latent = Reshape((1, INNER_SIZE))(latent)
        conv1 = Conv1D(conv1_size, (1,), activation='mish', padding='same', name=name + '_conv1')(latent)
        up1 = UpSampling1D(shape[0], name=name + '_up1')(conv1)
        conv2 = Conv1D(shape[1], (6,), activation='mish', padding='same', name=name + '_conv2')(up1)
        return conv2
    def create(self):
        """ Creates MoE model.

        Returns
        -------
        model: Model
            A Mixture of Experts model
        """

        inputs = Input(shape=self.input_shape)
        if self.units is not None:
            gate_activations = Dense(
                self.units, kernel_regularizer=self.kernel_regularizer)(inputs)
            gate_activations = Dense(
                self.num_classes * (self.num_experts + 1),
                kernel_regularizer=self.kernel_regularizer)(gate_activations)
        else:
            gate_activations = Dense(
                self.num_classes * (self.num_experts + 1),
                kernel_regularizer=self.kernel_regularizer)(inputs)

        expert_activations = Dense(
            self.num_classes * self.num_experts,
            kernel_regularizer=self.kernel_regularizer)(inputs)

        # (Batch * #Labels) x (num_experts + 1)
        gate_reshaped = Reshape(
            (self.num_classes, self.num_experts + 1))(gate_activations)
        gating_distribution = Activation('softmax')(gate_reshaped)

        # (Batch * #Labels) x num_experts
        expert_reshaped = Reshape(
            (self.num_classes, self.num_experts))(expert_activations)
        expert_distribution = Activation('sigmoid')(expert_reshaped)

        slice_gating = Lambda(lambda x: x[:, :, :self.num_experts])(
            gating_distribution)
        probs = Multiply()([slice_gating, expert_distribution])

        outputs = Lambda(lambda x: sum(x, axis=2))(probs)
        model = Model(inputs, outputs)

        if self.summary:
            model.summary()

        return model
Ejemplo n.º 21
0
 def __attention_3d_block(self, _lstm_output, _time_steps) -> Layer:
     """https://github.com/philipperemy/keras-attention-mechanism/blob/master/attention_lstm.py
     """
     att = Permute((2, 1))(_lstm_output)
     att = Reshape((_lstm_output.shape[2].value, _time_steps))(att)
     att = Dense(_time_steps, activation='softmax')(att)
     att_probs = Permute((2, 1), name='attention_vec')(att)
     return Multiply(name='attention_mul')([_lstm_output, att_probs])
Ejemplo n.º 22
0
def policy_layer(x, n=6):
    conv = Convolution2D(2, (1, 1))(x)
    norm = BatchNormalization()(conv)
    relu = Activation('relu')(norm)
    flat = Flatten()(relu)
    dense = Dense(n * n, activation='sigmoid')(flat)
    shaped = Reshape((n, n))(dense)
    return shaped
Ejemplo n.º 23
0
def bypass_image_features_graph_with_gap(image_feature_size, image_top_layer, image_top_layer_dropout_rate):
    image_size = int(np.sqrt(image_feature_size))
    vinput = Input((image_feature_size, 512), name="input_images")
    outputs = Reshape((image_size, image_size, 512))(vinput)
    outputs = GlobalAveragePooling2D(name="baseline_gap")(outputs)
    if image_top_layer:
        outputs = __add_top_layer(outputs, image_top_layer_dropout_rate)
    return outputs, vinput
Ejemplo n.º 24
0
def model_predictor(inputs, predictor_type, use_batch_norm):
    window_length = inputs.get_shape()[2]
    assert predictor_type in ['cnn', 'lstm'], 'type must be either cnn or lstm'
    assert window_length >= 3, 'window length must be at least 3'
    if predictor_type == 'cnn':
        net = Conv2D(filters=32,
                     kernel_size=(1, 3),
                     padding='same',
                     data_format='channels_last')(inputs)
        if use_batch_norm:
            net = BatchNormalization()(net)
        net = Activation("relu")(net)
        net = Conv2D(filters=32,
                     kernel_size=(1, window_length - 2),
                     padding='valid',
                     data_format='channels_last')(net)
        if use_batch_norm:
            net = BatchNormalization()(net)
        net = Activation("relu")(net)
        if DEBUG:
            print('After conv2d:', net.shape)
        net = Flatten()(net)
        if DEBUG:
            print('Output:', net.shape)
    elif predictor_type == 'lstm':
        num_stocks = inputs.get_shape()[1]
        window_length = inputs.get_shape()[2]
        features = inputs.get_shape()[3]

        hidden_dim = 32

        if DEBUG:
            print('Shape input:', inputs.shape)
        #net = Lambda(squeeze_middle2axes_operator, output_shape=squeeze_middle2axes_shape)(inputs)
        #net = Lambda(squeeze_first2axes_operator, output_shape=squeeze_first2axes_shape)(inputs)

        net = Lambda(lambda x: tf.transpose(x, [0, 2, 1, 3]))(inputs)
        if DEBUG:
            print('Shape input after transpose:', net.shape)

        #net = Reshape((window_length, features))(inputs)
        net = Reshape((window_length, num_stocks * features))(net)

        #net = tf.squeeze(inputs, axis=0)
        # reorder
        #net = tf.transpose(net, [1, 0, 2])
        if DEBUG:
            print('Reshaped input:', net.shape)
        net = LSTM(hidden_dim)(net)
        if DEBUG:
            print('After LSTM:', net.shape)
        #net = Reshape((num_stocks, hidden_dim))(inputs)
        if DEBUG:
            print('Output:', net.shape)
    else:
        raise NotImplementedError

    return net
Ejemplo n.º 25
0
def stresnet(c_conf=(3, 2, 32, 32), p_conf=(3, 2, 32, 32), t_conf=(3, 2, 32, 32),
             external_dim=8, nb_residual_unit=3, CF=64):
    '''
    C - Temporal Closeness
    P - Period
    T - Trend
    conf = (len_seq, nb_flow, map_height, map_width)
    external_dim
    '''

    # main input
    main_inputs = []
    outputs = []
    for conf in [c_conf, p_conf, t_conf]:
        if conf is not None:
            len_seq, nb_flow, map_height, map_width = conf
            input = Input(shape=(nb_flow * len_seq, map_height, map_width))
            main_inputs.append(input)
            # Conv1
            conv1 = Convolution2D(
                filters=CF, kernel_size=(3, 3), padding="same")(input)
            # [nb_residual_unit] Residual Units
            residual_output = ResUnits(_residual_unit, nb_filter=CF,
                                       repetations=nb_residual_unit)(conv1)
            # Conv2
            activation = Activation('relu')(residual_output)
            conv2 = Convolution2D(
                filters=nb_flow, kernel_size=(3, 3), padding="same")(activation)
            outputs.append(conv2)

    # parameter-matrix-based fusion
    if len(outputs) == 1:
        main_output = outputs[0]
    else:
        from BikeNYC.DST_network.ilayer import iLayer
        new_outputs = []
        for output in outputs:
            new_outputs.append(iLayer()(output))
        main_output = Add()(new_outputs)

    # fusing with external component
    if external_dim != None and external_dim > 0:
        # external input
        external_input = Input(shape=(external_dim,))
        main_inputs.append(external_input)
        embedding = Dense(units=10)(external_input)
        embedding = Activation('relu')(embedding)
        h1 = Dense(units=nb_flow * map_height * map_width)(embedding)
        activation = Activation('relu')(h1)
        external_output = Reshape((nb_flow, map_height, map_width))(activation)
        main_output = Add()([main_output, external_output])
    else:
        print('external_dim:', external_dim)

    main_output = Activation('tanh')(main_output)
    model = Model(input=main_inputs, output=main_output)

    return model
Ejemplo n.º 26
0
def get_cnn1d_tx_selu(output_size, img_height, img_width, show=True):
    model_input = Input(shape=(img_height * img_width, ), name='Main_input')
    x = Reshape((img_height, img_width), name='Reshape_1')(model_input)
    x = Permute((2, 1), name='Permute_1')(x)
    x = Conv1D(filters=256,
               kernel_size=48,
               strides=1,
               padding='same',
               activation='selu',
               name='Conv1D_selu_1')(x)
    x = Conv1D(filters=128,
               kernel_size=48,
               strides=1,
               padding='same',
               activation='selu',
               name='Conv1D_selu_2')(x)
    x = Conv1D(filters=64,
               kernel_size=48,
               strides=1,
               padding='same',
               activation='selu',
               name='Conv1D_selu_3')(x)
    x = Conv1D(filters=32,
               kernel_size=48,
               strides=1,
               padding='same',
               activation='selu',
               name='Conv1D_selu_4')(x)
    x = Conv1D(filters=16,
               kernel_size=48,
               strides=1,
               padding='same',
               activation='selu',
               name='Conv1D_selu_5')(x)
    x = Conv1D(filters=8,
               kernel_size=48,
               strides=1,
               padding='same',
               activation='selu',
               name='Conv1D_selu_6')(x)
    x = Flatten(name='Flatten_1')(x)
    x = Dense(512, activation='selu', name='Dense_selu_1')(x)
    x = Dense(512, activation='selu', name='Dense_selu_2')(x)
    x = Dense(512, activation='selu', name='Dense_selu_3')(x)
    cnn1d_output = Dense(output_size,
                         activation='linear',
                         name='Output_Dense_linear')(x)
    cnn1d = Model(inputs=model_input,
                  outputs=cnn1d_output,
                  name='CNN1D_Tx_selu')

    if show:
        print('CNN1D Tx summary:')
        cnn1d.summary()
        print()

    return cnn1d
Ejemplo n.º 27
0
 def Build(self):
     # input layer is from GlobalAveragePooling:
     inLayer = Input([self.latentSize])
     # reexpand the input from flat:
     net = Dense(self.intermediateSize, activation='relu')(inLayer)
     net = Dense(np.prod(self.inputShape), activation='sigmoid')(net)
     net = Reshape(self.inputShape)(net)
     
     return Model(inLayer, net)
Ejemplo n.º 28
0
def image_features_graph_flatten(output_layer, image_feature_size, input_shape,
                                 image_top_layer,
                                 image_top_layer_dropout_rate):
    inputs, outputs = __image_features_graph(output_layer, input_shape)
    outputs = Reshape((image_feature_size, 512),
                      name="flatten_filters")(outputs)
    if image_top_layer:
        outputs = __add_top_layer(outputs, image_top_layer_dropout_rate)
    return outputs, inputs
Ejemplo n.º 29
0
 def make_generator_model(self, noise_input, image_size):
     '''Generate the fake image using noise_input'''
     x = Dense(7*7*256, use_bias=False, kernel_initializer='glorot_uniform', spectral_normalization=True)(noise_input)
     x = Reshape((7, 7, 256))(x)
     x = ResizeImage(image_size, image_size)(x)
     x = Conv2D(3, (1, 1), strides=1, padding='SAME', spectral_normalization=True)(x)
     
     model = Model(noise_input, x)
     return model
Ejemplo n.º 30
0
    def __init__(self, game, encoder):
        """
        NNet model, copied from Othello NNet, with reduced fully connected layers fc1 and fc2 and reduced nnet_args.num_channels
        :param game: game configuration
        :param encoder: Encoder, used to encode game boards
        """
        from rts.src.config_class import CONFIG

        # game params
        self.board_x, self.board_y, num_encoders = game.getBoardSize()
        self.action_size = game.getActionSize()
        """
        num_encoders = CONFIG.nnet_args.encoder.num_encoders
        """
        num_encoders = encoder.num_encoders

        # Neural Net
        self.input_boards = Input(shape=(
            self.board_x, self.board_y,
            num_encoders))  # s: batch_size x board_x x board_y x num_encoders

        x_image = Reshape(
            (self.board_x, self.board_y, num_encoders)
        )(self.input_boards)  # batch_size  x board_x x board_y x num_encoders
        h_conv1 = Activation('relu')(BatchNormalization(axis=3)(Conv2D(
            CONFIG.nnet_args.num_channels, 3, padding='same', use_bias=False)(
                x_image)))  # batch_size  x board_x x board_y x num_channels
        h_conv2 = Activation('relu')(BatchNormalization(axis=3)(Conv2D(
            CONFIG.nnet_args.num_channels, 3, padding='same', use_bias=False)(
                h_conv1)))  # batch_size  x board_x x board_y x num_channels
        h_conv3 = Activation('relu')(
            BatchNormalization(axis=3)(Conv2D(CONFIG.nnet_args.num_channels,
                                              3,
                                              padding='valid',
                                              use_bias=False)(h_conv2))
        )  # batch_size  x (board_x-2) x (board_y-2) x num_channels
        h_conv4 = Activation('relu')(
            BatchNormalization(axis=3)(Conv2D(CONFIG.nnet_args.num_channels,
                                              3,
                                              padding='valid',
                                              use_bias=False)(h_conv3))
        )  # batch_size  x (board_x-4) x (board_y-4) x num_channels
        h_conv4_flat = Flatten()(h_conv4)
        s_fc1 = Dropout(CONFIG.nnet_args.dropout)(Activation('relu')(
            BatchNormalization(axis=1)(Dense(
                256, use_bias=False)(h_conv4_flat))))  # batch_size x 1024
        s_fc2 = Dropout(CONFIG.nnet_args.dropout)(Activation('relu')(
            BatchNormalization(axis=1)(Dense(
                128, use_bias=False)(s_fc1))))  # batch_size x 1024
        self.pi = Dense(self.action_size, activation='softmax',
                        name='pi')(s_fc2)  # batch_size x self.action_size
        self.v = Dense(1, activation='tanh', name='v')(s_fc2)  # batch_size x 1

        self.model = Model(inputs=self.input_boards, outputs=[self.pi, self.v])
        self.model.compile(
            loss=['categorical_crossentropy', 'mean_squared_error'],
            optimizer=Adam(CONFIG.nnet_args.lr))