# Gabor fitler with cifar10 data

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))])

batch_size= 10, shuffle=True, **kwargs)

batch_size= 10, shuffle=True, **kwargs)

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 + 1)/(2b - 1))
sigma_x2 = (lambdaa/pi)(sqrt(np.log(2)/2))((2b + 1)/(2b - 1))
sigma_x3 = (lambdaa/pi)(sqrt(np.log(2)/2))((2b + 1)/(2b - 1))
sigma_x4 = (lambdaa/pi)(sqrt(np.log(2)/2))((2b + 1)/(2b - 1))
sigma_x5 = (lambdaa/pi)(sqrt(np.log(2)/2))((2b + 1)/(2b - 1))
sigma_x6 = (lambdaa/pi)(sqrt(np.log(2)/2))((2b + 1)/(2b - 1))
sigma_y1 = sigma_x1/gamma
sigma_y2 = sigma_x2/gamma
sigma_y3 = sigma_x3/gamma
sigma_y4 = sigma_x1/gamma
sigma_y5 = sigma_x2/gamma
sigma_y6 = sigma_x3/gamma

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()
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)