aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/ops/image_ops_test.cc
blob: 517af26b44f53d85979e1194d1f0d6d8814cb1e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");

You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/framework/node_def_builder.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference_testutil.h"
#include "tensorflow/core/framework/tensor_testutil.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/platform/test.h"

namespace tensorflow {

TEST(ImageOpsTest, SampleDistortedBoundingBox_ShapeFn) {
  ShapeInferenceTestOp op("SampleDistortedBoundingBox");
  INFER_OK(op, "?;?", "[3];[3];[1,1,4]");
}

TEST(ImageOpsTest, Resize_ShapeFn) {
  for (const char* op_name : {"ResizeArea", "ResizeBicubic", "ResizeBilinear",
                              "ResizeNearestNeighbor"}) {
    ShapeInferenceTestOp op(op_name);
    op.input_tensors.resize(2);

    // Inputs are images and size.

    // Rank and size checks.
    INFER_ERROR("Shape must be rank 4 but is rank 5", op, "[1,2,3,4,5];?");
    INFER_ERROR("Shape must be rank 4 but is rank 3", op, "[1,2,3];?");
    INFER_ERROR("Shape must be rank 1 but is rank 0", op, "?;[]");
    INFER_ERROR("Dimension must be 2 but is 3", op, "?;[3]");

    // When the size tensor is not a constant, the middle dims are unknown.
    INFER_OK(op, "[1,?,3,?];[2]", "[d0_0,?,?,d0_3]");

    Tensor size_tensor = test::AsTensor<int32>({20, 30});
    op.input_tensors[1] = &size_tensor;
    INFER_OK(op, "[1,?,3,?];[2]", "[d0_0,20,30,d0_3]");
  }
}

TEST(ImageOpsTest, DecodeGif) {
  ShapeInferenceTestOp op("DecodeGif");

  // Rank check.
  INFER_ERROR("Shape must be rank 0 but is rank 1", op, "[1]");

  // Output is always ?,?,?,3.
  INFER_OK(op, "?", "[?,?,?,3]");
  INFER_OK(op, "[]", "[?,?,?,3]");
}

TEST(ImageOpsTest, DecodeImage_ShapeFn) {
  for (const char* op_name : {"DecodeJpeg", "DecodePng"}) {
    ShapeInferenceTestOp op(op_name);

    // Rank check.
    INFER_ERROR("Shape must be rank 0 but is rank 1", op, "[1]");

    // Set the channel to zero - output is not known.
    TF_ASSERT_OK(NodeDefBuilder("test", op_name)
                     .Input({"a", 0, DT_STRING})
                     .Finalize(&op.node_def));
    INFER_OK(op, "[]", "[?,?,?]");

    // Set the channel and so that part of output shape is known.
    TF_ASSERT_OK(NodeDefBuilder("test", op_name)
                     .Input({"a", 0, DT_STRING})
                     .Attr("channels", 4)
                     .Finalize(&op.node_def));
    INFER_OK(op, "[]", "[?,?,4]");

    // Negative channel value is rejected.
    TF_ASSERT_OK(NodeDefBuilder("test", op_name)
                     .Input({"a", 0, DT_STRING})
                     .Attr("channels", -1)
                     .Finalize(&op.node_def));
    INFER_ERROR("channels must be non-negative, got -1", op, "[]");
  }
}

TEST(ImageOpsTest, DecodeAndCropJpeg_ShapeFn) {
  const char* op_name = "DecodeAndCropJpeg";
  ShapeInferenceTestOp op(op_name);

  // Check the number of inputs.
  INFER_ERROR("Wrong number of inputs passed: 1 while 2 expected", op, "[1]");

  // Rank check.
  INFER_ERROR("Shape must be rank 0 but is rank 1", op, "[1];?");

  // Set the channel to zero - output is not known.
  TF_ASSERT_OK(NodeDefBuilder("test", op_name)
                   .Input({"img", 0, DT_STRING})
                   .Input({"crop_window", 1, DT_INT32})
                   .Finalize(&op.node_def));
  INFER_OK(op, "[];[?]", "[?,?,?]");

  // Set the channel, so that part of output shape is known.
  TF_ASSERT_OK(NodeDefBuilder("test", op_name)
                   .Input({"img", 0, DT_STRING})
                   .Input({"crop_window", 1, DT_INT32})
                   .Attr("channels", 4)
                   .Finalize(&op.node_def));
  INFER_OK(op, "[];[?]", "[?,?,4]");

  // Negative channel value is rejected.
  TF_ASSERT_OK(NodeDefBuilder("test", op_name)
                   .Input({"img", 0, DT_STRING})
                   .Input({"crop_window", 1, DT_INT32})
                   .Attr("channels", -1)
                   .Finalize(&op.node_def));
  INFER_ERROR("channels must be non-negative, got -1", op, "[];[]");
}

TEST(ImageOpsTest, DecodeAndCropJpeg_InvalidCropWindow) {
  const char* op_name = "DecodeAndCropJpeg";
  ShapeInferenceTestOp op(op_name);

  // Check the number of inputs.
  INFER_ERROR("Wrong number of inputs passed: 1 while 2 expected", op, "[1]");

  // Rank check.
  INFER_ERROR("Shape must be rank 0 but is rank 1", op, "[1];?");

  // Set the channel to zero - output is not known.
  TF_ASSERT_OK(NodeDefBuilder("test", op_name)
                   .Input({"img", 0, DT_STRING})
                   .Input({"crop_window", 1, DT_INT32})
                   .Finalize(&op.node_def));
  INFER_OK(op, "[];[?]", "[?,?,?]");
}

TEST(ImageOpsTest, EncodeImage_ShapeFn) {
  for (const char* op_name : {"EncodeJpeg", "EncodePng"}) {
    ShapeInferenceTestOp op(op_name);

    // Rank check.
    INFER_ERROR("Shape must be rank 3 but is rank 2", op, "[1,2]");

    INFER_OK(op, "[1,?,3]", "[]");  // output is always scalar.
  }
}

TEST(ImageOpsTest, ExtractJpegShape_ShapeFn) {
  ShapeInferenceTestOp op("ExtractJpegShape");

  // Rank check.
  INFER_ERROR("Shape must be rank 0 but is rank 1", op, "[1]");

  // Only specify input data. Output must be a 1-D tensor with 3 elements.
  INFER_OK(op, "?", "[3]");
}

TEST(ImageOpsTest, Colorspace_ShapeFn) {
  for (const char* op_name : {"HSVToRGB", "RGBToHSV"}) {
    ShapeInferenceTestOp op(op_name);

    // Rank check.
    INFER_ERROR("Shape must be at least rank 1 but is rank 0", op, "[]");

    // Input's last dim is required to be 3.
    INFER_ERROR("Dimension must be 3 but is 4", op, "[1,2,4]");
    INFER_OK(op, "[1,2,3]", "[d0_0,d0_1,d0_2]");
    INFER_OK(op, "[1,2,?]", "[d0_0,d0_1,3]");
    INFER_OK(op, "?", "?");
  }
}

TEST(ImageOpsTest, ExtractGlimpse_ShapeFn) {
  ShapeInferenceTestOp op("ExtractGlimpse");
  op.input_tensors.resize(2);

  // Inputs are input, size, offsets.

  // Rank and size checks.
  INFER_ERROR("Shape must be rank 4 but is rank 5", op, "[1,2,3,4,5];?;?");
  INFER_ERROR("Shape must be rank 4 but is rank 3", op, "[1,2,3];?;?");
  INFER_ERROR("Shape must be rank 1 but is rank 0", op, "?;[];?");
  INFER_ERROR("Dimension must be 2 but is 3", op, "?;[3];?");
  INFER_ERROR("Shape must be rank 2 but is rank 3", op, "?;?;[1,2,3]");

  // When the size tensor is not a constant, the middle dims are unknown.
  INFER_OK(op, "[1,?,3,?];[2];?", "[d0_0,?,?,d0_3]");

  Tensor size_tensor = test::AsTensor<int32>({20, 30});
  op.input_tensors[1] = &size_tensor;
  INFER_OK(op, "[1,?,3,?];[2];?", "[d0_0,20,30,d0_3]");

  // input.dim(0) and offsets.dim(0) are both the batch dimension.
  INFER_OK(op, "[?,?,3,?];[2];[1,?]", "[d2_0,20,30,d0_3]");
  INFER_OK(op, "[1,?,3,?];[2];[1,?]", "[d0_0|d2_0,20,30,d_0|d0_3]");
  INFER_ERROR("Dimensions must be equal, but are 10 and 1", op,
              "[10,?,?,?];?;[1,2]");
}

TEST(ImageOpsTest, CropAndResize_ShapeFn) {
  ShapeInferenceTestOp op("CropAndResize");
  op.input_tensors.resize(4);

  // Inputs are:  input, boxes, box_ind, and crop_size.

  // Rank and size checks.
  INFER_ERROR("Shape must be rank 4 but is rank 5", op, "[1,2,3,4,5];?;?;?");
  INFER_ERROR("Shape must be rank 4 but is rank 3", op, "[1,2,3];?;?;?");
  INFER_ERROR("Shape must be rank 2 but is rank 3", op, "?;[1,2,3];?;?");
  INFER_ERROR("Shape must be rank 1 but is rank 2", op, "?;?;[1,2];?");
  INFER_ERROR("Shape must be rank 1 but is rank 2", op, "?;?;?;[1,2]");
  INFER_ERROR("Dimension must be 2 but is 1", op, "?;?;?;[1]");

  // When the size tensor is not a constant, the middle dims are unknown.
  INFER_OK(op, "[1,?,3,?];?;?;[2]", "[?,?,?,d0_3]");

  Tensor size_tensor = test::AsTensor<int32>({20, 30});
  op.input_tensors[3] = &size_tensor;
  INFER_OK(op, "[1,?,3,?];?;?;[2]", "[?,20,30,d0_3]");

  // boxes.dim(0) and box_ind.dim(0) are both the num_boxes dim.
  INFER_OK(op, "[1,?,3,?];[2,4];?;[2]", "[d1_0,20,30,d0_3]");
  INFER_OK(op, "[1,?,3,?];?;[2];[2]", "[d2_0,20,30,d0_3]");
  INFER_OK(op, "[1,?,3,?];[?,4];[?];[2]", "[d1_0|d3_0,20,30,d0_3]");
  INFER_ERROR("Dimensions must be equal, but are 2 and 1", op, "?;[2,?];[1];?");

  // boxes.dim(1) must be 4.
  INFER_ERROR("Dimension must be 4 but is 3", op, "?;[?,3];?;?");
}

TEST(ImageOpsTest, ResizeNearestNeighborGrad_ShapeFn) {
  ShapeInferenceTestOp op("ResizeNearestNeighborGrad");
  op.input_tensors.resize(2);

  // Rank and size checks.
  INFER_ERROR("Shape must be rank 4 but is rank 3", op, "[1,2,3];?");
  INFER_ERROR("Shape must be rank 1 but is rank 2", op, "?;[1,2]")
  INFER_ERROR("Dimension must be 2 but is 1", op, "?;[1]");

  // When the size tensor is not a constant, the middle dims are unknown.
  INFER_OK(op, "[1,?,3,?];[2]", "[d0_0,?,?,d0_3]");

  Tensor size_tensor = test::AsTensor<int32>({20, 30});
  op.input_tensors[1] = &size_tensor;
  INFER_OK(op, "[1,?,3,?];[2]", "[d0_0,20,30,d0_3]");
}

TEST(ImageOpsTest, CropAndResizeGradImage_ShapeFn) {
  ShapeInferenceTestOp op("CropAndResizeGradImage");
  op.input_tensors.resize(4);

  // Rank checks.
  INFER_ERROR("Shape must be rank 1 but is rank 2", op, "?;?;?;[1,2]");

  // Unknown image_size should result in output of rank 4 with unknown dims.
  INFER_OK(op, "?;?;?;?", "[?,?,?,?]");

  // Known image_size should result in full shape information.
  Tensor image_size = test::AsTensor<int32>({10, 20, 30, 40});
  op.input_tensors[3] = &image_size;
  INFER_OK(op, "?;?;?;[1]", "[10, 20, 30, 40]");
}

TEST(ImageOpsTest, RandomCrop_ShapeFn) {
  ShapeInferenceTestOp op("RandomCrop");
  op.input_tensors.resize(2);

  // Rank checks.
  INFER_ERROR("must be rank 3", op, "[1,2];?");
  INFER_ERROR("must be equal", op, "?;[3]");
  INFER_ERROR("must be equal", op, "?;[1,2]");

  // Unknown size tensor values.
  INFER_OK(op, "[?,?,?];[2]", "[?,?,d0_2]");

  // Known size should result in full shape information.
  Tensor size = test::AsTensor<int64>({10, 20});
  op.input_tensors[1] = &size;
  INFER_OK(op, "[?,?,?];[2]", "[10,20,d0_2]");
}

TEST(ImageOpsTest, QuantizedResizeBilinear_ShapeFn) {
  ShapeInferenceTestOp op("QuantizedResizeBilinear");
  op.input_tensors.resize(4);

  NodeDefBuilder builder =
      NodeDefBuilder("test", "QuantizedResizeBilinear")
          .Input(NodeDefBuilder::NodeOut{"images", 0, DT_QINT32})
          .Input(NodeDefBuilder::NodeOut{"size", 0, DT_INT32})
          .Input(NodeDefBuilder::NodeOut{"min", 0, DT_FLOAT})
          .Input(NodeDefBuilder::NodeOut{"max", 0, DT_FLOAT})
          .Attr("T", DT_QINT32)
          .Attr("Toutput", DT_QINT32);
  TF_ASSERT_OK(builder.Finalize(&op.node_def));

  // When the size tensor is not a constant, the middle dims are unknown.
  INFER_OK(op, "[1,?,3,?];[2];[];[]",
           "[d0_0,?,?,d0_3];[];[]");  // output rank unknown
  INFER_ERROR("must be rank 0", op, "[1,?,3,?];[2];[?];[]");
  INFER_ERROR("must be rank 0", op, "[1,?,3,?];[2];[];[?]");

  const Tensor size_tensor = test::AsTensor<int32>({20, 30});
  op.input_tensors.at(1) = &size_tensor;
  INFER_OK(op, "[1,?,3,?];[2];[];[]", "[d0_0,20,30,d0_3];[];[]");
}

TEST(ImageOpsTest, DrawBoundingBoxes_ShapeFn) {
  ShapeInferenceTestOp op("DrawBoundingBoxes");
  op.input_tensors.resize(2);

  // Check images.
  INFER_ERROR("must be rank 4", op, "[1,?,3];?");
  INFER_ERROR("should be either 1 (GRY), 3 (RGB), or 4 (RGBA)",
      op, "[1,?,?,5];?");

  // Check boxes.
  INFER_ERROR("must be rank 3", op, "[1,?,?,4];[1,4]");
  INFER_ERROR("Dimension must be 4", op, "[1,?,?,4];[1,2,2]");

  // OK shapes.
  INFER_OK(op, "[4,?,?,4];?", "in0");
  INFER_OK(op, "[?,?,?,?];[?,?,?]", "in0");
  INFER_OK(op, "[4,?,?,4];[?,?,?]", "in0");
  INFER_OK(op, "[4,?,?,4];[?,?,4]", "in0");
}
}  // end namespace tensorflow