Beispiel #1
0
def conv2d_fixed_padding(inputs, filters, kernel_size, strides, data_format):
    """Strided 2-D convolution with explicit padding."""
    # The padding is consistent and is based only on `kernel_size`, not on the
    # dimensions of `inputs` (as opposed to using `tf.layers.conv2d` alone).

    inputs_for_logging = inputs
    if strides > 1:
        inputs = fixed_padding(inputs, kernel_size, data_format)

    outputs = tf.layers.conv2d(
        inputs=inputs,
        filters=filters,
        kernel_size=kernel_size,
        strides=strides,
        padding=('SAME' if strides == 1 else 'VALID'),
        use_bias=False,
        kernel_initializer=tf.variance_scaling_initializer(
            distribution="truncated_normal"),
        data_format=data_format)

    resnet_log_helper.log_conv2d(input_tensor=inputs_for_logging,
                                 output_tensor=outputs,
                                 stride=strides,
                                 filters=filters,
                                 initializer=mlperf_log.TRUNCATED_NORMAL,
                                 use_bias=False)

    return outputs
def conv2d_fixed_padding(inputs,
                         filters,
                         kernel_size,
                         strides,
                         is_training,
                         data_format='channels_first'):
  """Strided 2-D convolution with explicit padding.

  The padding is consistent and is based only on `kernel_size`, not on the
  dimensions of `inputs` (as opposed to using `tf.layers.conv2d` alone).

  Args:
    inputs: `Tensor` of size `[batch, channels, height_in, width_in]`.
    filters: `int` number of filters in the convolution.
    kernel_size: `int` size of the kernel to be used in the convolution.
    strides: `int` strides of the convolution.
    is_training: `bool` for whether the model is in training.
    data_format: `str` either "channels_first" for `[batch, channels, height,
        width]` or "channels_last for `[batch, height, width, channels]`.

  Returns:
    A `Tensor` of shape `[batch, filters, height_out, width_out]`.
  """
  inputs_for_logging = inputs
  if strides > 1:
    inputs = fixed_padding(inputs, kernel_size, data_format=data_format)

  outputs = tf.layers.conv2d(
      inputs=inputs,
      filters=filters,
      kernel_size=kernel_size,
      strides=strides,
      padding=('SAME' if strides == 1 else 'VALID'),
      use_bias=False,
      kernel_initializer=tf.variance_scaling_initializer(),
      data_format=data_format)

  if is_training and FLAGS.mlperf_logging:
    resnet_log_helper.log_conv2d(
        input_tensor=inputs_for_logging,
        output_tensor=outputs,
        stride=strides,
        filters=filters,
        initializer=mlperf_log.TRUNCATED_NORMAL,
        use_bias=False)
  return outputs
Beispiel #3
0
 def log_conv2d(self, input_tensor, output_tensor, stride_height,
                stride_width, filters, initializer, use_bias):
     """Log a conv2d call."""
     if self.model == 'resnet50':
         assert stride_height == stride_width, (
             '--ml_perf_compliance_logging does not support convolutions where '
             'the stride height is not equal to the stride width. '
             'stride_height=%d, stride_width=%d' %
             (stride_height, stride_width))
         if isinstance(initializer, tf.truncated_normal_initializer):
             initializer = tags.TRUNCATED_NORMAL
         elif (isinstance(initializer, tf.glorot_uniform_initializer)
               or initializer is None):
             initializer = 'glorot_uniform'
         resnet_log_helper.log_conv2d(input_tensor, output_tensor,
                                      stride_width, filters, initializer,
                                      use_bias)