Facenet: How i can get the feature vector from this model?

Created on 15 Oct 2019  路  1Comment  路  Source: davidsandberg/facenet

How i can get the feature vector from this model?

Most helpful comment

Here is my code suggestion: (TF 1.14)

```
import tensorflow as tf
import numpy as np
import cv2

def load_pb(path_to_pb):
with tf.gfile.GFile(path_to_pb, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name='')
return graph

graph = load_pb(fpath_to_pb_file)

input = graph.get_tensor_by_name('input:0')
output = graph.get_tensor_by_name('embeddings:0')
phase_train_placeholder = graph.get_tensor_by_name("phase_train:0")

img = cv2.imread(path_to_test_image)

def preprocess_img(x):
x = cv2.resize(x, (160, 160))
mean = np.mean(x)
std = np.std(x)
std_adj = np.maximum(std, 1.0/np.sqrt(x.size))
y = np.multiply(np.subtract(x, mean), 1/std_adj)
return np.expand_dims(y, 0)

with tf.Session(graph=graph) as sess:
embed = sess.run(output, feed_dict={input:preprocess_img(img), phase_train_placeholder:False})
print(embed)```

>All comments

Here is my code suggestion: (TF 1.14)

```
import tensorflow as tf
import numpy as np
import cv2

def load_pb(path_to_pb):
with tf.gfile.GFile(path_to_pb, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name='')
return graph

graph = load_pb(fpath_to_pb_file)

input = graph.get_tensor_by_name('input:0')
output = graph.get_tensor_by_name('embeddings:0')
phase_train_placeholder = graph.get_tensor_by_name("phase_train:0")

img = cv2.imread(path_to_test_image)

def preprocess_img(x):
x = cv2.resize(x, (160, 160))
mean = np.mean(x)
std = np.std(x)
std_adj = np.maximum(std, 1.0/np.sqrt(x.size))
y = np.multiply(np.subtract(x, mean), 1/std_adj)
return np.expand_dims(y, 0)

with tf.Session(graph=graph) as sess:
embed = sess.run(output, feed_dict={input:preprocess_img(img), phase_train_placeholder:False})
print(embed)```

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arunxz98 picture arunxz98  路  3Comments

kuaikuaikim picture kuaikuaikim  路  3Comments

RaviRaaja picture RaviRaaja  路  3Comments

shdyn picture shdyn  路  4Comments

mayank26saxena picture mayank26saxena  路  4Comments