tf.keras.layers.Conv2D()

本文详细介绍了Keras中的Conv2D层,该层用于2D卷积操作,如图像处理。讨论了参数如滤波器数量、卷积核大小、步幅、填充、数据格式、膨胀率等对输出的影响,并给出了多个示例来演示不同设置下的输出形状变化。此外,还提到了激活函数、偏差、初始化器、正则化和约束等关键概念。
摘要由CSDN通过智能技术生成

官方文档如下:

2D convolution layer (e.g. spatial convolution over images).
This layer creates a convolution kernel that is convolved with the layer input to produce a tensor of outputs. If use_bias is True, a bias vector is created and added to the outputs. Finally, if activation is not None, it is applied to the outputs as well.
When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the sample axis), e.g. input_shape=(128, 128, 3) for 128x128 RGB pictures in data_format=“channels_last”.
Examples:

The inputs are 28x28 RGB images with channels_last and the batch

size is 4.

input_shape = (4, 28, 28, 3)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv2D(
… 2, 3, activation=‘relu’, input_shape=input_shape[1:])(x)

print(y.shape)
(4, 26, 26, 2)

With dilation_rate as 2.

input_shape = (4, 28, 28, 3)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv2D(
… 2, 3, activation=‘relu’, dilation_rate=2, input_shape=input_shape[1:])(x)

print(y.shape)
(4, 24, 24, 2)

With padding as “same”.

input_shape = (4, 28, 28, 3)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv2D(
… 2, 3, activation=‘relu’, padding=“same”, input_shape=input_shape[1:])(x)

print(y.shape)
(4, 28, 28, 2)

With extended batch shape [4, 7]:

input_shape = (4, 7, 28, 28, 3)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv2D(
… 2, 3, activation=‘relu’, input_shape=input_shape[2:])(x)

print(y.shape)
(4, 7, 26, 26, 2)
Arguments: filters: Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). kernel_size: An integer or tuple/list of 2 integers, specifying the height and width of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. strides: An integer or tuple/list of 2 integers, specifying the strides of the convolution along the height and width. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value != 1 is incompatible with specifying any dilation_rate value != 1. padding: one of “valid” or “same” (case-insensitive). “valid” means no padding. “same” results in padding evenly to the left/right or up/down of the input such that output has the same height/width dimension as the input. data_format: A string, one of channels_last (default) or channels_first. The ordering of the dimensions in the inputs. channels_last corresponds to inputs with shape (batch_size, height, width, channels) while channels_first corresponds to inputs with shape (batch_size, channels, height, width). It defaults to the image_data_format value found in your Keras config file at ~/.keras/keras.json. If you never set it, then it will be channels_last. dilation_rate: an integer or tuple/list of 2 integers, specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. Currently, specifying any dilation_rate value != 1 is incompatible with specifying any stride value != 1. groups: A positive integer specifying the number of groups in which the input is split along the channel axis. Each group is convolved separately with filters / groups filters. The output is the concatenation of all the groups results along the channel axis. Input channels and filters must both be divisible by groups. activation: Activation function to use. If you don’t specify anything, no activation is applied (see keras.activations). use_bias: Boolean, whether the layer uses a bias vector. kernel_initializer: Initializer for the kernel weights matrix (see keras.initializers). bias_initializer: Initializer for the bias vector (see keras.initializers). kernel_regularizer: Regularizer function applied to the kernel weights matrix (see keras.regularizers). bias_regularizer: Regularizer function applied to the bias vector (see keras.regularizers). activity_regularizer: Regularizer function applied to the output of the layer (its “activation”) (see keras.regularizers). kernel_constraint: Constraint function applied to the kernel matrix (see keras.constraints). bias_constraint: Constraint function applied to the bias vector (see keras.constraints). Input shape: 4+D tensor with shape: batch_shape + (channels, rows, cols) if data_format=‘channels_first’ or 4+D tensor with shape: batch_shape + (rows, cols, channels) if data_format=‘channels_last’. Output shape: 4+D tensor with shape: batch_shape + (filters, new_rows, new_cols) if data_format=‘channels_first’ or 4+D tensor with shape: batch_shape + (new_rows, new_cols, filters) if data_format=‘channels_last’. rows and cols values might have changed due to padding.
Returns: A tensor of rank 4+ representing activation(conv2d(inputs, kernel) + bias).
Raises: ValueError: if padding is “causal”. ValueError: when both strides > 1 and dilation_rate > 1.

翻译如下:

