是否可以在没有模型或运行会话的情况下使用Conv2D图层?

时间:2019-03-04 18:43:55

标签: python tensorflow

我正在写一篇有关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问题是,我无法从该张量读取数据。 无需使用需要运行会话的.eval()函数吗?

1 个答案:

答案 0 :(得分:1)

避免会话的唯一方法是使用渴望执行:

import tensorflow as tf
tf.enable_eager_execution()

请注意,在使用任何TF操作之前,您需要启用它!现在,按定义对TF ops进行评估。在您的示例中,您可以使用

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数组以进行进一步处理。有关渴望执行的更多信息,请阅读TF网站上的the basic tutorials和/或the guide