【发布时间】:2021-12-09 19:58:47
【问题描述】:
您好,我正在将 arcface 模型从 python 导出到 python 中的 tensorflow.js 我得到以下预测:
-2.47331879e+02, 2.43775589e+02, 1.48011673e+02,....
但在 javascript 中,我得到了以下内容。
-247.3305206298828,243.7752685546875,148.0111541748047,....
但输入是同一张图片,唯一不同的是在 TensorFlow 中,我需要使用以下行将数组转换为张量:
face = tf.tensor(face[0][0])
为什么我得到不同的结果?
编辑
我正在使用此代码在 python 和 javascript 中将图像作为数组获取
Python 代码:
img = Image.open('file.jpg')
img = img.resize((112, 112), Image.ANTIALIAS)
img = np.expand_dims(img, axis=0)
img = tf.convert_to_tensor(img, dtype='int32')
图像保存在文件中,代码如下:
with open('data.txt', 'w') as outfile:
json.dump(np.array(img).tolist(), outfile)
并在 javascript 代码中粘贴变量。
Javascript 代码:
face = tf.tensor(face[0][0], [1, 112, 112, 3], 'int32')
const display = document.getElementById('display');
modelLayer.then(model => display.innerHTML =
model.predict(face).dataSync())
EDIT2
javascript预测和Python预测的欧式距离为0.021145982611487882
谢谢
【问题讨论】:
-
这些数字看起来非常接近,所以我猜这与 Python 与 JavaScript 中使用的数字的精度有关。
-
我也这么认为,但我想在两种语言中是否可以截断到相同的“精度”
标签: python tensorflow tensorflow.js