这一层创建了一个卷积核,它与层输入进行卷积以产生输出张量。如果use_bias为真,则创建一个偏差向量并添加到输出中。最后,如果激活不是None,它也会应用到输出。
当使用这一层作为模型的第一层时,提供关键字参数input_shape(整数元组,不包括样本轴),例如,对于data_format="channels_last"中的128x128 RGB图片,输入input_shape=(128, 128, 3)。
参数:filters: Integer,输出空间的维数(即卷积中输出滤波器的数量)。kernel_size:一个整数或2个整数的元组/列表,指定2D卷积窗口的高度和宽度。可以是单个整数,为所有空间维度指定相同的值。步幅:一个整数或2个整数的元组/列表,指定沿着高度和宽度卷积的步幅。可以是单个整数,为所有空间维度指定相同的值。指定任何stride值!= 1与指定任何dilation_rate值!= 1是不兼容的。填充:“有效”或“相同”(不区分大小写)之一。“有效”意味着没有填充。“same"的结果是在输入的左/右或上/下均匀填充,这样输出的高度/宽度与输入的尺寸相同。data_format:一个字符串,channels_last(默认)或channels_first之一。输入中维度的顺序。channels_last对应于形状的输入(batch_size,高度,宽度,通道),而channels_first对应于形状的输入(batch_size,通道,高度,宽度)。默认值为在Keras配置文件~/. Keras / Keras .json中找到的image_data_format值。如果您从未设置它,那么它将是channels_last。dilation_rate:一个整数或2个整数的元组/列表,指定用于膨胀卷积的膨胀率。可以是单个整数,为所有空间维度指定相同的值。目前,指定任何dilation_rate值!= 1与指定任何stride值!= 1是不兼容的。组:一个正整数,指定沿着通道轴分割输入的组的数量。每个组分别与过滤器/组过滤器进行卷积。输出是所有组结果沿通道轴的连接。输入通道和过滤器都必须被组整除。activation:要使用的激活功能。如果不指定任何内容,则不会应用任何激活(参见keras.activation)。use_bias:布尔值,层是否使用偏差向量。kernel_initializer:内核权重矩阵的初始化器(参见keras. Initializer)。bias_initializer:偏置向量的初始化器(参见keras. Initializer)。kernel_regularizer:应用于内核权值矩阵的正则化函数(参见keras. Regularizer)。bias_regularizer:应用于偏置向量的正则化函数(参见keras. Regularizer)。activity_regularizer:应用于层输出的正则化函数(它的“激活”)(参见keras. Regularizer)。kernel_constraint:应用于内核矩阵的约束函数(参见keras.constraints)。bias_constraint:应用于偏差向量的约束函数(参见keras.constraints)。如果data_format=‘channels_first’,则输入4+D张量:batch_shape + (rows, cols),如果data_format=‘channels_last’,则输入4+D张量。输出形状:4+D张量:batch_shape + (filters, new_rows, new_cols)如果data_format=‘channels_first’,或者4+D张量:batch_shape + (new_rows, new_cols, filters)如果data_format=‘channels_last’。行和cols值可能由于填充而改变。
返回:一个等级为4+的张量,表示激活(conv2d(输入,内核)+偏置)。
引发:ValueError:如果填充是"因果的”。ValueError:当两个步数>1和膨胀速率>1.

逐句对照翻译:

