keras.layers.Conv2D() 函数参数

Conv2D (二维卷积层)

这一层创建了一个卷积核,它与这一层的输入卷积以产生输出张量。

[En]

This layer creates a convolution kernel, which convolution with the input of this layer to produce an output tensor.

当使用此层作为模型的第一层时,提供关键字参数 input_shape (整数元组,不包括样本轴,不需要写batch_size)

def __init__(self, filters,
             kernel_size,
             strides=(1, 1),
             padding='valid',
             data_format=None,
             dilation_rate=(1, 1),
             activation=None,
             use_bias=True,
             kernel_initializer='glorot_uniform',
             bias_initializer='zeros',
             kernel_regularizer=None,
             bias_regularizer=None,
             activity_regularizer=None,
             kernel_constraint=None,
             bias_constraint=None,
             **kwargs):

filters

int 类型,表示卷积核个数,filters 影响的是最后输入结果的的第四个维度的变化

import tensorflow as tf
from tensorflow.keras.layers import Conv2D

input_shape = (4, 600, 600, 3)
input = tf.random.normal(input_shape)
x = keras.layers.Conv2D(64, (1, 1), strides=(1, 1), name='conv1')(input)
print(x.shape)

OUTPUT:
(4, 600, 600, 64)

kernel_size

指示卷积核的大小。如果是方阵,可以直接写成数字,这会影响输出结果中间两个数据的维度。

[En]

Indicates the size of the convolution kernel. If it is a square matrix, it can be written as a number directly, which affects the dimensions of the two data in the middle of the output result.

x = Conv2D(64, (2, 2), strides=(1, 1), name='conv1')(input)

print(x.shape)

OUTPUT:
(4, 599, 599, 64)

strides

tuple (int, int) 步长,同样会影响输出的中间两个维度,值得注意的是,括号里的数据可以不一致,分别控制横坐标和纵坐标

x = Conv2D(64, 1, strides=(2, 2), name='conv1')(input)
print(x.shape)

OUTPUT:
(4, 300, 300, 64)

padding

是否对周围进行填充, same 即使通过 kernel_size 缩小了维度,但是四周会填充 0,保持原先的维度; valid 表示存储不为 0 的有效信息

a = Conv2D(64, 1, strides=(2, 2), padding="same" , name='conv1')(input)
b = Conv2D(64, 3, strides=(2, 2), padding="same" , name='conv1')(input)
c = Conv2D(64, 3, strides=(1, 1), padding="same" , name='conv1')(input)
d = Conv2D(64, 3, strides=(1, 1), padding="valid", name='conv1')(input)
print(a.shape, b.shape, c.shape, d.shape)

OUTPUT:
(4, 300, 300, 64)
(4, 300, 300, 64)
(4, 600, 600, 64)
(4, 598, 598, 64)

activation

激活函数,如果 activation 不是 None,则它会应用于输出

use_bias

boolean,表示是否使用偏置量,如果 use_bias 为真,则创建一个偏置项并添加到输出中

data_format

用于规定 input_shape 的格式

如果不填写,默认是 channels_last,否则可以填写 channels_first。前者的会把 input_shape 这个三元组给识别成 (batch_size, height, width, channels),后者则会识别成 (batch_size, channels, height, width) 不过样本轴 (batch_size) 不需要自己填写

dilation_rate

int, tuple(int, int), list[int, int],指定用于扩展卷积的扩展率。可以是单个整数,为所有空间维度指定相同的值。该参数定义了卷积核处理数据时各值的间距。

返回一个四维的张量

第一个数是 batch 的大小,也就是有几组数据;后三个数表示一个张量的大小

Original: https://blog.csdn.net/SP_FA/article/details/119181599
Author: SP FA
Title: keras.layers.Conv2D() 函数参数

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/508891/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球