我正在写一篇关于 Conv2D 网络的文章,我想预览 Conv2D 网络在图像上的效果,我可以使用简单的 keras 模型并获取第一层(即 conv)的输出来获得此效果,但我想要一种更简单的方法。
所以我这样做了:
layer = Conv2D(10, (3, 3), input_shape=[1080, 1080, 3])
tensor_in = tf.convert_to_tensor(img, dtype="float")
tensor_out = layer(tensor_in)
上面的代码工作正常,我最终得到一个张量tensor_out
。
问题是我无法从这个张量中读取数据。
有没有办法在不使用需要运行 session 的 .eval()
函数的情况下完成此操作?
最佳答案
避免 session 的唯一方法是使用急切执行:
import tensorflow as tf
tf.enable_eager_execution()
请注意,您需要在使用任何 TF 操作之前启用它!现在,TF 操作按照其定义进行评估。在您的示例中,您可以使用
layer = Conv2D(10, (3, 3), input_shape=[1080, 1080, 3])
tensor_in = tf.convert_to_tensor(img, dtype="float")
tensor_out = layer(tensor_in)
result = tensor_out.numpy()
获取 numpy 数组以进行进一步处理。有关急切执行的更多信息,请阅读 the basic tutorials和/或the guide在 TF 网站上。
关于python - 是否可以在没有模型或运行 session 的情况下使用 Conv2D 层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54989594/