Exemplo n.º 1
0
 def compute_dilation2d(image_tensor, kernel_tensor):
     return nn_ops.dilation2d(image_tensor,
                              kernel_tensor,
                              strides=strides,
                              rates=rates,
                              padding=padding,
                              name="dilation2d")
Exemplo n.º 2
0
    def _ConstructAndTestGradient(self, image_shape, kernel_shape, strides,
                                  rates, padding, use_gpu):
        """Verifies the gradients of the dilation function.

    Args:
      image_shape: Input shape, [batch, in_height, in_width, channels].
      kernel_shape: Filter shape, [filter_height, filter_width, channels].
      strides: Output strides, specified as [stride_height, stride_width].
      rates: Atrous rates, specified as [rate_height, rate_width].
      padding: Padding type.
      use_gpu: Whether we are running on GPU.
    """
        assert image_shape[3] == kernel_shape[2]

        np.random.seed(1)  # Make it reproducible.
        image = np.random.random_sample(image_shape).astype(np.float32)
        kernel = np.random.random_sample(kernel_shape).astype(np.float32)
        image_init = np.random.random_sample(image_shape).astype(np.float32)
        kernel_init = np.random.random_sample(kernel_shape).astype(np.float32)

        strides = [1] + strides + [1]
        rates = [1] + rates + [1]

        with self.test_session(use_gpu=use_gpu):
            image_tensor = constant_op.constant(image,
                                                shape=image_shape,
                                                name="input")
            kernel_tensor = constant_op.constant(kernel,
                                                 shape=kernel_shape,
                                                 name="filter")
            out_tensor = nn_ops.dilation2d(image_tensor,
                                           kernel_tensor,
                                           strides=strides,
                                           rates=rates,
                                           padding=padding,
                                           name="dilation2d")
            out_shape = out_tensor.eval().shape

            # Small delta is necessary for argmax to remain the same.
            err = gradient_checker.compute_gradient_error(
                [image_tensor, kernel_tensor], [image_shape, kernel_shape],
                out_tensor,
                out_shape, [image_init, kernel_init],
                delta=1e-3)

        print("Dilation gradient error = %f" % err)
        self.assertLess(err, 1e-4)
  def _ConstructAndTestGradient(self, image_shape, kernel_shape, strides, rates,
                                padding, use_gpu):
    """Verifies the gradients of the dilation function.

    Args:
      image_shape: Input shape, [batch, in_height, in_width, channels].
      kernel_shape: Filter shape, [filter_height, filter_width, channels].
      strides: Output strides, specified as [stride_height, stride_width].
      rates: Atrous rates, specified as [rate_height, rate_width].
      padding: Padding type.
      use_gpu: Whether we are running on GPU.
    """
    assert image_shape[3] == kernel_shape[2]

    np.random.seed(1)  # Make it reproducible.
    image = np.random.random_sample(image_shape).astype(np.float32)
    kernel = np.random.random_sample(kernel_shape).astype(np.float32)
    image_init = np.random.random_sample(image_shape).astype(np.float32)
    kernel_init = np.random.random_sample(kernel_shape).astype(np.float32)

    strides = [1] + strides + [1]
    rates = [1] + rates + [1]

    with self.cached_session(use_gpu=use_gpu):
      image_tensor = constant_op.constant(
          image, shape=image_shape, name="input")
      kernel_tensor = constant_op.constant(
          kernel, shape=kernel_shape, name="filter")
      out_tensor = nn_ops.dilation2d(
          image_tensor,
          kernel_tensor,
          strides=strides,
          rates=rates,
          padding=padding,
          name="dilation2d")
      out_shape = self.evaluate(out_tensor).shape

      # Small delta is necessary for argmax to remain the same.
      err = gradient_checker.compute_gradient_error(
          [image_tensor, kernel_tensor], [image_shape, kernel_shape],
          out_tensor,
          out_shape, [image_init, kernel_init],
          delta=1e-3)

    print("Dilation gradient error = %f" % err)
    self.assertLess(err, 1e-4)
Exemplo n.º 4
0
    def _VerifyValues(self, image, kernel, strides, rates, padding, out,
                      use_gpu):
        """Verifies the output values of the dilation function.

    Args:
      image: Input tensor with shape: [batch, in_height, in_width, channels].
      kernel: Filter tensor with shape: [filter_height, filter_width, channels].
      strides: Output strides, specified as [stride_height, stride_width].
      rates: Atrous rates, specified as [rate_height, rate_width].
      padding: Padding type.
      out: Expected output.
      use_gpu: Whether we are running on GPU.
    """
        strides = [1] + strides + [1]
        rates = [1] + rates + [1]

        with self.test_session(use_gpu=use_gpu):
            out_tensor = nn_ops.dilation2d(constant_op.constant(image),
                                           constant_op.constant(kernel),
                                           strides=strides,
                                           rates=rates,
                                           padding=padding,
                                           name="dilation2d")
            self.assertAllClose(out, out_tensor.eval())
  def _VerifyValues(self, image, kernel, strides, rates, padding, out, use_gpu):
    """Verifies the output values of the dilation function.

    Args:
      image: Input tensor with shape: [batch, in_height, in_width, channels].
      kernel: Filter tensor with shape: [filter_height, filter_width, channels].
      strides: Output strides, specified as [stride_height, stride_width].
      rates: Atrous rates, specified as [rate_height, rate_width].
      padding: Padding type.
      out: Expected output.
      use_gpu: Whether we are running on GPU.
    """
    strides = [1] + strides + [1]
    rates = [1] + rates + [1]

    with self.cached_session(use_gpu=use_gpu):
      out_tensor = nn_ops.dilation2d(
          constant_op.constant(image),
          constant_op.constant(kernel),
          strides=strides,
          rates=rates,
          padding=padding,
          name="dilation2d")
      self.assertAllClose(out, self.evaluate(out_tensor))