-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
53 lines (40 loc) · 1.7 KB
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# arcFace.py
from rknnlite.api import RKNNLite
from PIL import Image
import numpy as np
import os
# Initialize RKNN model
rknn = RKNNLite()
rknn.load_rknn('./model/arcface.rknn')
rknn.init_runtime()
# Preprocess the source image
img_src = Image.open('./dataset/testImg/0204.jpg')
img_src = img_src.resize((112, 112))
img_src = img_src.convert('RGB')
img_src = np.array(img_src).astype(np.float32)
img_src = img_src[np.newaxis, ...]
# Get inference for the source image
output_src = rknn.inference(inputs=[img_src], data_format='nhwc')[0][0]
# Directory containing target images
target_dir = './dataset/testImg'
# Iterate over all .jpg files in the target directory
for filename in os.listdir(target_dir):
if filename.endswith('.jpg'):
# Preprocess each target image
img_target = Image.open(os.path.join(target_dir, filename))
img_target = img_target.resize((112, 112))
img_target = img_target.convert('RGB')
img_target = np.array(img_target).astype(np.float32)
img_target = img_target[np.newaxis, ...]
# Get inference for the target image
output_target = rknn.inference(inputs=[img_target],
# Format of the input data
data_format='nhwc')[0][0]
# Calculate the cosine similarity
cos_sim = np.dot(output_src, output_target) / (np.linalg.norm(output_src) * np.linalg.norm(output_target))
# # Calculate the L2 distance (Mean Squared Error)
mse_dist = np.linalg.norm(output_src - output_target)
# Print the results
print(f"File: {filename}")
print(f"Cosine Sim: {cos_sim}")
print(f"MSE Distance: {mse_dist}\n")