def __init__(self, nspecies, n_hidden_precisions, inputs=None, hidden_activation=tf.nn.tanh): '''Initialize neural precisions layers''' self.nspecies = nspecies if inputs is None: inputs = self.nspecies + 1 inp = Dense(n_hidden_precisions, activation=hidden_activation, use_bias=True, name="prec_hidden", input_shape=(inputs, )) act_layer = Dense(4, activation=tf.nn.sigmoid, name="prec_act", bias_constraint=NonNeg()) deg_layer = Dense(4, activation=tf.nn.sigmoid, name="prec_deg", bias_constraint=NonNeg()) self.act = Sequential([inp, act_layer]) self.deg = Sequential([inp, deg_layer]) for layer in [inp, act_layer, deg_layer]: weights, bias = layer.weights variable_summaries(weights, layer.name + "_kernel", False) variable_summaries(bias, layer.name + "_bias", False)
def build(self, input_shapes): self.kernel1 = self.add_weight(name='kernel', shape=(1, 1, self.actors_size, 1), initializer=RandomUniform(minval=0, maxval=1, seed=None), trainable=True, constraint=NonNeg(), dtype='float64') self.kernel2 = self.add_weight(name='bias', shape=(1, ), initializer=RandomUniform(minval=0, maxval=1, seed=None), trainable=True, constraint=NonNeg(), dtype='float64') self.kernel3 = self.add_weight(name='beta', shape=(1, ), initializer=RandomUniform(minval=0, maxval=1, seed=None), trainable=True, constraint=NonNeg(), dtype='float64')
def _get_theta(self, params_local): """Return theta.""" theta = { # 'rho': tf.constant( # params_local['rho'], dtype=K.floatx(), name='rho' # ), # 'tau': tf.constant( # params_local['tau'], dtype=K.floatx(), name='tau' # ), # 'beta': tf.constant( # params_local['beta'], dtype=K.floatx(), name='beta' # ), # 'gamma': tf.constant( # params_local['gamma'], dtype=K.floatx(), name='gamma' # ), 'rho': tf.Variable(initial_value=params_local['rho'], trainable=False, constraint=GreaterEqualThan(min_value=1.), dtype=K.floatx(), name='rho'), 'tau': tf.Variable(initial_value=params_local['tau'], trainable=False, constraint=GreaterEqualThan(min_value=1.), dtype=K.floatx(), name='tau'), 'beta': tf.Variable(initial_value=params_local['beta'], trainable=False, constraint=GreaterEqualThan(min_value=1.), dtype=K.floatx(), name='beta'), 'gamma': tf.Variable(initial_value=params_local['gamma'], trainable=False, constraint=NonNeg(), dtype=K.floatx(), name='gamma'), 'phi': tf.Variable(initial_value=params_local['phi'], trainable=True, constraint=NonNeg(), dtype=K.floatx(), name='phi'), 'lambda_a': tf.Variable(initial_value=params_local['lambda_a'], trainable=True, constraint=NonNeg(), dtype=K.floatx(), name='lambda_a'), 'lambda_w': tf.Variable(initial_value=params_local['lambda_w'], trainable=True, constraint=NonNeg(), dtype=K.floatx(), name='lambda_w') } return theta
def _get_theta(self, params_local): """Return theta.""" # TODO theta = { 'rho': tf.Variable(initial_value=params_local['rho'], trainable=False, constraint=GreaterEqualThan(min_value=1.), dtype=K.floatx(), name='rho'), 'tau': tf.Variable(initial_value=params_local['tau'], trainable=False, constraint=GreaterEqualThan(min_value=1.), dtype=K.floatx(), name='tau'), 'beta': tf.Variable(initial_value=params_local['beta'], trainable=False, constraint=GreaterEqualThan(min_value=1.), dtype=K.floatx(), name='beta'), 'gamma': tf.Variable(initial_value=params_local['gamma'], trainable=False, constraint=NonNeg(), dtype=K.floatx(), name='gamma'), 'phi': tf.Variable(initial_value=params_local['phi'], trainable=True, constraint=NonNeg(), dtype=K.floatx(), name='phi'), 'alpha': tf.Variable(initial_value=params_local['alpha'], trainable=True, constraint=ProjectAttention(), shape=[self.n_dim], dtype=K.floatx(), name='alpha'), 'kappa': tf.Variable( initial_value=params_local['kappa'], trainable=True, constraint=NonNeg(), # TODO convexity constraint? shape=[self.n_dim], dtype=K.floatx(), name='kappa') } return theta
def __init__(self, units=1, input_dim=32): super(Lambda_parameter, self).__init__() w_init = tf.keras.initializers.Constant(value=0) self.w = tf.Variable(initial_value=w_init(shape=(units, 1), dtype="float32"), trainable=True, constraint=NonNeg())
def build(self, input_shape): self.bias = self.add_weight(name='{}_tau'.format(self.name), shape=(1, 1, 1), regularizer=reg.l2(l=self.reg_param), initializer="glorot_normal", constraint=NonNeg(), dtype=tf.float32, trainable=True) super(SoftThreshold, self).build(input_shape)
def build_model(dim): model = Sequential([ Dense( 1, kernel_initializer="he_uniform", kernel_constraint=NonNeg(), input_shape=(dim, ), ) ]) model.compile(optimizer="adam", loss="mse") return model
def build(self, input_shape): wr = (np.random.rand(self.kern, self.kern, self.shots)) wg = (np.random.rand(self.kern, self.kern, self.shots)) wb = (np.random.rand(self.kern, self.kern, self.shots)) wc = (np.random.rand(self.kern, self.kern, self.shots)) wt = wr + wg + wb + wc wr = tf.constant_initializer(tf.math.divide(wr, wt)) wg = tf.constant_initializer(tf.math.divide(wg, wt)) wb = tf.constant_initializer(tf.math.divide(wb, wt)) wc = tf.constant_initializer(tf.math.divide(wc, wt)) self.wr = self.add_weight(name='wr', shape=(1, self.kern, self.kern, 1, self.shots), initializer=wr, trainable=True, constraint=NonNeg()) self.wg = self.add_weight(name='wg', shape=(1, self.kern, self.kern, 1, self.shots), initializer=wg, trainable=True, constraint=NonNeg()) self.wb = self.add_weight(name='wb', shape=(1, self.kern, self.kern, 1, self.shots), initializer=wb, trainable=True, constraint=NonNeg()) self.wc = self.add_weight(name='wc', shape=(1, self.kern, self.kern, 1, self.shots), initializer=wc, trainable=True, constraint=NonNeg())
def create_bidirectional_lstm(train_shape: tuple, depth: int = 1, n_nodes: int = 10, loss='mse', compilation_kwargs={}): """ Helper function to create various bidirectional LSTM models. Parameters ---------- train_shape: Shape of the training data, should be following the use of the ``make_lookback`` function. depth: How deep to construct the BiLSTM n_nodes: How wide each layer of the BiLSTM should be loss: Loss function Returns ------- model: The compiled tensorflow model (using the sequential API) """ from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.layers import LSTM from tensorflow.keras.layers import Bidirectional from tensorflow.keras.constraints import NonNeg model = Sequential() if depth == 1: # If single layer, just create it model.add( Bidirectional(LSTM(n_nodes), input_shape=(train_shape[1], train_shape[2]))) else: # For deeper networks we need to set some additional parameters model.add( Bidirectional( LSTM(n_nodes, return_sequences=True), input_shape=(train_shape[1], train_shape[2]), )) for i in range(1, depth - 1): model.add(Bidirectional(LSTM(n_nodes, return_sequences=True))) model.add(Bidirectional(LSTM(n_nodes))) # Output layer is just one value model.add(Dense(1, activation='relu', kernel_constraint=NonNeg())) model.compile(loss=loss, optimizer='adam', metrics=list(make_metrics().values())) return model
def build(self, input_shape): # Defining parameters to optimize. if self.found_sigma: initializer = RandomUniform(minval=0.0001, maxval=1, seed=None) # initializer = Constant(0.84089642) self.sigma = self.add_weight(name='sigma', shape=(1, 1), initializer=initializer, constraint=NonNeg(), trainable=True) super(UnsharpMaskFixedLambda, self).build(input_shape) # Be sure to call this at the end
def build(self, input_shape): il = input_shape ''' We need to figure out what # expect input_shape to contain 2 items, [(batch, i1), (batch, i2, i3)] ''' self.kernel = self.add_weight(name='kernel', shape=(self.Num_of_cooperativities, 1), initializer=RandomUniform(minval=0, maxval=5, seed=None), constraint=NonNeg(), trainable=True, dtype='float64')
def build(self, input_shape): # Defining parameters to optimize. if self.found_sigma: self.sigma = self.add_weight(name='sigma', shape=(1, 1), initializer=Constant(0.2), regularizer=self.regularizer_sigma, constraint=NonNeg(), trainable=True) ''' self.sigma = self.add_weight(name='sigma', shape=(1,1), initializer=RandomUniform(minval=0.0001, maxval=1, seed=None), regularizer=self.kernel_regularizer_sigma, constraint = NonNeg(), trainable=True) ''' else: self.sigma = K.constant(np.array(self.sigma)) self.amount = self.add_weight(name='amount', shape=(1, 1), initializer=Constant(5), regularizer=self.regularizer_amount, constraint=MaxNorm(max_value=100, axis=0), trainable=True) ''' self.amount = self.add_weight(name='amount', shape=(1,1), initializer=RandomUniform(minval=0, maxval=10, seed=None), regularizer=self.kernel_regularizer_amount, constraint = MaxNorm(max_value=100, axis=0), trainable=True) ''' super(UnsharpMaskScikit, self).build(input_shape) # Be sure to call this at the end
def build(self, input_shape): if self.found_sigma: initializer_sigma = RandomUniform(minval=0.0001, maxval=1, seed=None) self.sigma = self.add_weight(name='sigma', shape=(1, 1), initializer=initializer_sigma, constraint=NonNeg(), trainable=self.found_sigma) if self.amount: print("Initializing amount as a constant value: %s" % self.amount) initializer_amount = Constant(self.amount) else: initializer_amount = RandomUniform(minval=0, maxval=1, seed=None) self.amount = self.add_weight(name='amount', shape=(1, 1), initializer=initializer_amount, constraint=None, trainable=True) super(UnsharpMaskLoG, self).build(input_shape)
def build(self, input_shape): # Defining parameters to optimize. if self.found_sigma: initializer = RandomUniform(minval=0.0001, maxval=1, seed=None) # initializer = Constant(0.84089642) self.sigma = self.add_weight(name='sigma', shape=(1, 1), initializer=initializer, constraint=NonNeg(), trainable=True) if self.amount: print("Initializing amount as a constant value: %s" % self.amount) initializer_amount = Constant(self.amount) else: initializer_amount = RandomUniform(minval=0, maxval=1, seed=None) self.amount = self.add_weight(name='amount', shape=(1, 1), initializer=initializer_amount, trainable=True) super(UnsharpMask, self).build(input_shape) # Be sure to call this at the end
def local_network_function(input_img): # encoder # input = 512 x 512 x number_img_in (wide and thin) conv1 = Conv2D(64, (3, 3), activation="relu", padding="same")( input_img ) # 512 x 512 x 32 pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) # 14 x 14 x 32 conv2 = Conv2D(128, (3, 3), activation="relu", padding="same")( pool1 ) # 256 x 256 x 64 pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) # 7 x 7 x 64# conv3 = Conv2D(256, (3, 3), activation="relu", padding="same")( pool2 ) # 128 x 128 x 128 (small and thick) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = Conv2D(512, (3, 3), activation="relu", padding="same")( pool3 ) # 128 x 128 x 128 (small and thick) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) conv5 = Conv2D(1024, (3, 3), activation="relu", padding="same")( pool4 ) # 128 x 128 x 128 (small and thick) # decoder up1 = UpSampling2D((2, 2))(conv5) # 14 x 14 x 128 conc_up_1 = Concatenate()([up1, conv4]) conv7 = Conv2D(512, (3, 3), activation="relu", padding="same")( conc_up_1 ) # 256 x 256 x 64 up2 = UpSampling2D((2, 2))(conv7) # 28 x 28 x 64 conc_up_2 = Concatenate()([up2, conv3]) conv8 = Conv2D(256, (3, 3), activation="relu", padding="same")( conc_up_2 ) # 512 x 512 x 1 up3 = UpSampling2D((2, 2))(conv8) # 28 x 28 x 64 conc_up_3 = Concatenate()([up3, conv2]) conv9 = Conv2D(128, (3, 3), activation="relu", padding="same")( conc_up_3 ) # 512 x 512 x 1 up4 = UpSampling2D((2, 2))(conv9) # 28 x 28 x 64 conc_up_4 = Concatenate()([up4, conv1]) conv10 = Conv2D(64, (3, 3), activation="relu", padding="same")( conc_up_4 ) # 512 x 512 x 1 decoded = Conv2D(1, (1, 1), activation=None, padding="same")( conv10 ) # 512 x 512 x 1 decoded = Conv2D( 1, (5, 5), activation=None, padding="same", use_bias=False, kernel_constraint=NonNeg(), kernel_regularizer=regularizers.l2(0.01), )( decoded ) # 512 x 512 x 1 return decoded
def get_siamese_model(name=None, input_shape=(224, 224, 3), embedding_vec_size=512, not_freeze_last=2): """ Model architecture """ if name == "InceptionV3": base_model = inception_v3.InceptionV3( weights='imagenet', include_top=False) model_preprocess_input = inception_v3.preprocess_input if name == "InceptionResNetV2": base_model = inception_resnet_v2.InceptionResNetV2( weights='imagenet', include_top=False) model_preprocess_input = inception_resnet_v2.preprocess_input if name == "DenseNet121": base_model = densenet.DenseNet121( weights='imagenet', include_top=False) model_preprocess_input = densenet.preprocess_input if name == "DenseNet169": base_model = densenet.DenseNet169( weights='imagenet', include_top=False) model_preprocess_input = densenet.preprocess_input if name == "DenseNet201": base_model = densenet.DenseNet201( weights='imagenet', include_top=False) model_preprocess_input = densenet.preprocess_input if name == "MobileNetV2": base_model = mobilenet_v2.MobileNetV2( weights='imagenet', include_top=False) model_preprocess_input = mobilenet_v2.preprocess_input if name == "MobileNet": base_model = mobilenet.MobileNet( weights='imagenet', include_top=False) model_preprocess_input = mobilenet.preprocess_input if name == "ResNet50": base_model = resnet50.ResNet50( weights='imagenet', include_top=False) model_preprocess_input = resnet50.preprocess_input if name == "VGG16": base_model = vgg16.VGG16( weights='imagenet', include_top=False) model_preprocess_input = vgg16.preprocess_input if name == "VGG19": base_model = vgg19.VGG19( weights='imagenet', include_top=False) model_preprocess_input = vgg19.preprocess_input if name == "Xception": base_model = xception.Xception( weights='imagenet', include_top=False) model_preprocess_input = xception.preprocess_input # Verifica se existe base_model if 'base_model' not in locals(): return ["InceptionV3", "InceptionResNetV2", "DenseNet121", "DenseNet169", "DenseNet201", "MobileNetV2", "MobileNet", "ResNet50", "VGG16", "VGG19", "Xception" ] # desativando treinamento for layer in base_model.layers[:-not_freeze_last]: layer.trainable = False x = base_model.layers[-1].output x = GlobalAveragePooling2D()(x) x = Dense( embedding_vec_size, activation='linear', # sigmoid? relu? name='embedding', use_bias=False )(x) model = Model( inputs=base_model.input, outputs=x ) left_input = Input(input_shape) right_input = Input(input_shape) # Generate the encodings (feature vectors) for the two images encoded_l = model(left_input) encoded_r = model(right_input) # Add a customized layer to compute the absolute difference between the encodings L1_layer = Lambda(lambda tensors: K.abs(tensors[0] - tensors[1])) L1_distance = L1_layer([encoded_l, encoded_r]) # Add a dense layer with a sigmoid unit to generate the similarity score prediction = Dense( 1, activation=Activation(gaussian), use_bias=False, kernel_constraint=NonNeg() )(L1_distance) # Connect the inputs with the outputs siamese_net = Model( inputs=[left_input, right_input], outputs=prediction ) return { "model": siamese_net, "preprocess_input": model_preprocess_input }