def testGlimpseNonNormalizedNonCentered(self):
   img = constant_op.constant(np.arange(25).reshape((1, 5, 5, 1)),
                              dtype=dtypes.float32)
   with self.test_session():
     result1 = image_ops.extract_glimpse(img, [3, 3], [[0, 0]],
                                         centered=False, normalized=False)
     result2 = image_ops.extract_glimpse(img, [3, 3], [[1, 0]],
                                         centered=False, normalized=False)
     self.assertAllEqual(np.asarray([[0, 1, 2], [5, 6, 7], [10, 11, 12]]),
                         result1.eval()[0, :, :, 0])
     self.assertAllEqual(np.asarray([[5, 6, 7], [10, 11, 12], [15, 16, 17]]),
                         result2.eval()[0, :, :, 0])
Esempio n. 2
0
 def testEmptyTensor(self):
     empty_image = np.zeros((0, 4, 3, 0))
     offsets = np.zeros((0, 2))
     with self.cached_session():
         result = image_ops.extract_glimpse(empty_image, [1, 1], offsets)
         self.assertAllEqual(np.zeros((0, 1, 1, 0), dtype=np.float32),
                             self.evaluate(result))
 def testEmptyTensor(self):
   empty_image = np.zeros((0, 4, 3, 0))
   offsets = np.zeros((0, 2))
   with self.cached_session():
     result = image_ops.extract_glimpse(empty_image, [1, 1], offsets)
     self.assertAllEqual(
         np.zeros((0, 1, 1, 0), dtype=np.float32), self.evaluate(result))
Esempio n. 4
0
 def testGlimpseNonNormalizedNonCentered(self):
     img = constant_op.constant(np.arange(25).reshape((1, 5, 5, 1)),
                                dtype=dtypes.float32)
     with self.test_session():
         result1 = image_ops.extract_glimpse(img, [3, 3], [[0, 0]],
                                             centered=False,
                                             normalized=False)
         result2 = image_ops.extract_glimpse(img, [3, 3], [[1, 0]],
                                             centered=False,
                                             normalized=False)
         self.assertAllEqual(
             np.asarray([[0, 1, 2], [5, 6, 7], [10, 11, 12]]),
             result1.eval()[0, :, :, 0])
         self.assertAllEqual(
             np.asarray([[5, 6, 7], [10, 11, 12], [15, 16, 17]]),
             result2.eval()[0, :, :, 0])
