HamadYA / GhostFaceNets

This repository contains the official implementation of GhostFaceNets, State-Of-The-Art lightweight face recognition models.
https://ieeexplore.ieee.org/document/10098610
MIT License
194 stars 38 forks source link

Simple way to extract face Embeddings? #28

Closed vlavorini closed 1 year ago

vlavorini commented 1 year ago

Hello,

is there a simple way to extracts embeddings, in order to use the tool with Vecot DBs (reverse search, ect.)?

Thank you

HamadYA commented 1 year ago

Hello,

is there a simple way to extracts embeddings, in order to use the tool with Vecot DBs (reverse search, ect.)?

Thank you

Hi,

Please refer to the inference below:

https://github.com/HamadYA/GhostFaceNets/issues/12#issuecomment-1567254521

HamadYA commented 1 year ago

Hello,

is there a simple way to extracts embeddings, in order to use the tool with Vecot DBs (reverse search, ect.)?

Thank you

Or you can use:

import os import cv2 import numpy as np import pandas as pd import tensorflow as tf from tqdm import tqdm from sklearn.preprocessing import normalize from sklearn.metrics import roc_curve, auc

gpus = tf.config.experimental.list_physical_devices("GPU") for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) print(gpus)

class Eval_image: def init(self, model_interf, img_path, output_dir): if isinstance(model_interf, str) and model_interf.endswith("h5"): model = tf.keras.models.load_model(model_interf) self.model_interf = lambda imms: model((imms - 127.5) * 0.0078125).numpy() else: self.model_interf = model_interf self.dist_func = lambda aa, bb: np.dot(aa, bb) self.output_dir = output_dir self.img_path = img_path self.emb, self.img_class, self.filename = self.prepare_image_and_embedding(self.img_path, self.output_dir)

def prepare_image_and_embedding(self, img_path, output_dir):
    img_shape = (112, 112)
    img = cv2.imread(img_path)
    img = cv2.resize(img, img_shape, interpolation=cv2.INTER_AREA)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = np.expand_dims(img, axis=0)

    emb = self.model_interf(img)
    emb = normalize(np.array(emb).astype("float32"))[0]
    img_class = int(img_path.split('/')[-2])
    filename = os.path.basename(img_path)

    np.savez(output_dir, embs=emb, imm_classes=img_class, filenames=filename)

    return emb, img_class, filename

img_path = 'HR_Gallery_aligned_112_112' output_dir = 'res' model_file = '/home/mohamadalansari/Downloads/GhostFaceNets/checkpoints/GhostFaceNet_W1.3_S1_ArcFace.h5'

emb, img_class, filename = Eval_image(model_file, img_path, output_dir)