Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


products:sbc:edge2:npu:demos:facenet

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
products:sbc:edge2:npu:demos:facenet [2023/08/22 06:08]
louis created
products:sbc:edge2:npu:demos:facenet [2025/04/09 23:38] (current)
louis
Line 1: Line 1:
-====== Demo6 facenet ======+~~tag> NPU FaceNet Edge2 PyTorch~~
  
-===== Get Source Code =====+====== FaceNet PyTorch Edge2 Demo - 6 ======
  
-The codes we use.+{{indexmenu_n>6}} 
 + 
 +===== Introduction ===== 
 + 
 +FaceNet is a face recognition model. It will convert a face image into a feature map. Compare the feature map between image and face database. Here are two judgment indicators, cosine similarity and Euclidean distance. The closer the cosine similarity is to 1 and the closer the Euclidean distance is to 0, the more similar is between two faces. 
 + 
 +Here takes **lin_1.jpg** as example. Inference results on Edge2. 
 + 
 +{{:products:sbc:edge2:npu:demos:facenet-result.png?400|}} 
 + 
 +===== Train Model ===== 
 + 
 +The codes we use [[gh>bubbliiiing/facenet-pytorch]].
  
 ```shell ```shell
Line 34: Line 46:
 ==== Get convert tool ==== ==== Get convert tool ====
  
-Download Tool from [[https://github.com/rockchip-linux/rknn-toolkit2.git|Rockchip Github]].+Download Tool from [[gh>rockchip-linux/rknn-toolkit2]].
  
 ```shell ```shell
Line 41: Line 53:
 ``` ```
  
-Install dependences and RKNN toolkit2 packages,+Install dependences and RKNN toolkit2 packages.
  
 ```shell ```shell
Line 51: Line 63:
 ``` ```
  
-==== convert ====+==== Convert ====
  
 After training model, modify facenet-pytorch/nets/facenet.py as follows. After training model, modify facenet-pytorch/nets/facenet.py as follows.
  
-```shell+```diff
 diff --git a/nets/facenet.py b/nets/facenet.py diff --git a/nets/facenet.py b/nets/facenet.py
 index e7a6fcd..93a81f1 100644 index e7a6fcd..93a81f1 100644
Line 72: Line 84:
 Create a python file written as follows and run to convert model to onnx. Create a python file written as follows and run to convert model to onnx.
  
-```shell+```python export.py
 import torch import torch
 import numpy as np import numpy as np
Line 86: Line 98:
 ``` ```
  
-Enter rknn-toolkit2/examples/onnx/yolov5 and modify test.py as follows.+Enter ''rknn-toolkit2/examples/onnx/yolov5'' and modify ''test.py'' as follows.
  
-```shell+```python test.py
 # Create RKNN object # Create RKNN object
 rknn = RKNN(verbose=True) rknn = RKNN(verbose=True)
Line 99: Line 111:
 # Load ONNX model # Load ONNX model
 print('--> Loading model') print('--> Loading model')
-ret = rknn.load_onnx(model=./facenet.onnx)+ret = rknn.load_onnx(model='./facenet.onnx')
 if ret != 0: if ret != 0:
     print('Load model failed!')     print('Load model failed!')
Line 107: Line 119:
 # Build model # Build model
 print('--> Building model') print('--> Building model')
-ret = rknn.build(do_quantization=T rue, dataset=./dataset.txt)+ret = rknn.build(do_quantization=True, dataset='./dataset.txt')
 if ret != 0: if ret != 0:
     print('Build model failed!')     print('Build model failed!')
Line 115: Line 127:
 # Export RKNN model # Export RKNN model
 print('--> Export rknn model') print('--> Export rknn model')
-ret = rknn.export_rknn(./facenet.rknn)+ret = rknn.export_rknn('./facenet.rknn')
 if ret != 0: if ret != 0:
     print('Export rknn model failed!')     print('Export rknn model failed!')
Line 122: Line 134:
 ``` ```
  
-Run test.py to generate rknn model.+Run ''test.py'' to generate rknn model.
  
 ```shell ```shell
Line 132: Line 144:
 ==== Get source code ==== ==== Get source code ====
  
-Clone the source code form our [[https://github.com/khadas/edge2-npu|Github]].+Clone the source code from our [[gh>khadas/edge2-npu]].
  
 ```shell ```shell
Line 149: Line 161:
 === Picture input demo === === Picture input demo ===
  
-Put facenet.rknn in edge2-npu/C++/facenet/data/model.+Put ''facenet.rknn'' in ''edge2-npu/C++/facenet/data/model''.
  
 There are two modes of this demo. One is converting face images into feature vectors and saving vectors in face library. Another is comparing input face image with faces in library and outputting Euclidean distance and cosine similarity. There are two modes of this demo. One is converting face images into feature vectors and saving vectors in face library. Another is comparing input face image with faces in library and outputting Euclidean distance and cosine similarity.
  
-Put library faces in edge2-npu/C++/facenet/img and complie.+Put library faces in ''edge2-npu/C++/facenet/img'' and complie.
  
 ```shell ```shell
-// compile+# Compile
 $ bash build.sh $ bash build.sh
  
-// run mode 1+# Run mode 1
 $ cd install/facenet $ cd install/facenet
 $ ./facenet data/model/facenet.rknn 1 $ ./facenet data/model/facenet.rknn 1
 ``` ```
  
-After running mode 1, a file named face_feature_lib will generate in edge2-npu/C++/facenet. Had this file, you can run mode 2.+After running mode 1, a file named ''face_feature_lib'' will generate in ''edge2-npu/C++/facenet''. Had this file, you can run mode 2.
  
 ```shell ```shell
-// run mode 2+# Run mode 2
 $ ./facenet data/model/facenet.rknn data/img/lin_1.jpg $ ./facenet data/model/facenet.rknn data/img/lin_1.jpg
 ``` ```
  
Last modified: 2023/08/22 06:08 by louis