Coder Social home page Coder Social logo

ikintosh / gabornet Goto Github PK

View Code? Open in Web Editor NEW
103.0 5.0 26.0 86.83 MB

Home Page: https://ikintosh.github.io/GaborNet/

License: MIT License

Python 100.00%
gabor gabor-filters pytorch neural-network convolutional-neural-networks image-processing

gabornet's Introduction

Hi there! I'm Andrey ๐Ÿ‘‹


  • ๐Ÿ”ญ Iโ€™m currently working on Advertisment platform at bol.com
  • ๐ŸŒฑ And trying to find time for open source contributions
  • ๐Ÿ‘ฏ Iโ€™m looking to collaborate on developing ETNA package
  • ๐Ÿ’ฌ Ask me about time series and open source development

You may find my publication and conference talks:


๐Ÿ“ซ How to reach me:

Linkedin Account Twitter Account Gmail Account

gabornet's People

Contributors

deepsource-autofix[bot] avatar deepsourcebot avatar ikintosh avatar morrme avatar restyled-commits avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gabornet's Issues

Make weights fixed in eval mode

Is there any way to make GaborLayer not to calculate weights every time in evaluate or no_grads mode?

I guess I should change something in forward method, but I have no idea what to do.

GaborNet/GaborLayers.py

Lines 71 to 93 in 435e031

def forward(self, input):
y, x = torch.meshgrid([torch.linspace(-self.x0+1, self.x0, self.kernel_size[0]), torch.linspace(-self.y0+1, self.y0, self.kernel_size[1])])
x = x.to(device)
y = y.to(device)
weight = torch.empty(self.weight.shape, requires_grad=False).to(device)
for i in range(self.out_channels):
for j in range(self.in_channels):
sigma = self.sigma[i, j].expand_as(y)
freq = self.freq[i, j].expand_as(y)
theta = self.theta[i, j].expand_as(y)
psi = self.psi[i, j].expand_as(y)
rotx = x * torch.cos(theta) + y * torch.sin(theta)
roty = -x * torch.sin(theta) + y * torch.cos(theta)
g = torch.zeros(y.shape)
g = torch.exp(-0.5 * ((rotx ** 2 + roty ** 2) / (sigma + 1e-3) ** 2))
g = g * torch.cos(freq * rotx + psi)
g = g / (2*3.14*sigma**2)
weight[i, j] = g
self.weight.data[i, j] = g
return F.conv2d(input, weight, self.bias, self.stride, self.padding, self.dilation, self.groups)

Deal with sanity_check code

sanity_check requires heavy computation (probably with GPU) so it cannot be part of test suite. However it is nice to have sanity_check case to be able to test layer performance.

Add simple test case to test perfomance

When refactor, the performance (in this case, ability to learn proper image representation) should be tested. So it's necessary to find simple dataset and add test case to train simple CNN on it and monitor accuracy metric.

Vectorize kernel creation

Remove loops

def calculate_weights(self):
for i in range(self.conv_layer.out_channels):
for j in range(self.conv_layer.in_channels):
sigma = self.sigma[i, j].expand_as(self.y)
freq = self.freq[i, j].expand_as(self.y)
theta = self.theta[i, j].expand_as(self.y)
psi = self.psi[i, j].expand_as(self.y)
rotx = self.x * torch.cos(theta) + self.y * torch.sin(theta)
roty = -self.x * torch.sin(theta) + self.y * torch.cos(theta)
g = torch.exp(
-0.5 * ((rotx ** 2 + roty ** 2) / (sigma + self.delta) ** 2)
)
g = g * torch.cos(freq * rotx + psi)
g = g / (2 * math.pi * sigma ** 2)
self.conv_layer.weight.data[i, j] = g

Remove private class inheritance

This should be removed from implementation:

from torch.nn.modules.conv import _ConvNd
from torch.nn.modules.utils import _pair

class GaborConv2d(_ConvNd):
...

Difference between layers

Hello,
Could you please tell me what is the difference between "GaborSmallConv2d", "GaborConv2d" and "GaborCurvedConv2d"? from the math point of view.

Minor modifications

Hello,
Thank you @iKintosh for providing your code.
I'm currently trying to use your layer in my code (with torch 1.1.0) and I ran into some minor problems

I think the following lines should be updated in your code but I'm not sure so I decided to just point them out here :

1- Line 19 -> padding_mode should be added

super(GaborSmallConv2d, self).__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, False, _pair(0), groups, bias, padding_mode='zeros')

2- Line 24 -> .type(torch.Tensor) should be added to make learnable parameter floating

self.theta = nn.Parameter(0.628*torch.randint(0, 6, (out_channels, in_channels)).type(torch.Tensor))

3- Line 62 -> same as #1

super(GaborConv2d, self).__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, False, _pair(0), groups, bias, padding_mode='zeros')

4- Line 72 -> second parameter for meshgrid should be self.x0-1 not self.x0 (??)

y, x = torch.meshgrid([torch.linspace(-self.x0+1, self.x0, self.kernel_size[0]), torch.linspace(-self.y0+1, self.y0, self.kernel_size[1])])

5- Line 105 -> same as #1

super(GaborCurvedConv2d, self).__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, False, _pair(0), groups, bias, padding_mode='zeros')

Questions about calculate_weights()

Hi Andrey, I have a couple of questions about calculate_weights().

  1. in line 106-109, why do you need expand_as(self.y)?
  2. in line 118, why do you need to divide g by (2 * math.pi * sigma ** 2)? I cannot find this in the original expression of Gabor filters.
    Thank you!

Remove numbers

Should have proper names:

  • 3.14 / 2
  • 1.41
  • 8
  • 0.5
  • 1e-3
  • 3.14

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.