本文整理汇总了Python中keras.layers.Conv2D方法的典型用法代码示例。如果您正苦于以下问题:Python layers.Conv2D方法的具体用法?Python layers.Conv2D怎么用?Python layers.Conv2D使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers
的用法示例。
在下文中一共展示了layers.Conv2D方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_cae_model
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def build_cae_model(height=32, width=32, channel=3):
"""
build convolutional autoencoder model
"""
input_img = Input(shape=(height, width, channel))
# encoder
net = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
net = MaxPooling2D((2, 2), padding='same')(net)
net = Conv2D(8, (3, 3), activation='relu', padding='same')(net)
net = MaxPooling2D((2, 2), padding='same')(net)
net = Conv2D(4, (3, 3), activation='relu', padding='same')(net)
encoded = MaxPooling2D((2, 2), padding='same', name='enc')(net)
# decoder
net = Conv2D(4, (3, 3), activation='relu', padding='same')(encoded)
net = UpSampling2D((2, 2))(net)
net = Conv2D(8, (3, 3), activation='relu', padding='same')(net)
net = UpSampling2D((2, 2))(net)
net = Conv2D(16, (3, 3), activation='relu', padding='same')(net)
net = UpSampling2D((2, 2))(net)
decoded = Conv2D(channel, (3, 3), activation='sigmoid', padding='same')(net)
return Model(input_img, decoded)
示例2: d_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def d_block(inp, fil, p = True):
skip = Conv2D(fil, 1, padding = 'same', kernel_initializer = 'he_normal')(inp)
out = Conv2D(filters = fil, kernel_size = 3, padding = 'same', kernel_initializer = 'he_normal')(inp)
out = LeakyReLU(0.2)(out)
out = Conv2D(filters = fil, kernel_size = 3, padding = 'same', kernel_initializer = 'he_normal')(out)
out = LeakyReLU(0.2)(out)
out = Conv2D(fil, 1, padding = 'same', kernel_initializer = 'he_normal')(out)
out = add([out, skip])
out = LeakyReLU(0.2)(out)
if p:
out = AveragePooling2D()(out)
return out
示例3: g_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def g_block(inp, fil, u = True):
if u:
out = UpSampling2D(interpolation = 'bilinear')(inp)
else:
out = Activation('linear')(inp)
skip = Conv2D(fil, 1, padding = 'same', kernel_initializer = 'he_normal')(out)
out = Conv2D(filters = fil, kernel_size = 3, padding = 'same', kernel_initializer = 'he_normal')(out)
out = LeakyReLU(0.2)(out)
out = Conv2D(filters = fil, kernel_size = 3, padding = 'same', kernel_initializer = 'he_normal')(out)
out = LeakyReLU(0.2)(out)
out = Conv2D(fil, 1, padding = 'same', kernel_initializer = 'he_normal')(out)
out = add([out, skip])
out = LeakyReLU(0.2)(out)
return out
示例4: build_model
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def build_model(x_train, num_classes):
# Reset default graph. Keras leaves old ops in the graph,
# which are ignored for execution but clutter graph
# visualization in TensorBoard.
tf.reset_default_graph()
inputs = KL.Input(shape=x_train.shape[1:], name="input_image")
x = KL.Conv2D(32, (3, 3), activation='relu', padding="same",
name="conv1")(inputs)
x = KL.Conv2D(64, (3, 3), activation='relu', padding="same",
name="conv2")(x)
x = KL.MaxPooling2D(pool_size=(2, 2), name="pool1")(x)
x = KL.Flatten(name="flat1")(x)
x = KL.Dense(128, activation='relu', name="dense1")(x)
x = KL.Dense(num_classes, activation='softmax', name="dense2")(x)
return KM.Model(inputs, x, "digit_classifier_model")
# Load MNIST Data
示例5: conv2d
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def conv2d(h_num_filters, h_filter_width, h_stride, h_use_bias):
def compile_fn(di, dh):
layer = layers.Conv2D(dh['num_filters'], (dh['filter_width'],) * 2,
strides=(dh['stride'],) * 2,
use_bias=dh['use_bias'],
padding='SAME')
def fn(di):
return {'out': layer(di['in'])}
return fn
return siso_keras_module(
'Conv2D', compile_fn, {
'num_filters': h_num_filters,
'filter_width': h_filter_width,
'stride': h_stride,
'use_bias': h_use_bias,
})
示例6: InceptionLayer
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def InceptionLayer(self, a, b, c, d):
def func(x):
x1 = Conv2D(a, (1, 1), padding='same', activation='relu')(x)
x2 = Conv2D(b, (1, 1), padding='same', activation='relu')(x)
x2 = Conv2D(b, (3, 3), padding='same', activation='relu')(x2)
x3 = Conv2D(c, (1, 1), padding='same', activation='relu')(x)
x3 = Conv2D(c, (3, 3), dilation_rate = 2, strides = 1, padding='same', activation='relu')(x3)
x4 = Conv2D(d, (1, 1), padding='same', activation='relu')(x)
x4 = Conv2D(d, (3, 3), dilation_rate = 3, strides = 1, padding='same', activation='relu')(x4)
y = Concatenate(axis = -1)([x1, x2, x3, x4])
return y
return func
示例7: _conv2d_same
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def _conv2d_same(x, filters, prefix, stride=1, kernel_size=3, rate=1):
# 计算padding的数量,hw是否需要收缩
if stride == 1:
return Conv2D(filters,
(kernel_size, kernel_size),
strides=(stride, stride),
padding='same', use_bias=False,
dilation_rate=(rate, rate),
name=prefix)(x)
else:
kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1)
pad_total = kernel_size_effective - 1
pad_beg = pad_total // 2
pad_end = pad_total - pad_beg
x = ZeroPadding2D((pad_beg, pad_end))(x)
return Conv2D(filters,
(kernel_size, kernel_size),
strides=(stride, stride),
padding='valid', use_bias=False,
dilation_rate=(rate, rate),
name=prefix)(x)
示例8: DCGAN_discriminator
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def DCGAN_discriminator():
nb_filters = 64
nb_conv = int(np.floor(np.log(128) / np.log(2)))
list_filters = [nb_filters * min(8, (2 ** i)) for i in range(nb_conv)]
input_img = Input(shape=(128, 128, 3))
x = Conv2D(list_filters[0], (3, 3), strides=(2, 2), name="disc_conv2d_1", padding="same")(input_img)
x = BatchNormalization(axis=-1)(x)
x = LeakyReLU(0.2)(x)
# Next convs
for i, f in enumerate(list_filters[1:]):
name = "disc_conv2d_%s" % (i + 2)
x = Conv2D(f, (3, 3), strides=(2, 2), name=name, padding="same")(x)
x = BatchNormalization(axis=-1)(x)
x = LeakyReLU(0.2)(x)
x_flat = Flatten()(x)
x_out = Dense(1, activation="sigmoid", name="disc_dense")(x_flat)
discriminator_model = Model(inputs=input_img, outputs=[x_out])
return discriminator_model
示例9: _initial_conv_block_inception
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def _initial_conv_block_inception(input, initial_conv_filters, weight_decay=5e-4):
''' Adds an initial conv block, with batch norm and relu for the DPN
Args:
input: input tensor
initial_conv_filters: number of filters for initial conv block
weight_decay: weight decay factor
Returns: a keras tensor
'''
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
x = Conv2D(initial_conv_filters, (7, 7), padding='same', use_bias=False, kernel_initializer='he_normal',
kernel_regularizer=l2(weight_decay), strides=(2, 2))(input)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
return x
示例10: _bn_relu_conv_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def _bn_relu_conv_block(input, filters, kernel=(3, 3), stride=(1, 1), weight_decay=5e-4):
''' Adds a Batchnorm-Relu-Conv block for DPN
Args:
input: input tensor
filters: number of output filters
kernel: convolution kernel size
stride: stride of convolution
Returns: a keras tensor
'''
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
x = Conv2D(filters, kernel, padding='same', use_bias=False, kernel_initializer='he_normal',
kernel_regularizer=l2(weight_decay), strides=stride)(input)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
return x
示例11: _conv_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def _conv_block(inp, convs, skip=True):
x = inp
count = 0
len_convs = len(convs)
for conv in convs:
if count == (len_convs - 2) and skip:
skip_connection = x
count += 1
if conv['stride'] > 1: x = ZeroPadding2D(((1,0),(1,0)))(x) # peculiar padding as darknet prefer left and top
x = Conv2D(conv['filter'],
conv['kernel'],
strides=conv['stride'],
padding='valid' if conv['stride'] > 1 else 'same', # peculiar padding as darknet prefer left and top
name='conv_' + str(conv['layer_idx']),
use_bias=False if conv['bnorm'] else True)(x)
if conv['bnorm']: x = BatchNormalization(epsilon=0.001, name='bnorm_' + str(conv['layer_idx']))(x)
if conv['leaky']: x = LeakyReLU(alpha=0.1, name='leaky_' + str(conv['layer_idx']))(x)
return add([skip_connection, x]) if skip else x
#SPP block uses three pooling layers of sizes [5, 9, 13] with strides one and all outputs together with the input are concatenated to be fed
#to the FC block
示例12: conv_2d
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def conv_2d(filters, kernel_shape, strides, padding, input_shape=None):
"""
Defines the right convolutional layer according to the
version of Keras that is installed.
:param filters: (required integer) the dimensionality of the output
space (i.e. the number output of filters in the
convolution)
:param kernel_shape: (required tuple or list of 2 integers) specifies
the strides of the convolution along the width and
height.
:param padding: (required string) can be either 'valid' (no padding around
input or feature map) or 'same' (pad to ensure that the
output feature map size is identical to the layer input)
:param input_shape: (optional) give input shape if this is the first
layer of the model
:return: the Keras layer
"""
if LooseVersion(keras.__version__) >= LooseVersion('2.0.0'):
if input_shape is not None:
return Conv2D(filters=filters, kernel_size=kernel_shape,
strides=strides, padding=padding,
input_shape=input_shape)
else:
return Conv2D(filters=filters, kernel_size=kernel_shape,
strides=strides, padding=padding)
else:
if input_shape is not None:
return Convolution2D(filters, kernel_shape[0], kernel_shape[1],
subsample=strides, border_mode=padding,
input_shape=input_shape)
else:
return Convolution2D(filters, kernel_shape[0], kernel_shape[1],
subsample=strides, border_mode=padding)
示例13: ss_bt
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def ss_bt(self, x, dilation, strides=(1, 1), padding='same'):
x1, x2 = self.channel_split(x)
filters = (int(x.shape[-1]) // self.groups)
x1 = layers.Conv2D(filters, kernel_size=(3, 1), strides=strides, padding=padding)(x1)
x1 = layers.Activation('relu')(x1)
x1 = layers.Conv2D(filters, kernel_size=(1, 3), strides=strides, padding=padding)(x1)
x1 = layers.BatchNormalization()(x1)
x1 = layers.Activation('relu')(x1)
x1 = layers.Conv2D(filters, kernel_size=(3, 1), strides=strides, padding=padding, dilation_rate=(dilation, 1))(
x1)
x1 = layers.Activation('relu')(x1)
x1 = layers.Conv2D(filters, kernel_size=(1, 3), strides=strides, padding=padding, dilation_rate=(1, dilation))(
x1)
x1 = layers.BatchNormalization()(x1)
x1 = layers.Activation('relu')(x1)
x2 = layers.Conv2D(filters, kernel_size=(1, 3), strides=strides, padding=padding)(x2)
x2 = layers.Activation('relu')(x2)
x2 = layers.Conv2D(filters, kernel_size=(3, 1), strides=strides, padding=padding)(x2)
x2 = layers.BatchNormalization()(x2)
x2 = layers.Activation('relu')(x2)
x2 = layers.Conv2D(filters, kernel_size=(1, 3), strides=strides, padding=padding, dilation_rate=(1, dilation))(
x2)
x2 = layers.Activation('relu')(x2)
x2 = layers.Conv2D(filters, kernel_size=(3, 1), strides=strides, padding=padding, dilation_rate=(dilation, 1))(
x2)
x2 = layers.BatchNormalization()(x2)
x2 = layers.Activation('relu')(x2)
x_concat = layers.concatenate([x1, x2], axis=-1)
x_add = layers.add([x, x_concat])
output = self.channel_shuffle(x_add)
return output
开发者ID:JACKYLUO1991,项目名称:Face-skin-hair-segmentaiton-and-skin-color-evaluation,代码行数:34,代码来源:lednet.py
示例14: down_sample
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def down_sample(self, x, filters):
x_filters = int(x.shape[-1])
x_conv = layers.Conv2D(filters - x_filters, kernel_size=3, strides=(2, 2), padding='same')(x)
x_pool = layers.MaxPool2D()(x)
x = layers.concatenate([x_conv, x_pool], axis=-1)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
return x
开发者ID:JACKYLUO1991,项目名称:Face-skin-hair-segmentaiton-and-skin-color-evaluation,代码行数:10,代码来源:lednet.py
示例15: apn_module
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Conv2D [as 别名]
def apn_module(self, x):
def right(x):
x = layers.AveragePooling2D()(x)
x = layers.Conv2D(self.classes, kernel_size=1, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.UpSampling2D(interpolation='bilinear')(x)
return x
def conv(x, filters, kernel_size, stride):
x = layers.Conv2D(filters, kernel_size=kernel_size, strides=(stride, stride), padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
return x
x_7 = conv(x, int(x.shape[-1]), 7, stride=2)
x_5 = conv(x_7, int(x.shape[-1]), 5, stride=2)
x_3 = conv(x_5, int(x.shape[-1]), 3, stride=2)
x_3_1 = conv(x_3, self.classes, 3, stride=1)
x_3_1_up = layers.UpSampling2D(interpolation='bilinear')(x_3_1)
x_5_1 = conv(x_5, self.classes, 5, stride=1)
x_3_5 = layers.add([x_5_1, x_3_1_up])
x_3_5_up = layers.UpSampling2D(interpolation='bilinear')(x_3_5)
x_7_1 = conv(x_7, self.classes, 3, stride=1)
x_3_5_7 = layers.add([x_7_1, x_3_5_up])
x_3_5_7_up = layers.UpSampling2D(interpolation='bilinear')(x_3_5_7)
x_middle = conv(x, self.classes, 1, stride=1)
x_middle = layers.multiply([x_3_5_7_up, x_middle])
x_right = right(x)
x_middle = layers.add([x_middle, x_right])
return x_middle
开发者ID:JACKYLUO1991,项目名称:Face-skin-hair-segmentaiton-and-skin-color-evaluation,代码行数:37,代码来源:lednet.py