我正在尝试使用Tensorflow-Hub来从图像中提取特征向量。但是,我不确定将Tensorflow-Hub输出(张量)转换为numpy向量的意图。这是一个简单的示例:
from keras.preprocessing.image import load_img
import tensorflow_hub as hub
import tensorflow as tf
import numpy as np
im = load_img('sample.png')
im = np.expand_dims(im.resize((299,299)), 0)
module = hub.Module("https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1")
out = module(im)
o = np.add(out, 0)
type(o)
docs指示“ NumPy操作会自动将张量转换为NumPy ndarrays”,但是我上面的np.add()
调用返回对象类型tensorflow.python.framework.ops.Tensor
。有谁知道我如何从out
获取一个numpy数组?任何指针将不胜感激!
版本:
# output from `pip freeze | grep tensorflow`
tensorflow==1.14.0
tensorflow-estimator==1.14.0
tensorflow-hub==0.1.1
tensorflow-probability==0.6.0
答案 0 :(得分:1)
以下应该起作用。但是我没有检查输出是否有意义。但是它在多次运行中都返回了一致的结果。
im = load_img('sample.png')
im = np.expand_dims(im.resize((299,299)), 0)
module = hub.Module("https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1")
out = module(im)
with tf.Session() as sess:
tf.global_variables_initializer().run()
o = sess.run(out)
o = np.add(o, 0)
print(type(o))
答案 1 :(得分:0)
你可以使用
out.numpy()
type(out)
# <class 'tensorflow.python.framework.ops.EagerTensor'>
type(out.numpy()
# <class 'numpy.ndarray'>