Пример #1
0
def bilateral_slice_apply(grid,
                          guide,
                          input_image,
                          has_offset=True,
                          name=None):
    """Slices into a bilateral grid using the guide map.

  Args:
    grid: (Tensor) [batch_size, grid_h, grid_w, depth, n_outputs]
      grid to slice from.
    guide: (Tensor) [batch_size, h, w ] guide map to slice along.
    input_image: (Tensor) [batch_size, h, w, n_input] input data onto which to
      apply the affine transform.
    name: (string) name for the operation.
  Returns:
    sliced: (Tensor) [batch_size, h, w, n_outputs] sliced output.
  """

    with tf.name_scope(name):
        gridshape = grid.get_shape().as_list()
        if len(gridshape) == 6:
            gs = tf.shape(grid)
            _, _, _, _, n_out, n_in = gridshape
            grid = tf.reshape(
                grid, tf.stack([gs[0], gs[1], gs[2], gs[3], gs[4] * gs[5]]))
            # grid = tf.concat(tf.unstack(grid, None, axis=5), 4)

        sliced = hdrnet_ops.bilateral_slice_apply(grid,
                                                  guide,
                                                  input_image,
                                                  has_offset=has_offset)
        return sliced
Пример #2
0
    def run_bilateral_slice_apply_grad(self,
                                       grid_data,
                                       guide_data,
                                       input_data,
                                       use_gpu=False):
        dev = _get_device_string(use_gpu)

        graph = tf.Graph()
        with graph.as_default():
            with tf.device(dev):
                grid_tensor = tf.convert_to_tensor(grid_data,
                                                   name='grid',
                                                   dtype=tf.float32)
                guide_tensor = tf.convert_to_tensor(guide_data,
                                                    name='guide',
                                                    dtype=tf.float32)
                input_tensor = tf.convert_to_tensor(input_data,
                                                    name='input',
                                                    dtype=tf.float32)
                output_tensor = ops.bilateral_slice_apply(grid_tensor,
                                                          guide_tensor,
                                                          input_tensor,
                                                          has_offset=True)
                grid_grad_tensor, guide_grad_tensor, _ = tf.gradients(
                    output_tensor, [grid_tensor, guide_tensor, input_tensor])
                with self.test_session(graph=graph,
                                       use_gpu=use_gpu,
                                       force_gpu=use_gpu) as sess:
                    grid_grad_data, guide_grad_data = sess.run(
                        [grid_grad_tensor, guide_grad_tensor])

        return (grid_grad_tensor, guide_grad_tensor, grid_grad_data,
                guide_grad_data)
Пример #3
0
    def run_grad_test(self, batch_size, h, w, input_channels, gh, gw, gd,
                      output_channels, use_gpu, grad_tensor_name):
        grid_shape = (batch_size, gh, gw, gd,
                      (1 + input_channels) * output_channels)
        guide_shape = (batch_size, h, w)
        input_shape = (batch_size, h, w, input_channels)
        output_shape = (batch_size, h, w, output_channels)

        grid_data = np.random.rand(*grid_shape).astype(np.float32)
        guide_data = np.random.rand(*guide_shape).astype(np.float32)
        input_data = np.random.rand(*input_shape).astype(np.float32)

        dev = _get_device_string(use_gpu)

        tf.reset_default_graph()
        graph = tf.Graph()
        with graph.as_default():
            with tf.device(dev):
                grid_tensor = tf.convert_to_tensor(grid_data,
                                                   name='grid',
                                                   dtype=tf.float32)
                guide_tensor = tf.convert_to_tensor(guide_data,
                                                    name='guide',
                                                    dtype=tf.float32)
                input_tensor = tf.convert_to_tensor(input_data,
                                                    name='input',
                                                    dtype=tf.float32)

                output_tensor = ops.bilateral_slice_apply(grid_tensor,
                                                          guide_tensor,
                                                          input_tensor,
                                                          has_offset=True)

                if grad_tensor_name == 'grid':
                    grad_tensor = grid_tensor
                    grad_shape = grid_shape
                elif grad_tensor_name == 'guide':
                    grad_tensor = guide_tensor
                    grad_shape = guide_shape
                elif grad_tensor_name == 'input':
                    grad_tensor = input_tensor
                    grad_shape = input_shape

                # It is important to use self.test_session, which will disable the
                # graph optimization, otherwise it won't use GPU ops. See details here:
                # https://github.com/tensorflow/tensorflow/issues/2054
                with self.test_session(graph=graph,
                                       use_gpu=use_gpu,
                                       force_gpu=use_gpu):
                    err = tf.test.compute_gradient_error(
                        grad_tensor, grad_shape, output_tensor, output_shape)
                self.assertLess(err, 1e-2)