aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/kernels/decode_image_op.cc
diff options
context:
space:
mode:
authorGravatar Yong Tang <yong.tang.github@outlook.com>2018-01-23 10:01:18 -0800
committerGravatar Rasmus Munk Larsen <rmlarsen@google.com>2018-01-23 10:01:18 -0800
commit7c53bc7fbec18e8c6157832d89737bcc881ecfed (patch)
tree9034e19e65d3ea8165d2ac727fec0b2fdfb92152 /tensorflow/core/kernels/decode_image_op.cc
parenta73a6cff9aa1ee3a6db00cbeccbebe192372b05a (diff)
Propagate the error string of GIF processing for decode_gif (#16113)
* Propagate the error message to exception of `InvalidArgumentError` for `decode_gif` This fix tries to improve the error thrown by `decode_gif` to include the error string generated by GIF processing. Previously, the error was not very indicative as the error string returned by GIF processing was hidden: ``` .......... InvalidArgumentError (see above for traceback): Invalid GIF data, size 2091369 [[Node: DecodeGif = DecodeGif[_device="/job:localhost/replica:0/task:0/device:CPU:0"](ReadFile)]] ``` This fix propagate the error string to be part of the `InvalidArgumentError`: ``` InvalidArgumentError (see above for traceback): Invalid GIF data (size 2091369), can't process optimized gif [[Node: DecodeGif = DecodeGif[_device="/job:localhost/replica:0/task:0/device:CPU:0"](ReadFile)]] ``` This fix fixes 15838. Signed-off-by: Yong Tang <yong.tang.github@outlook.com> * Reformat with clang-format -i --style=Google Signed-off-by: Yong Tang <yong.tang.github@outlook.com> * Add unit test for decode_gif to cover changes so that error messages like `can't process optimized gif` are propagated to the exception of `InvalidArgumentError`. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
Diffstat (limited to 'tensorflow/core/kernels/decode_image_op.cc')
-rw-r--r--tensorflow/core/kernels/decode_image_op.cc39
1 files changed, 19 insertions, 20 deletions
diff --git a/tensorflow/core/kernels/decode_image_op.cc b/tensorflow/core/kernels/decode_image_op.cc
index ceb152c3f0..44dcbf834c 100644
--- a/tensorflow/core/kernels/decode_image_op.cc
+++ b/tensorflow/core/kernels/decode_image_op.cc
@@ -87,11 +87,10 @@ class DecodeImageOp : public OpKernel {
channels_ = 3;
} else {
OP_REQUIRES_OK(context, context->GetAttr("channels", &channels_));
- OP_REQUIRES(
- context,
- channels_ == 0 || channels_ == 1 || channels_ == 3 || channels_ == 4,
- errors::InvalidArgument("channels must be 0, 1, 3, or 4, got ",
- channels_));
+ OP_REQUIRES(context, channels_ == 0 || channels_ == 1 || channels_ == 3 ||
+ channels_ == 4,
+ errors::InvalidArgument(
+ "channels must be 0, 1, 3, or 4, got ", channels_));
}
flags_.components = channels_;
@@ -115,9 +114,8 @@ class DecodeImageOp : public OpKernel {
if (format_ == kJpgFormat) {
OP_REQUIRES_OK(context, context->GetAttr("ratio", &flags_.ratio));
- OP_REQUIRES(context,
- flags_.ratio == 1 || flags_.ratio == 2 || flags_.ratio == 4 ||
- flags_.ratio == 8,
+ OP_REQUIRES(context, flags_.ratio == 1 || flags_.ratio == 2 ||
+ flags_.ratio == 4 || flags_.ratio == 8,
errors::InvalidArgument("ratio must be 1, 2, 4, or 8, got ",
flags_.ratio));
OP_REQUIRES_OK(context, context->GetAttr("fancy_upscaling",
@@ -132,9 +130,8 @@ class DecodeImageOp : public OpKernel {
string dct_method;
OP_REQUIRES_OK(context, context->GetAttr("dct_method", &dct_method));
OP_REQUIRES(
- context,
- (dct_method.empty() || dct_method == "INTEGER_FAST" ||
- dct_method == "INTEGER_ACCURATE"),
+ context, (dct_method.empty() || dct_method == "INTEGER_FAST" ||
+ dct_method == "INTEGER_ACCURATE"),
errors::InvalidArgument("dct_method must be one of "
"{'', 'INTEGER_FAST', 'INTEGER_ACCURATE'}"));
if (dct_method == "INTEGER_FAST") {
@@ -160,9 +157,9 @@ class DecodeImageOp : public OpKernel {
errors::InvalidArgument("Expected image (JPEG, PNG, or GIF), got ",
FileFormatString(magic, input)));
OP_REQUIRES(context, input.size() <= std::numeric_limits<int>::max(),
- errors::InvalidArgument(
- FileFormatString(magic, input),
- " contents are too large for int: ", input.size()));
+ errors::InvalidArgument(FileFormatString(magic, input),
+ " contents are too large for int: ",
+ input.size()));
OP_REQUIRES(context, magic == kPngFormat || channel_bits_ == 8,
errors::InvalidArgument(FileFormatString(magic, input),
" does not support uint16 output"));
@@ -215,10 +212,9 @@ class DecodeImageOp : public OpKernel {
input.data(), input.size(), flags, nullptr /* nwarn */,
[=, &output](int width, int height, int channels) -> uint8* {
Status status(context->allocate_output(
- 0,
- format_ == kGifFormat
- ? TensorShape({1, height, width, channels})
- : TensorShape({height, width, channels}),
+ 0, format_ == kGifFormat
+ ? TensorShape({1, height, width, channels})
+ : TensorShape({height, width, channels}),
&output));
if (!status.ok()) {
VLOG(1) << status;
@@ -294,6 +290,7 @@ class DecodeImageOp : public OpKernel {
// Decode GIF, allocating tensor once the size is known.
Tensor* output = nullptr;
+ string error_string;
OP_REQUIRES(
context,
gif::Decode(input.data(), input.size(),
@@ -320,8 +317,10 @@ class DecodeImageOp : public OpKernel {
return nullptr;
}
return output->flat<uint8>().data();
- }),
- errors::InvalidArgument("Invalid GIF data, size ", input.size()));
+ },
+ &error_string),
+ errors::InvalidArgument("Invalid GIF data (size ", input.size(), "), ",
+ error_string));
}
private: