Created
March 6, 2021 14:58
-
-
Save AugustoBarros/2bd40d7fddf10f94d217d8601d21d601 to your computer and use it in GitHub Desktop.
rafael
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.applications import MobileNet | |
# Construindo a SNN | |
input_shape=(224,224, 3) # formato das imagens de entrada | |
left_input = Input(input_shape) | |
right_input = Input(input_shape) | |
# inicializando os kernels | |
W_init = keras.initializers.RandomNormal(mean = 0.0, stddev = 1e-2) | |
# inicializando os bias | |
b_init = keras.initializers.RandomNormal(mean = 0.5, stddev = 1e-2) | |
# declarando as camadas da SNN | |
model = keras.models.Sequential([ | |
MobileNet(include_top=False,input_shape=input_shape), | |
keras.layers.Flatten(), | |
keras.layers.Dense(256, activation='sigmoid', kernel_initializer=W_init, bias_initializer=b_init) | |
]) | |
model.layers[0].trainable = False | |
# pegando a saída (mapa de features) da rede do lado esquerdo | |
encoded_l = model(left_input) | |
# pegando a saída (mapa de features) da rede do lado direito | |
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]) | |
# camada que calcula a diferença entre os mapas de características | |
#subtracted = keras.layers.Subtract()([encoded_l, encoded_r]) | |
# camada que faz a predição de ser ou não da mesma categoria | |
prediction = Dense(1, activation='sigmoid', bias_initializer=b_init)(L1_distance) | |
siamese_net = Model([left_input, right_input], prediction) | |
# compilando o modelo | |
optimizer= Adam(learning_rate=0.0006) | |
siamese_net.compile(loss='binary_crossentropy', optimizer=optimizer) | |
# plotando o modelo | |
siamese_net.summary() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment