简体   繁体   中英

Is it possible to use a Conv2D layer without a model or a running Session?

I'm writing an article about Conv2D nets and I want to preview the effect of a Conv2D net on an image, I could get this using a simple keras model and getting the output of first layer (which is the conv), but I wanted a simpler way.

So I did this:

layer = Conv2D(10, (3, 3), input_shape=[1080, 1080, 3])
tensor_in = tf.convert_to_tensor(img, dtype="float")
tensor_out = layer(tensor_in)

The code above works fine and I end up with a tensor tensor_out . The problem is I couldn't manage to read the data from this tensor. is there anyway to do it without having to use the .eval() function that requires a running session?

The only way to avoid sessions is to use eager execution:

import tensorflow as tf
tf.enable_eager_execution()

Note that you need to enable it before using any TF ops! Now, TF ops are evaluated as they are defined. In your example you can use

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()

to get a numpy array for further processing. For more information on eager execution read the basic tutorials and/or the guide on the TF website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM