Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict contrast_factor as scalar for AdjustContrastv2 shape #20030

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions tensorflow/core/ops/image_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ REGISTER_OP("AdjustContrast")
.Attr("T: {uint8, int8, int16, int32, int64, float, double}")
.Deprecated(2, "Use AdjustContrastv2 instead")
.SetShapeFn([](InferenceContext* c) {
// The contrast_factor, min_value, max_value should be scalar only.
ShapeHandle unused;
TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &unused));
TF_RETURN_IF_ERROR(c->WithRank(c->input(2), 0, &unused));
TF_RETURN_IF_ERROR(c->WithRank(c->input(3), 0, &unused));
return shape_inference::UnchangedShapeWithRankAtLeast(c, 3);
});

Expand All @@ -357,6 +362,9 @@ REGISTER_OP("AdjustContrastv2")
.Input("contrast_factor: float")
.Output("output: float")
.SetShapeFn([](InferenceContext* c) {
// The contrast_factor should be scalar only.
ShapeHandle unused;
TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &unused));
return shape_inference::UnchangedShapeWithRankAtLeast(c, 3);
});

Expand Down
8 changes: 8 additions & 0 deletions tensorflow/python/ops/image_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,14 @@ def testRandomContrast(self):
y_tf = self._adjustContrastTf(x_np, contrast_factor)
self.assertAllClose(y_tf, y_np, rtol=1e-5, atol=1e-5)

def testContrastFactorShape(self):
x_shape = [1, 2, 2, 3]
x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1]
x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape)
with self.assertRaisesRegexp(
ValueError, 'Shape must be rank 0 but is rank 1'):
image_ops.adjust_contrast(x_np, [2.0])


class AdjustBrightnessTest(test_util.TensorFlowTestCase):

Expand Down