Gabor fitler with cifar10 data

Zidane: <p>Hi,<br>
I implemented gabor filter for cifar10 data using this code</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torchvision<br>
from torchvision import datasets, transforms<br>
import tensorflow as tf<br>
import tensorflow_io as tfio<br>
import numpy as np<br>
import math<br>
from math import pi<br>
from math import sqrt<br>
import matplotlib.pyplot as plt</p>
<h1>Read config to figure out whether to use CUDA.</h1>
<p>device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)</p>
<h1>CUDA-related parameters. (Got this from pytorch’s official tutorials.)</h1>
<p>kwargs = {‘num_workers’: 1, ‘pin_memory’: True} if torch.cuda.is_available()else {}</p>
<h1>Load Cifar10 dataset using standard torchvision package.</h1>
<p>data_transform = transforms.Compose([transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])</p>
<p>cifar_trainset = torch.utils.data.DataLoader(datasets.CIFAR10(root=’./data’,<br>
train=True, download=True,transform=data_transform),<br>
batch_size= 10, shuffle=True, **kwargs)</p>
<p>cifar_valset = torch.utils.data.DataLoader(datasets.CIFAR10(root=’./data’,<br>
train=True, download=True,transform=data_transform),<br>
batch_size= 10, shuffle=True, **kwargs)</p>
<p>cifar_testset = torch.utils.data.DataLoader( datasets.CIFAR10(root=’./data’,<br>
train=False, download=True, transform=data_transform),<br>
batch_size= 10, shuffle=False, **kwargs)</p>
<h1>to show the images before and after gabor filter</h1>
<p>def imshow(img):<br>
img = img / 2 + 0.5     # unnormalize<br>
npimg = img.numpy()<br>
npimg = plt.imshow(np.transpose(npimg, (1, 2, 0)))</p>
<p><span class="hashtag">#calculating</span> sigmas using the formula from the paper ‘Adding biological constraints<br>
<span class="hashtag">#to</span> CNNs makes image classification more human-like and robust’<br>
lambdaa = 5<br>
b = [1,1.4,1.8]<br>
gamma = [0.5,1]</p>
<p>sigma_x1 = (lambdaa/pi)<em>(sqrt(np.log(2)/2))</em>((2<strong>b[0] + 1)/(2</strong>b[0] - 1))<br>
sigma_x2 = (lambdaa/pi)<em>(sqrt(np.log(2)/2))</em>((2<strong>b[1] + 1)/(2</strong>b[1] - 1))<br>
sigma_x3 = (lambdaa/pi)<em>(sqrt(np.log(2)/2))</em>((2<strong>b[2] + 1)/(2</strong>b[2] - 1))<br>
sigma_x4 = (lambdaa/pi)<em>(sqrt(np.log(2)/2))</em>((2<strong>b[0] + 1)/(2</strong>b[0] - 1))<br>
sigma_x5 = (lambdaa/pi)<em>(sqrt(np.log(2)/2))</em>((2<strong>b[1] + 1)/(2</strong>b[1] - 1))<br>
sigma_x6 = (lambdaa/pi)<em>(sqrt(np.log(2)/2))</em>((2<strong>b[2] + 1)/(2</strong>b[2] - 1))<br>
sigma_y1 = sigma_x1/gamma[1]<br>
sigma_y2 = sigma_x2/gamma[1]<br>
sigma_y3 = sigma_x3/gamma[1]<br>
sigma_y4 = sigma_x1/gamma[0]<br>
sigma_y5 = sigma_x2/gamma[0]<br>
sigma_y6 = sigma_x3/gamma[0]</p>
<p>thetas = np.array([0, pi/8, pi/4, pi<em>3/8, pi/2, pi</em>5/8, pi<em>3/4, pi</em>7/8])<br>
offsets = np.array([0, pi/2, pi, pi*3/2])<br>
sigmas = np.array([[sigma_x1, sigma_y1], [sigma_x2, sigma_y2], [sigma_x3, sigma_y3],<br>
[sigma_x4, sigma_y4], [sigma_x5, sigma_y5], [sigma_x6, sigma_y6]])</p>
<p>for batch_idx, (x, t) in enumerate(cifar_trainset):<br>
current_batch_size = x.data.size()[0]<br>
x, t = x.to(device), t.to(device)</p>
<pre><code>#imshow(torchvision.utils.make_grid(x))
for theta in thetas:
    for offset in offsets:
        for sigma in sigmas:
             x1 = tfio.experimental.filter.gabor(x, freq = 0.2,
                     sigma = sigma,theta = theta, nstds=31, offset = offset)

                 
x1 = tf.add(x1, 1)
x1 = x1.numpy()       # converting tensorflow tensor to numpy
x1 = np.array(x1)    # done to becaue dtype = complex64 had to be converted
x1 = np.uint8(x1)
x2 = torch.from_numpy(x1)  # converting numpy to pytorch tensor
np.add(x1, 1, out=x1)

imshow(torchvision.utils.make_grid(x2))
</code></pre>
<p>But the images after being gabor filtered and stacked using ‘x1 = tfio.experimental.filter.gabor(x, freq = 0.2,sigma = sigma,theta = theta, nstds=31, offset = offset)’ look unclear. I think the problem is in the values I am giving to the gabor filter parameters. Does anyone tried implementing gabor filter for cifar10 data using any method and it worked out? Could you help?</p>