我试着开始使用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)文档表示"NumPy操作自动将张量转换为NumPy ndarray“,但上面的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发布于 2019-12-16 22:34:18
下列各点应能发挥作用。但我没有检查输出是否有意义。但是它在多次运行中返回一致的结果。
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))发布于 2021-03-05 02:37:23
您可以使用
out.numpy()
type(out)
# <class 'tensorflow.python.framework.ops.EagerTensor'>
type(out.numpy()
# <class 'numpy.ndarray'>https://stackoverflow.com/questions/59363950
复制相似问题