本文整理汇总了Python中keras.layers.Add方法的典型用法代码示例。如果您正苦于以下问题:Python layers.Add方法的具体用法?Python layers.Add怎么用?Python layers.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers
的用法示例。
在下文中一共展示了layers.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: conv_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def conv_block(input, base, k=1, dropout=0.0):
init = input
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
x = BatchNormalization(axis=channel_axis, momentum=0.1, epsilon=1e-5, gamma_initializer='uniform')(input)
x = Activation('relu')(x)
x = Convolution2D(base * k, (3, 3), padding='same', kernel_initializer='he_normal',
use_bias=False)(x)
if dropout > 0.0: x = Dropout(dropout)(x)
x = BatchNormalization(axis=channel_axis, momentum=0.1, epsilon=1e-5, gamma_initializer='uniform')(x)
x = Activation('relu')(x)
x = Convolution2D(base * k, (3, 3), padding='same', kernel_initializer='he_normal',
use_bias=False)(x)
m = Add()([init, x])
return m
示例2: residual
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [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
示例3: expand_conv
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def expand_conv(init, base, k, strides=(1, 1)):
x = Convolution2D(base * k, (3, 3), padding='same', strides=strides, kernel_initializer='he_normal',
use_bias=False)(init)
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
x = BatchNormalization(axis=channel_axis, momentum=0.1, epsilon=1e-5, gamma_initializer='uniform')(x)
x = Activation('relu')(x)
x = Convolution2D(base * k, (3, 3), padding='same', kernel_initializer='he_normal',
use_bias=False)(x)
skip = Convolution2D(base * k, (1, 1), padding='same', strides=strides, kernel_initializer='he_normal',
use_bias=False)(init)
m = Add()([x, skip])
return m
示例4: __call__
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def __call__(self, x, encoder_output, return_attention=False):
x_embedded = self._embedding(x)
pos_encoding = self._position_encoding(x)
pos_encoding_embedded = self._position_embedding(pos_encoding)
x = Add()([x_embedded, pos_encoding_embedded])
self_atts = []
enc_atts = []
for layer in self._layers:
x, self_att, enc_att = layer(x, encoder_output)
if return_attention:
self_atts.append(self_att)
enc_atts.append(enc_att)
if return_attention:
return [x, self_atts, enc_atts]
else:
return x
示例5: _residual_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def _residual_block(self, units, inputs):
out = Dense(units=units, kernel_regularizer=self.kernel_regularizer, activation=self.activation,
kernel_initializer=self.kernel_initializer, kernel_constraint=self.kernel_constraint,
use_bias=self.use_bias, bias_regularizer=self.bias_regularizer,
bias_initializer=self.bias_initializer, bias_constraint=self.bias_constraint)(inputs)
out = Dropout(self.dropout)(out)
out = Dense(units=units, kernel_regularizer=self.kernel_regularizer, activation=self.activation,
kernel_initializer=self.kernel_initializer, kernel_constraint=self.kernel_constraint,
use_bias=self.use_bias, bias_regularizer=self.bias_regularizer,
bias_initializer=self.bias_initializer, bias_constraint=self.bias_constraint)(out)
out = BatchNormalization(trainable=True)(out)
if K.int_shape(inputs)[-1] != K.int_shape(out)[-1]:
inputs = Dense(units=units, kernel_regularizer=self.kernel_regularizer, activation=self.activation,
kernel_initializer=self.kernel_initializer, kernel_constraint=self.kernel_constraint,
use_bias=self.use_bias, bias_regularizer=self.bias_regularizer,
bias_initializer=self.bias_initializer, bias_constraint=self.bias_constraint)(inputs)
out = Add()([inputs, out])
return out
示例6: shortcut_pool
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def shortcut_pool(inputs, output, filters=256, pool_type='max', shortcut=True):
"""
ResNet(shortcut连接|skip连接|residual连接),
这里是用shortcut连接. 恒等映射, block+f(block)
再加上 downsampling实现
参考: https://github.com/zonetrooper32/VDCNN/blob/keras_version/vdcnn.py
:param inputs: tensor
:param output: tensor
:param filters: int
:param pool_type: str, 'max'、'k-max' or 'conv' or other
:param shortcut: boolean
:return: tensor
"""
if shortcut:
conv_2 = Conv1D(filters=filters, kernel_size=1, strides=2, padding='SAME')(inputs)
conv_2 = BatchNormalization()(conv_2)
output = downsampling(output, pool_type=pool_type)
out = Add()([output, conv_2])
else:
out = ReLU(inputs)
out = downsampling(out, pool_type=pool_type)
if pool_type is not None: # filters翻倍
out = Conv1D(filters=filters*2, kernel_size=1, strides=1, padding='SAME')(out)
out = BatchNormalization()(out)
return out
示例7: __init__
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def __init__(self, name: str, num_heads: int,
residual_dropout: float = 0, attention_dropout: float = 0,
activation: Optional[Union[str, Callable]] = 'gelu',
compression_window_size: int = None,
use_masking: bool = True,
vanilla_wiring=False):
self.attention_layer = MultiHeadSelfAttention(
num_heads, use_masking=use_masking, dropout=attention_dropout,
compression_window_size=compression_window_size,
name=f'{name}_self_attention')
self.norm1_layer = LayerNormalization(name=f'{name}_normalization1')
self.dropout_layer = (
Dropout(residual_dropout, name=f'{name}_dropout')
if residual_dropout > 0
else lambda x: x)
self.norm2_layer = LayerNormalization(name=f'{name}_normalization2')
self.transition_layer = TransformerTransition(
name=f'{name}_transition', activation=activation)
self.addition_layer = Add(name=f'{name}_add')
self.vanilla_wiring = vanilla_wiring
示例8: expand_conv
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def expand_conv(init, base, k, strides=(1, 1)):
x = Convolution2D(base * k, (3, 3), padding='same', strides=strides, kernel_initializer='he_normal',
use_bias=False)(init)
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
x = BatchRenormalization(axis=channel_axis, momentum=0.1, epsilon=1e-5, gamma_init='uniform')(x)
x = Activation('relu')(x)
x = Convolution2D(base * k, (3, 3), padding='same', kernel_initializer='he_normal',
use_bias=False)(x)
skip = Convolution2D(base * k, (1, 1), padding='same', strides=strides, kernel_initializer='he_normal',
use_bias=False)(init)
m = Add()([x, skip])
return m
示例9: build_generator
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def build_generator(self):
"""Resnet Generator"""
def residual_block(layer_input):
"""Residual block described in paper"""
d = Conv2D(64, kernel_size=3, strides=1, padding='same')(layer_input)
d = BatchNormalization(momentum=0.8)(d)
d = Activation('relu')(d)
d = Conv2D(64, kernel_size=3, strides=1, padding='same')(d)
d = BatchNormalization(momentum=0.8)(d)
d = Add()([d, layer_input])
return d
# Image input
img = Input(shape=self.img_shape)
l1 = Conv2D(64, kernel_size=3, padding='same', activation='relu')(img)
# Propogate signal through residual blocks
r = residual_block(l1)
for _ in range(self.residual_blocks - 1):
r = residual_block(r)
output_img = Conv2D(self.channels, kernel_size=3, padding='same', activation='tanh')(r)
return Model(img, output_img)
示例10: identity_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def identity_block(input_tensor, kernel_size, filters, stage, block,
use_bias=True, train_bn=True):
"""The identity_block is the block that has no conv layer at shortcut
# Arguments
input_tensor: input tensor
kernel_size: default 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
use_bias: Boolean. To use or not use a bias in conv layers.
train_bn: Boolean. Train or freeze Batch Norm layers
"""
nb_filter1, nb_filter2, nb_filter3 = filters
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
x = KL.Conv2D(nb_filter1, (1, 1), name=conv_name_base + '2a',
use_bias=use_bias)(input_tensor)
x = BatchNorm(name=bn_name_base + '2a')(x, training=train_bn)
x = KL.Activation('relu')(x)
x = KL.Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same',
name=conv_name_base + '2b', use_bias=use_bias)(x)
x = BatchNorm(name=bn_name_base + '2b')(x, training=train_bn)
x = KL.Activation('relu')(x)
x = KL.Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c',
use_bias=use_bias)(x)
x = BatchNorm(name=bn_name_base + '2c')(x, training=train_bn)
x = KL.Add()([x, input_tensor])
x = KL.Activation('relu', name='res' + str(stage) + block + '_out')(x)
return x
示例11: conv_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def conv_block(input_tensor, kernel_size, filters, stage, block,
strides=(2, 2), use_bias=True, train_bn=True):
"""conv_block is the block that has a conv layer at shortcut
# Arguments
input_tensor: input tensor
kernel_size: default 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
use_bias: Boolean. To use or not use a bias in conv layers.
train_bn: Boolean. Train or freeze Batch Norm layers
Note that from stage 3, the first conv layer at main path is with subsample=(2,2)
And the shortcut should have subsample=(2,2) as well
"""
nb_filter1, nb_filter2, nb_filter3 = filters
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
x = KL.Conv2D(nb_filter1, (1, 1), strides=strides,
name=conv_name_base + '2a', use_bias=use_bias)(input_tensor)
x = BatchNorm(name=bn_name_base + '2a')(x, training=train_bn)
x = KL.Activation('relu')(x)
x = KL.Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same',
name=conv_name_base + '2b', use_bias=use_bias)(x)
x = BatchNorm(name=bn_name_base + '2b')(x, training=train_bn)
x = KL.Activation('relu')(x)
x = KL.Conv2D(nb_filter3, (1, 1), name=conv_name_base +
'2c', use_bias=use_bias)(x)
x = BatchNorm(name=bn_name_base + '2c')(x, training=train_bn)
shortcut = KL.Conv2D(nb_filter3, (1, 1), strides=strides,
name=conv_name_base + '1', use_bias=use_bias)(input_tensor)
shortcut = BatchNorm(name=bn_name_base + '1')(shortcut, training=train_bn)
x = KL.Add()([x, shortcut])
x = KL.Activation('relu', name='res' + str(stage) + block + '_out')(x)
return x
示例12: get_srresnet_model
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def get_srresnet_model(input_channel_num=3, feature_dim=64, resunit_num=16):
def _residual_block(inputs):
x = Conv2D(feature_dim, (3, 3), padding="same", kernel_initializer="he_normal")(inputs)
x = BatchNormalization()(x)
x = PReLU(shared_axes=[1, 2])(x)
x = Conv2D(feature_dim, (3, 3), padding="same", kernel_initializer="he_normal")(x)
x = BatchNormalization()(x)
m = Add()([x, inputs])
return m
inputs = Input(shape=(None, None, input_channel_num))
x = Conv2D(feature_dim, (3, 3), padding="same", kernel_initializer="he_normal")(inputs)
x = PReLU(shared_axes=[1, 2])(x)
x0 = x
for i in range(resunit_num):
x = _residual_block(x)
x = Conv2D(feature_dim, (3, 3), padding="same", kernel_initializer="he_normal")(x)
x = BatchNormalization()(x)
x = Add()([x, x0])
x = Conv2D(input_channel_num, (3, 3), padding="same", kernel_initializer="he_normal")(x)
model = Model(inputs=inputs, outputs=x)
return model
# UNet: code from https://github.com/pietz/unet-keras
示例13: resblock_body
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [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
示例14: _inverted_res_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def _inverted_res_block(inputs, expansion, stride, alpha, filters, block_id, skip_connection, rate=1):
in_channels = inputs.shape[-1].value # inputs._keras_shape[-1]
pointwise_conv_filters = int(filters * alpha)
pointwise_filters = _make_divisible(pointwise_conv_filters, 8)
x = inputs
prefix = 'expanded_conv_{}_'.format(block_id)
if block_id:
# Expand
x = Conv2D(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 = Activation(relu6, name=prefix + 'expand_relu')(x)
else:
prefix = 'expanded_conv_'
# Depthwise
x = DepthwiseConv2D(kernel_size=3, strides=stride, activation=None,
use_bias=False, padding='same', dilation_rate=(rate, rate),
name=prefix + 'depthwise')(x)
x = BatchNormalization(epsilon=1e-3, momentum=0.999,
name=prefix + 'depthwise_BN')(x)
x = Activation(relu6, name=prefix + 'depthwise_relu')(x)
# Project
x = Conv2D(pointwise_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 skip_connection:
return Add(name=prefix + 'add')([inputs, x])
# if in_channels == pointwise_filters and stride == 1:
# return Add(name='res_connect_' + str(block_id))([inputs, x])
return x
示例15: res_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Add [as 别名]
def res_block(input_tensor, f):
x = input_tensor
x = Conv2D(f, kernel_size=3, kernel_initializer=conv_init, use_bias=False, padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2D(f, kernel_size=3, kernel_initializer=conv_init, use_bias=False, padding="same")(x)
x = Add()([x, input_tensor])
x = LeakyReLU(alpha=0.2)(x)
return x