首页 > 解决方案 > 将 Tensorflow 张量转换为 Numpyarray

问题描述

我有一个class 'tensorflow.python.framework.ops.Tensor作为输出,需要将其转换为一个 numpy 数组。

.nu​​mpy()不起作用,因为它不是一个eagerTensor。.eval 也不能正常工作,因为我使用的是tensorflow >2.0

有没有其他方法可以解决这个问题?

img_height=330
img_width=600
img_depth=23

save_model="saved_Models/wheatModel"
prediction_data_path=["data/stacked/MOD13Q1.A2017.2738.tif","data/stacked/MOD13Q1.A2017.889.tif","data/stacked/MOD13Q1.A2017.923.tif"]

prediction_data=dataConv.preparePredictionData(prediction_data_path)

prediction_reshaped=dataConv.reshapeFiles(prediction_data,img_width,img_height,img_depth)

x_ds =tf.stack(prediction_reshaped)

model = tf.keras.models.load_model(save_model)

model.predict(x_ds)

image=model.get_layer(name='prediction_image').output
n,output_width,output_height,output_depth,output_channels=image.shape
print(type(image))
image=tf.reshape(image,(output_width,output_height,output_depth))
print(type(image))

image.numpy()

所以在上面的代码中。

  1. 我加载训练有素的模型

  2. 预测给定的图像

  3. 获取倒数第二层的输出

  4. 重塑此数据

  5. 现在我想将此张量转换为 numpyarray

标签: numpytensorflowtype-conversiontensor

解决方案


推荐阅读