本文整理匯總了Python中keras.layers.merge.Add方法的典型用法代碼示例。如果您正苦於以下問題:Python merge.Add方法的具體用法?Python merge.Add怎麽用?Python merge.Add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類keras.layers.merge
的用法示例。
在下文中一共展示了merge.Add方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _build_residual_block
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def _build_residual_block(args, x):
cnn_filter_num = args['cnn_filter_num']
cnn_filter_size = args['cnn_filter_size']
l2_reg = args['l2_reg']
in_x = x
x = Conv2D(filters=cnn_filter_num, kernel_size=cnn_filter_size, padding="same",
data_format="channels_first", kernel_regularizer=l2(l2_reg))(x)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
x = Conv2D(filters=cnn_filter_num, kernel_size=cnn_filter_size, padding="same",
data_format="channels_first", kernel_regularizer=l2(l2_reg))(x)
x = BatchNormalization(axis=1)(x)
x = Add()([in_x, x])
x = Activation("relu")(x)
return x
示例2: add_conv_layer
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def add_conv_layer(input_list, layer_name, nb_filters, kernel_size, padding, dropout_rate=0.1,
activation='relu', strides=1, attention_level=0, conv_option="normal", prev_conv_tensors=None):
conv_layer = Convolution1D(filters=nb_filters, kernel_size=kernel_size, padding=padding,
activation=activation, strides=strides, name=layer_name)
max_pooling_layer = GlobalMaxPooling1D()
dropout_layer = Dropout(dropout_rate)
output_list, conv_output_list = [], []
for i in range(len(input_list)):
input = input_list[i]
conv_tensor = conv_layer(input)
if conv_option == "ResNet":
conv_tensor = Add()([conv_tensor, prev_conv_tensors[i][-1]])
dropout_tensor = dropout_layer(conv_tensor)
#conv_pooling_tensor = max_pooling_layer(conv_tensor)
output_list.append(dropout_tensor)
#conv_output_list.append(conv_pooling_tensor)
conv_output_list.append(conv_tensor)
return output_list, conv_output_list
示例3: conv_block
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def conv_block(x0, scale):
x = Conv2D(int(64*scale), (1, 1))(x0)
x = InstanceNormalization()(x)
x = LeakyReLU()(x)
x = Conv2D(int(64*scale), (3, 3), padding='same')(x)
x = InstanceNormalization()(x)
x = LeakyReLU()(x)
x = Conv2D(int(256*scale), (1, 1))(x)
x = InstanceNormalization()(x)
x1 = Conv2D(int(256*scale), (1, 1))(x0)
x1 = InstanceNormalization()(x1)
x = Add()([x, x1])
x = LeakyReLU()(x)
return x
示例4: residual_short
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def residual_short(prev_layer, level, pad=1, lvl=1, sub_lvl=1, modify_stride=False):
prev_layer = Activation('relu')(prev_layer)
block_1 = residual_conv(prev_layer, level,
pad=pad, lvl=lvl, sub_lvl=sub_lvl,
modify_stride=modify_stride)
block_2 = short_convolution_branch(prev_layer, level,
lvl=lvl, sub_lvl=sub_lvl,
modify_stride=modify_stride)
added = Add()([block_1, block_2])
return added
示例5: residual_empty
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def residual_empty(prev_layer, level, pad=1, lvl=1, sub_lvl=1):
prev_layer = Activation('relu')(prev_layer)
block_1 = residual_conv(prev_layer, level, pad=pad,
lvl=lvl, sub_lvl=sub_lvl)
block_2 = empty_branch(prev_layer)
added = Add()([block_1, block_2])
return added
示例6: residual_short
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def residual_short(prev_layer, level, pad=1, lvl=1, sub_lvl=1,
modify_stride=False):
prev_layer = Activation('relu')(prev_layer)
block_1 = residual_conv(prev_layer, level,
pad=pad, lvl=lvl, sub_lvl=sub_lvl,
modify_stride=modify_stride)
block_2 = short_convolution_branch(prev_layer, level,
lvl=lvl, sub_lvl=sub_lvl,
modify_stride=modify_stride)
added = Add()([block_1, block_2])
return added
示例7: _build_residual_block
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def _build_residual_block(self, x):
mc = self.config.model
in_x = x
x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same",
data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(x)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same",
data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(x)
x = BatchNormalization(axis=1)(x)
x = Add()([in_x, x])
x = Activation("relu")(x)
return x
示例8: add_images_with_tiled_vector_layer
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def add_images_with_tiled_vector_layer(images, vector, image_shape=None, vector_shape=None):
"""Tile a vector as if it were channels onto every pixel of an image.
This version is designed to be used as layers within a Keras model.
# Params
images: a list of images to combine, must have equal dimensions
vector: the 1D vector to tile onto every pixel
image_shape: Tuple with 3 entries defining the shape (batch, height, width)
images should be expected to have, do not specify the number
of batches.
vector_shape: Tuple with 3 entries defining the shape (batch, height, width)
images should be expected to have, do not specify the number
of batches.
"""
with K.name_scope('add_images_with_tiled_vector_layer'):
if not isinstance(images, list):
images = [images]
if vector_shape is None:
# check if K.shape, K.int_shape, or vector.get_shape().as_list()[1:] is better
# https://github.com/fchollet/keras/issues/5211
vector_shape = K.int_shape(vector)[1:]
if image_shape is None:
# check if K.shape, K.int_shape, or image.get_shape().as_list()[1:] is better
# https://github.com/fchollet/keras/issues/5211
image_shape = K.int_shape(images[0])[1:]
vector = Reshape([1, 1, vector_shape[-1]])(vector)
tile_shape = (int(1), int(image_shape[0]), int(image_shape[1]), int(1))
tiled_vector = Lambda(lambda x: K.tile(x, tile_shape))(vector)
x = Add()([] + images + [tiled_vector])
return x
示例9: TileOnto
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def TileOnto(x,z,zlen,xsize,add=False):
z = Reshape([1,1,zlen])(z)
tile_shape = (int(1), int(xsize[0]), int(xsize[1]), 1)
z = Lambda(lambda x: K.tile(x, tile_shape))(z)
if not add:
x = Concatenate(axis=-1)([x,z])
else:
x = Add()([x,z])
return x
示例10: DenseHelper
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def DenseHelper(x, dense_size, dropout_rate, repeat):
'''
Add a repeated number of dense layers of the same size.
'''
for i in range(repeat):
if i < repeat - 1:
dr = 0.
else:
dr = dropout_rate
AddDense(x, dense_size, "relu", dr)
return x
示例11: GetHuskyActorModel
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def GetHuskyActorModel(x, num_options, pose_size,
dropout_rate=0.5, batchnorm=True):
'''
Make an "actor" network that takes in an encoded image and an "option"
label and produces the next command to execute.
'''
xin = Input([int(d) for d in x.shape[1:]], name="actor_h_in")
x0in = Input([int(d) for d in x.shape[1:]], name="actor_h0_in")
pose_in = Input((pose_size,), name="actor_pose_in")
option_in = Input((num_options,), name="actor_o_in")
x = xin
x0 = x0in
dr, bn = dropout_rate, False
use_lrelu = False
x = Concatenate(axis=-1)([x, x0])
x = AddConv2D(x, 32, [3,3], 1, dr, "same", lrelu=use_lrelu, bn=bn)
# Add arm, gripper
y = pose_in
y = AddDense(y, 32, "relu", 0., output=True, constraint=3)
x = TileOnto(x, y, 32, (8,8), add=False)
x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)
# Add arm, gripper
y2 = AddDense(option_in, 64, "relu", 0., output=True, constraint=3)
x = TileOnto(x, y2, 64, (6,6), add=False)
x = AddConv2D(x, 128, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)
x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)
x = Flatten()(x)
x = AddDense(x, 512, "relu", dr, output=True, bn=bn)
x = AddDense(x, 512, "relu", dr, output=True, bn=bn) # Same setup as the state decoders
pose = AddDense(x, pose_size, "linear", 0., output=True)
actor = Model([x0in, xin, option_in, pose_in], [pose], name="actor")
return actor
示例12: GetHuskyPoseModel
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def GetHuskyPoseModel(x, num_options, pose_size,
dropout_rate=0.5, batchnorm=True):
'''
Make an "actor" network that takes in an encoded image and an "option"
label and produces the next command to execute.
'''
xin = Input([int(d) for d in x.shape[1:]], name="pose_h_in")
x0in = Input([int(d) for d in x.shape[1:]], name="pose_h0_in")
pose_in = Input((pose_size,), name="pose_pose_in")
option_in = Input((num_options,), name="pose_o_in")
x = xin
x0 = x0in
dr, bn = dropout_rate, False
use_lrelu = False
x = Concatenate(axis=-1)([x, x0])
x = AddConv2D(x, 32, [3,3], 1, dr, "same", lrelu=use_lrelu, bn=bn)
# Add arm, gripper
y = pose_in
y = AddDense(y, 32, "relu", 0., output=True, constraint=3)
x = TileOnto(x, y, 32, (8,8), add=False)
x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)
# Add arm, gripper
y2 = AddDense(option_in, 64, "relu", 0., output=True, constraint=3)
x = TileOnto(x, y2, 64, (6,6), add=False)
x = AddConv2D(x, 128, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)
x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)
x = Flatten()(x)
x = AddDense(x, 512, "relu", dr, output=True, bn=bn)
x = AddDense(x, 512, "relu", dr, output=True, bn=bn) # Same setup as the state decoders
pose = AddDense(x, pose_size, "linear", 0., output=True)
pose = Model([x0in, xin, option_in, pose_in], [pose], name="pose")
return pose
示例13: GetPoseModel
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def GetPoseModel(x, num_options, arm_size, gripper_size,
dropout_rate=0.5, batchnorm=True):
'''
Make an "actor" network that takes in an encoded image and an "option"
label and produces the next command to execute.
'''
img_shape = [int(d) for d in x.shape[1:]]
img_in = Input(img_shape,name="policy_img_in")
img0_in = Input(img_shape,name="policy_img0_in")
arm = Input((arm_size,), name="ee_in")
gripper = Input((gripper_size,), name="gripper_in")
option_in = Input((48,), name="actor_o_in")
ins = [img0_in, img_in, option_in, arm, gripper]
x0, x = img0_in, img_in
dr, bn = dropout_rate, False
use_lrelu = False
x = Concatenate(axis=-1)([x, x0])
x = AddConv2D(x, 32, [3,3], 1, dr, "same", lrelu=use_lrelu, bn=bn)
# Add arm, gripper
y = Concatenate()([arm, gripper])
y = AddDense(y, 32, "relu", 0., output=True, constraint=3)
x = TileOnto(x, y, 32, (8,8), add=False)
x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)
# Add arm, gripper
y2 = AddDense(option_in, 64, "relu", 0., output=True, constraint=3)
x = TileOnto(x, y2, 64, (6,6), add=False)
x = AddConv2D(x, 128, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)
x = AddConv2D(x, 64, [3,3], 1, dr, "valid", lrelu=use_lrelu, bn=bn)
x = Flatten()(x)
x = AddDense(x, 512, "relu", 0., output=True, bn=False)
x = AddDense(x, 512, "relu", 0., output=True, bn=False) # Same setup as the state decoders
arm = AddDense(x, arm_size, "linear", 0., output=True)
gripper = AddDense(x, gripper_size, "sigmoid", 0., output=True)
actor = Model(ins, [arm, gripper], name="pose")
return actor
示例14: res_block
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def res_block(input_tensor, filters, kernel_size=(3, 3), strides=(1, 1), use_dropout=False):
"""實例化Keras Resnet塊。
Arguments:
input_tensor {[type]} -- 輸入張量
filters {[type]} -- filters
Keyword Arguments:
kernel_size {tuple} -- [description] (default: {(3,3)})
strides {tuple} -- [description] (default: {(1,1)})
use_dropout {bool} -- [description] (default: {False})
"""
x = ReflectionPadding2D((1, 1))(input_tensor)
x = Conv2D(filters=filters, kernel_size=kernel_size, strides=strides)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
if use_dropout:
x=Dropout(0.5)(x)
x = ReflectionPadding2D((1, 1))(x)
x = Conv2D(filters=filters, kernel_size=kernel_size, strides=strides)(x)
x = BatchNormalization()(x)
merged = Add()([input_tensor, x])
return merged
示例15: resnet_block
# 需要導入模塊: from keras.layers import merge [as 別名]
# 或者: from keras.layers.merge import Add [as 別名]
def resnet_block(n_filter, kernel_size=(3,3), pool=(1,1), n_conv_per_block=2,
batch_norm=False, kernel_initializer='he_normal', activation='relu'):
n_conv_per_block >= 2 or _raise(ValueError('required: n_conv_per_block >= 2'))
len(pool) == len(kernel_size) or _raise(ValueError('kernel and pool sizes must match.'))
n_dim = len(kernel_size)
n_dim in (2,3) or _raise(ValueError('resnet_block only 2d or 3d.'))
conv_layer = Conv2D if n_dim == 2 else Conv3D
conv_kwargs = dict (
padding = 'same',
use_bias = not batch_norm,
kernel_initializer = kernel_initializer,
)
channel_axis = -1 if backend_channels_last() else 1
def f(inp):
x = conv_layer(n_filter, kernel_size, strides=pool, **conv_kwargs)(inp)
if batch_norm:
x = BatchNormalization(axis=channel_axis)(x)
x = Activation(activation)(x)
for _ in range(n_conv_per_block-2):
x = conv_layer(n_filter, kernel_size, **conv_kwargs)(x)
if batch_norm:
x = BatchNormalization(axis=channel_axis)(x)
x = Activation(activation)(x)
x = conv_layer(n_filter, kernel_size, **conv_kwargs)(x)
if batch_norm:
x = BatchNormalization(axis=channel_axis)(x)
if any(p!=1 for p in pool) or n_filter != K.int_shape(inp)[-1]:
inp = conv_layer(n_filter, (1,)*n_dim, strides=pool, **conv_kwargs)(inp)
x = Add()([inp, x])
x = Activation(activation)(x)
return x
return f