예제 #1
0
 def testInt32Axis(self):
   x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]])
   with self.test_session() as sess:
     y0, idx0 = gen_array_ops.unique_v2(x, axis=[0])
     tf_y0, tf_idx0 = sess.run([y0, idx0])
     y1, idx1 = gen_array_ops.unique_v2(x, axis=[1])
     tf_y1, tf_idx1 = sess.run([y1, idx1])
   self.assertAllEqual(tf_y0, np.array([[1, 0, 0], [2, 0, 0]]))
   self.assertAllEqual(tf_idx0, np.array([0, 0, 1]))
   self.assertAllEqual(tf_y1, np.array([[1, 0], [1, 0], [2, 0]]))
   self.assertAllEqual(tf_idx1, np.array([0, 1, 1]))
예제 #2
0
 def testInt32Axis(self):
     x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]])
     with self.test_session() as sess:
         y0, idx0 = gen_array_ops.unique_v2(x, axis=[0])
         tf_y0, tf_idx0 = sess.run([y0, idx0])
         y1, idx1 = gen_array_ops.unique_v2(x, axis=[1])
         tf_y1, tf_idx1 = sess.run([y1, idx1])
     self.assertAllEqual(tf_y0, np.array([[1, 0, 0], [2, 0, 0]]))
     self.assertAllEqual(tf_idx0, np.array([0, 0, 1]))
     self.assertAllEqual(tf_y1, np.array([[1, 0], [1, 0], [2, 0]]))
     self.assertAllEqual(tf_idx1, np.array([0, 1, 1]))
예제 #3
0
 def testInt32Axis(self):
   for dtype in [np.int32, np.int64]:
     x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]])
     with self.cached_session() as sess:
       y0, idx0 = gen_array_ops.unique_v2(x, axis=np.array([0], dtype))
       tf_y0, tf_idx0 = self.evaluate([y0, idx0])
       y1, idx1 = gen_array_ops.unique_v2(x, axis=np.array([1], dtype))
       tf_y1, tf_idx1 = self.evaluate([y1, idx1])
     self.assertAllEqual(tf_y0, np.array([[1, 0, 0], [2, 0, 0]]))
     self.assertAllEqual(tf_idx0, np.array([0, 0, 1]))
     self.assertAllEqual(tf_y1, np.array([[1, 0], [1, 0], [2, 0]]))
     self.assertAllEqual(tf_idx1, np.array([0, 1, 1]))
 def testInt32Axis(self):
   for dtype in [np.int32, np.int64]:
     x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]])
     with self.cached_session() as sess:
       y0, idx0 = gen_array_ops.unique_v2(x, axis=np.array([0], dtype))
       tf_y0, tf_idx0 = self.evaluate([y0, idx0])
       y1, idx1 = gen_array_ops.unique_v2(x, axis=np.array([1], dtype))
       tf_y1, tf_idx1 = self.evaluate([y1, idx1])
     self.assertAllEqual(tf_y0, np.array([[1, 0, 0], [2, 0, 0]]))
     self.assertAllEqual(tf_idx0, np.array([0, 0, 1]))
     self.assertAllEqual(tf_y1, np.array([[1, 0], [1, 0], [2, 0]]))
     self.assertAllEqual(tf_idx1, np.array([0, 1, 1]))
예제 #5
0
 def testInt32Axis(self):
   for dtype in [np.int32, np.int64]:
     x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]])
     y0, idx0 = gen_array_ops.unique_v2(x, axis=np.array([0], dtype))
     self.assertEqual(y0.shape.rank, 2)
     tf_y0, tf_idx0 = self.evaluate([y0, idx0])
     y1, idx1 = gen_array_ops.unique_v2(x, axis=np.array([1], dtype))
     self.assertEqual(y1.shape.rank, 2)
     tf_y1, tf_idx1 = self.evaluate([y1, idx1])
     self.assertAllEqual(tf_y0, np.array([[1, 0, 0], [2, 0, 0]]))
     self.assertAllEqual(tf_idx0, np.array([0, 0, 1]))
     self.assertAllEqual(tf_y1, np.array([[1, 0], [1, 0], [2, 0]]))
     self.assertAllEqual(tf_idx1, np.array([0, 1, 1]))
예제 #6
0
def unique_2d(tensor):
    from tensorflow.python.ops import gen_array_ops
    ids, idx = gen_array_ops.unique_v2(tensor, axis=[0])
    # static shape of unique_v2 ids (0th) and idx(1th) are swapped by mistake.
    ids = tf.reshape(ids, [-1, tf.shape(tensor)[1]])
    idx = tf.reshape(idx, [-1])
    return ids, idx
예제 #7
0
  def testBoolV2(self):
    x = np.random.choice([True, False], size=7000)
    y, idx = gen_array_ops.unique_v2(x, axis=np.array([], np.int32))
    tf_y, tf_idx = self.evaluate([y, idx])

    self.assertEqual(len(x), len(tf_idx))
    self.assertEqual(len(tf_y), len(np.unique(x)))
    for i in range(len(x)):
      self.assertEqual(x[i], tf_y[tf_idx[i]])