This layer creates a convolution kernel that is convolved with the layer input to produce a tensor of outputs.
这一层创建了一个卷积核,它与层输入进行卷积以产生输出张量。
If use_bias is True, a bias vector is created and added to the outputs.
如果use_bias为真,则创建一个偏差向量并添加到输出中。
Finally, if activation is not None, it is applied to the outputs as well.
最后,如果激活不是None,它也会应用到输出。
When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the sample axis), e.g. input_shape=(128, 128, 3) for 128x128 RGB pictures in data_format=“channels_last”.
当使用这一层作为模型的第一层时,提供关键字参数input_shape(整数元组,不包括样本轴),例如,对于data_format="channels_last"中的128x128 RGB图片,输入input_shape=(128, 128, 3)。
Arguments: filters: Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution).
参数:filters: Integer,输出空间的维数(即卷积中输出滤波器的数量)。
kernel_size: An integer or tuple/list of 2 integers, specifying the height and width of the 2D convolution window.
kernel_size:一个整数或2个整数的元组/列表,指定2D卷积窗口的高度和宽度。
Can be a single integer to specify the same value for all spatial dimensions.
可以是单个整数,为所有空间维度指定相同的值。
strides: An integer or tuple/list of 2 integers, specifying the strides of the convolution along the height and width.
步幅:一个整数或2个整数的元组/列表,指定沿着高度和宽度卷积的步幅。
Can be a single integer to specify the same value for all spatial dimensions.
可以是单个整数,为所有空间维度指定相同的值。
Specifying any stride value != 1 is incompatible with specifying any dilation_rate value != 1.
指定任何stride值!= 1与指定任何dilation_rate值!= 1是不兼容的。
padding: one of “valid” or “same” (case-insensitive).
填充:“有效”或“相同”(不区分大小写)之一。
“valid” means no padding.
“有效”意味着没有填充。
“same” results in padding evenly to the left/right or up/down of the input such that output has the same height/width dimension as the input.
“same"的结果是在输入的左/右或上/下均匀填充,这样输出的高度/宽度与输入的尺寸相同。
data_format: A string, one of channels_last (default) or channels_first.
data_format:一个字符串,channels_last(默认)或channels_first之一。
The ordering of the dimensions in the inputs.
输入中维度的顺序。
channels_last corresponds to inputs with shape (batch_size, height, width, channels) while channels_first corresponds to inputs with shape (batch_size, channels, height, width).
channels_last对应于形状的输入(batch_size,高度,宽度,通道),而channels_first对应于形状的输入(batch_size,通道,高度,宽度)。
It defaults to the image_data_format value found in your Keras config file at ~/.keras/keras.json.
默认值为在Keras配置文件~/. Keras / Keras .json中找到的image_data_format值。
If you never set it, then it will be channels_last.
如果您从未设置它,那么它将是channels_last。
dilation_rate: an integer or tuple/list of 2 integers, specifying the dilation rate to use for dilated convolution.
dilation_rate:一个整数或2个整数的元组/列表,指定用于膨胀卷积的膨胀率。
Can be a single integer to specify the same value for all spatial dimensions.
可以是单个整数,为所有空间维度指定相同的值。
Currently, specifying any dilation_rate value != 1 is incompatible with specifying any stride value != 1.
目前,指定任何dilation_rate值!= 1与指定任何stride值!= 1是不兼容的。
groups: A positive integer specifying the number of groups in which the input is split along the channel axis.
组:一个正整数,指定沿着通道轴分割输入的组的数量。
Each group is convolved separately with filters / groups filters.
每个组分别与过滤器/组过滤器进行卷积。
The output is the concatenation of all the groups results along the channel axis.
输出是所有组结果沿通道轴的连接。
Input channels and filters must both be divisible by groups.
输入通道和过滤器都必须被组整除。
activation: Activation function to use.
activation:要使用的激活功能。
If you don’t specify anything, no activation is applied (see keras.activations).
如果不指定任何内容,则不会应用任何激活(参见keras.activation)。
use_bias: Boolean, whether the layer uses a bias vector.
use_bias:布尔值,层是否使用偏差向量。
kernel_initializer: Initializer for the kernel weights matrix (see keras.initializers).
kernel_initializer:内核权重矩阵的初始化器(参见keras. Initializer)。
bias_initializer: Initializer for the bias vector (see keras.initializers).
bias_initializer:偏置向量的初始化器(参见keras. Initializer)。
kernel_regularizer: Regularizer function applied to the kernel weights matrix (see keras.regularizers).
kernel_regularizer:应用于内核权值矩阵的正则化函数(参见keras. Regularizer)。
bias_regularizer: Regularizer function applied to the bias vector (see keras.regularizers).
bias_regularizer:应用于偏置向量的正则化函数(参见keras. Regularizer)。
activity_regularizer: Regularizer function applied to the output of the layer (its “activation”) (see keras.regularizers).
activity_regularizer:应用于层输出的正则化函数(它的“激活”)(参见keras. Regularizer)。
kernel_constraint: Constraint function applied to the kernel matrix (see keras.constraints).
kernel_constraint:应用于内核矩阵的约束函数(参见keras.constraints)。
bias_constraint: Constraint function applied to the bias vector (see keras.constraints).
bias_constraint:应用于偏差向量的约束函数(参见keras.constraints)。
Input shape: 4+D tensor with shape: batch_shape + (channels, rows, cols) if data_format=‘channels_first’ or 4+D tensor with shape: batch_shape + (rows, cols, channels) if data_format=‘channels_last’.
如果data_format=‘channels_first’,则输入4+D张量:batch_shape + (rows, cols),如果data_format=‘channels_last’,则输入4+D张量。
Output shape: 4+D tensor with shape: batch_shape + (filters, new_rows, new_cols) if data_format=‘channels_first’ or 4+D tensor with shape: batch_shape + (new_rows, new_cols, filters) if data_format=‘channels_last’.
输出形状:4+D张量:batch_shape + (filters, new_rows, new_cols)如果data_format=‘channels_first’,或者4+D张量:batch_shape + (new_rows, new_cols, filters)如果data_format=‘channels_last’。
rows and cols values might have changed due to padding.
行和cols值可能由于填充而改变。
Returns: A tensor of rank 4+ representing activation(conv2d(inputs, kernel) + bias).
返回:一个等级为4+的张量,表示激活(conv2d(输入,内核)+偏置)。
Raises: ValueError: if padding is “causal”.
引发:ValueError:如果填充是"因果的”。
ValueError: when both strides >
ValueError:当两个步数>
1 and dilation_rate >
1和膨胀速率>
1.
1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值