[英]How to convert Tensor into NumPy array
I have trained ResNet50
model on my data.我已经根据我的数据训练
ResNet50
model。 I want to get the output of a custom layer while making the prediction.我想在进行预测时获得自定义层的 output。 I tried using the below code to get the output of a custom layer, it gives data in a tensor format, but I need the data in a
NumPy array
format.我尝试使用以下代码获取自定义层的 output,它以张量格式提供数据,但我需要
NumPy array
格式的数据。 I tried to convert the tensor to NumPy array but getting errors, I have followed this post , but it wasn't helpful我试图将张量转换为 NumPy 数组但出现错误,我已经关注了这篇文章,但没有帮助
Can anyone share some thoughts, any advice will be very helpful谁能分享一些想法,任何建议都会很有帮助
from keras.models import load_model
import tensorflow as tf
import numpy as np
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.Session(config=config)
model = load_model(model_path) # load trained model
data = load_data(data_path) # load data for predictions
result = model.predict(data)
print(type(result_dev))
#<class 'numpy.ndarray'>
result = model.get_layer('avg_pool').output
print(type(result))
#<class 'tensorflow.python.framework.ops.Tensor'>
Things I tried我试过的东西
result = result.numpy()
AttributeError: 'Tensor' object has no attribute 'numpy'
AttributeError: 'Tensor' object 没有属性 'numpy'
result = result.eval(session=tf.compat.v1.Session())
2020-09-22 11:21:59.522138: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-09-22 11:21:59.522343: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
2020-09-22 11:21:59.522138: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] 从 SysFS 读取成功的 NUMA 节点有负值(-1),但必须至少有一个 NUMA 节点,所以返回NUMA 节点零 2020-09-22 11:21:59.522343: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] 找到具有属性的设备 0:
Dependency Installed:依赖安装:
tensorflow-gpu==1.15.0
You can only convert tensors to numpy arrays during Eager execution.在 Eager 执行期间,您只能将张量转换为 numpy arrays。 Since you're using a version older than 2.0 this is not enabled by default.
由于您使用的版本早于 2.0,因此默认情况下未启用。
In any case, you can call this after importing tensorflow:在任何情况下,您都可以在导入 tensorflow 后调用它:
tf.compat.v1.enable_eager_execution()
Depending on your framework and usecase, you might also have to run tf.config.run_functions_eagerly
(if you have tf.function
defined anywhere).根据您的框架和用例,您可能还必须运行
tf.config.run_functions_eagerly
(如果您在任何地方定义了tf.function
)。 For better support of the Eager mode, you should upgrade tensorflow to the newest version and use tf.keras
as your code might not work properly with older standalone versions of Keras
.为了更好地支持 Eager 模式,您应该将 tensorflow 升级到最新版本并使用
tf.keras
,因为您的代码可能无法与Keras
的旧独立版本一起正常工作。 In newer versions, you can specify your keras model to run eagerly like this:在较新的版本中,您可以指定您的 keras model 像这样急切地运行:
model.run_eagerly = True
A tensor
can be converted into a numpy
array using following function of tensorflow
:可以使用以下 tensorflow 的
tensorflow
将tensor
转换为numpy
数组:
import tensorflow as tf
tf.make_ndarray(
tensor
)
For example:例如:
# Tensor a has shape (2,3)
a = tf.constant([[1,2,3],[4,5,6]])
proto_tensor = tf.make_tensor_proto(a) # convert `tensor a` to a proto tensor
tf.make_ndarray(proto_tensor) # output: array([[1, 2, 3],
# [4, 5, 6]], dtype=int32)
# output has shape (2,3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.