def build_triplet_model(self, shape, dimensions): net = self.build_embedding(shape, dimensions) # Receive 3 inputs # Decide which of the two alternatives is closest to the original # x - Original Image # x1 - Alternative 1 # x2 - Alternative 2 x = Input(shape=shape, name='x') x1 = Input(shape=shape, name='x1') x2 = Input(shape=shape, name='x2') # Get the embedded values net_anchor = net(x) net_positive = net(x1) net_negative = net(x2) # The Lamda layer produces output using given function. Here its Euclidean distance. positive_dist = subtract(net_anchor, net_positive) negative_dist = subtract(net_anchor, net_negative) positive_dist = norm(positive_dist) negative_dist = norm(negative_dist) # Compare out = Lambda(lambda a: a[0] - a[1])([positive_dist, negative_dist]) return Model(inputs=[x, x1, x2], outputs=out), x, net_anchor
def build_pretrained_model(self, shape, dimensions): # Using vgg16 pre-trained weights net = cifar10vgg(x_shape=shape, train=False).model for _ in range(7): net.pop() net.add(GlobalMaxPooling2D(name='gap')) net.add(Dense(dimensions, activation='relu')) #net.add(Lambda(lambda x: K.l2_normalize(x, axis=1))) #net = self.build_embedding(shape, dimensions) # Receive 3 inputs # Decide which of the two alternatives is closest to the original x = Input(shape=shape, name='x') x1 = Input(shape=shape, name='x1') x2 = Input(shape=shape, name='x2') # Get the embedded values net_anchor = net(x) net_positive = net(x1) net_negative = net(x2) # The Lamda layer produces output using given function. Here its Euclidean distance. positive_dist = subtract(net_anchor, net_positive) negative_dist = subtract(net_anchor, net_negative) positive_dist = norm(positive_dist) negative_dist = norm(negative_dist) # Compare out = Lambda(lambda a: a[0] - a[1])([positive_dist, negative_dist]) return Model(inputs=[x, x1, x2], outputs=out), x, net_anchor
def build_triplets_model_test(self, shape, dimensions): net = self.build_embedding(shape, dimensions) # Receive 3 inputs # Decide which of the two alternatives is closest to the original # x - Original Image # x1 - Alternative 1 # x2 - Alternative 2 x = Input(shape=shape, name='x') x1 = Input(shape=shape, name='x1') x2 = Input(shape=shape, name='x2') # Get the embedded values e = net(x) e1 = net(x1) e2 = net(x2) #e = BN()(e) #e1 = BN()(e1) #e2 = BN()(e2) # Get the differences d1 = subtract(e, e1) d2 = subtract(e, e2) # Normalize the differences n1 = norm(d1) n2 = norm(d2) # Compare out = Activation('sigmoid')(subtract(n2, n1)) return Model(inputs=[x, x1, x2], outputs=[out, e, e1, e2, d1, d2, n1, n2])