Esempio n. 5
0
    def _VerifyValues(self, tensor_in_sizes, glimpse_sizes, offsets,
                      expected_rows, expected_cols):
        """Verifies the output values of the glimpse extraction kernel.

    Args:
      tensor_in_sizes: Input tensor dimensions in [input_rows, input_cols].
      glimpse_sizes: Dimensions of the glimpse in [glimpse_rows, glimpse_cols].
      offsets: Relative location of the center of the glimpse in the input
        image expressed as [row_offset, col_offset].
      expected_rows: A list containing the expected row numbers (None for
         out of bound entries that are expected to be replaced by uniform
         random entries in [0,1) ).
      expected_cols: Same as expected_rows, but for column numbers.
    """

        rows = tensor_in_sizes[0]
        cols = tensor_in_sizes[1]
        # Row Tensor with entries by row.
        # [[ 1 1 1 ... ]
        #  [ 2 2 2 ... ]
        #  [ 3 3 3 ... ]
        #  [ ...
        # ]
        t_rows = array_ops.tile([[1.0 * r] for r in range(1, rows + 1)],
                                [1, cols],
                                name='tile_rows')

        # Shuffle to switch to a convention of (batch_size, height, width, depth).
        t_rows_4d = array_ops.transpose(
            array_ops.expand_dims(array_ops.expand_dims(t_rows, 0), 3),
            [0, 2, 1, 3])

        # Column Tensor with entries by column.
        # [[ 1 2 3 4 ... ]
        #  [ 1 2 3 4 ... ]
        #  [ 1 2 3 4 ... ]
        #  [ ...         ]
        # ]
        t_cols = array_ops.tile([[1.0 * r for r in range(1, cols + 1)]],
                                [rows, 1],
                                name='tile_cols')

        # Shuffle to switch to a convention of (batch_size, height, width, depth).
        t_cols_4d = array_ops.transpose(
            array_ops.expand_dims(array_ops.expand_dims(t_cols, 0), 3),
            [0, 2, 1, 3])

        # extract_glimpses from Row and Column Tensor, respectively.
        # Switch order for glimpse_sizes and offsets to switch from (row, col)
        # convention to tensorflows (height, width) convention.
        t1 = constant_op.constant([glimpse_sizes[1], glimpse_sizes[0]],
                                  shape=[2])
        t2 = constant_op.constant([offsets[1], offsets[0]], shape=[1, 2])
        glimpse_rows = (array_ops.transpose(
            image_ops.extract_glimpse(t_rows_4d, t1, t2), [0, 2, 1, 3]))
        glimpse_cols = (array_ops.transpose(
            image_ops.extract_glimpse(t_cols_4d, t1, t2), [0, 2, 1, 3]))

        # Evaluate the TensorFlow Graph.
        with self.cached_session() as sess:
            value_rows, value_cols = self.evaluate(
                [glimpse_rows, glimpse_cols])

        # Check dimensions of returned glimpse.
        self.assertEqual(value_rows.shape[1], glimpse_sizes[0])
        self.assertEqual(value_rows.shape[2], glimpse_sizes[1])
        self.assertEqual(value_cols.shape[1], glimpse_sizes[0])
        self.assertEqual(value_cols.shape[2], glimpse_sizes[1])

        # Check entries.
        min_random_val = 0
        max_random_val = max(rows, cols)
        for i in range(glimpse_sizes[0]):
            for j in range(glimpse_sizes[1]):
                if expected_rows[i] is None or expected_cols[j] is None:
                    self.assertGreaterEqual(value_rows[0][i][j][0],
                                            min_random_val)
                    self.assertLessEqual(value_rows[0][i][j][0],
                                         max_random_val)
                    self.assertGreaterEqual(value_cols[0][i][j][0],
                                            min_random_val)
                    self.assertLessEqual(value_cols[0][i][j][0],
                                         max_random_val)
                else:
                    self.assertEqual(value_rows[0][i][j][0], expected_rows[i])
                    self.assertEqual(value_cols[0][i][j][0], expected_cols[j])
  def _VerifyValues(self, tensor_in_sizes, glimpse_sizes, offsets,
                    expected_rows, expected_cols):
    """Verifies the output values of the glimpse extraction kernel.

    Args:
      tensor_in_sizes: Input tensor dimensions in [input_rows, input_cols].
      glimpse_sizes: Dimensions of the glimpse in [glimpse_rows, glimpse_cols].
      offsets: Relative location of the center of the glimpse in the input
        image expressed as [row_offset, col_offset].
      expected_rows: A list containing the expected row numbers (None for
         out of bound entries that are expected to be replaced by uniform
         random entries in [0,1) ).
      expected_cols: Same as expected_rows, but for column numbers.
    """

    rows = tensor_in_sizes[0]
    cols = tensor_in_sizes[1]
    # Row Tensor with entries by row.
    # [[ 1 1 1 ... ]
    #  [ 2 2 2 ... ]
    #  [ 3 3 3 ... ]
    #  [ ...
    # ]
    t_rows = array_ops.tile(
        [[1.0 * r] for r in range(1, rows + 1)], [1, cols], name='tile_rows')

    # Shuffle to switch to a convention of (batch_size, height, width, depth).
    t_rows_4d = array_ops.transpose(
        array_ops.expand_dims(array_ops.expand_dims(t_rows, 0), 3),
        [0, 2, 1, 3])

    # Column Tensor with entries by column.
    # [[ 1 2 3 4 ... ]
    #  [ 1 2 3 4 ... ]
    #  [ 1 2 3 4 ... ]
    #  [ ...         ]
    # ]
    t_cols = array_ops.tile(
        [[1.0 * r for r in range(1, cols + 1)]], [rows, 1], name='tile_cols')

    # Shuffle to switch to a convention of (batch_size, height, width, depth).
    t_cols_4d = array_ops.transpose(
        array_ops.expand_dims(array_ops.expand_dims(t_cols, 0), 3),
        [0, 2, 1, 3])

    # extract_glimpses from Row and Column Tensor, respectively.
    # Switch order for glimpse_sizes and offsets to switch from (row, col)
    # convention to tensorflows (height, width) convention.
    t1 = constant_op.constant([glimpse_sizes[1], glimpse_sizes[0]], shape=[2])
    t2 = constant_op.constant([offsets[1], offsets[0]], shape=[1, 2])
    glimpse_rows = (array_ops.transpose(
        image_ops.extract_glimpse(t_rows_4d, t1, t2), [0, 2, 1, 3]))
    glimpse_cols = (array_ops.transpose(
        image_ops.extract_glimpse(t_cols_4d, t1, t2), [0, 2, 1, 3]))

    # Evaluate the TensorFlow Graph.
    with self.test_session() as sess:
      value_rows, value_cols = sess.run([glimpse_rows, glimpse_cols])

    # Check dimensions of returned glimpse.
    self.assertEqual(value_rows.shape[1], glimpse_sizes[0])
    self.assertEqual(value_rows.shape[2], glimpse_sizes[1])
    self.assertEqual(value_cols.shape[1], glimpse_sizes[0])
    self.assertEqual(value_cols.shape[2], glimpse_sizes[1])

    # Check entries.
    min_random_val = 0
    max_random_val = max(rows, cols)
    for i in range(glimpse_sizes[0]):
      for j in range(glimpse_sizes[1]):
        if expected_rows[i] is None or expected_cols[j] is None:
          self.assertGreaterEqual(value_rows[0][i][j][0], min_random_val)
          self.assertLessEqual(value_rows[0][i][j][0], max_random_val)
          self.assertGreaterEqual(value_cols[0][i][j][0], min_random_val)
          self.assertLessEqual(value_cols[0][i][j][0], max_random_val)
        else:
          self.assertEqual(value_rows[0][i][j][0], expected_rows[i])
          self.assertEqual(value_cols[0][i][j][0], expected_cols[j])