Bih*_*ihy 3 image-processing conv-neural-network keras gabor-filter pytorch
我正在尝试构建一个带有一些转换层的 CNN,其中层中的一半滤波器是固定的,另一半在训练模型时是可学习的。但我没有找到任何相关内容。
我想做的与他们在本文中所做的类似https://arxiv.org/pdf/1705.04748.pdf
有没有办法在 Keras、Pytorch 中做到这一点......
当然。在 PyTorch 中你可以使用nn.Conv2d
和
weight
手动将其参数设置为您想要的过滤器一个简单的例子是:
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()
Run Code Online (Sandbox Code Playgroud)