【发布时间】:2021-01-08 06:24:37
【问题描述】:
我已经根据我的数据训练了 ResNet50 模型。我想在进行预测时获得自定义层的输出。我尝试使用下面的代码来获取自定义层的输出,它以张量格式提供数据,但我需要NumPy array 格式的数据。我尝试将张量转换为 NumPy 数组,但出现错误,我遵循了这个 post,但没有帮助
任何人都可以分享一些想法,任何建议都会非常有帮助
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'>
我尝试过的事情
选项 1
result = result.numpy()
AttributeError: 'Tensor' 对象没有属性 'numpy'
选项 2
result = result.eval(session=tf.compat.v1.Session())
2020-09-22 11:21:59.522138: 我 tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] 成功 从 SysFS 读取的 NUMA 节点具有负值 (-1),但必须有 至少一个 NUMA 节点,所以返回 NUMA 节点为零 2020-09-22 11:21:59.522343:我 tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] 找到设备 0 带属性:
已安装依赖项:
tensorflow-gpu==1.15.0
【问题讨论】:
标签: python numpy tensorflow tensor