def testConstructGradientWithLargeVolumes(self, use_tape):
        with test_util.AbstractGradientTape(use_tape=use_tape) as tape:
            batch_size = 4
            planes = 8
            height = 32
            width = 32
            ksize = 5
            shape = (batch_size, planes, height, width, 1)

            volumes = variables.Variable(
                np.random.uniform(size=np.prod(shape)).reshape(shape),
                name='inputs')

            tape.watch(volumes)
            patches = array_ops.extract_volume_patches(
                volumes,
                ksizes=[1, ksize, ksize, ksize, 1],
                strides=[1, 1, 1, 1, 1],
                padding='SAME')
            # Github issue: #20146
            # tf.extract_volume_patches() gradient very slow at graph construction
            # time.
            gradients = tape.gradient(patches, volumes)
            # Won't time out.
            self.assertIsNotNone(gradients)
Example #2
0
  def _VerifyValues(self, image, ksizes, strides, padding, patches):
    """Tests input-output pairs for the ExtractVolumePatches op.

    Args:
      image: Input tensor with shape:
             [batch, in_planes, in_rows, in_cols, depth].
      ksizes: Patch size specified as: [ksize_planes, ksize_rows, ksize_cols].
      strides: Output strides, specified as:
               [stride_planes, stride_rows, stride_cols].
      padding: Padding type.
      patches: Expected output.

    Note:
      rates are not supported as of now.
    """
    ksizes = [1] + ksizes + [1]
    strides = [1] + strides + [1]

    out_tensor = array_ops.extract_volume_patches(
        constant_op.constant(image),
        ksizes=ksizes,
        strides=strides,
        padding=padding,
        name="im2col_3d")
    self.assertAllClose(patches, self.evaluate(out_tensor))
Example #3
0
    def call(self, inputs, **kwargs):
        # region Getting parameters for extraction
        if self.rank == 1:
            sizes = (1, 1, *self.kernel_size, 1)
            strides = (1, 1, *self.strides, 1)
            rates = (1, 1, *self.dilation_rate, 1)
        else:
            sizes = (1, *self.kernel_size, 1)
            strides = (1, *self.strides, 1)
            rates = (1, *self.dilation_rate, 1)
        # endregion

        # region Extraction
        if self.rank == 1:
            expanded_inputs = tf.expand_dims(inputs, axis=1)
            outputs = extract_image_patches(expanded_inputs, sizes=sizes, strides=strides, rates=rates, padding=self.padding)
            outputs = tf.squeeze(outputs, axis=1)
        elif self.rank == 2:
            outputs = extract_image_patches(inputs, sizes=sizes, strides=strides, rates=rates, padding=self.padding)
        elif self.rank == 3:
            outputs = extract_volume_patches(inputs, ksizes=sizes, strides=strides, padding=self.padding)
        else:
            raise AttributeError("Invalid rank : self.rank is {}.".format(self.rank))
        # endregion

        if len(outputs.shape) != len(inputs.shape):
            raise ValueError(outputs.shape, inputs.shape, self.name)

        return outputs
 def testConstructGradientWithLargeVolumess(self):
   batch_size = 4
   planes = 8
   height = 32
   width = 32
   ksize = 5
   volumes = variable_scope.get_variable(
       'inputs', (batch_size, planes, height, width, 1))
   patches = array_ops.extract_volume_patches(
       volumes,
       ksizes=[1, ksize, ksize, ksize, 1],
       strides=[1, 1, 1, 1, 1],
       padding='SAME')
   # Github issue: #20146
   # tf.extract_volume_patches() gradient very slow at graph construction time
   gradients = gradients_impl.gradients(patches, volumes)
   # Won't time out.
   self.assertIsNotNone(gradients)
    def testGradient(self, in_shape, ksizes, strides):
        # Set graph seed for determinism.
        random_seed = 42
        random_seed_lib.set_random_seed(random_seed)

        with self.cached_session():
            np.random.seed(random_seed)
            in_val = constant_op.constant(np.random.random(in_shape),
                                          dtype=dtypes.float32)

            for padding in ['VALID', 'SAME']:
                out_val = array_ops.extract_volume_patches(
                    in_val, ksizes, strides, padding)
                out_shape = out_val.get_shape().as_list()

                err = gradient_checker.compute_gradient_error(
                    in_val, in_shape, out_val, out_shape)

                print('extract_volume_patches gradient err: %.4e' % err)
                self.assertLess(err, 1e-4)
  def testGradient(self):
    # Set graph seed for determinism.
    random_seed = 42
    random_seed_lib.set_random_seed(random_seed)

    with self.cached_session():
      for test_case in self._TEST_CASES:
        np.random.seed(random_seed)
        in_shape = test_case['in_shape']
        in_val = constant_op.constant(
            np.random.random(in_shape), dtype=dtypes.float32)

        for padding in ['VALID', 'SAME']:
          out_val = array_ops.extract_volume_patches(
              in_val, test_case['ksizes'], test_case['strides'], padding)
          out_shape = out_val.get_shape().as_list()

          err = gradient_checker.compute_gradient_error(in_val, in_shape,
                                                        out_val, out_shape)

          print('extract_volume_patches gradient err: %.4e' % err)
          self.assertLess(err, 1e-4)
 def extract(in_val,
             ksizes=ksizes,
             strides=strides,
             padding=padding):
     return array_ops.extract_volume_patches(
         in_val, ksizes, strides, padding)