Keras Conv2d 自己的过滤器 [英] Keras Conv2d own filters

查看:25
本文介绍了Keras Conv2d 自己的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用自己的过滤器而不是 Conv2D 中的过滤器数量设置参数过滤器数组

it is possible to set as param filter array with own filters instead of number of filters in Conv2D

filters = [[[1,0,0],[1,0,0],[1,0,0]],
     [[1,0,0],[0,1,0],[0,0,1]],
     [[0,1,0],[0,1,0],[0,1,0]],
     [[0,0,1],[0,0,1],[0,0,1]]]
model = Sequential()
model.add(Conv2D(filters, (3, 3), activation='relu', input_shape=(3, 1024, 1024), data_format='channels_first'))

推荐答案

您必须牢记 Conv2D 网络的目的是训练这些过滤器值.我的意思是,在使用形态滤波器的传统图像处理任务中,我们应该设计滤波器内核,然后在整个图像(卷积)中迭代它们.

You must have in mind that the purpose of a Conv2D network is to train these filters values. I mean, in a traditional image processing task using morphological filters we are supposed to design the filter kernels and then iterate them through the whole image (convolution).

在深度学习方法中,我们试图完成相同的任务.但是在这里我们假设我们不知道应该使用哪些过滤器,尽管我们确切地知道我们在寻找什么(标记的图像).当我们训练卷积神经网络时,我们会向它展示我们想要什么,并要求它找出自己的权重,即过滤器值.

In a deep learning approach we are trying to do the same task. But here instead we assume we don't know which filters should be used, although we know exactly what we are looking for (the labeled images). When we are training a convolutional neural network we are showing to it what we want and asking it to find out its own weights, i.e. the filters values.

因此,在这种情况下,我们应该只定义我们想要训练的过滤器数量(在您的情况下为 4 个过滤器)以及它们将如何初始化.它们的权重将在训练网络时设置.

So, in this context, we should just define how many filters we want to train (in your case, 4 filters) and how they will be initialized. Their weights will be set when training the network.

有很多方法可以初始化您的过滤器权重(例如,将它们全部设置为零或一;或使用随机函数来保证它们会捕捉到不同的图像特征).Keras Conv2D 函数默认使用 'glorot uniform' 算法,如 中所述https://keras.io/layers/convolutional/#conv2d.

There are many ways to initialize your filters weights (e.g. setting them all to zero or one; or using a random function to guarantee that distinct image characteristics would be catched by them). The Keras Conv2D function uses as default the 'glorot uniform' algorithm, as specified in https://keras.io/layers/convolutional/#conv2d.

如果你真的想按照你展示的方式初始化你的过滤器权重,你可以编写你自己的函数(看看https://keras.io/initializers/) 并通过 kernel_initializer 参数传递:

If you really want to initialize your filters weights in the way you have showed, you can write your own function (take a look at https://keras.io/initializers/) and pass it via kernel_initializer parameter:

model.add(Conv2D(number_of_filters, (3, 3), activation='relu', input_shape=(3, 1024, 1024), kernel_initializer=your_function, data_format='channels_first'))

这篇关于Keras Conv2d 自己的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