简体   繁体   中英

How to load pretrained model, created with insightface in python?

I am trying to run this code

from deepface.commons import functions
import cv2
import numpy as np


model = cv2.dnn.readNetFromTorch("home/kaspis/Natrenovany/model-symbol.json")

img1_path = "home/kaspis/001.jpg"
img2_path = "home/kaspis/006.jpg"

img1 = functions.detectFace(img1_path, target_size=(96, 96))[0]
img2 = functions.detectFace(img2_path, target_size=(96, 96))[0]
img1_blob = cv2.dnn.blobFromImage(img1)
img2_blob = cv2.dnn.blobFromImage(img2)

model.setInput(img1_blob)
img1_representation = model.forward()

model.setInput(img2_blob)
img2_representation = model.forward()


def findCosineDistance(source_representation, test_representation):
 a = np.matmul(np.transpose(source_representation), test_representation)
 b = np.sum(np.multiply(source_representation, source_representation))
 c = np.sum(np.multiply(test_representation, test_representation))
 return 1 - (a / (np.sqrt(b) * np.sqrt(c)))


def findEuclideanDistance(source_representation, test_representation):
    euclidean_distance = source_representation - test_representation


    euclidean_distance = np.sum(np.multiply(euclidean_distance, euclidean_distance))
    euclidean_distance = np.sqrt(euclidean_distance)
    return euclidean_distance
    euclidean_distance = findEuclideanDistance(img1_representation[0], img2_representation[0])
    cosine_distance = findCosineDistance(l2_normalize(img1_representation[0]), l2_normalize(img2_representation[0]))

    if euclidean_distance < 0.60:
     return True
    else:
     return False

It's not done yet, main point is to load pretrained model I created with insightface https://github.com/deepinsight/insightface

But there is an error

File "/home/kaspis/PycharmProjects/pythonProject/main.py", line 6, in <module>
    model = cv2.dnn.readNetFromTorch("home/kaspis/Natrenovany/model-symbol.json")
cv2.error: OpenCV(4.4.0) /tmp/pip-req-build-6amqbhlx/opencv/modules/dnn/src/torch/THDiskFile.cpp:496: error: (-2:Unspecified error) cannot open <home/kaspis/Natrenovany/model-symbol.json> in mode r  in function 'THDiskFile_new'

Not sure what to do about it, am newbie, I dont even know if am loading model correctly, I have only 2 files, model-0001.params and model-symbol.json

OS: Ubuntu 18.04 Program: Pycharm

In the end I found out that OpenCV does not support M.net models, so easy fix is to convert from M.net to ONNX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM