本文整理汇总了Python中tensorflow.keras.layers.MaxPool2D方法的典型用法代码示例。如果您正苦于以下问题:Python layers.MaxPool2D方法的具体用法?Python layers.MaxPool2D怎么用?Python layers.MaxPool2D使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.layers
的用法示例。
在下文中一共展示了layers.MaxPool2D方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_and_append_layer
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def create_and_append_layer(self, layer, list_to_append_layer_to, activation=None, output_layer=False):
"""Creates and appends a layer to the list provided"""
layer_name = layer[0].lower()
assert layer_name in self.valid_cnn_hidden_layer_types, "Layer name {} not valid, use one of {}".format(
layer_name, self.valid_cnn_hidden_layer_types)
if layer_name == "conv":
list_to_append_layer_to.extend([Conv2D(filters=layer[1], kernel_size=layer[2],
strides=layer[3], padding=layer[4], activation=activation,
kernel_initializer=self.initialiser_function)])
elif layer_name == "maxpool":
list_to_append_layer_to.extend([MaxPool2D(pool_size=(layer[1], layer[1]),
strides=(layer[2], layer[2]), padding=layer[3])])
elif layer_name == "avgpool":
list_to_append_layer_to.extend([AveragePooling2D(pool_size=(layer[1], layer[1]),
strides=(layer[2], layer[2]), padding=layer[3])])
elif layer_name == "linear":
list_to_append_layer_to.extend([Dense(layer[1], activation=activation, kernel_initializer=self.initialiser_function)])
else:
raise ValueError("Wrong layer name")
示例2: residual_block_id
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def residual_block_id(self,tensor, feature_n,name=None):
if name != None:
depconv_1 = DepthwiseConv2D(3,2,padding='same',name=name+"/dconv")(tensor)
conv_2 = Conv2D(feature_n,1,name=name+"/conv")(depconv_1)
else:
depconv_1 = DepthwiseConv2D(3,2,padding='same')(tensor)
conv_2 = Conv2D(feature_n,1)(depconv_1)
maxpool_1 = MaxPool2D(pool_size=(2,2),strides=(2,2),padding='same')(tensor)
conv_zeros = Conv2D(feature_n/2,2,strides=2,use_bias=False,kernel_initializer=tf.zeros_initializer())(tensor)
padding_1 = Concatenate(axis=-1)([maxpool_1,conv_zeros])#self.feature_padding(maxpool_1)
add = Add()([padding_1,conv_2])
relu = ReLU()(add)
return relu
#def feature_padding(self,tensor,channels_n=0):
# #pad = tf.keras.layers.ZeroPadding2D(((0,0),(0,0),(0,tensor.shape[3])))(tensor)
# return Concatenate(axis=3)([tensor,pad])
示例3: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def __init__(self,
in_channels,
out_channels,
strides,
body_class=ResBlock,
return_down=False,
data_format="channels_last",
**kwargs):
super(DLAResBlock, self).__init__(**kwargs)
self.return_down = return_down
self.downsample = (strides > 1)
self.project = (in_channels != out_channels)
self.body = body_class(
in_channels=in_channels,
out_channels=out_channels,
strides=strides,
data_format=data_format,
name="body")
self.activ = nn.ReLU()
if self.downsample:
self.downsample_pool = nn.MaxPool2D(
pool_size=strides,
strides=strides,
data_format=data_format,
name="downsample_pool")
if self.project:
self.project_conv = conv1x1_block(
in_channels=in_channels,
out_channels=out_channels,
activation=None,
data_format=data_format,
name="project_conv")
示例4: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def __init__(self, layers_info, output_activation=None, hidden_activations="relu", dropout= 0.0, initialiser="default",
batch_norm=False, y_range=(), random_seed=0, input_dim=None):
Model.__init__(self)
self.valid_cnn_hidden_layer_types = {'conv', 'maxpool', 'avgpool', 'linear'}
self.valid_layer_types_with_no_parameters = (MaxPool2D, AveragePooling2D)
Base_Network.__init__(self, layers_info, output_activation, hidden_activations, dropout, initialiser,
batch_norm, y_range, random_seed, input_dim)
示例5: encode
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def encode(filters, pool=False, norm=True):
"""downsample sequential model."""
net = Seq()
net.add(
layers.Conv2D(
filters, 3, strides=2, padding="same", kernel_initializer="he_normal"
)
)
if pool:
net.add(layers.MaxPool2D(pool_size=(2, 2)))
if norm:
net.add(layers.BatchNormalization())
net.add(layers.ReLU())
return net
示例6: _hourglass_module
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def _hourglass_module(input, stage_index, number_of_keypoints):
if stage_index == 0:
return _inverted_bottleneck(input, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3), []
else:
# down sample
x = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='SAME')(input)
# block front
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
stage_index -= 1
# block middle
x, middle_layers = _hourglass_module(x, stage_index=stage_index, number_of_keypoints=number_of_keypoints)
# block back
x = _inverted_bottleneck(x, up_channel_rate=6, channels=number_of_keypoints, is_subsample=False, kernel_size=3)
# up sample
upsampling_size = (2, 2) # (x.shape[1] * 2, x.shape[2] * 2)
x = layers.UpSampling2D(size=upsampling_size, interpolation='bilinear')(x)
upsampling_layer = x
# jump layer
x = _inverted_bottleneck(input, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=number_of_keypoints, is_subsample=False, kernel_size=3)
jump_branch_layer = x
# add
x = upsampling_layer + jump_branch_layer
middle_layers.append(x)
return x, middle_layers
示例7: keras_model
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def keras_model():
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Dropout, Input
inputs = Input(shape=(28, 28, 1))
x = Conv2D(32, (3, 3),activation='relu', padding='valid')(inputs)
x = MaxPool2D(pool_size=(2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu')(x)
x = MaxPool2D(pool_size=(2, 2))(x)
x = Flatten()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
outputs = Dense(_NUM_CLASSES, activation='softmax')(x)
return tf.keras.Model(inputs, outputs)
示例8: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def __init__(self, compression_factor=0.5, pool_size=2, **kwargs):
# super(TransitionDown, self).__init__(self, **kwargs)
self.concat = Concatenate()
self.compression_factor = compression_factor
self.pool = layers.MaxPool2D(pool_size)
示例9: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def __init__(self, filters, n_downsample, bottleneck_factor=2):
self.filters = filters
self.bottleneck_factor = bottleneck_factor
n_downsample = n_downsample - 1
self.n_downsample = int(np.maximum(0, n_downsample))
self.conv_7x7 = Conv2D(
filters,
(7, 7),
strides=(2, 2),
padding="same",
activation="relu",
use_bias=False,
)
self.res_blocks = []
self.pool_layers = []
for idx in range(n_downsample):
res_block = ResidualBlock(filters, bottleneck_factor)
max_pool = MaxPool2D(pool_size=(2, 2), strides=(2, 2))
self.res_blocks.append(res_block)
self.pool_layers.append(max_pool)
self.res_output = [
ResidualBlock(filters, bottleneck_factor),
ResidualBlock(filters, bottleneck_factor),
]
示例10: down_module
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def down_module(inputs, out1, out2):
x = layers.MaxPool2D(2, 2)(inputs)
x = res_layer0(x, out1)
out = res_layer1(x, out2)
return out
示例11: base_module
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def base_module(inputs, outchannels):
x = layers.Conv2D(128, 7, strides=2, padding="same")(inputs)
x = layers.BatchNormalization()(x)
x = layers.MaxPool2D(2, strides=2)(x)
out = res_layer1(x, outchannels)
return out
示例12: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def __init__(self, num_classes, first_channel=24, channels_per_stage=(132, 264, 528)):
super(ShuffleNetv2, self).__init__(name="ShuffleNetv2")
self.num_classes = num_classes
self.conv1_bn_relu = Conv2D_BN_ReLU(first_channel, 3, 2)
self.pool1 = MaxPool2D(3, strides=2, padding="SAME")
self.stage2 = ShufflenetStage(first_channel, channels_per_stage[0], 4)
self.stage3 = ShufflenetStage(channels_per_stage[0], channels_per_stage[1], 8)
self.stage4 = ShufflenetStage(channels_per_stage[1], channels_per_stage[2], 4)
#self.conv5_bn_relu = Conv2D_BN_ReLU(1024, 1, 1)
self.gap = GlobalAveragePooling2D()
self.linear = Dense(num_classes)
示例13: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def __init__(self,rgb_mean=None,
**kwargs):
super(DexiNed, self).__init__(**kwargs)
self.rgbn_mean = rgb_mean
self.block_1 = DoubleConvBlock(32, 64, stride=(2,2),use_act=False)
self.block_2 = DoubleConvBlock(128,use_act=False)
self.dblock_3 = _DenseBlock(2, 256)
self.dblock_4 = _DenseBlock(3, 512)
self.dblock_5 = _DenseBlock(3, 512)
self.dblock_6 = _DenseBlock(3, 256)
self.maxpool = layers.MaxPool2D(pool_size=(3, 3), strides=2, padding='same')
# first skip connection
self.side_1 = SingleConvBlock(128,k_size=(1,1),stride=(2,2),use_bs=True,
w_init=weight_init)
self.side_2 = SingleConvBlock(256,k_size=(1,1),stride=(2,2),use_bs=True,
w_init=weight_init)
self.side_3 = SingleConvBlock(512,k_size=(1,1),stride=(2,2),use_bs=True,
w_init=weight_init)
self.side_4 = SingleConvBlock(512,k_size=(1,1),stride=(1,1),use_bs=True,
w_init=weight_init)
# self.side_5 = SingleConvBlock(256,k_size=(1,1),stride=(1,1),use_bs=True,
# w_init=weight_init)
self.pre_dense_2 = SingleConvBlock(256,k_size=(1,1),stride=(2,2),
w_init=weight_init) # use_bn=True
self.pre_dense_3 = SingleConvBlock(256,k_size=(1,1),stride=(1,1),use_bs=True,
w_init=weight_init)
self.pre_dense_4 = SingleConvBlock(512,k_size=(1,1),stride=(1,1),use_bs=True,
w_init=weight_init)
self.pre_dense_5_0 = SingleConvBlock(512, k_size=(1,1),stride=(2,2),
w_init=weight_init) # use_bn=True
self.pre_dense_5 = SingleConvBlock(512,k_size=(1,1),stride=(1,1),use_bs=True,
w_init=weight_init)
self.pre_dense_6 = SingleConvBlock(256,k_size=(1,1),stride=(1,1),use_bs=True,
w_init=weight_init)
self.up_block_1 = UpConvBlock(1)
self.up_block_2 = UpConvBlock(1)
self.up_block_3 = UpConvBlock(2)
self.up_block_4 = UpConvBlock(3)
self.up_block_5 = UpConvBlock(4)
self.up_block_6 = UpConvBlock(4)
self.block_cat = SingleConvBlock(
1,k_size=(1,1),stride=(1,1),
w_init=tf.constant_initializer(1/5))
示例14: _build_layer_components
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def _build_layer_components(self):
"""Builds the layers components and set _layers attribute."""
self.max_pool1 = MaxPool2D(pool_size=(3, 3), strides=2, padding="valid")
self.conv_block1 = [
Conv2D(
int(self.num_filters * 1.5),
kernel_size=(3, 3),
strides=2,
padding="valid",
activation=tf.nn.relu)
]
self.conv_block2 = [
Conv2D(
filters=self.num_filters,
kernel_size=1,
strides=1,
activation=tf.nn.relu,
padding="same")
]
self.conv_block2.append(
Conv2D(
filters=self.num_filters,
kernel_size=3,
strides=1,
activation=tf.nn.relu,
padding="same"))
self.conv_block2.append(
Conv2D(
filters=int(self.num_filters * 1.5),
kernel_size=3,
strides=2,
activation=tf.nn.relu,
padding="valid"))
self.concat_layer = Concatenate()
self.activation_layer = ReLU()
self._layers = self.conv_block1 + self.conv_block2
self._layers.extend(
[self.max_pool1, self.concat_layer, self.activation_layer])
示例15: build_model
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import MaxPool2D [as 别名]
def build_model(num_classes, image_width=None, channels=1):
"""
build CNN-RNN model
"""
def vgg_style(input_tensor):
"""
The original feature extraction structure from CRNN paper.
Related paper: https://ieeexplore.ieee.org/abstract/document/7801919
"""
x = layers.Conv2D(
filters=64,
kernel_size=3,
padding='same',
activation='relu')(input_tensor)
x = layers.MaxPool2D(pool_size=2, padding='same')(x)
x = layers.Conv2D(
filters=128,
kernel_size=3,
padding='same',
activation='relu')(x)
x = layers.MaxPool2D(pool_size=2, padding='same')(x)
x = layers.Conv2D(filters=256, kernel_size=3, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.Conv2D(filters=256, kernel_size=3, padding='same',
activation='relu')(x)
x = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 1),
padding='same')(x)
x = layers.Conv2D(filters=512, kernel_size=3, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.Conv2D(filters=512, kernel_size=3, padding='same',
activation='relu')(x)
x = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 1),
padding='same')(x)
x = layers.Conv2D(filters=512, kernel_size=2)(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
return x
img_input = keras.Input(shape=(32, image_width, channels))
x = vgg_style(img_input)
x = layers.Reshape((-1, 512))(x)
x = layers.Bidirectional(layers.LSTM(units=256, return_sequences=True))(x)
x = layers.Bidirectional(layers.LSTM(units=256, return_sequences=True))(x)
x = layers.Dense(units=num_classes)(x)
return keras.Model(inputs=img_input, outputs=x, name='CRNN')