本文整理汇总了Python中tensorflow.keras.layers.ZeroPadding2D方法的典型用法代码示例。如果您正苦于以下问题:Python layers.ZeroPadding2D方法的具体用法?Python layers.ZeroPadding2D怎么用?Python layers.ZeroPadding2D使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.layers
的用法示例。
在下文中一共展示了layers.ZeroPadding2D方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def __init__(self,
strides=2,
extra_padding=False,
data_format="channels_last",
**kwargs):
super(PnasMaxPoolBlock, self).__init__(**kwargs)
self.extra_padding = extra_padding
self.data_format = data_format
self.pool = MaxPool2d(
pool_size=3,
strides=strides,
padding=1,
data_format=data_format,
name="pool")
if self.extra_padding:
self.pad = nn.ZeroPadding2D(
padding=((1, 0), (1, 0)),
data_format=data_format)
示例2: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def __init__(self,
extra_padding=False,
data_format="channels_last",
**kwargs):
super(NasMaxPoolBlock, self).__init__(**kwargs)
self.extra_padding = extra_padding
self.data_format = data_format
self.pool = MaxPool2d(
pool_size=3,
strides=2,
padding=1,
data_format=data_format,
name="pool")
if self.extra_padding:
self.pad = nn.ZeroPadding2D(
padding=((1, 0), (1, 0)),
data_format=data_format)
示例3: _pad_or_crop_to_shape_2D
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def _pad_or_crop_to_shape_2D(x, in_shape, tgt_shape):
'''
in_shape, tgt_shape are both 2x1 numpy arrays
'''
in_shape = np.asarray(in_shape)
tgt_shape = np.asarray(tgt_shape)
print('Padding input from {} to {}'.format(in_shape, tgt_shape))
im_diff = in_shape - tgt_shape
if im_diff[0] < 0:
pad_amt = (int(np.ceil(abs(im_diff[0]) / 2.0)), int(np.floor(abs(im_diff[0]) / 2.0)))
x = ZeroPadding2D((pad_amt, (0, 0)))(x)
if im_diff[1] < 0:
pad_amt = (int(np.ceil(abs(im_diff[1]) / 2.0)), int(np.floor(abs(im_diff[1]) / 2.0)))
x = ZeroPadding2D(((0, 0), pad_amt))(x)
if im_diff[0] > 0:
crop_amt = (int(np.ceil(im_diff[0] / 2.0)), int(np.floor(im_diff[0] / 2.0)))
x = Cropping2D((crop_amt, (0, 0)))(x)
if im_diff[1] > 0:
crop_amt = (int(np.ceil(im_diff[1] / 2.0)), int(np.floor(im_diff[1] / 2.0)))
x = Cropping2D(((0, 0), crop_amt))(x)
return x
示例4: residual
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def residual(_x, out_dim, name, stride=1):
shortcut = _x
num_channels = K.int_shape(shortcut)[-1]
_x = ZeroPadding2D(padding=1, name=name + '.pad1')(_x)
_x = Conv2D(out_dim, 3, strides=stride, use_bias=False, name=name + '.conv1')(_x)
_x = BatchNormalization(epsilon=1e-5, name=name + '.bn1')(_x)
_x = Activation('relu', name=name + '.relu1')(_x)
_x = Conv2D(out_dim, 3, padding='same', use_bias=False, name=name + '.conv2')(_x)
_x = BatchNormalization(epsilon=1e-5, name=name + '.bn2')(_x)
if num_channels != out_dim or stride != 1:
shortcut = Conv2D(out_dim, 1, strides=stride, use_bias=False, name=name + '.shortcut.0')(
shortcut)
shortcut = BatchNormalization(epsilon=1e-5, name=name + '.shortcut.1')(shortcut)
_x = Add(name=name + '.add')([_x, shortcut])
_x = Activation('relu', name=name + '.relu')(_x)
return _x
示例5: resblock_body
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def resblock_body(x, num_filters, num_blocks, all_narrow=True):
'''A series of resblocks starting with a downsampling Convolution2D'''
# Darknet uses left and top padding instead of 'same' mode
x = ZeroPadding2D(((1,0),(1,0)))(x)
x = DarknetConv2D_BN_Mish(num_filters, (3,3), strides=(2,2))(x)
res_connection = DarknetConv2D_BN_Mish(num_filters//2 if all_narrow else num_filters, (1,1))(x)
x = DarknetConv2D_BN_Mish(num_filters//2 if all_narrow else num_filters, (1,1))(x)
for i in range(num_blocks):
y = compose(
DarknetConv2D_BN_Mish(num_filters//2, (1,1)),
DarknetConv2D_BN_Mish(num_filters//2 if all_narrow else num_filters, (3,3)))(x)
x = Add()([x,y])
x = DarknetConv2D_BN_Mish(num_filters//2 if all_narrow else num_filters, (1,1))(x)
x = Concatenate()([x , res_connection])
return DarknetConv2D_BN_Mish(num_filters, (1,1))(x)
示例6: get_padding
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def get_padding(pad_type, padding):
if pad_type == "reflect":
return ReflectionPadding2D(padding)
elif pad_type == "constant":
return ZeroPadding2D(padding)
else:
raise ValueError(f"Unrecognized pad_type {pad_type}")
示例7: convolution
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def convolution(_x, k, out_dim, name, stride=1):
padding = (k - 1) // 2
_x = ZeroPadding2D(padding=padding, name=name + '.pad')(_x)
_x = Conv2D(out_dim, k, strides=stride, use_bias=False, name=name + '.conv')(_x)
_x = BatchNormalization(epsilon=1e-5, name=name + '.bn')(_x)
_x = Activation('relu', name=name + '.relu')(_x)
return _x
示例8: resblock_body
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def resblock_body(x, num_filters, num_blocks):
'''A series of resblocks starting with a downsampling Convolution2D'''
# Darknet uses left and top padding instead of 'same' mode
x = ZeroPadding2D(((1,0),(1,0)))(x)
x = DarknetConv2D_BN_Leaky(num_filters, (3,3), strides=(2,2))(x)
for i in range(num_blocks):
y = compose(
DarknetConv2D_BN_Leaky(num_filters//2, (1,1)),
DarknetConv2D_BN_Leaky(num_filters, (3,3)))(x)
x = Add()([x,y])
return x
示例9: depthwise_separable_resblock_body
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def depthwise_separable_resblock_body(x, num_filters, num_blocks):
'''A series of resblocks starting with a downsampling Convolution2D'''
# Darknet uses left and top padding instead of 'same' mode
x = ZeroPadding2D(((1,0),(1,0)))(x)
x = Darknet_Depthwise_Separable_Conv2D_BN_Leaky(num_filters, (3,3), strides=(2,2))(x)
for i in range(num_blocks):
y = compose(
DarknetConv2D_BN_Leaky(num_filters//2, (1,1)),
Darknet_Depthwise_Separable_Conv2D_BN_Leaky(num_filters, (3,3)))(x)
x = Add()([x,y])
return x
示例10: _ep_block
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def _ep_block(inputs, filters, stride, expansion, block_id):
#in_channels = backend.int_shape(inputs)[-1]
in_channels = inputs.shape.as_list()[-1]
pointwise_conv_filters = int(filters)
x = inputs
prefix = 'ep_block_{}_'.format(block_id)
# Expand
x = Conv2D(int(expansion * in_channels), kernel_size=1, padding='same', use_bias=False, activation=None, name=prefix + 'expand')(x)
x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'expand_BN')(x)
x = ReLU(6., name=prefix + 'expand_relu')(x)
# Depthwise
if stride == 2:
x = ZeroPadding2D(padding=correct_pad(K, x, 3), name=prefix + 'pad')(x)
x = DepthwiseConv2D(kernel_size=3, strides=stride, activation=None, use_bias=False, padding='same' if stride == 1 else 'valid', name=prefix + 'depthwise')(x)
x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'depthwise_BN')(x)
x = ReLU(6., name=prefix + 'depthwise_relu')(x)
# Project
x = Conv2D(pointwise_conv_filters, kernel_size=1, padding='same', use_bias=False, activation=None, name=prefix + 'project')(x)
x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'project_BN')(x)
if in_channels == pointwise_conv_filters and stride == 1:
return Add(name=prefix + 'add')([inputs, x])
return x
示例11: identity_block
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def identity_block(x, f_kernel_size, filters, dilation, pad):
filters_1, filters_2, filters_3 = filters
x_shortcut = x
# stage 1
x = Conv2D(filters=filters_1, kernel_size=(1, 1), strides=(1, 1), padding='valid',
kernel_initializer=glorot_uniform(seed=0))(x)
x = BatchNormalization(momentum=0.95, axis=-1)(x)
x = Activation(activation='relu')(x)
# stage 2
x = ZeroPadding2D(padding=pad)(x)
x = Conv2D(filters=filters_2, kernel_size=f_kernel_size, strides=(1, 1),
dilation_rate=dilation, kernel_initializer=glorot_uniform(seed=0))(x)
x = BatchNormalization(momentum=0.95, axis=-1)(x)
x = Activation(activation='relu')(x)
# stage 3
x = Conv2D(filters=filters_3, kernel_size=(1, 1), strides=(1, 1), padding='valid',
kernel_initializer=glorot_uniform(seed=0))(x)
x = BatchNormalization(momentum=0.95, axis=-1)(x)
# stage 4
x = Add()([x, x_shortcut])
x = Activation(activation='relu')(x)
return x
示例12: convolutional_block
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def convolutional_block(x, f_kernel_size, filters, strides, dilation, pad):
filters_1, filters_2, filters_3 = filters
x_shortcut = x
# stage 1
x = Conv2D(filters=filters_1, kernel_size=(1, 1), strides=strides, padding='valid',
kernel_initializer=glorot_uniform(seed=0))(x)
x = BatchNormalization(momentum=0.95, axis=-1)(x)
x = Activation(activation='relu')(x)
# stage 2
x = ZeroPadding2D(padding=pad)(x)
x = Conv2D(filters=filters_2, kernel_size=f_kernel_size, strides=(1, 1), dilation_rate=dilation,
kernel_initializer=glorot_uniform(seed=0))(x)
x = BatchNormalization(momentum=0.95, axis=-1)(x)
x = Activation(activation='relu')(x)
# stage 3
x = Conv2D(filters=filters_3, kernel_size=(1, 1), strides=(1, 1),
kernel_initializer=glorot_uniform(seed=0))(x)
x = BatchNormalization(momentum=0.95, axis=-1)(x)
x = Activation(activation='relu')(x)
# stage 4
x_shortcut = Conv2D(filters=filters_3, kernel_size=(1, 1), strides=strides, padding='valid',
kernel_initializer=glorot_uniform(seed=0))(x_shortcut)
x_shortcut = BatchNormalization(momentum=0.95, axis=-1)(x_shortcut)
# stage 5
x = Add()([x, x_shortcut])
x = Activation(activation='relu')(x)
return x
示例13: identity_block
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def identity_block(input_tensor, kernel_size, filters, stage, block):
'''The identity_block is the block that has no conv layer at shortcut
# Arguments
input_tensor: input tensor
kernel_size: defualt 3, the kernel size of middle conv layer at main path
filters: list of integers, the nb_filters of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
'''
eps = 1.1e-5
nb_filter1, nb_filter2, nb_filter3 = filters
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
scale_name_base = 'scale' + str(stage) + block + '_branch'
x = Conv2D(nb_filter1, (1, 1), name=conv_name_base + '2a', use_bias=False)(input_tensor)
x = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '2a')(x)
x = Scale(axis=bn_axis, name=scale_name_base + '2a')(x)
x = Activation('relu', name=conv_name_base + '2a_relu')(x)
x = ZeroPadding2D((1, 1), name=conv_name_base + '2b_zeropadding')(x)
x = Conv2D(nb_filter2, kernel_size,
name=conv_name_base + '2b', use_bias=False)(x)
x = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '2b')(x)
x = Scale(axis=bn_axis, name=scale_name_base + '2b')(x)
x = Activation('relu', name=conv_name_base + '2b_relu')(x)
x = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', use_bias=False)(x)
x = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '2c')(x)
x = Scale(axis=bn_axis, name=scale_name_base + '2c')(x)
x = Add(name='res' + str(stage) + block)([x, input_tensor])
x = Activation('relu', name='res' + str(stage) + block + '_relu')(x)
return x
示例14: block2
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def block2(x, filters, kernel_size=3, stride=1,
conv_shortcut=False, name=None):
"""A residual block.
# Arguments
x: input tensor.
filters: integer, filters of the bottleneck layer.
kernel_size: default 3, kernel size of the bottleneck layer.
stride: default 1, stride of the first layer.
conv_shortcut: default False, use convolution shortcut if True,
otherwise identity shortcut.
name: string, block label.
# Returns
Output tensor for the residual block.
"""
bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
preact = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_preact_bn')(x)
preact = layers.Activation('relu', name=name + '_preact_relu')(preact)
if conv_shortcut is True:
shortcut = layers.Conv2D(4 * filters, 1, strides=stride,
name=name + '_0_conv')(preact)
else:
shortcut = layers.MaxPooling2D(
1, strides=stride)(x) if stride > 1 else x
x = layers.Conv2D(filters, 1, strides=1, use_bias=False,
name=name + '_1_conv')(preact)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_1_bn')(x)
x = layers.Activation('relu', name=name + '_1_relu')(x)
x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name=name + '_2_pad')(x)
x = layers.Conv2D(filters, kernel_size, strides=stride,
use_bias=False, name=name + '_2_conv')(x)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_2_bn')(x)
x = layers.Activation('relu', name=name + '_2_relu')(x)
x = layers.Conv2D(4 * filters, 1, name=name + '_3_conv')(x)
x = layers.Add(name=name + '_out')([shortcut, x])
return x
示例15: block3
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import ZeroPadding2D [as 别名]
def block3(x, filters, kernel_size=3, stride=1, groups=32,
conv_shortcut=True, name=None):
"""A residual block.
# Arguments
x: input tensor.
filters: integer, filters of the bottleneck layer.
kernel_size: default 3, kernel size of the bottleneck layer.
stride: default 1, stride of the first layer.
groups: default 32, group size for grouped convolution.
conv_shortcut: default True, use convolution shortcut if True,
otherwise identity shortcut.
name: string, block label.
# Returns
Output tensor for the residual block.
"""
bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
if conv_shortcut is True:
shortcut = layers.Conv2D((64 // groups) * filters, 1, strides=stride,
use_bias=False, name=name + '_0_conv')(x)
shortcut = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_0_bn')(shortcut)
else:
shortcut = x
x = layers.Conv2D(filters, 1, use_bias=False, name=name + '_1_conv')(x)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_1_bn')(x)
x = layers.Activation('relu', name=name + '_1_relu')(x)
c = filters // groups
x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name=name + '_2_pad')(x)
x = layers.DepthwiseConv2D(kernel_size, strides=stride, depth_multiplier=c,
use_bias=False, name=name + '_2_conv')(x)
x_shape = backend.int_shape(x)[1:-1]
x = layers.Reshape(x_shape + (groups, c, c))(x)
output_shape = x_shape + (groups,
c) if backend.backend() == 'theano' else None
x = layers.Lambda(lambda x: sum([x[:, :, :, :, i] for i in range(c)]),
output_shape=output_shape, name=name + '_2_reduce')(x)
x = layers.Reshape(x_shape + (filters,))(x)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_2_bn')(x)
x = layers.Activation('relu', name=name + '_2_relu')(x)
x = layers.Conv2D((64 // groups) * filters, 1, use_bias=False,
name=name + '_3_conv')(x)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_3_bn')(x)
x = layers.Add(name=name + '_add')([shortcut, x])
x = layers.Activation('relu', name=name + '_out')(x)
return x