4
votes
    from fr_utils import *
    from inception_blocks_v2 import *

    def triplet_loss(y_true, y_pred, alpha=0.3):
        """
        Implementation of the triplet loss as defined by formula (3)

        Arguments:
        y_pred -- python list containing three objects:
                anchor -- the encodings for the anchor images, of shape (None, 128)
                positive -- the encodings for the positive images, of shape (None, 128)
                negative -- the encodings for the negative images, of shape (None, 128)

        Returns:
        loss -- real number, value of the loss
        """

        anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]

        # Step 1: Compute the (encoding) distance between the anchor and the positive, you will need to sum over axis=-1
        pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis=-1)
        # Step 2: Compute the (encoding) distance between the anchor and the negative, you will need to sum over axis=-1
        neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis=-1)
        # Step 3: subtract the two previous distances and add alpha.
        basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), alpha)
        # Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
        loss = tf.reduce_sum(tf.maximum(basic_loss, 0.0))

        return loss

    def main():
        FRmodel = faceRecoModel(input_shape=(3, 96, 96))
        FRmodel.compile(optimizer='adam', loss=triplet_loss, metrics=['accuracy'])
        FRmodel.save('face-rec_Google.h5')
        print_summary(model)

    main()

The error shown in this code is as follows

Got inputs shapes: %s' % (input_shape))

ValueError: A Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 128, 12, 192), (None, 32, 12, 192), (None, 32, 12, 102), (None, 64, 12, 192)]

I try looking on the internet for he error but did not find a solution

1
It means the tensors you are trying to concatenate with each other must have the same shape except for the last axis. For example, the first tensor has shape (None, 128, 12, 192) and the second has a shape of (None, 32, 12, 192). So the second axis in these two tensor are not equal: 128 != 32. The third axis in all of your tensors are 12, so that's fine. But the second axis in all of them must be equal as well but they are 128, 32, 32, 64. - today
new error: Invalid reduction dimension 3 for input with 2 dimensions. for 'lambda_2/l2_normalize/Sum' (op: 'Sum') with input shapes: [?,128], [] and with computed input tensors: input[1] = <3>. - Sanjeev
Without seeing your complete model code it is very hard to tell what's wrong. Further, this seems to be another issue which you means you need to ask a new question. - today

1 Answers

1
votes

You just need to change representation for your images. I guess, that your images are represented as 3-dimensional array [rows][cols][channels] where color channel is in last dimension. This code will move color channel into first dimension [channels][rows][cols]:

from keras import backend as K
K.set_image_data_format('channels_first')