簡體   English   中英

如何加載在 python 中使用 insightface 創建的預訓練 model?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM