Skip to content

Instantly share code, notes, and snippets.

@bigsnarfdude
Last active February 14, 2019 00:23
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 bigsnarfdude/6140913bf8318b2e85032531292a4fca to your computer and use it in GitHub Desktop.
Save bigsnarfdude/6140913bf8318b2e85032531292a4fca to your computer and use it in GitHub Desktop.
keras two image input model
# coding: utf-8
# In[38]:
from keras.layers import Input, Concatenate # symbolic version
from keras.layers import Input, Conv2D, Flatten, Dense
from keras.models import Model
# In[53]:
"""
This model is a binary check of two images are the same image. YES or NO
"""
import keras
from keras.layers import Conv2D, MaxPooling2D, Input, Dense, Flatten
from keras.models import Model
digit_input = Input(shape=(27, 27, 1))
x = Conv2D(64, (3, 3))(digit_input)
x = Conv2D(64, (3, 3))(x)
x = MaxPooling2D((2, 2))(x)
out = Flatten()(x)
vision_model = Model(digit_input, out)
# Then define the tell-digits-apart model
digit_a = Input(shape=(27, 27, 1))
digit_b = Input(shape=(27, 27, 1))
# The vision model will be shared, weights and all
out_a = vision_model(digit_a)
out_b = vision_model(digit_b)
concatenated = keras.layers.concatenate([out_a, out_b])
out = Dense(1, activation='sigmoid')(concatenated) # same image?
classification_model = Model([digit_a, digit_b], out)
classification_model.summary()
# In[33]:
image_input1 = Input((32, 32, 3))
image_input2 = Input((32, 32, 3))
conv_layer1 = Conv2D(32, (3,3))(image_input1)
flat_layer1 = Flatten()(conv_layer1)
conv_layer2 = Conv2D(32, (3,3))(image_input2)
flat_layer2 = Flatten()(conv_layer2)
concat_layer= Concatenate()([flat_layer1, flat_layer2])
predictions = Dense(5, activation='softmax')(concat_layer)
# define a model with a list of two inputs
model = Model(inputs=[image_input1, image_input2], outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
#model.fit(data, labels)
# In[35]:
model.summary()
# In[32]:
model.fit(data, labels)
from keras.layers import Input, Concatenate, Conv2D, Flatten, Dense
from keras.models import Model
image_input1 = Input((32, 32, 3))
image_input2 = Input((32, 32, 3))
conv_layer1 = Conv2D(32, (3,3))(image_input1)
flat_layer1 = Flatten()(conv_layer1)
conv_layer2 = Conv2D(32, (3,3))(image_input2)
flat_layer2 = Flatten()(conv_layer2)
concat_layer= Concatenate()([flat_layer1, flat_layer2])
output = Dense(3)(concat_layer)
# define a model with a list of two inputs
model = Model(inputs=[image_input1, image_input2], outputs=output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment