Open Shashankatyayan opened 4 years ago
You may get face features (512-dimensional array) from a Tom Hanks image. Get face features from another image. Measure the distance between them. If they are close enough, then Tom Hanks is probably depicted on the second image. See https://github.com/deepinsight/insightface/blob/master/deploy/test.py#L29
Hi @bartolootrit , I calculate cosine similarty using two image feature, its work fine but there are some problem some of the different image comparistion getting little bit closer. for example i used compare tom cruise and brad pitt then i got two embedded vectors use those vector i calculate cosine similarity, its approximatly 0.31390348 and compare between two different tom cruise faces ,i got 0.4873174. my question is how can calculate accurate similarity value between two images Need to compare with this image this master tom cruise image this is brad pitt image
@Jayanes
def test_distance(retina_dir, retina_name, model_dir, model_name, img_paths: list):
def get_distance(vec1, vec2):
return (
np.linalg.norm(vec1 - vec2),
abs(
1 -
vec1.dot(vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
)
)
# See RetinaFace/retinaface.py
face_detector = RetinaFace(os.path.join(retina_dir, retina_name), 0, -1, 'net3')
sym, arg_params, aux_params = mx.model.load_checkpoint(
os.path.join(model_dir, model_name),
0 # epoch
)
all_layers = sym.get_internals()
sym = all_layers['fc1_output']
ctx = mx.cpu()
model = mx.mod.Module(symbol=sym, label_names=[], context=ctx)
model.bind(
data_shapes=[('data', (1, 3, 112, 112))]
)
model.set_params(arg_params, aux_params)
features = []
for img_path in img_paths:
img = cv2.imread(img_path)
faces, landmarks = face_detector.detect(img, 0.8)
if faces is None:
raise Exception('No faces')
bbox = faces[0][0:4]
# See common/face_preprocess.py
nimg = preprocess(img, bbox, landmarks[0], image_size='112,112')
nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
face_aligned = np.transpose(nimg, (2, 0, 1))
model.forward(
mx.io.DataBatch(
data=(
mx.nd.array((face_aligned,)),
)
),
is_train=False
)
features.append(sklearn.preprocessing.normalize((model.get_outputs()[0].asnumpy()[0],))[0])
img_count = len(features)
for i in range(img_count - 1):
for j in range(i + 1, img_count):
print(img_paths[i], ' vs ', img_paths[j])
l2, cos = get_distance(features[i], features[j])
print('l2: ', l2, 'cos: ', cos)
test_distance(
'Retinaface',
'R50',
'LResNet100E-IR_ArcFace',
'model',
[
'won-braun1.jpg', 'won-braun2.jpg',
'korolov1.jpg', 'korolov2.jpg'
]
)
won-braun1.jpg vs won-braun2.jpg
l2: 0.8025358232022287 cos: 0.32203187376143927
won-braun1.jpg vs korolov1.jpg
l2: 1.333820825123439 cos: 0.8895389967664851
won-braun1.jpg vs korolov2.jpg
l2: 1.2872760437404656 cos: 0.828539806394052
won-braun2.jpg vs korolov1.jpg
l2: 1.326834418288402 cos: 0.8802447867773604
won-braun2.jpg vs korolov1.jpg
l2: 1.270593205498804 cos: 0.8072035469298626
korolov1.jpg vs korolov2.jpg
l2: 0.8487838952553743 cos: 0.3602170504224427
How can i make the pre-trained model return me the person name on basis of which the training has been made. For eg. if i am using the Tom Hanks image from the deploy folder, can i get the model return the name of Tom Hanks and corresponding embedding