[英]How to load pretrained model, created with insightface in python?
我正在尝试运行这段代码
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
还没有完成,主要是加载预训练的 model 我用 insightface 创建的https://github.com/deepinsight/insightface
但是有一个错误
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'
不知道该怎么办,我是新手,我什至不知道是否正确加载 model,我只有 2 个文件,model-0001.params 和 model-symbol.json
操作系统:Ubuntu 18.04 程序:Pycharm
最后我发现 OpenCV 不支持 M.net 模型,所以简单的解决方法是从 M.net 转换到 ONNX。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.