diff --git a/src/TensorFlowNET.Core/Operations/gen_image_ops.cs b/src/TensorFlowNET.Core/Operations/gen_image_ops.cs index 8b81dc8a..9240b590 100644 --- a/src/TensorFlowNET.Core/Operations/gen_image_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_image_ops.cs @@ -62,6 +62,19 @@ namespace Tensorflow }); } + public static Tensor decode_image(Tensor contents, + long channels = 0, + TF_DataType dtype = TF_DataType.TF_UINT8, + bool expand_animations = true, + string name = null) + => tf.Context.ExecuteOp("DecodeImage", name, + new ExecuteOpArgs(contents).SetAttributes(new + { + channels, + dtype, + expand_animations + })); + public static Tensor decode_jpeg(Tensor contents, long channels = 0, long ratio = 1, diff --git a/src/TensorFlowNET.Core/Operations/image_ops_impl.cs b/src/TensorFlowNET.Core/Operations/image_ops_impl.cs index f9e9061c..de74b281 100644 --- a/src/TensorFlowNET.Core/Operations/image_ops_impl.cs +++ b/src/TensorFlowNET.Core/Operations/image_ops_impl.cs @@ -1698,80 +1698,16 @@ new_height, new_width"); public static Tensor decode_image(Tensor contents, int channels = 0, TF_DataType dtype = TF_DataType.TF_UINT8, string name = null, bool expand_animations = true) { - return tf_with(ops.name_scope(name, "decode_image"), scope => - { - var substr = tf.strings.substr(contents, 0, 3); - - Func _jpeg = () => - { - int jpeg_channels = channels; - var good_channels = math_ops.not_equal(jpeg_channels, 4, name: "check_jpeg_channels"); - string channels_msg = "Channels must be in (None, 0, 1, 3) when decoding JPEG 'images'"; - var assert_channels = control_flow_ops.Assert(good_channels, new string[] { channels_msg }); - return tf_with(ops.control_dependencies(new[] { assert_channels }), delegate - { - return convert_image_dtype(gen_image_ops.decode_jpeg(contents, channels), dtype); - }); - }; + var scope = ops.name_scope(name, "decode_image"); + scope.__enter__(); - /*Func _gif = () => - { - int gif_channels = channels; - var good_channels = math_ops.logical_and( - math_ops.not_equal(gif_channels, 1, name: "check_gif_channels"), - math_ops.not_equal(gif_channels, 4, name: "check_gif_channels")); - - string channels_msg = "Channels must be in (None, 0, 3) when decoding GIF images"; - var assert_channels = control_flow_ops.Assert(good_channels, new string[] { channels_msg }); - return tf_with(ops.control_dependencies(new[] { assert_channels }), delegate - { - var result = convert_image_dtype(gen_image_ops.decode_gif(contents), dtype); - if (!expand_animations) - result = array_ops.gather(result, 0); - return result; - }); - }; + var result = gen_image_ops.decode_image(contents, + channels: channels, + dtype: dtype, + expand_animations: expand_animations); - Func _bmp = () => - { - int bmp_channels = channels; - var signature = tf.strings.substr(contents, 0, 2); - var is_bmp = math_ops.equal(signature, "BM", name: "is_bmp"); - string decode_msg = "Unable to decode bytes as JPEG, PNG, GIF, or BMP"; - var assert_decode = control_flow_ops.Assert(is_bmp, new string[] { decode_msg }); - var good_channels = math_ops.not_equal(bmp_channels, 1, name: "check_channels"); - string channels_msg = "Channels must be in (None, 0, 3) when decoding BMP images"; - var assert_channels = control_flow_ops.Assert(good_channels, new string[] { channels_msg }); - return tf_with(ops.control_dependencies(new[] { assert_decode, assert_channels }), delegate - { - return convert_image_dtype(gen_image_ops.decode_bmp(contents), dtype); - }); - }; - - Func _png = () => - { - return convert_image_dtype(gen_image_ops.decode_png( - contents, - channels, - dtype: dtype), - dtype); - }; - - Func check_gif = () => - { - var gif = tf.constant(new byte[] { 0x47, 0x49, 0x46 }, TF_DataType.TF_STRING); - var is_gif = math_ops.equal(substr, gif, name: name); - return control_flow_ops.cond(is_gif, _gif, _bmp, name: "cond_gif"); - }; - - Func check_png = () => - { - return control_flow_ops.cond(is_png(contents), _png, check_gif, name: "cond_png"); - };*/ - - // return control_flow_ops.cond(is_jpeg(contents), _jpeg, check_png, name: "cond_jpeg"); - return _jpeg() as Tensor; - }); + scope.__exit__(); + return result; } public static Tensor crop_and_resize(Tensor image, Tensor boxes, Tensor box_ind, Tensor crop_size, string method, float extrapolation_value, string name) diff --git a/src/TensorFlowNET.Core/ops.name_scope.cs b/src/TensorFlowNET.Core/ops.name_scope.cs index 336afa2a..3872d5b1 100644 --- a/src/TensorFlowNET.Core/ops.name_scope.cs +++ b/src/TensorFlowNET.Core/ops.name_scope.cs @@ -111,18 +111,6 @@ namespace Tensorflow get_default_graph()._name_stack = old_scope_name; } - [DebuggerNonUserCode] - public void __init__() - { - - } - - [DebuggerNonUserCode] - public void __del__() - { - - } - /// /// __enter__() /// diff --git a/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs b/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs index a53635d4..29ad9ad8 100644 --- a/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs +++ b/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs @@ -26,7 +26,7 @@ namespace TensorFlowNET.UnitTest public void decode_image() { var img = tf.image.decode_image(contents); - Assert.AreEqual(img.name, "decode_image/Identity:0"); + Assert.AreEqual(img.name, "decode_image/DecodeImage:0"); } [TestMethod]