(x,
                 is_training,
                 scope,
                 n,
                 k,
                 weight_decay,
                 reuse=tf.AUTO_REUSE,
                 params=None,
                 moments=None,
                 backprop_through_moments=True,
                 use_bounded_activation=False,
                 keep_spatial_dims=False)
A wide ResNet.
A wide ResNet.
A wide ResNet .
def _wide_resnet(x,
                 is_training,
                 scope,
                 n,
                 k,
                 weight_decay,
                 reuse=tf.AUTO_REUSE,
                 params=None,
                 moments=None,
                 backprop_through_moments=True,
                 use_bounded_activation=False,
                 keep_spatial_dims=False):
  """A wide ResNet."""
  widths = [i * k for i in (16, 32, 64)]
  params_keys, params_vars = [], []
  moments_keys, moments_vars = [], []

  def _update_params_lists(params_dict, params_keys, params_vars):
    params_keys.extend(params_dict.keys())
    params_vars.extend(params_dict.values())

  def _update_moments_lists(moments_dict, moments_keys, moments_vars):
    moments_keys.extend(moments_dict.keys())
    moments_vars.extend(moments_dict.values())

  with tf.variable_scope(scope, reuse=reuse):
    with tf.variable_scope('conv1'):
      x, conv_params = conv(x, [3, 3], 16, 1, weight_decay, params=params)
      _update_params_lists(conv_params, params_keys, params_vars)

    def _wide_resnet_block(x, depths, stride, use_project, moments):
      """Wrapper for a wide resnet block."""
      x, block_params, block_moments = wide_resnet_block(
          x,
          depths,
          stride,
          weight_decay,
          params=params,
          moments=moments,
          use_project=use_project,
          is_training=is_training,
          backprop_through_moments=backprop_through_moments,
          use_bounded_activation=use_bounded_activation)
      return x, block_params, block_moments

    with tf.variable_scope('conv2_x'):
      with tf.variable_scope('wide_block_0'):
        if widths[0] == 16:
          use_project = False
        else:
          use_project = True
        x, block_params, block_moments = _wide_resnet_block(
            x, widths[0], 1, use_project, moments=moments)
        _update_params_lists(block_params, params_keys, params_vars)
        _update_moments_lists(block_moments, moments_keys, moments_vars)
      for i in range(1, n):
        with tf.variable_scope('wide_block_%d' % i):
          x, block_params, block_moments = _wide_resnet_block(
              x, widths[0], 1, use_project, moments=moments)
          _update_params_lists(block_params, params_keys, params_vars)
          _update_moments_lists(block_moments, moments_keys, moments_vars)

    with tf.variable_scope('conv3_x'):
      with tf.variable_scope('wide_block_0'):
        x, block_params, block_moments = _wide_resnet_block(
            x, widths[1], 2, True, moments=moments)
        _update_params_lists(block_params, params_keys, params_vars)
        _update_moments_lists(block_moments, moments_keys, moments_vars)
      for i in range(1, n):
        with tf.variable_scope('wide_block_%d' % i):
          x, block_params, block_moments = _wide_resnet_block(
              x, widths[1], 1, use_project, moments=moments)
          _update_params_lists(block_params, params_keys, params_vars)
          _update_moments_lists(block_moments, moments_keys, moments_vars)

    with tf.variable_scope('conv4_x'):
      with tf.variable_scope('wide_block_0'):
        x, block_params, block_moments = _wide_resnet_block(
            x, widths[2], 2, True, moments=moments)
        _update_params_lists(block_params, params_keys, params_vars)
        _update_moments_lists(block_moments, moments_keys, moments_vars)
      for i in range(1, n):
        with tf.variable_scope('wide_block_%d' % i):
          x, block_params, block_moments = _wide_resnet_block(
              x, widths[2], 1, use_project, moments=moments)
          _update_params_lists(block_params, params_keys, params_vars)
          _update_moments_lists(block_moments, moments_keys, moments_vars)

    with tf.variable_scope('embedding_layer'):
      x, bn_params, bn_moments = bn(
          x,
          params=params,
          moments=moments,
          is_training=is_training,
          backprop_through_moments=backprop_through_moments)
      _update_params_lists(bn_params, params_keys, params_vars)
      _update_moments_lists(bn_moments, moments_keys, moments_vars)

      x = relu(x, use_bounded_activation=use_bounded_activation)
    img_w, img_h = x.get_shape().as_list()[1:3]
    x = tf.nn.avg_pool(
        x, ksize=[1, img_w, img_h, 1], strides=[1, 1, 1, 1], padding='VALID')
    # x.shape: [X, 1, 1, 128]
    if not keep_spatial_dims:
      x = tf.reshape(x, [-1, widths[2]])
    params = collections.OrderedDict(zip(params_keys, params_vars))
    moments = collections.OrderedDict(zip(moments_keys, moments_vars))

    return_dict = {'embeddings': x, 'params': params, 'moments': moments}
    return return_dict
