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

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?