Triplet Loss in Keras/Tensorflow backend

1
2
import tensorflow as tf
tf.set_random_seed(1)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

1 Response

Hi, I'm using your code as pattern for my, as I'm trying to implement triplet loss with keras too. The main difference is that I'm using a sequential model, so I can not use "merge". My model looks like this:

def build_model(img_x, img_y):
input_shape = (img_x, img_y, 3)
reid_model = Sequential()
reid_model.add(Conv2D(32, kernel_size=(3, 3), strides=(1, 1),
activation='relu',
input_shape=input_shape))
reid_model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
reid_model.add(Flatten())
reid_model.add(Dense(512, activation='sigmoid'))

anchor = Input(shape=(128, 254, 3))
positive = Input(shape=(128, 254, 3))
negative = Input(shape=(128, 254, 3))

anchor_embed = reid_model(anchor)
positive_embed = reid_model(positive)
negative_embed = reid_model(negative)

dist_anchor_positive = distance(anchor_embed, positive_embed)
dist_anchor_negative = distance(anchor_embed, negative_embed)
loss = triplets_max(dist_anchor_positive, dist_anchor_negative, 0.05)

model = Model(inputs=[anchor, positive, negative], outputs=loss)
model.compile(optimizer='Adam', loss='mean_absolute_error')
return model

But I'm getting a ValueError: Output tensors to a Model must be the output of a Keras `Layer`. Any suggestion?

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.