def _wide_resnet ( x , is_training , scope , n , k , weight_decay , reuse = tf . AUTO_REUSE , params = None , moments = None , backprop_through_moments = True , use_bounded_activation = False , keep_spatial_dims = False ) : widths = [ i * k for i in ( 16 , 32 , 64 ) ] params_keys , params_vars = [ ] , [ ] moments_keys , moments_vars = [ ] , [ ] def _update_params_lists ( params_dict , params_keys , params_vars ) : params_keys . extend ( params_dict . keys ( ) ) params_vars . extend ( params_dict . values ( ) ) def _update_moments_lists ( moments_dict , moments_keys , moments_vars ) : moments_keys . extend ( moments_dict . keys ( ) ) moments_vars . extend ( moments_dict . values ( ) ) with tf . variable_scope ( scope , reuse = reuse ) : with tf . variable_scope ( 'conv1' ) : x , conv_params = conv ( x , [ 3 , 3 ] , 16 , 1 , weight_decay , params = params ) _update_params_lists ( conv_params , params_keys , params_vars ) def _wide_resnet_block ( x , depths , stride , use_project , moments ) : """Wrapper for a wide resnet block.""" x , block_params , block_moments = wide_resnet_block ( x , depths , stride , weight_decay , params = params , moments = moments , use_project = use_project , is_training = is_training , backprop_through_moments = backprop_through_moments , use_bounded_activation = use_bounded_activation ) return x , block_params , block_moments with tf . variable_scope ( 'conv2_x' ) : with tf . variable_scope ( 'wide_block_0' ) : if widths [ 0 ] == 16 : use_project = False else : use_project = True x , block_params , block_moments = _wide_resnet_block ( x , widths [ 0 ] , 1 , use_project , moments = moments ) _update_params_lists ( block_params , params_keys , params_vars ) _update_moments_lists ( block_moments , moments_keys , moments_vars ) for i in range ( 1 , n ) : with tf . variable_scope ( 'wide_block_%d' % i ) : x , block_params , block_moments = _wide_resnet_block ( x , widths [ 0 ] , 1 , use_project , moments = moments ) _update_params_lists ( block_params , params_keys , params_vars ) _update_moments_lists ( block_moments , moments_keys , moments_vars ) with tf . variable_scope ( 'conv3_x' ) : with tf . variable_scope ( 'wide_block_0' ) : x , block_params , block_moments = _wide_resnet_block ( x , widths [ 1 ] , 2 , True , moments = moments ) _update_params_lists ( block_params , params_keys , params_vars ) _update_moments_lists ( block_moments , moments_keys , moments_vars ) for i in range ( 1 , n ) : with tf . variable_scope ( 'wide_block_%d' % i ) : x , block_params , block_moments = _wide_resnet_block ( x , widths [ 1 ] , 1 , use_project , moments = moments ) _update_params_lists ( block_params , params_keys , params_vars ) _update_moments_lists ( block_moments , moments_keys , moments_vars ) with tf . variable_scope ( 'conv4_x' ) : with tf . variable_scope ( 'wide_block_0' ) : x , block_params , block_moments = _wide_resnet_block ( x , widths [ 2 ] , 2 , True , moments = moments ) _update_params_lists ( block_params , params_keys , params_vars ) _update_moments_lists ( block_moments , moments_keys , moments_vars ) for i in range ( 1 , n ) : with tf . variable_scope ( 'wide_block_%d' % i ) : x , block_params , block_moments = _wide_resnet_block ( x , widths [ 2 ] , 1 , use_project , moments = moments ) _update_params_lists ( block_params , params_keys , params_vars ) _update_moments_lists ( block_moments , moments_keys , moments_vars ) with tf . variable_scope ( 'embedding_layer' ) : x , bn_params , bn_moments = bn ( x , params = params , moments = moments , is_training = is_training , backprop_through_moments = backprop_through_moments ) _update_params_lists ( bn_params , params_keys , params_vars ) _update_moments_lists ( bn_moments , moments_keys , moments_vars ) x = relu ( x , use_bounded_activation = use_bounded_activation ) img_w , img_h = x . get_shape ( ) . as_list ( ) [ 1 : 3 ] x = tf . nn . avg_pool ( x , ksize = [ 1 , img_w , img_h , 1 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'VALID' ) # x.shape: [X, 1, 1, 128] if not keep_spatial_dims : x = tf . reshape ( x , [ - 1 , widths [ 2 ] ] ) params = collections . OrderedDict ( zip ( params_keys , params_vars ) ) moments = collections . OrderedDict ( zip ( moments_keys , moments_vars ) ) return_dict = { 'embeddings' : x , 'params' : params , 'moments' : moments } return return_dict