我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用tensorflow.contrib.layers.avg_pool2d()。
def aux_logit_layer( inputs, num_classes, is_training ): with tf.variable_scope("pool2d"): pooled = layers.avg_pool2d(inputs, [ 5, 5 ], stride = 3 ) with tf.variable_scope("conv11"): conv11 = layers.conv2d( pooled, 128, [1, 1] ) with tf.variable_scope("flatten"): flat = tf.reshape( conv11, [-1, 2048] ) with tf.variable_scope("fc"): fc = layers.fully_connected( flat, 1024, activation_fn=None ) with tf.variable_scope("drop"): drop = layers.dropout( fc, 0.3, is_training = is_training ) with tf.variable_scope( "linear" ): linear = layers.fully_connected( drop, num_classes, activation_fn=None ) with tf.variable_scope("soft"): soft = tf.nn.softmax( linear ) return soft
def _block_a(net, scope='BlockA'): # 35 x 35 x 384 grid # default padding = SAME # default stride = 1 with tf.variable_scope(scope): with tf.variable_scope('Br1_Pool'): br1 = layers.avg_pool2d(net, [3, 3], scope='Pool1_3x3') br1 = layers.conv2d(br1, 96, [1, 1], scope='Conv1_1x1') with tf.variable_scope('Br2_1x1'): br2 = layers.conv2d(net, 96, [1, 1], scope='Conv1_1x1') with tf.variable_scope('Br3_3x3'): br3 = layers.conv2d(net, 64, [1, 1], scope='Conv1_1x1') br3 = layers.conv2d(br3, 96, [3, 3], scope='Conv2_3x3') with tf.variable_scope('Br4_3x3Dbl'): br4 = layers.conv2d(net, 64, [1, 1], scope='Conv1_1x1') br4 = layers.conv2d(br4, 96, [3, 3], scope='Conv2_3x3') br4 = layers.conv2d(br4, 96, [3, 3], scope='Conv3_3x3') net = tf.concat(3, [br1, br2, br3, br4], name='Concat1') # 35 x 35 x 384 return net
def _block_b(net, scope='BlockB'): # 17 x 17 x 1024 grid # default padding = SAME # default stride = 1 with tf.variable_scope(scope): with tf.variable_scope('Br1_Pool'): br1 = layers.avg_pool2d(net, [3, 3], scope='Pool1_3x3') br1 = layers.conv2d(br1, 128, [1, 1], scope='Conv1_1x1') with tf.variable_scope('Br2_1x1'): br2 = layers.conv2d(net, 384, [1, 1], scope='Conv1_1x1') with tf.variable_scope('Br3_7x7'): br3 = layers.conv2d(net, 192, [1, 1], scope='Conv1_1x1') br3 = layers.conv2d(br3, 224, [1, 7], scope='Conv2_1x7') br3 = layers.conv2d(br3, 256, [7, 1], scope='Conv3_7x1') with tf.variable_scope('Br4_7x7Dbl'): br4 = layers.conv2d(net, 192, [1, 1], scope='Conv1_1x1') br4 = layers.conv2d(br4, 192, [1, 7], scope='Conv2_1x7') br4 = layers.conv2d(br4, 224, [7, 1], scope='Conv3_7x1') br4 = layers.conv2d(br4, 224, [1, 7], scope='Conv4_1x7') br4 = layers.conv2d(br4, 256, [7, 1], scope='Conv5_7x1') net = tf.concat(3, [br1, br2, br3, br4], name='Concat1') # 17 x 17 x 1024 return net
def _block_c(net, scope='BlockC'): # 8 x 8 x 1536 grid # default padding = SAME # default stride = 1 with tf.variable_scope(scope): with tf.variable_scope('Br1_Pool'): br1 = layers.avg_pool2d(net, [3, 3], scope='Pool1_3x3') br1 = layers.conv2d(br1, 256, [1, 1], scope='Conv1_1x1') with tf.variable_scope('Br2_1x1'): br2 = layers.conv2d(net, 256, [1, 1], scope='Conv1_1x1') with tf.variable_scope('Br3_3x3'): br3 = layers.conv2d(net, 384, [1, 1], scope='Conv1_1x1') br3a = layers.conv2d(br3, 256, [1, 3], scope='Conv2_1x3') br3b = layers.conv2d(br3, 256, [3, 1], scope='Conv3_3x1') with tf.variable_scope('Br4_7x7Dbl'): br4 = layers.conv2d(net, 384, [1, 1], scope='Conv1_1x1') br4 = layers.conv2d(br4, 448, [1, 7], scope='Conv2_1x7') br4 = layers.conv2d(br4, 512, [7, 1], scope='Conv3_7x1') br4a = layers.conv2d(br4, 256, [1, 7], scope='Conv4a_1x7') br4b = layers.conv2d(br4, 256, [7, 1], scope='Conv4b_7x1') net = tf.concat(3, [br1, br2, br3a, br3b, br4a, br4b], name='Concat1') # 8 x 8 x 1536 return net
def __call__(self, inputs, reuse = True): with tf.variable_scope(self.name) as vs: tf.get_variable_scope() if reuse: vs.reuse_variables() conv1 = tcl.conv2d(inputs, num_outputs = 64, kernel_size = (7, 7), stride = (2, 2), padding = 'SAME') conv1 = tcl.batch_norm(conv1) conv1 = tf.nn.relu(conv1) conv1 = tcl.max_pool2d(conv1, kernel_size = (3, 3), stride = (2, 2), padding = 'SAME') x = conv1 filters = 64 first_layer = True for i, r in enumerate(self.repetitions): x = _residual_block(self.block_fn, filters = filters, repetition = r, is_first_layer = first_layer)(x) filters *= 2 if first_layer: first_layer = False _, h, w, ch = x.shape.as_list() outputs = tcl.avg_pool2d(x, kernel_size = (h, w), stride = (1, 1)) outputs = tcl.flatten(outputs) logits = tcl.fully_connected(outputs, num_outputs = self.num_output, activation_fn = None) return logits
def _output(net, endpoints, num_classes, pre_act=False): with tf.variable_scope('Output'): if pre_act: net = layers.batch_norm(net, activation_fn=tf.nn.relu) shape = net.get_shape() net = layers.avg_pool2d(net, shape[1:3], scope='Pool1_Global') endpoints['OutputPool1'] = net net = layers.flatten(net) net = layers.fully_connected(net, num_classes, activation_fn=None, scope='Logits') endpoints['Logits'] = net # num_classes return net
def _block_a_reduce(net, endpoints, k=192, l=224, m=256, n=384, scope='BlockReduceA'): # 35 x 35 -> 17 x 17 reduce # inception-v4: k=192, l=224, m=256, n=384 # inception-resnet-v1: k=192, l=192, m=256, n=384 # inception-resnet-v2: k=256, l=256, m=384, n=384 # default padding = VALID # default stride = 1 with arg_scope([layers.conv2d, layers.max_pool2d, layers.avg_pool2d], padding='VALID'): with tf.variable_scope(scope): with tf.variable_scope('Br1_Pool'): br1 = layers.max_pool2d(net, [3, 3], stride=2, scope='Pool1_3x3/2') # 17 x 17 x input with tf.variable_scope('Br2_3x3'): br2 = layers.conv2d(net, n, [3, 3], stride=2, scope='Conv1_3x3/2') # 17 x 17 x n with tf.variable_scope('Br3_3x3Dbl'): br3 = layers.conv2d(net, k, [1, 1], padding='SAME', scope='Conv1_1x1') br3 = layers.conv2d(br3, l, [3, 3], padding='SAME', scope='Conv2_3x3') br3 = layers.conv2d(br3, m, [3, 3], stride=2, scope='Conv3_3x3/2') # 17 x 17 x m net = tf.concat(3, [br1, br2, br3], name='Concat1') # 17 x 17 x input + n + m # 1024 for v4 (384 + 384 + 256) # 896 for res-v1 (256 + 384 +256) # 1152 for res-v2 (384 + 384 + 384) endpoints[scope] = net print('%s output shape: %s' % (scope, net.get_shape())) return net
def _block_stem_res(net, endpoints, scope='Stem'): # Simpler _stem for inception-resnet-v1 network # NOTE observe endpoints of first 3 layers # default padding = VALID # default stride = 1 with arg_scope([layers.conv2d, layers.max_pool2d, layers.avg_pool2d], padding='VALID'): with tf.variable_scope(scope): # 299 x 299 x 3 net = layers.conv2d(net, 32, [3, 3], stride=2, scope='Conv1_3x3/2') endpoints[scope + '/Conv1'] = net # 149 x 149 x 32 net = layers.conv2d(net, 32, [3, 3], scope='Conv2_3x3') endpoints[scope + '/Conv2'] = net # 147 x 147 x 32 net = layers.conv2d(net, 64, [3, 3], padding='SAME', scope='Conv3_3x3') endpoints[scope + '/Conv3'] = net # 147 x 147 x 64 net = layers.max_pool2d(net, [3, 3], stride=2, scope='Pool1_3x3/2') # 73 x 73 x 64 net = layers.conv2d(net, 80, [1, 1], padding='SAME', scope='Conv4_1x1') # 73 x 73 x 80 net = layers.conv2d(net, 192, [3, 3], scope='Conv5_3x3') # 71 x 71 x 192 net = layers.conv2d(net, 256, [3, 3], stride=2, scope='Conv6_3x3/2') # 35 x 35 x 256 endpoints[scope] = net print('%s output shape: %s' % (scope, net.get_shape())) return net
def _block_b_reduce_res(net, endpoints, ver=2, scope='BlockReduceB'): # 17 x 17 -> 8 x 8 reduce # configure branch filter numbers br3_num = 256 br4_num = 256 if ver == 1: br3_inc = 0 br4_inc = 0 else: br3_inc = 32 br4_inc = 32 with arg_scope([layers.conv2d, layers.max_pool2d, layers.avg_pool2d], padding='VALID'): with tf.variable_scope(scope): with tf.variable_scope('Br1_Pool'): br1 = layers.max_pool2d(net, [3, 3], stride=2, scope='Pool1_3x3/2') with tf.variable_scope('Br2_3x3'): br2 = layers.conv2d(net, 256, [1, 1], padding='SAME', scope='Conv1_1x1') br2 = layers.conv2d(br2, 384, [3, 3], stride=2, scope='Conv2_3x3/2') with tf.variable_scope('Br3_3x3'): br3 = layers.conv2d(net, br3_num, [1, 1], padding='SAME', scope='Conv1_1x1') br3 = layers.conv2d(br3, br3_num + br3_inc, [3, 3], stride=2, scope='Conv2_3x3/2') with tf.variable_scope('Br4_3x3Dbl'): br4 = layers.conv2d(net, br4_num, [1, 1], padding='SAME', scope='Conv1_1x1') br4 = layers.conv2d(br4, br4_num + 1*br4_inc, [3, 3], padding='SAME', scope='Conv2_3x3') br4 = layers.conv2d(br4, br4_num + 2*br4_inc, [3, 3], stride=2, scope='Conv3_3x3/2') net = tf.concat(3, [br1, br2, br3, br4], name='Concat1') # 8 x 8 x 1792 v1, 2144 v2 (paper indicates 2048 but only get this if we use a v1 config for this block) endpoints[scope] = net print('%s output shape: %s' % (scope, net.get_shape())) return net
def _block_output(net, endpoints, num_classes=1000, dropout_keep_prob=0.5, scope='Output'): with tf.variable_scope(scope): # 8 x 8 x 1536 shape = net.get_shape() net = layers.avg_pool2d(net, shape[1:3], padding='VALID', scope='Pool1_Global') endpoints['Output/Pool1'] = net # 1 x 1 x 1536 net = layers.dropout(net, dropout_keep_prob) net = layers.flatten(net) # 1536 net = layers.fully_connected(net, num_classes, activation_fn=None, scope='Logits') # num classes endpoints['Logits'] = net return net
def _block_stem(net, endpoints, scope='Stem'): # Stem shared by inception-v4 and inception-resnet-v2 (resnet-v1 uses simpler _stem below) # NOTE observe endpoints of first 3 layers with arg_scope([layers.conv2d, layers.max_pool2d, layers.avg_pool2d], padding='VALID'): with tf.variable_scope(scope): # 299 x 299 x 3 net = layers.conv2d(net, 32, [3, 3], stride=2, scope='Conv1_3x3/2') endpoints[scope + '/Conv1'] = net # 149 x 149 x 32 net = layers.conv2d(net, 32, [3, 3], scope='Conv2_3x3') endpoints[scope + '/Conv2'] = net # 147 x 147 x 32 net = layers.conv2d(net, 64, [3, 3], padding='SAME', scope='Conv3_3x3') endpoints[scope + '/Conv3'] = net # 147 x 147 x 64 with tf.variable_scope('Br1A_Pool'): br1a = layers.max_pool2d(net, [3, 3], stride=2, scope='Pool1_3x3/2') with tf.variable_scope('Br1B_3x3'): br1b = layers.conv2d(net, 96, [3, 3], stride=2, scope='Conv4_3x3/2') net = tf.concat(3, [br1a, br1b], name='Concat1') endpoints[scope + '/Concat1'] = net # 73 x 73 x 160 with tf.variable_scope('Br2A_3x3'): br2a = layers.conv2d(net, 64, [1, 1], padding='SAME', scope='Conv5_1x1') br2a = layers.conv2d(br2a, 96, [3, 3], scope='Conv6_3x3') with tf.variable_scope('Br2B_7x7x3'): br2b = layers.conv2d(net, 64, [1, 1], padding='SAME', scope='Conv5_1x1') br2b = layers.conv2d(br2b, 64, [7, 1], padding='SAME', scope='Conv6_7x1') br2b = layers.conv2d(br2b, 64, [1, 7], padding='SAME', scope='Conv7_1x7') br2b = layers.conv2d(br2b, 96, [3, 3], scope='Conv8_3x3') net = tf.concat(3, [br2a, br2b], name='Concat2') endpoints[scope + '/Concat2'] = net # 71 x 71 x 192 with tf.variable_scope('Br3A_3x3'): br3a = layers.conv2d(net, 192, [3, 3], stride=2, scope='Conv9_3x3/2') with tf.variable_scope('Br3B_Pool'): br3b = layers.max_pool2d(net, [3, 3], stride=2, scope='Pool2_3x3/2') net = tf.concat(3, [br3a, br3b], name='Concat3') endpoints[scope + '/Concat3'] = net print('%s output shape: %s' % (scope, net.get_shape())) # 35x35x384 return net
def _build_inception_v4( inputs, stack_counts=[4, 7, 3], dropout_keep_prob=0.8, num_classes=1000, is_training=True, scope=''): """Inception v4 from http://arxiv.org/abs/ Args: inputs: a tensor of size [batch_size, height, width, channels]. dropout_keep_prob: dropout keep_prob. num_classes: number of predicted classes. is_training: whether is training or not. scope: Optional scope for op_scope. Returns: a list containing 'logits' Tensors and a dict of Endpoints. """ # endpoints will collect relevant activations for external use, for example, summaries or losses. endpoints = {} name_scope_net = tf.name_scope(scope, 'Inception_v4', [inputs]) arg_scope_train = arg_scope([layers.batch_norm, layers.dropout], is_training=is_training) arg_scope_conv = arg_scope([layers.conv2d, layers.max_pool2d, layers.avg_pool2d], stride=1, padding='SAME') with name_scope_net, arg_scope_train, arg_scope_conv: net = _block_stem(inputs, endpoints) # 35 x 35 x 384 with tf.variable_scope('Scale1'): net = _stack(net, endpoints, fn=_block_a, count=stack_counts[0], scope='BlockA') # 35 x 35 x 384 with tf.variable_scope('Scale2'): net = _block_a_reduce(net, endpoints) # 17 x 17 x 1024 net = _stack(net, endpoints, fn=_block_b, count=stack_counts[1], scope='BlockB') # 17 x 17 x 1024 with tf.variable_scope('Scale3'): net = _block_b_reduce(net, endpoints) # 8 x 8 x 1536 net = _stack(net, endpoints, fn=_block_c, count=stack_counts[2], scope='BlockC') # 8 x 8 x 1536 logits = _block_output(net, endpoints, num_classes, dropout_keep_prob, scope='Output') endpoints['Predictions'] = tf.nn.softmax(logits, name='Predictions') return logits, endpoints