def subsample(inputs, factor, scope=None):
"""Subsamples the input along the spatial dimensions.
Args:
inputs: A `Tensor` of size [batch, height_in, width_in, channels].
factor: The subsampling factor.
scope: Optional variable_scope.
Returns:
output: A `Tensor` of size [batch, height_out, width_out, channels] with the
input, either intact (if factor == 1) or subsampled (if factor > 1).
"""
if factor == 1:
return inputs
else:
return slim.max_pool2d(inputs, [1, 1], stride=factor, scope=scope)
python类max_pool2d()的实例源码
WhatWhereAutoencoder.py 文件源码
项目:Tensorflow_WhatWhereAutoencoder
作者: yselivonchyk
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def max_pool_with_argmax(net, stride):
"""
Tensorflow default implementation does not provide gradient operation on max_pool_with_argmax
Therefore, we use max_pool_with_argmax to extract mask and
plain max_pool for, eeem... max_pooling.
"""
with tf.name_scope('MaxPoolArgMax'):
_, mask = tf.nn.max_pool_with_argmax(
net,
ksize=[1, stride, stride, 1],
strides=[1, stride, stride, 1],
padding='SAME')
mask = tf.stop_gradient(mask)
net = slim.max_pool2d(net, kernel_size=[stride, stride], stride=FLAGS.pool_size)
return net, mask
# Thank you, @https://github.com/Pepslee
def reduction_a(net, k, l, m, n):
with tf.variable_scope('Branch_0'):
tower_conv = slim.conv2d(net, n, 3, stride=2, padding='VALID',
scope='Conv2d_1a_3x3')
with tf.variable_scope('Branch_1'):
tower_conv1_0 = slim.conv2d(net, k, 1, scope='Conv2d_0a_1x1')
tower_conv1_1 = slim.conv2d(tower_conv1_0, l, 3,
scope='Conv2d_0b_3x3')
tower_conv1_2 = slim.conv2d(tower_conv1_1, m, 3,
stride=2, padding='VALID',
scope='Conv2d_1a_3x3')
with tf.variable_scope('Branch_2'):
tower_pool = slim.max_pool2d(net, 3, stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
net = tf.concat([tower_conv, tower_conv1_2, tower_pool], 3)
return net
def reduction_b(net):
with tf.variable_scope('Branch_0'):
tower_conv = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
tower_conv_1 = slim.conv2d(tower_conv, 384, 3, stride=2,
padding='VALID', scope='Conv2d_1a_3x3')
with tf.variable_scope('Branch_1'):
tower_conv1 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
tower_conv1_1 = slim.conv2d(tower_conv1, 256, 3, stride=2,
padding='VALID', scope='Conv2d_1a_3x3')
with tf.variable_scope('Branch_2'):
tower_conv2 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
tower_conv2_1 = slim.conv2d(tower_conv2, 256, 3,
scope='Conv2d_0b_3x3')
tower_conv2_2 = slim.conv2d(tower_conv2_1, 256, 3, stride=2,
padding='VALID', scope='Conv2d_1a_3x3')
with tf.variable_scope('Branch_3'):
tower_pool = slim.max_pool2d(net, 3, stride=2, padding='VALID',
scope='MaxPool_1a_3x3')
net = tf.concat([tower_conv_1, tower_conv1_1,
tower_conv2_2, tower_pool], 3)
return net
def _build_network(self):
with slim.arg_scope([slim.conv2d],
activation_fn=tf.nn.relu,
weights_regularizer=slim.l2_regularizer(self.weight_decay),
weights_initializer= self.weights_initializer,
biases_initializer = self.biases_initializer):
with slim.arg_scope([slim.conv2d, slim.max_pool2d],
padding='SAME',
data_format = self.data_format):
with tf.variable_scope(self.basenet_type):
basenet, end_points = net_factory.get_basenet(self.basenet_type, self.inputs);
with tf.variable_scope('extra_layers'):
self.net, self.end_points = self._add_extra_layers(basenet, end_points);
with tf.variable_scope('seglink_layers'):
self._add_seglink_layers();
def _extra_conv_arg_scope_with_bn(weight_decay=0.00001,
activation_fn=None,
batch_norm_decay=0.997,
batch_norm_epsilon=1e-5,
batch_norm_scale=True):
batch_norm_params = {
'decay': batch_norm_decay,
'epsilon': batch_norm_epsilon,
'scale': batch_norm_scale,
'updates_collections': tf.GraphKeys.UPDATE_OPS_EXTRA,
}
with slim.arg_scope(
[slim.conv2d],
weights_regularizer=slim.l2_regularizer(weight_decay),
weights_initializer=slim.variance_scaling_initializer(),
activation_fn=tf.nn.relu,
normalizer_fn=slim.batch_norm,
normalizer_params=batch_norm_params):
with slim.arg_scope([slim.batch_norm], **batch_norm_params):
with slim.arg_scope([slim.max_pool2d], padding='SAME') as arg_sc:
return arg_sc
def subsample(inputs, factor, scope=None):
"""Subsamples the input along the spatial dimensions.
Args:
inputs: A `Tensor` of size [batch, height_in, width_in, channels].
factor: The subsampling factor.
scope: Optional variable_scope.
Returns:
output: A `Tensor` of size [batch, height_out, width_out, channels] with the
input, either intact (if factor == 1) or subsampled (if factor > 1).
"""
if factor == 1:
return inputs
else:
return slim.max_pool2d(inputs, [1, 1], stride=factor, scope=scope)
def _build_base(self):
with tf.variable_scope(self._scope, self._scope):
net = resnet_utils.conv2d_same(self._image, 64, 7, stride=2, scope='conv1')
net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [0, 0]])
net = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID', scope='pool1')
return net
def _crop_pool_layer(self, bottom, rois, name):
with tf.variable_scope(name) as scope:
batch_ids = tf.squeeze(tf.slice(rois, [0, 0], [-1, 1], name="batch_id"), [1])
# Get the normalized coordinates of bounding boxes
bottom_shape = tf.shape(bottom)
height = (tf.to_float(bottom_shape[1]) - 1.) * np.float32(self._feat_stride[0])
width = (tf.to_float(bottom_shape[2]) - 1.) * np.float32(self._feat_stride[0])
x1 = tf.slice(rois, [0, 1], [-1, 1], name="x1") / width
y1 = tf.slice(rois, [0, 2], [-1, 1], name="y1") / height
x2 = tf.slice(rois, [0, 3], [-1, 1], name="x2") / width
y2 = tf.slice(rois, [0, 4], [-1, 1], name="y2") / height
# Won't be back-propagated to rois anyway, but to save time
bboxes = tf.stop_gradient(tf.concat([y1, x1, y2, x2], axis=1))
pre_pool_size = cfg.POOLING_SIZE * 2
crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [pre_pool_size, pre_pool_size], name="crops")
return slim.max_pool2d(crops, [2, 2], padding='SAME')
def _image_to_head(self, is_training, reuse=False):
with tf.variable_scope(self._scope, self._scope, reuse=reuse):
# [VGG16] conv1
# input shape : 224 * 224 * 3
# output shape : 112 * 112 * 64
net = slim.repeat(self._image, 2, slim.conv2d, 64, [3, 3],
trainable=False, scope='conv1')
net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='pool1')
# [VGG16] conv2
# input shape : 112 * 112 * 64
# output shape : 56 * 56 * 128
net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3],
trainable=False, scope='conv2')
net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='pool2')
# [Hand Detection] REMOVE net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='pool3')
# [Hand Detection] conv3
# input shape : 56 * 56 * 128
# output shape : 56 * 56 * 256
net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3],
trainable=is_training, scope='conv3')
to_be_normalized_1 = net
# [Hand Detection] conv4
# input shape : 56 * 56 * 256
# output shape : 56 * 56 * 256
net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3],
trainable=is_training, scope='conv4')
to_be_normalized_2 = net
# [Hand Detection] conv5
# input shape : 56 * 56 * 256
# output shape : 56 * 56 * 256
net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3],
trainable=is_training, scope='conv5')
to_be_normalized_3 = net
return to_be_normalized_1, to_be_normalized_2, to_be_normalized_3
def svhnnet(inputs, scope='svhnnet', is_training=True, reuse=False):
layers = OrderedDict()
net = inputs
with tf.variable_scope(scope, reuse=reuse):
with ExitStack() as stack:
stack.enter_context(
slim.arg_scope(
[slim.fully_connected, slim.conv2d],
activation_fn=tf.nn.relu,
weights_regularizer=slim.l2_regularizer(2.5e-5)))
stack.enter_context(
slim.arg_scope([slim.max_pool2d, slim.conv2d],
padding='SAME'))
net = slim.conv2d(net, 64, 5, scope='conv1')
net = slim.max_pool2d(net, 3, stride=2, scope='pool1')
layers['pool1'] = net
net = slim.conv2d(net, 64, 5, scope='conv2')
net = slim.max_pool2d(net, 3, stride=2, scope='pool2')
layers['pool2'] = net
net = slim.conv2d(net, 128, 5, scope='conv3')
layers['conv3'] = net
net = tf.contrib.layers.flatten(net)
net = slim.fully_connected(net, 3072, scope='fc4')
layers['fc4'] = net
net = slim.fully_connected(net, 2048, scope='fc5')
layers['fc5'] = net
net = slim.fully_connected(net, 10, activation_fn=None, scope='fc6')
layers['fc6'] = net
return net, layers
def lenet(inputs, scope='lenet', is_training=True, reuse=False, num_classes=10):
layers = OrderedDict()
net = inputs
with tf.variable_scope(scope, reuse=reuse):
with ExitStack() as stack:
stack.enter_context(
slim.arg_scope(
[slim.fully_connected, slim.conv2d],
activation_fn=tf.nn.relu,
weights_regularizer=slim.l2_regularizer(2.5e-5)))
stack.enter_context(slim.arg_scope([slim.conv2d], padding='VALID'))
net = slim.conv2d(net, 20, 5, scope='conv1')
layers['conv1'] = net
net = slim.max_pool2d(net, 2, stride=2, scope='pool1')
layers['pool1'] = net
net = slim.conv2d(net, 50, 5, scope='conv2')
layers['conv2'] = net
net = slim.max_pool2d(net, 2, stride=2, scope='pool2')
layers['pool2'] = net
net = tf.contrib.layers.flatten(net)
net = slim.fully_connected(net, 500, scope='fc3')
layers['fc3'] = net
net = slim.fully_connected(net, num_classes, activation_fn=None, scope='fc4')
layers['fc4'] = net
return net, layers
def conv_net_kelz(inputs):
"""Builds the ConvNet from Kelz 2016."""
with slim.arg_scope(
[slim.conv2d, slim.fully_connected],
activation_fn=tf.nn.relu,
weights_initializer=tf.contrib.layers.variance_scaling_initializer(
factor=2.0, mode='FAN_AVG', uniform=True)):
net = slim.conv2d(inputs, 32, [3, 3], scope='conv1')
net = slim.conv2d(
net, 32, [3, 3], scope='conv2', normalizer_fn=slim.batch_norm)
net = slim.max_pool2d(net, [1, 2], stride=[1, 2], scope='pool2')
net = slim.dropout(net, 0.25, scope='dropout2')
net = slim.conv2d(net, 64, [3, 3], scope='conv3')
net = slim.max_pool2d(net, [1, 2], stride=[1, 2], scope='pool3')
net = slim.dropout(net, 0.25, scope='dropout3')
# Flatten while preserving batch and time dimensions.
dims = tf.shape(net)
net = tf.reshape(net, (dims[0], dims[1],
net.shape[2].value * net.shape[3].value), 'flatten4')
net = slim.fully_connected(net, 512, scope='fc5')
net = slim.dropout(net, 0.5, scope='dropout5')
return net
def _maxpool(self, x, kernel_size):
p = np.floor((kernel_size - 1) / 2).astype(np.int32)
p_x = tf.pad(x, [[0, 0], [p, p], [p, p], [0, 0]])
return slim.max_pool2d(p_x, kernel_size)
def squeezenet_inference(inputs, is_training, keep_prob):
nets = slim.conv2d(inputs, 64,
[3, 3], scope='conv1')
nets = slim.max_pool2d(nets, [3, 3], padding='SAME', scope='pool1') # 56*48*64
nets = fire_module(nets, 16, 64, scope='fire2')
nets = fire_module(nets, 16, 64, scope='fire3')
nets = slim.max_pool2d(nets, [3, 3], padding='SAME', scope='pool1') # 28*24*128
nets = fire_module(nets, 32, 128, scope='fire4')
nets = fire_module(nets, 32, 128, scope='fire5')
nets = slim.max_pool2d(nets, [3, 3], padding='SAME', scope='pool5') # 14*12*256
nets = fire_module(nets, 48, 192, scope='fire6')
nets = fire_module(nets, 48, 192, scope='fire7')
nets = slim.max_pool2d(nets, [3, 3], padding='SAME', scope='pool6') # 7*6*384
nets = fire_module(nets, 64, 256, scope='fire8')
nets = fire_module(nets, 64, 256, scope='fire9') # 7*6*512
nets = slim.dropout(nets, keep_prob, is_training=is_training, scope='dropout9')
nets = slim.avg_pool2d(nets, [7, 6], scope='pool9') # 1*1*512
return nets
def fire_module(inputs,
squeeze_depth,
expand_depth,
reuse=None,
scope=None,
outputs_collections=None):
with tf.variable_scope(scope, 'fire', [inputs], reuse=reuse):
with slim.arg_scope([slim.conv2d, slim.max_pool2d],
outputs_collections=None):
net = squeeze(inputs, squeeze_depth)
outputs = expand(net, expand_depth)
return outputs
def densenet_a(inputs,
num_classes=1000,
is_training=True,
keep_prob=0.2,
growth_rate=32,
reduction=0.6,
spatial_squeeze=True,
scope='densenet_121'):
"""
Densenet 121-Layers version.
"""
with tf.name_scope(scope, 'densenet_121', [inputs]) as sc:
end_points_collection = sc + '_end_points'
# Collect outputs for conv2d, fully_connected and max_pool2d.
with slim.arg_scope([slim.conv2d, slim.max_pool2d,
slim.avg_pool2d],
outputs_collections=end_points_collection):
nets = densenet_inference(inputs, is_training, keep_prob, growth_rate, reduction)
nets = slim.conv2d(nets, num_classes, [1, 1],
activation_fn=None,
normalizer_fn=None,
scope='logits')
end_points = slim.utils.convert_collection_to_dict(end_points_collection)
if spatial_squeeze:
nets = tf.squeeze(nets, [1, 2], name='logits/squeezed')
return nets, end_points
def maxpool(self, x, kernel_size):
p = np.floor((kernel_size - 1) / 2).astype(np.int32)
p_x = tf.pad(x, [[0, 0], [p, p], [p, p], [0, 0]])
return slim.max_pool2d(p_x, kernel_size)
def vgg_16(inputs,
variables_collections=None,
scope='vgg_16',
reuse=None):
"""
modification of vgg_16 in TF-slim
see original code in https://github.com/tensorflow/models/blob/master/slim/nets/vgg.py
"""
with tf.variable_scope(scope, 'vgg_16', [inputs]) as sc:
# Collect outputs for conv2d, fully_connected and max_pool2d.
with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d]):
conv1 = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1', biases_initializer=None,
variables_collections=variables_collections, reuse=reuse)
pool1, argmax_1 = tf.nn.max_pool_with_argmax(conv1, [1,2,2,1], [1,2,2,1], padding='VALID', name='pool1')
conv2 = slim.repeat(pool1, 2, slim.conv2d, 128, [3, 3], scope='conv2', biases_initializer=None,
variables_collections=variables_collections, reuse=reuse)
pool2, argmax_2 = tf.nn.max_pool_with_argmax(conv2, [1,2,2,1], [1,2,2,1], padding='VALID', name='pool2')
conv3 = slim.repeat(pool2, 3, slim.conv2d, 256, [3, 3], scope='conv3', biases_initializer=None,
variables_collections=variables_collections, reuse=reuse)
pool3, argmax_3 = tf.nn.max_pool_with_argmax(conv3, [1,2,2,1], [1,2,2,1], padding='VALID', name='pool3')
conv4 = slim.repeat(pool3, 3, slim.conv2d, 512, [3, 3], scope='conv4', biases_initializer=None,
variables_collections=variables_collections, reuse=reuse)
pool4, argmax_4 = tf.nn.max_pool_with_argmax(conv4, [1,2,2,1], [1,2,2,1], padding='VALID', name='pool4')
conv5 = slim.repeat(pool4, 3, slim.conv2d, 512, [3, 3], scope='conv5', biases_initializer=None,
variables_collections=variables_collections, reuse=reuse)
pool5, argmax_5 = tf.nn.max_pool_with_argmax(conv5, [1,2,2,1], [1,2,2,1], padding='VALID', name='pool5')
# return argmax
argmax = (argmax_1, argmax_2, argmax_3, argmax_4, argmax_5)
# return feature maps
features = (conv1, conv2, conv3, conv4, conv5)
return pool5, argmax, features
def fire_module(inputs,
squeeze_depth,
expand_depth,
reuse=None,
scope=None,
outputs_collections=None):
with tf.variable_scope(scope, 'fire', [inputs], reuse=reuse):
with slim.arg_scope([slim.conv2d, slim.max_pool2d],
outputs_collections=None):
net = squeeze(inputs, squeeze_depth)
outputs = expand(net, expand_depth)
return outputs