TensorFlow.NET icon indicating copy to clipboard operation
TensorFlow.NET copied to clipboard

[Question]: Tensorflow.RuntimeError : 'Attempting to capture an EagerTensor without building a function.'

Open Cilouche opened this issue 1 year ago • 2 comments

Description

Hi, I'm working on this "crop_to_bounding_box()" function in order to Crop an image to a specified bounding box. But I get this error in "crop_to_bounding_box()" function

Tensorflow.RuntimeError: 'Attempting to capture an EagerTensor without building a function.' any suggestion, please

the code :

var image = tf.image.decode_jpeg(tf.io.read_file(filname),1); // Set the variable values here //Offset variables values var offset_height = 20; var offset_width = 20; // Target variables values var target_height = 20; var target_width = 20; var graph = tf.Graph().as_default(); var ts = tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width);

Alternatives

No response

Cilouche avatar Apr 27 '23 08:04 Cilouche

Hi, this error is because the tf.image has not been updated for a long time and many implementations still stay in tf1.x. I've submit a partial fix for ir in #1043 . However with that, you need to specify the image shape yourself, which may lead to some inconvenience. Here's the example code:

        var graph = tf.Graph().as_default(); // Make sure the graph mode is on before reading the image
        var image = tf.image.decode_jpeg(tf.io.read_file("xxx"), 1);
        // Set the variable values here
        //Offset variables values
        var offset_height = 20;
        var offset_width = 20;
        // Target variables values
        var target_height = 20;
        var target_width = 20;
        image.shape = (400, 500, 1); // need to specify the image shape
        var ts = tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width);
        graph.Exit();

If you don't want to specify the image shape manually, you can read the image under eager mode first and get the shape before jumping into the code above, though that will sacrifice some performance.

I'll update the whole tf.image module next month, which will completely fix this issue. It's a work that costs time so now I can only give a quick but not so good fix for it. Thank you for reporting us this issue. :)

Besides, we'll publish a new library Tensorflow.NET.OpencvAdapter soon, which enables using Tensorflow.NET with OpencvSharp. It may also be an alternative. Besides, SharpCV may works but I'm not sure for that.

AsakusaRinne avatar Apr 27 '23 17:04 AsakusaRinne

thanks :)

Cilouche avatar Apr 28 '23 08:04 Cilouche