Skip to content

Instantly share code, notes, and snippets.

@Dok11
Last active September 14, 2019 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dok11/9e1b177570fbb41322e5778376a53dbe to your computer and use it in GitHub Desktop.
Save Dok11/9e1b177570fbb41322e5778376a53dbe to your computer and use it in GitHub Desktop.
Shared models for keras
import tensorflow as tf
from tensorflow.python.keras import Input, Model
from tensorflow.python.keras.layers import Conv2D, MaxPooling2D, Dropout, concatenate, \
Flatten, Dense
IMG_SHAPE = (60, 90, 1)
# Defined a shared model
shared_input = Input(IMG_SHAPE)
shared_layer = Conv2D(8, (7, 7), strides=3, input_shape=IMG_SHAPE, padding='valid', activation='relu')(shared_input)
shared_layer = MaxPooling2D(pool_size=(2, 2))(shared_layer)
shared_layer = Conv2D(16, (5, 5), strides=2, padding='same', activation='relu')(shared_layer)
shared_layer = MaxPooling2D(pool_size=(2, 2))(shared_layer)
shared_layer = Conv2D(32, (3, 3), padding='same', activation='relu')(shared_layer)
shared_layer = MaxPooling2D(pool_size=(2, 2))(shared_layer)
shared_model = Model(shared_input, shared_layer, name='shared_model')
# Define two input images
image_a = Input(IMG_SHAPE)
image_b = Input(IMG_SHAPE)
branch_a = shared_model(image_a)
branch_b = shared_model(image_b)
merged_layers = concatenate([branch_a, branch_b])
merged_layers = Flatten()(merged_layers)
merged_layers = Dense(1024, activation='relu')(merged_layers)
output = Dense(1, kernel_initializer='normal', activation='linear')(merged_layers)
model = Model(inputs=[image_a, image_b], outputs=output)
model.compile(optimizer=tf.keras.optimizers.Adam(0.00005), loss='mse', metrics=['mae'])
model.summary()
@Dok11
Copy link
Author

Dok11 commented Sep 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment