简体   繁体   English

将图层的一半滤镜设置为不可训练的keras / tensorflow

[英]Set half of the filters of a layer as not trainable keras/tensorflow

I'm trying to train a model suggested by this research paper where I set half of the filters of a convolution layer to Gabor filters and the rest are random weights which are initialized by default. 我正在尝试训练研究论文建议的模型,其中我将卷积层的一半滤波器设置为Gabor滤波器,其余的是默认初始化的随机权重。 Normally, if I have to set a layer as not trainable, I set the trainable attribute as False . 通常,如果我必须将图层设置为不可训练,我将trainable属性设置为False But here I have to freeze only half of the filters of a layer and I have no idea how to do so. 但是在这里我只需要冻结一层过滤器的一半,我不知道该怎么做。 Any help would be really appreciated. 任何帮助将非常感激。 I'm using Keras with Tensorflow backend. 我正在使用带有Tensorflow后端的Keras。

How about making two convolutional layers which are getting the same input and (nearly) the same parameters? 如何制作两个获得相同输入和(几乎)相同参数的卷积层? So one of them is trainable wir random weights at initialization and the other layer is non trainable with the gabor filters. 因此,其中一个是在初始化时可训练的随机权重,而另一个层是用gabor过滤器无法训练的。

You could then merge the outputs of the two layers together in a way that it looks like it's the output from one convolutional network. 然后,您可以将两个层的输出合并在一起,使其看起来像是来自一个卷积网络的输出。

Here is an example for demonstration (you need to use Keras functional API ): 以下是演示示例(您需要使用Keras功能API ):

n_filters = 32

my_input = Input(shape=...)
conv_freezed = Conv2D(n_filters/2, (3,3), ...)
conv_trainable = Conv2D(n_filters/2, (3,3), ...)

conv_freezed_out = conv_freezed(my_input)
conv_trainable_out = conv_trainable(my_input)
conv_out = concatenate([conv_freezed_out, conv_trainable_out])

# set weights and freeze the layer
conv_freezed.set_weights(...)
conv_freezed.trainable = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM