【问题标题】:How to load pretrained model, created with insightface in python?如何加载在python中使用insightface创建的预训练模型?
【发布时间】:2020-10-29 02:35:02
【问题描述】:

我正在尝试运行此代码

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

还没有完成,重点是加载我用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'

不知道该怎么办,我是新手,我什至不知道是否正确加载模型,我只有 2 个文件,model-0001.params 和 model-symbol.json

操作系统:Ubuntu 18.04 程序:Pycharm

【问题讨论】:

  • 请提供预期的minimal, reproducible example。您的大部分代码都在错误点之后——它没有被执行,因此与问题无关。 InsightFace 给出了加载模型的示例?请提供文件存在的证据——也许在打开它之前检查它。

标签: python numpy


【解决方案1】:

最后我发现 OpenCV 不支持 MXnet 模型,所以简单的解决方法是从 MXnet 转换为 ONNX。

【讨论】:

    猜你喜欢
    • 2021-02-19
    • 2016-08-17
    • 1970-01-01
    • 2021-07-16
    • 2020-02-07
    • 2019-07-17
    • 1970-01-01
    • 2019-09-09
    • 2017-08-19
    相关资源
    最近更新 更多