'Why is the distance between my encoding very small?

I'm a beginner trying to run triplet loss, but the model outputs a very small value for the distance between pictures, Before and after training. for example, the distance between a to n is 5.2625364e-06 before training and after 0.006740926. In addition, the output can look like 0.09876 so multiplying by 100 won't work... I'm pretty sure the problem is with my model or in the image to encoding function. In addition, I loaded a FRmodel and run it on my images, but instead of getting 0.2 output for example I got 9.34 or sometimes 11.846. It is important to mention my model got a 0.02 loss.

def make_model(input_shape):
  inputs = keras.Input(shape = input_shape)
  x = layers.Conv2D(64, 3,strides = 1, padding = 'same', activation = 'relu')(inputs)
  x = layers.MaxPooling2D()(x)
  x = layers.BatchNormalization()(x)
  x = layers.Dropout(0.2)(x)
   

  x = layers.Conv2D(128, 3,strides = 1, padding = 'same', activation = 'relu')(x)
  x = layers.MaxPooling2D()(x)
  x = layers.Dropout(0.3)(x)

  x = layers.Conv2D(256, 3,strides = 1, padding = 'same', activation = 'relu')(x)
  x = layers.MaxPooling2D()(x)
  x = layers.BatchNormalization()(x)
  x = layers.Dropout(0.3)(x)

  x = layers.Conv2D(512, 3,strides = 1, padding = 'same', activation = 'relu')(x)
  x = layers.BatchNormalization()(x)
  x = layers.MaxPooling2D()(x)
   
 
  x = layers.Dense(2048, activation='sigmoid')(x)
  x = layers.Dense(1024, activation='sigmoid')(x)
  x = layers.Dropout(0.2)(x)
  x = Flatten()(x)
  x = tf.keras.layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=1))(x)
  outputs = layers.Dense(128, activation = 'sigmoid')(x)
  return keras.Model(inputs, outputs)
triplet_model_anchor = Input((160,160, 3))
triplet_model_positive = Input((160, 160, 3))
triplet_model_negative = Input((160, 160, 3))
#triplet_model_out = triplet_loss([model(triplet_model_anchor), model(triplet_model_positive), model(triplet_model_negative)])
triplet_model_out = Concatenate()([model(triplet_model_anchor), model(triplet_model_positive), model(triplet_model_negative)])
triplet_model = keras.Model([triplet_model_anchor, triplet_model_positive, triplet_model_negative], [triplet_model_out])
def img_to_encoding(image_path, model):
  img2 = tf.keras.preprocessing.image.load_img(image_path, target_size = (160,160))
  img2 = np.around(np.array(img2) / 255.0, decimals=12)
  img2 = np.expand_dims(img2, 0)
  return model.predict(img2)

This is the distance between it pictures:

x = model.predict(true_signature['Itzik Yeyni'][0], 0))
y = model.predict(true_signature['Itzik Yeyni'][3], 0)
print(np.linalg.norm(x - y))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source