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 [2023/09/20 03:12] (current)
louis
Line 1: Line 1:
-====== Demo6 facenet ======+~~tag> NPU FaceNet Edge2 PyTorch~~ 
 + 
 +====== FaceNet PyTorch Edge2 Demo - 6 ====== 
 + 
 +{{indexmenu_n>6}}
  
 ===== Get Source Code ===== ===== Get Source Code =====
  
-The codes we use.+The codes we use [[gh>bubbliiiing/facenet-pytorch]].
  
 ```shell ```shell
Line 34: Line 38:
 ==== 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 45:
 ``` ```
  
-Install dependences and RKNN toolkit2 packages,+Install dependences and RKNN toolkit2 packages.
  
 ```shell ```shell
Line 51: Line 55:
 ``` ```
  
-==== 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 76:
 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 90:
 ``` ```
  
-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 103:
 # 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 111:
 # 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 119:
 # 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 126:
 ``` ```
  
-Run test.py to generate rknn model.+Run ''test.py'' to generate rknn model.
  
 ```shell ```shell
Line 132: Line 136:
 ==== 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 153:
 === 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