Skip to content

Commit 0440282

Browse files
committed
decode_image
1 parent 318f991 commit 0440282

File tree

4 files changed

+22
-85
lines changed

4 files changed

+22
-85
lines changed

src/TensorFlowNET.Core/Operations/gen_image_ops.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ public static Tensor convert_image_dtype(Tensor image, TF_DataType dtype, bool s
6262
});
6363
}
6464

65+
public static Tensor decode_image(Tensor contents,
66+
long channels = 0,
67+
TF_DataType dtype = TF_DataType.TF_UINT8,
68+
bool expand_animations = true,
69+
string name = null)
70+
=> tf.Context.ExecuteOp("DecodeImage", name,
71+
new ExecuteOpArgs(contents).SetAttributes(new
72+
{
73+
channels,
74+
dtype,
75+
expand_animations
76+
}));
77+
6578
public static Tensor decode_jpeg(Tensor contents,
6679
long channels = 0,
6780
long ratio = 1,

src/TensorFlowNET.Core/Operations/image_ops_impl.cs

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,80 +1698,16 @@ public static Tensor sobel_edges(Tensor image)
16981698
public static Tensor decode_image(Tensor contents, int channels = 0, TF_DataType dtype = TF_DataType.TF_UINT8,
16991699
string name = null, bool expand_animations = true)
17001700
{
1701-
return tf_with(ops.name_scope(name, "decode_image"), scope =>
1702-
{
1703-
var substr = tf.strings.substr(contents, 0, 3);
1704-
1705-
Func<ITensorOrOperation> _jpeg = () =>
1706-
{
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
1712-
{
1713-
return convert_image_dtype(gen_image_ops.decode_jpeg(contents, channels), dtype);
1714-
});
1715-
};
1701+
var scope = ops.name_scope(name, "decode_image");
1702+
scope.__enter__();
17161703

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
1727-
{
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-
};
1704+
var result = gen_image_ops.decode_image(contents,
1705+
channels: channels,
1706+
dtype: dtype,
1707+
expand_animations: expand_animations);
17341708

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
1746-
{
1747-
return convert_image_dtype(gen_image_ops.decode_bmp(contents), dtype);
1748-
});
1749-
};
1750-
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-
};
1759-
1760-
Func<ITensorOrOperation> check_gif = () =>
1761-
{
1762-
var gif = tf.constant(new byte[] { 0x47, 0x49, 0x46 }, TF_DataType.TF_STRING);
1763-
var is_gif = math_ops.equal(substr, gif, name: name);
1764-
return control_flow_ops.cond(is_gif, _gif, _bmp, name: "cond_gif");
1765-
};
1766-
1767-
Func<ITensorOrOperation> check_png = () =>
1768-
{
1769-
return control_flow_ops.cond(is_png(contents), _png, check_gif, name: "cond_png");
1770-
};*/
1771-
1772-
// return control_flow_ops.cond(is_jpeg(contents), _jpeg, check_png, name: "cond_jpeg");
1773-
return _jpeg() as Tensor;
1774-
});
1709+
scope.__exit__();
1710+
return result;
17751711
}
17761712

17771713
public static Tensor crop_and_resize(Tensor image, Tensor boxes, Tensor box_ind, Tensor crop_size, string method, float extrapolation_value, string name)

src/TensorFlowNET.Core/ops.name_scope.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,6 @@ public void __exit__()
111111
get_default_graph()._name_stack = old_scope_name;
112112
}
113113

114-
[DebuggerNonUserCode]
115-
public void __init__()
116-
{
117-
118-
}
119-
120-
[DebuggerNonUserCode]
121-
public void __del__()
122-
{
123-
124-
}
125-
126114
/// <summary>
127115
/// __enter__()
128116
/// </summary>

test/TensorFlowNET.Graph.UnitTest/ImageTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void Initialize()
2626
public void decode_image()
2727
{
2828
var img = tf.image.decode_image(contents);
29-
Assert.AreEqual(img.name, "decode_image/Identity:0");
29+
Assert.AreEqual(img.name, "decode_image/DecodeImage:0");
3030
}
3131

3232
[TestMethod]

0 commit comments

Comments
 (0)