From c20d458972f97797e859d406fa2dad3cdf3cec56 Mon Sep 17 00:00:00 2001 From: Shashank Gupta Date: Tue, 26 Mar 2019 23:07:33 +0530 Subject: [PATCH 1/6] Added array_ops.rank(y_pred) ```y_pred.shape.ndims``` -> Not supports **list** Whereas ```array_ops.rank(y_pred).numpy()``` works for all (Tensors, Numpy arrays, lists) --- tensorflow/python/keras/metrics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow/python/keras/metrics.py b/tensorflow/python/keras/metrics.py index 02429706505..9ea01c6fbe6 100644 --- a/tensorflow/python/keras/metrics.py +++ b/tensorflow/python/keras/metrics.py @@ -2273,10 +2273,10 @@ class MeanIoU(Metric): Update op. """ # Flatten the input if its rank > 1. - if y_pred.shape.ndims > 1: + if array_ops.rank(y_pred).numpy() > 1: y_pred = array_ops.reshape(y_pred, [-1]) - if y_true.shape.ndims > 1: + if array_ops.rank(y_true).numpy() > 1: y_true = array_ops.reshape(y_true, [-1]) if sample_weight is not None and sample_weight.shape.ndims > 1: From 9e6e6f0e11aa05763fc20b6b3f5af5398686ff74 Mon Sep 17 00:00:00 2001 From: Shashank Gupta Date: Wed, 27 Mar 2019 03:05:41 +0530 Subject: [PATCH 2/6] Converted y_pred and y_true into Tensors --- tensorflow/python/keras/metrics.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tensorflow/python/keras/metrics.py b/tensorflow/python/keras/metrics.py index 9ea01c6fbe6..a1d07bea48f 100644 --- a/tensorflow/python/keras/metrics.py +++ b/tensorflow/python/keras/metrics.py @@ -2272,11 +2272,15 @@ class MeanIoU(Metric): Returns: Update op. """ + # Converted y_true and y_pred into Tensors + y_true = ops.convert_to_tensor(y_true) + y_pred = ops.convert_to_tensor(y_pred) + # Flatten the input if its rank > 1. - if array_ops.rank(y_pred).numpy() > 1: + if y_pred.shape.ndims > 1: y_pred = array_ops.reshape(y_pred, [-1]) - if array_ops.rank(y_true).numpy() > 1: + if y_true.shape.ndims > 1: y_true = array_ops.reshape(y_true, [-1]) if sample_weight is not None and sample_weight.shape.ndims > 1: From 8a5f87c10029d36bc60e4754dc94f671a9d29324 Mon Sep 17 00:00:00 2001 From: Shashank Gupta Date: Wed, 27 Mar 2019 03:22:15 +0530 Subject: [PATCH 3/6] Added test case for the given list inputs --- tensorflow/python/keras/metrics.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tensorflow/python/keras/metrics.py b/tensorflow/python/keras/metrics.py index a1d07bea48f..056db5d191e 100644 --- a/tensorflow/python/keras/metrics.py +++ b/tensorflow/python/keras/metrics.py @@ -2272,9 +2272,14 @@ class MeanIoU(Metric): Returns: Update op. """ - # Converted y_true and y_pred into Tensors - y_true = ops.convert_to_tensor(y_true) - y_pred = ops.convert_to_tensor(y_pred) + + # If y_true and y_pred are lists then + # they are converted into Tensors + if type(y_true) == list: + y_true = ops.convert_to_tensor(y_true) + + if type(y_pred) == list: + y_pred = ops.convert_to_tensor(y_pred) # Flatten the input if its rank > 1. if y_pred.shape.ndims > 1: From d3bfa8d1f243d848e887632c31469686ddc656c6 Mon Sep 17 00:00:00 2001 From: Shashank Gupta Date: Thu, 25 Apr 2019 23:51:21 +0530 Subject: [PATCH 4/6] Update metrics.py --- tensorflow/python/keras/metrics.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tensorflow/python/keras/metrics.py b/tensorflow/python/keras/metrics.py index 056db5d191e..b298b73a52f 100644 --- a/tensorflow/python/keras/metrics.py +++ b/tensorflow/python/keras/metrics.py @@ -2275,11 +2275,8 @@ class MeanIoU(Metric): # If y_true and y_pred are lists then # they are converted into Tensors - if type(y_true) == list: - y_true = ops.convert_to_tensor(y_true) - - if type(y_pred) == list: - y_pred = ops.convert_to_tensor(y_pred) + y_true = math_ops.cast(y_true, self._dtype) + y_pred = math_ops.cast(y_pred, self._dtype) # Flatten the input if its rank > 1. if y_pred.shape.ndims > 1: From 252a7c7dbdf779849057244096b8629ac3cd4060 Mon Sep 17 00:00:00 2001 From: Shashank Gupta Date: Fri, 26 Apr 2019 01:13:39 +0530 Subject: [PATCH 5/6] Update metrics_test.py Inputs changed for the method "test_unweighted" of class "MeanIoU" --- tensorflow/python/keras/metrics_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow/python/keras/metrics_test.py b/tensorflow/python/keras/metrics_test.py index d210f0ebeea..82bce7791a3 100644 --- a/tensorflow/python/keras/metrics_test.py +++ b/tensorflow/python/keras/metrics_test.py @@ -1141,8 +1141,8 @@ class MeanIoUTest(test.TestCase): self.assertEqual(m_obj2.num_classes, 2) def test_unweighted(self): - y_pred = constant_op.constant([0, 1, 0, 1], dtype=dtypes.float32) - y_true = constant_op.constant([0, 0, 1, 1]) + y_pred = [0, 1, 0, 1] + y_true = [0, 0, 1, 1] m_obj = metrics.MeanIoU(num_classes=2) self.evaluate(variables.variables_initializer(m_obj.variables)) From 75255d675852af8d35ecdd5a5bc72009588cf268 Mon Sep 17 00:00:00 2001 From: Shashank Gupta Date: Fri, 26 Apr 2019 01:55:55 +0530 Subject: [PATCH 6/6] Update metrics.py --- tensorflow/python/keras/metrics.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tensorflow/python/keras/metrics.py b/tensorflow/python/keras/metrics.py index b298b73a52f..b666bfeb150 100644 --- a/tensorflow/python/keras/metrics.py +++ b/tensorflow/python/keras/metrics.py @@ -2273,8 +2273,6 @@ class MeanIoU(Metric): Update op. """ - # If y_true and y_pred are lists then - # they are converted into Tensors y_true = math_ops.cast(y_true, self._dtype) y_pred = math_ops.cast(y_pred, self._dtype)