From 0eb1515e11361483e3c390168427c22824b84a7c Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Mon, 8 Jan 2024 16:59:20 +0000 Subject: [PATCH] avoid dimension imcompability error created pickle may have 2622 dimensional vectors but VGG-Face is not creating 4096 dimensional vectors. If they are mismatch, then raise a meaningful error --- deepface/DeepFace.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index 7a83587..4a157d3 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -616,6 +616,15 @@ def find( for index, instance in df.iterrows(): source_representation = instance[f"{model_name}_representation"] + target_dims = len(list(target_representation)) + source_dims = len(list(source_representation)) + if target_dims != source_dims: + raise ValueError( + "Source and target embeddings must have same dimensions but " + + f"{target_dims}:{source_dims}. Model structure may change" + + " after pickle created. Delete the {file_name} and re-run." + ) + if distance_metric == "cosine": distance = dst.findCosineDistance(source_representation, target_representation) elif distance_metric == "euclidean": @@ -636,6 +645,7 @@ def find( threshold = dst.findThreshold(model_name, distance_metric) result_df = result_df.drop(columns=[f"{model_name}_representation"]) + # pylint: disable=unsubscriptable-object result_df = result_df[result_df[f"{model_name}_{distance_metric}"] <= threshold] result_df = result_df.sort_values( by=[f"{model_name}_{distance_metric}"], ascending=True