def testReshapeDepth(self): """Tests that depth can be reshaped to the x dimension.""" fake = tf.placeholder(tf.float32, shape=(None, None, None, self.depth), name='inputs') real = _rand(self.batch_size, self.im_height, self.im_width, self.depth) with self.test_session() as sess: outputs = shapes.transposing_reshape(fake, src_dim=3, part_a=4, part_b=-1, dest_dim_a=2, dest_dim_b=3) res_image = sess.run([outputs], feed_dict={fake: real}) self.assertEqual(tuple(res_image[0].shape), (self.batch_size, self.im_height, self.im_width * 4, self.depth / 4))
def AddReShape(self, prev_layer, index): """Reshapes the input tensor by moving each (x_scale,y_scale) rectangle to. the depth dimension. NOTE that the TF convention is that inputs are [batch, y, x, depth]. Args: prev_layer: Input tensor. index: Position in model_str to start parsing Returns: Output tensor, end index in model_str. """ pattern = re.compile(R'(S)(?:{(\w)})?(\d+)\((\d+)x(\d+)\)(\d+),(\d+)') m = pattern.match(self.model_str, index) if m is None: return None, None name = self._GetLayerName(m.group(0), index, m.group(2)) src_dim = int(m.group(3)) part_a = int(m.group(4)) part_b = int(m.group(5)) dest_dim_a = int(m.group(6)) dest_dim_b = int(m.group(7)) if part_a == 0: part_a = -1 if part_b == 0: part_b = -1 prev_shape = tf.shape(prev_layer) layer = shapes.transposing_reshape( prev_layer, src_dim, part_a, part_b, dest_dim_a, dest_dim_b, name=name) # Compute scale factors. result_shape = tf.shape(layer) for i in xrange(len(self.reduction_factors)): if self.reduction_factors[i] is not None: factor1 = tf.cast(self.reduction_factors[i], tf.float32) factor2 = tf.cast(prev_shape[i], tf.float32) divisor = tf.cast(result_shape[i], tf.float32) self.reduction_factors[i] = tf.div(tf.multiply(factor1, factor2), divisor) return layer, m.end()
def testTransposingReshape_2_2_3_2_1(self): """Case: dest_a == src, dest_b < src: Split with Least sig part going left. """ with self.test_session() as sess: fake = tf.placeholder(tf.float32, shape=(None, None, None, 2), name='inputs') outputs = shapes.transposing_reshape(fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=2, dest_dim_b=1) # Make real inputs. The tensor looks like this: # tensor=[[[[0, 1][2, 3][4, 5][6, 7][8, 9][10, 11]] # [[12, 13][14, 15][16, 17][18, 19][20, 21][22, 23]] # [[[24, 25]... real = np.arange(120).reshape((5, 2, 6, 2)) np_array = sess.run([outputs], feed_dict={fake: real})[0] self.assertEqual(tuple(np_array.shape), (5, 6, 2, 2)) self.assertAllEqual(np_array[0, :, :, :], [[[0, 1], [6, 7]], [[12, 13], [18, 19]], [[2, 3], [8, 9]], [[14, 15], [20, 21]], [[4, 5], [10, 11]], [[16, 17], [22, 23]]])