Browse Source

decode_image

tags/TimeSeries
Oceania2018 3 years ago
parent
commit
044028284e
4 changed files with 22 additions and 85 deletions
  1. +13
    -0
      src/TensorFlowNET.Core/Operations/gen_image_ops.cs
  2. +8
    -72
      src/TensorFlowNET.Core/Operations/image_ops_impl.cs
  3. +0
    -12
      src/TensorFlowNET.Core/ops.name_scope.cs
  4. +1
    -1
      test/TensorFlowNET.Graph.UnitTest/ImageTest.cs

+ 13
- 0
src/TensorFlowNET.Core/Operations/gen_image_ops.cs View File

@@ -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,


+ 8
- 72
src/TensorFlowNET.Core/Operations/image_ops_impl.cs View File

@@ -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<ITensorOrOperation> _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<ITensorOrOperation> _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<ITensorOrOperation> _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<ITensorOrOperation> _png = () =>
{
return convert_image_dtype(gen_image_ops.decode_png(
contents,
channels,
dtype: dtype),
dtype);
};

Func<ITensorOrOperation> 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<ITensorOrOperation> 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)


+ 0
- 12
src/TensorFlowNET.Core/ops.name_scope.cs View File

@@ -111,18 +111,6 @@ namespace Tensorflow
get_default_graph()._name_stack = old_scope_name;
}

[DebuggerNonUserCode]
public void __init__()
{

}

[DebuggerNonUserCode]
public void __del__()
{

}

/// <summary>
/// __enter__()
/// </summary>


+ 1
- 1
test/TensorFlowNET.Graph.UnitTest/ImageTest.cs View File

@@ -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]


Loading…
Cancel
Save