Example #1
0
    def top(self, body_output, _):
        """Transform inputs from model space to target space.

    Perform the Xception "Exit flow", consisting of a single residual block and
    two separable convolutional upscalings followed by global spatial average
    pooling.

    Args:
      body_output: A Tensor with shape [batch, ?, ?, body_output_size].
    Returns:
      a Tensors, each with shape [batch_size, ?, ?, vocab_size]
    """
        with tf.variable_scope(self.name):
            x = body_output

            # Assume input is a square with self._body_input_depth channels.
            if self._is_2d:
                length_float = tf.to_float(tf.shape(x)[1])
                length_float *= tf.to_float(tf.shape(x)[2])
                spatial_dim_float = tf.sqrt(length_float)
                spatial_dim = tf.to_int32(spatial_dim_float)
                x_depth = int(x.get_shape()[3])
                x = tf.reshape(x, [-1, spatial_dim, spatial_dim, x_depth])
            x = common_layers.conv_block_downsample(x, self._kernel,
                                                    self._strides,
                                                    self._padding)
            x = tf.nn.relu(x)
            x = tf.reduce_mean(x, axis=[1, 2], keep_dims=True)
            res = common_layers.conv(x, self._vocab_size, (1, 1))
            return tf.expand_dims(res, 3)
 def testConv(self):
   x = np.random.rand(5, 7, 1, 11)
   with self.test_session() as session:
     y = common_layers.conv(tf.constant(x, dtype=tf.float32), 13, (3, 1))
     session.run(tf.global_variables_initializer())
     res = session.run(y)
   self.assertEqual(res.shape, (5, 5, 1, 13))
Example #3
0
 def deconv2d(cur, i, kernel_size, output_filters, activation=tf.nn.relu):
   thicker = common_layers.conv(
       cur,
       output_filters * 4,
       kernel_size,
       padding="SAME",
       activation=activation,
       name="deconv2d" + str(i))
   return tf.depth_to_space(thicker, 2)
Example #4
0
 def top(self, body_output, _):
   with tf.variable_scope(self.name):
     hidden_dim = self._model_hparams.hidden_size
     img_len = self._model_hparams.img_len
     channels = self._model_hparams.num_channels
     batch = common_layers.shape_list(body_output)[0]
     x = common_layers.conv(
         body_output,
         hidden_dim * channels, (1, 1),
         padding="VALID",
         activation=tf.nn.relu,
         name="decompress_conv")
     x = tf.reshape(x, [batch, img_len, img_len * channels, hidden_dim])
     x.set_shape([None, None, None, hidden_dim])
     x = common_layers.conv(
         x, self.top_dimensionality, (1, 1), name="output_conv")
     x = tf.reshape(x,
                    [-1, img_len, img_len, channels, self.top_dimensionality])
     return x
Example #5
0
 def top(self, body_output, _):
   # TODO(lukaszkaiser): work on a better way to generate large images.
   with tf.variable_scope(self.name):
     decompressed_inputs = common_layers.deconv_stride2_multistep(
         body_output,
         self._model_hparams.compress_steps,
         body_output.get_shape()[-1],
         name="deconv")
     return common_layers.conv(
         decompressed_inputs, self._vocab_size, (1, 1), padding="SAME")
Example #6
0
 def top(self, body_output, _):
   with tf.variable_scope(self.name):
     hidden_dim = self._model_hparams.hidden_size
     img_len = self._model_hparams.img_len
     channels = self._model_hparams.num_channels
     batch = common_layers.shape_list(body_output)[0]
     x = common_layers.conv(
         body_output,
         hidden_dim * channels, (1, 1),
         padding="VALID",
         activation=tf.nn.relu,
         name="decompress_conv")
     x = tf.reshape(x, [batch, img_len, img_len * channels, hidden_dim])
     x.set_shape([None, None, None, hidden_dim])
     x = common_layers.conv(
         x, self.top_dimensionality, (1, 1), name="output_conv")
     x = tf.reshape(x,
                    [-1, img_len, img_len, channels, self.top_dimensionality])
     return x
 def testConv(self):
   x = np.random.rand(5, 7, 1, 11)
   y = common_layers.conv(tf.constant(x, dtype=tf.float32), 13, (3, 1))
   self.evaluate(tf.global_variables_initializer())
   res = self.evaluate(y)
   self.assertEqual(res.shape, (5, 5, 1, 13))
Example #8
0
 def testConv(self):
     x = np.random.rand(5, 7, 1, 11)
     y = common_layers.conv(tf.constant(x, dtype=tf.float32), 13, (3, 1))
     self.evaluate(tf.global_variables_initializer())
     res = self.evaluate(y)
     self.assertEqual(res.shape, (5, 5, 1, 13))