Python tensorflow.batch_to_space_nd() Examples

The following are 17 code examples of tensorflow.batch_to_space_nd(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: batchtospace_op_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def _checkGrad(self, x, block_shape, crops):
    block_shape = np.array(block_shape)
    crops = np.array(crops).reshape((len(block_shape), 2))
    with self.test_session():
      tf_x = tf.convert_to_tensor(x)
      tf_y = tf.batch_to_space_nd(tf_x, block_shape, crops)
      epsilon = 1e-5
      ((x_jacob_t, x_jacob_n)) = tf.test.compute_gradient(
          tf_x,
          x.shape,
          tf_y,
          tf_y.get_shape().as_list(),
          x_init_value=x,
          delta=epsilon)

    self.assertAllClose(x_jacob_t, x_jacob_n, rtol=1e-2, atol=epsilon) 
Example #2
Source File: subpixel.py    From audio-super-res with MIT License 6 votes vote down vote up
def SubPixel1D_multichan(I, r):
  """One-dimensional subpixel upsampling layer

  Calls a tensorflow function that directly implements this functionality.
  We assume input has dim (batch, width, r).

  Works with multiple channels: (B,L,rC) -> (B,rL,C)
  """
  with tf.name_scope('subpixel'):
    _, w, rc = I.get_shape()
    assert rc % r == 0
    c = rc / r
    X = tf.transpose(I, [2,1,0]) # (rc, w, b)
    X = tf.batch_to_space_nd(X, [r], [[0,0]]) # (c, r*w, b)
    X = tf.transpose(X, [2,1,0])
    return X      

# ----------------------------------------------------------------------------

# demonstration 
Example #3
Source File: convert_test.py    From tf-encrypted with Apache License 2.0 5 votes vote down vote up
def test_batch_to_space_nd_convert(self):
        test_input = np.ones([8, 1, 3, 1])
        self._test_with_ndarray_input_fn(
            "batch_to_space_nd", test_input, protocol="Pond"
        ) 
Example #4
Source File: test_forward.py    From incubator-tvm with Apache License 2.0 5 votes vote down vote up
def _test_batch_to_space_nd(input_shape, block_shape, crops, dtype='int32'):
    data = np.random.uniform(0, 5, size=input_shape).astype(dtype)

    with tf.Graph().as_default():
        in_data = tf.placeholder(shape=input_shape, dtype=dtype)
        out = tf.batch_to_space_nd(in_data, block_shape, crops)

        compare_tf_with_tvm(data, in_data.name, out.name) 
Example #5
Source File: ops.py    From Tacotron2-Wavenet-Korean-TTS with MIT License 5 votes vote down vote up
def _phase_shift(self, inputs, batch_size, H, W, r1, r2):
        #Do a periodic shuffle on each output channel separately
        x = tf.reshape(inputs, [batch_size, H, W, r1, r2]) #[batch_size, H, W, r1, r2]

        #Width dim shuffle
        x = tf.transpose(x, [4, 2, 3, 1, 0]) #[r2, W, r1, H, batch_size]
        x = tf.batch_to_space_nd(x, [r2], [[0, 0]]) #[1, r2*W, r1, H, batch_size]
        x = tf.squeeze(x, [0]) #[r2*W, r1, H, batch_size]

        #Height dim shuffle
        x = tf.transpose(x, [1, 2, 0, 3]) #[r1, H, r2*W, batch_size]
        x = tf.batch_to_space_nd(x, [r1], [[0, 0]]) #[1, r1*H, r2*W, batch_size]
        x = tf.transpose(x, [3, 1, 2, 0]) #[batch_size, r1*H, r2*W, 1]

        return x 
Example #6
Source File: util.py    From inverse-compositional-STN with MIT License 5 votes vote down vote up
def imageSummaryMeanVar(opt,image,tag,H,W):
	image = tf.concat([image,np.zeros([2,H,W,3])],axis=0)
	imageOne = tf.batch_to_space_nd(image,crops=[[0,0],[0,0]],block_shape=[5,9])
	imagePermute = tf.reshape(imageOne,[H,5,W,9,-1])
	imageTransp = tf.transpose(imagePermute,[1,0,3,2,4])
	imageBlocks = tf.reshape(imageTransp,[1,H*5,W*9,-1])
	# imageBlocks = tf.cast(imageBlocks*255,tf.uint8)
	summary = tf.summary.image(tag,imageBlocks)
	return summary

# set optimizer for different learning rates 
Example #7
Source File: util.py    From inverse-compositional-STN with MIT License 5 votes vote down vote up
def imageSummaryMeanVar(opt,image,tag,H,W):
	imageOne = tf.batch_to_space_nd(image,crops=[[0,0],[0,0]],block_shape=[1,10])
	imagePermute = tf.reshape(imageOne,[H,1,W,10,-1])
	imageTransp = tf.transpose(imagePermute,[1,0,3,2,4])
	imageBlocks = tf.reshape(imageTransp,[1,H*1,W*10,-1])
	imageBlocks = tf.cast(imageBlocks*255,tf.uint8)
	summary = tf.summary.image(tag,imageBlocks)
	return summary

# set optimizer for different learning rates 
Example #8
Source File: subpixel.py    From audio-super-res with MIT License 5 votes vote down vote up
def SubPixel1D(I, r):
  """One-dimensional subpixel upsampling layer

  Calls a tensorflow function that directly implements this functionality.
  We assume input has dim (batch, width, r)
  """
  with tf.name_scope('subpixel'):
    X = tf.transpose(I, [2,1,0]) # (r, w, b)
    X = tf.batch_to_space_nd(X, [r], [[0,0]]) # (1, r*w, b)
    X = tf.transpose(X, [2,1,0])
    return X 
Example #9
Source File: modules.py    From Tacotron-2 with MIT License 5 votes vote down vote up
def _phase_shift(self, inputs, batch_size, H, W, r1, r2):
		#Do a periodic shuffle on each output channel separately
		x = tf.reshape(inputs, [batch_size, H, W, r1, r2]) #[batch_size, H, W, r1, r2]

		#Width dim shuffle
		x = tf.transpose(x, [4, 2, 3, 1, 0]) #[r2, W, r1, H, batch_size]
		x = tf.batch_to_space_nd(x, [r2], [[0, 0]]) #[1, r2*W, r1, H, batch_size]
		x = tf.squeeze(x, [0]) #[r2*W, r1, H, batch_size]

		#Height dim shuffle
		x = tf.transpose(x, [1, 2, 0, 3]) #[r1, H, r2*W, batch_size]
		x = tf.batch_to_space_nd(x, [r1], [[0, 0]]) #[1, r1*H, r2*W, batch_size]
		x = tf.transpose(x, [3, 1, 2, 0]) #[batch_size, r1*H, r2*W, 1]

		return x 
Example #10
Source File: convert_test.py    From tf-encrypted with Apache License 2.0 5 votes vote down vote up
def _construct_batch_to_space_nd(input_shape):
    a = tf.placeholder(tf.float32, shape=input_shape, name="input")
    block_shape = tf.constant([2, 2], dtype=tf.int32)
    crops = tf.constant([[0, 0], [2, 0]], dtype=tf.int32)
    x = tf.batch_to_space_nd(a, block_shape=block_shape, crops=crops)
    return x, a 
Example #11
Source File: batchtospace_op_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _testStaticShape(self, input_shape, block_shape, paddings, error):
    block_shape = np.array(block_shape)
    paddings = np.array(paddings)

    # Try with sizes known at graph construction time.
    with self.assertRaises(error):
      _ = tf.batch_to_space_nd(
          np.zeros(input_shape, np.float32), block_shape, paddings) 
Example #12
Source File: ops_test.py    From tf-encrypted with Apache License 2.0 5 votes vote down vote up
def _generic_masked_test(t, block_shape, crops):
        with tf.Session() as sess:
            out = tf.batch_to_space_nd(t, block_shape=block_shape, crops=crops)
            actual = sess.run(out)

        with tfe.protocol.Pond() as prot:
            b = prot.mask(prot.define_private_variable(t))
            out = prot.batch_to_space_nd(b, block_shape=block_shape, crops=crops)
            with tfe.Session() as sess:
                sess.run(tf.global_variables_initializer())
                final = sess.run(out.reveal())

        np.testing.assert_array_almost_equal(final, actual, decimal=3) 
Example #13
Source File: ops_test.py    From tf-encrypted with Apache License 2.0 5 votes vote down vote up
def _generic_private_test(t, block_shape, crops):
        with tf.Session() as sess:
            out = tf.batch_to_space_nd(t, block_shape=block_shape, crops=crops)
            actual = sess.run(out)

        with tfe.protocol.Pond() as prot:
            b = prot.define_private_variable(t)
            out = prot.batch_to_space_nd(b, block_shape=block_shape, crops=crops)
            with tfe.Session() as sess:
                sess.run(tf.global_variables_initializer())
                final = sess.run(out.reveal())

        np.testing.assert_array_almost_equal(final, actual, decimal=3) 
Example #14
Source File: ops_test.py    From tf-encrypted with Apache License 2.0 5 votes vote down vote up
def _generic_public_test(t, block_shape, crops):
        with tf.Session() as sess:
            out = tf.batch_to_space_nd(t, block_shape=block_shape, crops=crops)
            actual = sess.run(out)

        with tfe.protocol.Pond() as prot:
            b = prot.define_public_variable(t)
            out = prot.batch_to_space_nd(b, block_shape=block_shape, crops=crops)
            with tfe.Session() as sess:
                sess.run(tf.global_variables_initializer())
                final = sess.run(out)

        np.testing.assert_array_almost_equal(final, actual, decimal=3) 
Example #15
Source File: spacetobatch_op_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _testPad(self, inputs, block_shape, paddings, outputs):
    block_shape = np.array(block_shape)
    paddings = np.array(paddings).reshape((len(block_shape), 2))
    for use_gpu in [False, True]:
      with self.test_session(use_gpu=use_gpu):
        # outputs = space_to_batch(inputs)
        x_tf = tf.space_to_batch_nd(tf.to_float(inputs), block_shape, paddings)
        self.assertAllEqual(x_tf.eval(), outputs)
        # inputs = batch_to_space(outputs)
        x_tf = tf.batch_to_space_nd(tf.to_float(outputs), block_shape, paddings)
        self.assertAllEqual(x_tf.eval(), inputs) 
Example #16
Source File: batchtospace_op_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testUnknownShape(self):
    # Verify that input shape and paddings shape can be unknown.
    _ = tf.batch_to_space_nd(
        tf.placeholder(tf.float32),
        tf.placeholder(tf.int32, shape=(2,)),
        tf.placeholder(tf.int32))

    # Only number of input dimensions is known.
    t = tf.batch_to_space_nd(
        tf.placeholder(tf.float32, shape=(None, None, None, None)),
        tf.placeholder(tf.int32, shape=(2,)),
        tf.placeholder(tf.int32))
    self.assertEqual(4, t.get_shape().ndims)

    # Dimensions are partially known.
    t = tf.batch_to_space_nd(
        tf.placeholder(tf.float32, shape=(None, None, None, 2)),
        tf.placeholder(tf.int32, shape=(2,)),
        tf.placeholder(tf.int32))
    self.assertEqual([None, None, None, 2], t.get_shape().as_list())

    # Dimensions are partially known.
    t = tf.batch_to_space_nd(
        tf.placeholder(tf.float32, shape=(3 * 2 * 3, None, None, 2)), [2, 3],
        tf.placeholder(tf.int32))
    self.assertEqual([3, None, None, 2], t.get_shape().as_list())

    # Dimensions are partially known.
    t = tf.batch_to_space_nd(
        tf.placeholder(tf.float32, shape=(3 * 2 * 3, None, 2, 2)), [2, 3],
        [[1, 1], [0, 1]])
    self.assertEqual([3, None, 5, 2], t.get_shape().as_list())

    # Dimensions are fully known.
    t = tf.batch_to_space_nd(
        tf.placeholder(tf.float32, shape=(3 * 2 * 3, 2, 1, 2)), [2, 3],
        [[1, 1], [0, 0]])
    self.assertEqual([3, 2, 3, 2], t.get_shape().as_list()) 
Example #17
Source File: batchtospace_op_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _testDynamicShape(self, input_shape, block_shape, paddings):
    block_shape = np.array(block_shape)
    paddings = np.array(paddings)

    # Try with sizes unknown at graph construction time.
    input_placeholder = tf.placeholder(tf.float32)
    block_shape_placeholder = tf.placeholder(tf.int32, shape=block_shape.shape)
    paddings_placeholder = tf.placeholder(tf.int32)
    t = tf.batch_to_space_nd(input_placeholder, block_shape_placeholder,
                             paddings_placeholder)

    with self.assertRaises(ValueError):
      _ = t.eval({input_placeholder: np.zeros(input_shape, np.float32),
                  block_shape_placeholder: block_shape,
                  paddings_placeholder: paddings})