예제 #8
0
  def testInt32V2(self):
    # This test is only temporary, once V2 is used
    # by default, the axis will be wrapped to allow `axis=None`.
    x = np.random.randint(2, high=10, size=7000)
    y, idx = gen_array_ops.unique_v2(x, axis=np.array([], np.int32))
    tf_y, tf_idx = self.evaluate([y, idx])

    self.assertEqual(len(x), len(tf_idx))
    self.assertEqual(len(tf_y), len(np.unique(x)))
    for i in range(len(x)):
      self.assertEqual(x[i], tf_y[tf_idx[i]])
예제 #9
0
  def testInt32V2(self):
    # This test is only temporary, once V2 is used
    # by default, the axis will be wrapped to allow `axis=None`.
    x = np.random.randint(2, high=10, size=7000)
    with self.test_session() as sess:
      y, idx = gen_array_ops.unique_v2(x, axis=[])
      tf_y, tf_idx = sess.run([y, idx])

    self.assertEqual(len(x), len(tf_idx))
    self.assertEqual(len(tf_y), len(np.unique(x)))
    for i in range(len(x)):
      self.assertEqual(x[i], tf_y[tf_idx[i]])
예제 #10
0
    def test_tf_img_to_minmax(self):
        # Expected Result
        threshold = 127
        min_maxed_unique_values = (255, 0)
        number_of_black_color = 31760

        # 1) Get sample image as tf
        test_file_name: str = "sample.png"
        test_image_fullpath: str = os.path.join(
            self.test_path, self.test_resource_folder_name, test_file_name
        )
        grayscale_tf_image = tf_images.decode_png(test_image_fullpath, 1)

        # 2) Calculate min max
        min_maxed_grayscale_tf_image = tf_images.tf_img_to_minmax(
            grayscale_tf_image, threshold, (0, 255)
        )

        # check unique
        reshaped_min_maxed_grayscale_tf_image = tf.reshape(
            min_maxed_grayscale_tf_image, (-1, 1)
        )
        unique_min_max_values = gen_array_ops.unique_v2(
            reshaped_min_maxed_grayscale_tf_image, axis=[-2]
        )[0]

        self.assertTrue(
            tf.math.reduce_all(
                tf.math.equal(
                    unique_min_max_values,
                    tf.cast(
                        tf.expand_dims(tf.constant(min_maxed_unique_values), axis=-1),
                        tf.float32,
                    ),
                )
            )
        )
        self.assertTrue(
            tf.math.equal(
                tf.math.count_nonzero(min_maxed_grayscale_tf_image),
                number_of_black_color,
            )
        )
예제 #11
0
    def testShapeInferenceV2(self):
        """Test shape inference."""
        x = np.arange(6).reshape(3, 2, 1)
        _, idx = gen_array_ops.unique_v2(x, axis=[0])
        self.assertEqual(idx.shape.as_list(), [3])
        _, idx = gen_array_ops.unique_v2(x, axis=[1])
        self.assertEqual(idx.shape.as_list(), [2])
        _, idx = gen_array_ops.unique_v2(x, axis=[2])
        self.assertEqual(idx.shape.as_list(), [1])
        _, idx = gen_array_ops.unique_v2(x, axis=[-1])
        self.assertEqual(idx.shape.as_list(), [1])
        _, idx = gen_array_ops.unique_v2(x, axis=[-2])
        self.assertEqual(idx.shape.as_list(), [2])
        _, idx = gen_array_ops.unique_v2(x, axis=[-3])
        self.assertEqual(idx.shape.as_list(), [3])
        _, idx = gen_array_ops.unique_v2([0, 1, 2], axis=[])
        self.assertEqual(idx.shape.as_list(), [3])

        with self.assertRaisesRegexp(ValueError, "axis expects a 1D vector"):
            gen_array_ops.unique_v2(x, axis=[[0]])

        with self.assertRaisesRegexp(ValueError, "x expects a 1D vector"):
            gen_array_ops.unique_v2(x, axis=[])

        with self.assertRaisesRegexp(
                ValueError, "axis does not support input tensors larger than"):
            gen_array_ops.unique_v2(x, axis=[1, 2])

        with self.assertRaisesRegexp(
                ValueError, r"axis expects to be in the range \[-3, 3\)"):
            gen_array_ops.unique_v2(x, axis=[3])

        with self.assertRaisesRegexp(
                ValueError, r"axis expects to be in the range \[-3, 3\)"):
            gen_array_ops.unique_v2(x, axis=[-4])

        x_t = array_ops.placeholder(dtypes.int32, shape=None)
        _, idx = gen_array_ops.unique_v2(x_t, axis=[0])
        self.assertEqual(idx.shape.as_list(), [None])

        axis_t = array_ops.placeholder(dtypes.int32, shape=None)
        _, idx = gen_array_ops.unique_v2(x, axis=axis_t)
        self.assertEqual(idx.shape.as_list(), [None])