Facenet和单图像正向传播

时间:2019-11-21 19:19:08

标签: tensorflow keras neural-network tensorflow-datasets facial-identification

我跳入了我的项目,但立即陷入困境。除了他们获取整个数据集并将其用于他们的模型之外,我在网上看不到关于正向传播的任何明确信息。

我的项目的想法是识别面部并比较其输出向量,因为Facenet的最后一层是面部嵌入的128维数组。在线用户只是传播数据集,但是出于我自己的实践考虑,我只想直接看到一张脸并查看其输出,但我不知道该怎么做。

到目前为止,这就是我所拥有的,而我再次陷入困境:

import tensorflow
import numpy as np 
from tensorflow import keras
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from keras.optimizers import Adam

model = load_model('facenet_keras.h5')

pic = load_img('trump.jpg')
pic = img_to_array(pic)
pic = np.expand_dims(pic, axis=0)

model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])

我不知道从这里去哪里。我将图像整理成阵列,但迷路了

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

您正在寻找

$latest_id

??

https://keras.io/models/model/#predict

答案 1 :(得分:0)

我理解了塞思的答案。起初有点令人困惑,我发布的初始代码没有用,但是现在可以满足我的要求了。我这样做的方法仅仅是将示例代码从在线和教程中拼凑起来,然后投入使用。

import tensorflow
import numpy as np 
from tensorflow import keras
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array, load_img 
from keras.optimizers import Adam
from keras.applications.vgg16 import preprocess_input, decode_predictions

model = load_model('facenet_keras.h5', compile='False')

pic = load_img('trump.jpg', target_size=(160,160,3))
pic = img_to_array(pic)

pic = pic.reshape((1, pic.shape[0], pic.shape[1], pic.shape[2]))
pic = preprocess_input(pic)

model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])
output = model.predict(pic)
print(output)

据我所知,FaceNet输出一个128维数组[?],现在我很难解释所有这些含义。我会上网查找,但是如果有人可以提供人工协助,那就太好了

[[-0.36650193  0.07450597 -0.11253849  0.09382331  0.65579426  1.2289288
   0.7981924  -0.43557873 -0.9650308  -2.4071848   0.73522866 -1.2488084
  -0.7643076   0.97527826 -0.6454712  -1.3645316   1.0456135  -1.6320316
   0.5565866   0.09845503  0.7503164  -3.154975    0.6703275   2.3669581
   0.11742923  1.5341481   1.865606   -1.3307446  -0.632361   -1.6581261
   1.158609    1.8743702  -0.5332592  -0.06612988 -0.8802324   1.5062594
   1.9927465  -1.6820407   0.84190995  1.4670922   0.5759155   0.4494674
   0.35184044 -0.8682072  -1.1785389   2.2496219   0.9702482   0.5559205
  -1.5887636  -1.8496605   1.0645783   0.42627138  1.6334398   2.0875866
   0.05197076  3.3503294   0.46358824 -2.07692     1.5033835   1.7825121
   0.38589296  1.4082223   1.6586784   0.44597477 -0.39349917 -0.01715486
   2.3880703   0.05123563  0.6001561   1.7848682   0.57936746  2.2707727
  -0.17195459  1.8396529  -1.4007891   1.2714268  -0.41032675  0.64929354
  -1.7332536  -1.9563283  -0.52206075  0.866758   -0.6876267  -0.7875931
   0.9024028   0.6540389   1.4121637  -1.8792673   1.3698239   0.43517247
   0.1034093  -0.71052015  0.3376826   0.13816951 -0.9559467   3.2945871
   1.916734   -1.4701567   1.2339087   1.7374766  -1.2939825  -1.2702736
   1.970353   -0.5688637   1.004023   -1.9100393  -2.5775273   2.8778517
   1.4665067  -1.4564868  -1.6789169  -1.0139952   1.7792807  -1.4399014
  -1.2965738   1.1995381  -1.2554456   2.3882952   0.13599804 -1.6818564
  -0.5534592  -1.4732366   1.5166222  -0.28499228  0.96933156  0.4853603
   0.8890593  -2.5222735 ]]