@@ -1702,74 +1702,79 @@ public static Tensor sobel_edges(Tensor image)
17021702 public static Tensor decode_image ( Tensor contents , int channels = 0 , TF_DataType dtype = TF_DataType . TF_UINT8 ,
17031703 string name = null , bool expand_animations = true )
17041704 {
1705- Func < ITensorOrOperation > _jpeg = ( ) =>
1705+ return tf_with ( ops . name_scope ( name , "decode_image" ) , scope =>
17061706 {
1707- int jpeg_channels = channels ;
1708- var good_channels = math_ops . not_equal ( jpeg_channels , 4 , name : "check_jpeg_channels" ) ;
1709- string channels_msg = "Channels must be in (None, 0, 1, 3) when decoding JPEG 'images'" ;
1710- var assert_channels = control_flow_ops . Assert ( good_channels , new string [ ] { channels_msg } ) ;
1711- return tf_with ( ops . control_dependencies ( new [ ] { assert_channels } ) , delegate
1707+ var substr = tf . strings . substr ( contents , 0 , 3 ) ;
1708+
1709+ Func < ITensorOrOperation > _jpeg = ( ) =>
17121710 {
1713- return convert_image_dtype ( gen_image_ops . decode_jpeg ( contents , channels ) , dtype ) ;
1714- } ) ;
1715- } ;
1711+ int jpeg_channels = channels ;
1712+ var good_channels = math_ops . not_equal ( jpeg_channels , 4 , name : "check_jpeg_channels" ) ;
1713+ string channels_msg = "Channels must be in (None, 0, 1, 3) when decoding JPEG 'images'" ;
1714+ var assert_channels = control_flow_ops . Assert ( good_channels , new string [ ] { channels_msg } ) ;
1715+ return tf_with ( ops . control_dependencies ( new [ ] { assert_channels } ) , delegate
1716+ {
1717+ return convert_image_dtype ( gen_image_ops . decode_jpeg ( contents , channels ) , dtype ) ;
1718+ } ) ;
1719+ } ;
17161720
1717- Func < ITensorOrOperation > _gif = ( ) =>
1718- {
1719- int gif_channels = channels ;
1720- var good_channels = math_ops . logical_and (
1721- math_ops . not_equal ( gif_channels , 1 , name : "check_gif_channels" ) ,
1722- math_ops . not_equal ( gif_channels , 4 , name : "check_gif_channels" ) ) ;
1723-
1724- string channels_msg = "Channels must be in (None, 0, 3) when decoding GIF images" ;
1725- var assert_channels = control_flow_ops . Assert ( good_channels , new string [ ] { channels_msg } ) ;
1726- return tf_with ( ops . control_dependencies ( new [ ] { assert_channels } ) , delegate
1721+ /*Func<ITensorOrOperation> _gif = () =>
17271722 {
1728- var result = convert_image_dtype ( gen_image_ops . decode_gif ( contents ) , dtype ) ;
1729- if ( ! expand_animations )
1730- result = array_ops . gather ( result , 0 ) ;
1731- return result ;
1732- } ) ;
1733- } ;
1723+ int gif_channels = channels;
1724+ var good_channels = math_ops.logical_and(
1725+ math_ops.not_equal(gif_channels, 1, name: "check_gif_channels"),
1726+ math_ops.not_equal(gif_channels, 4, name: "check_gif_channels"));
1727+
1728+ string channels_msg = "Channels must be in (None, 0, 3) when decoding GIF images";
1729+ var assert_channels = control_flow_ops.Assert(good_channels, new string[] { channels_msg });
1730+ return tf_with(ops.control_dependencies(new[] { assert_channels }), delegate
1731+ {
1732+ var result = convert_image_dtype(gen_image_ops.decode_gif(contents), dtype);
1733+ if (!expand_animations)
1734+ result = array_ops.gather(result, 0);
1735+ return result;
1736+ });
1737+ };
17341738
1735- Func < ITensorOrOperation > _bmp = ( ) =>
1736- {
1737- int bmp_channels = channels ;
1738- var signature = tf . strings . substr ( contents , 0 , 2 ) ;
1739- var is_bmp = math_ops . equal ( signature , "BM" , name : "is_bmp" ) ;
1740- string decode_msg = "Unable to decode bytes as JPEG, PNG, GIF, or BMP" ;
1741- var assert_decode = control_flow_ops . Assert ( is_bmp , new string [ ] { decode_msg } ) ;
1742- var good_channels = math_ops . not_equal ( bmp_channels , 1 , name : "check_channels" ) ;
1743- string channels_msg = "Channels must be in (None, 0, 3) when decoding BMP images" ;
1744- var assert_channels = control_flow_ops . Assert ( good_channels , new string [ ] { channels_msg } ) ;
1745- return tf_with ( ops . control_dependencies ( new [ ] { assert_decode , assert_channels } ) , delegate
1739+ Func<ITensorOrOperation> _bmp = () =>
17461740 {
1747- return convert_image_dtype ( gen_image_ops . decode_bmp ( contents ) , dtype ) ;
1748- } ) ;
1749- } ;
1741+ int bmp_channels = channels;
1742+ var signature = tf.strings.substr(contents, 0, 2);
1743+ var is_bmp = math_ops.equal(signature, "BM", name: "is_bmp");
1744+ string decode_msg = "Unable to decode bytes as JPEG, PNG, GIF, or BMP";
1745+ var assert_decode = control_flow_ops.Assert(is_bmp, new string[] { decode_msg });
1746+ var good_channels = math_ops.not_equal(bmp_channels, 1, name: "check_channels");
1747+ string channels_msg = "Channels must be in (None, 0, 3) when decoding BMP images";
1748+ var assert_channels = control_flow_ops.Assert(good_channels, new string[] { channels_msg });
1749+ return tf_with(ops.control_dependencies(new[] { assert_decode, assert_channels }), delegate
1750+ {
1751+ return convert_image_dtype(gen_image_ops.decode_bmp(contents), dtype);
1752+ });
1753+ };
17501754
1751- Func < ITensorOrOperation > _png = ( ) =>
1752- {
1753- return convert_image_dtype ( gen_image_ops . decode_png (
1754- contents ,
1755- channels ,
1756- dtype : dtype ) ,
1757- dtype ) ;
1758- } ;
1755+ Func<ITensorOrOperation> _png = () =>
1756+ {
1757+ return convert_image_dtype(gen_image_ops.decode_png(
1758+ contents,
1759+ channels,
1760+ dtype: dtype),
1761+ dtype);
1762+ };
17591763
1760- Func < ITensorOrOperation > check_gif = ( ) =>
1761- {
1762- return control_flow_ops . cond ( is_gif ( contents ) , _gif , _bmp , name : "cond_gif" ) ;
1763- } ;
1764+ Func<ITensorOrOperation> check_gif = () =>
1765+ {
1766+ var gif = tf.constant(new byte[] { 0x47, 0x49, 0x46 }, TF_DataType.TF_STRING);
1767+ var is_gif = math_ops.equal(substr, gif, name: name);
1768+ return control_flow_ops.cond(is_gif, _gif, _bmp, name: "cond_gif");
1769+ };
17641770
1765- Func < ITensorOrOperation > check_png = ( ) =>
1766- {
1767- return control_flow_ops . cond ( is_png ( contents ) , _png , check_gif , name : "cond_png" ) ;
1768- } ;
1771+ Func<ITensorOrOperation> check_png = () =>
1772+ {
1773+ return control_flow_ops.cond(is_png(contents), _png, check_gif, name: "cond_png");
1774+ };*/
17691775
1770- return tf_with ( ops . name_scope ( name , "decode_image" ) , scope =>
1771- {
1772- return control_flow_ops . cond ( is_jpeg ( contents ) , _jpeg , check_png , name : "cond_jpeg" ) ;
1776+ // return control_flow_ops.cond(is_jpeg(contents), _jpeg, check_png, name: "cond_jpeg");
1777+ return _jpeg ( ) as Tensor ;
17731778 } ) ;
17741779 }
17751780
0 commit comments