gpt4 book ai didi

image-processing - 固定 Gabor 滤波器卷积神经网络

转载 作者:行者123 更新时间:2023-12-02 20:02:13 27 4
gpt4 key购买 nike

我正在尝试构建一个带有一些转换层的 CNN,其中层中一半的过滤器是固定的,另一半在训练模型时是可学习的。但我没有找到任何相关信息。

我正在尝试做的与他们在这篇论文中所做的相似https://arxiv.org/pdf/1705.04748.pdf

有没有办法在 Keras、Pytorch...

最佳答案

当然。在 PyTorch 中,您可以使用 nn.Conv2d

  1. 手动将其权重参数设置为您想要的过滤器
  2. 从学习中排除这些权重

一个简单的例子是:

import torch
import torch.nn as nn

class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()

self.conv_learning = nn.Conv2d(1, 5, 3, bias=False)
self.conv_gabor = nn.Conv2d(1, 5, 3, bias=False)
# weights HAVE TO be wrapped in `nn.Parameter` even if they are not learning
self.conv_gabor.weight = nn.Parameter(torch.randn(1, 5, 3, 3))

def forward(self, x):
y = self.conv_learning(x)
y = torch.sigmoid(y)
y = self.conv_gabor(y)

return y.mean()

model = Model()
xs = torch.randn(10, 1, 30, 30)
ys = torch.randn(10)
loss_fn = nn.MSELoss()

# we can exclude parameters from being learned here, by filtering them
# out based on some criterion. For instance if all your fixed filters have
# "gabor" in name, the following will do
learning_parameters = (param for name, param in model.named_parameters()
if 'gabor' not in name)
optim = torch.optim.SGD(learning_parameters, lr=0.1)

epochs = 10
for e in range(epochs):
y = model(xs)
loss = loss_fn(y, ys)

model.zero_grad()
loss.backward()
optim.step()

关于image-processing - 固定 Gabor 滤波器卷积神经网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592324/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com