Gabor filter
TensorFlow
image processing
neural networks
custom filters

using gabor filter in tensorflow , or any other filter instead of default one

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want to use a Gabor filter in TensorFlow, you are usually doing one of two things: applying a fixed image-processing kernel before a model, or initializing a convolution layer with a custom kernel. TensorFlow does not have a built-in “default filter” that you must accept. You can create your own kernel tensors and apply them directly with convolution operations.

Understand Where Custom Filters Fit

In TensorFlow, a filter is just a kernel tensor used by an operation such as tf.nn.conv2d. That means you can:

  • build a fixed Gabor kernel and convolve with it
  • build several Gabor kernels as a filter bank
  • initialize a trainable convolution with those kernels

The right choice depends on whether you want deterministic preprocessing or a learnable model layer.

Build a Gabor Kernel in Python

Here is a simple NumPy-based Gabor kernel generator that can be converted into a TensorFlow tensor.

python
1import numpy as np
2import tensorflow as tf
3
4def gabor_kernel(size=11, sigma=2.0, theta=0.0, lambd=4.0, gamma=0.5, psi=0.0):
5    half = size // 2
6    y, x = np.mgrid[-half:half + 1, -half:half + 1]
7
8    x_theta = x * np.cos(theta) + y * np.sin(theta)
9    y_theta = -x * np.sin(theta) + y * np.cos(theta)
10
11    gaussian = np.exp(-(x_theta**2 + (gamma**2) * y_theta**2) / (2 * sigma**2))
12    sinusoid = np.cos(2 * np.pi * x_theta / lambd + psi)
13
14    kernel = gaussian * sinusoid
15    kernel = kernel.astype(np.float32)
16    kernel /= np.sum(np.abs(kernel)) + 1e-8
17    return kernel
18
19kernel = gabor_kernel(theta=np.pi / 4)
20print(kernel.shape)

This produces one 2D kernel. TensorFlow convolution expects filter dimensions with channel structure added.

Apply the Kernel with tf.nn.conv2d

For a grayscale image, reshape the kernel to (height, width, in_channels, out_channels).

python
1import tensorflow as tf
2import numpy as np
3
4image = np.random.rand(1, 64, 64, 1).astype(np.float32)
5kernel_2d = gabor_kernel(size=11, theta=0.0)
6kernel_4d = kernel_2d[:, :, np.newaxis, np.newaxis]
7
8image_tf = tf.constant(image)
9kernel_tf = tf.constant(kernel_4d)
10
11filtered = tf.nn.conv2d(image_tf, kernel_tf, strides=1, padding="SAME")
12print(filtered.shape)

This is the direct TensorFlow way to use a fixed custom filter.

Create a Filter Bank with Multiple Orientations

Gabor filters are often used in banks rather than singly. For example, you may want several orientations.

python
1thetas = [0.0, np.pi / 4, np.pi / 2, 3 * np.pi / 4]
2kernels = [gabor_kernel(size=11, theta=t) for t in thetas]
3kernel_bank = np.stack(kernels, axis=-1)          # (11, 11, 4)
4kernel_bank = kernel_bank[:, :, np.newaxis, :]    # (11, 11, 1, 4)
5
6filtered_bank = tf.nn.conv2d(
7    tf.constant(image),
8    tf.constant(kernel_bank.astype(np.float32)),
9    strides=1,
10    padding="SAME",
11)
12
13print(filtered_bank.shape)  # one feature map per filter

This is useful for texture analysis, edge orientation detection, or handcrafted feature extraction.

Initialize a Keras Convolution Layer with Custom Filters

If you want the model to start from Gabor-like filters but still learn, initialize a Conv2D layer with those weights.

python
1import tensorflow as tf
2import numpy as np
3
4bank = kernel_bank.astype(np.float32)
5
6layer = tf.keras.layers.Conv2D(
7    filters=4,
8    kernel_size=(11, 11),
9    padding="same",
10    use_bias=False,
11    trainable=True,
12)
13
14dummy = tf.zeros((1, 64, 64, 1))
15layer(dummy)  # build the layer
16layer.set_weights([bank])

Now the network starts from your handcrafted filters instead of random initialization.

Fixed Filter vs Trainable Layer

Use a fixed Gabor layer when:

  • you want deterministic preprocessing
  • you are reproducing a classical vision pipeline
  • interpretability matters more than end-to-end learning

Use trainable initialization when:

  • you want a helpful starting point
  • the downstream task benefits from adaptation
  • you still want gradient-based optimization to refine the filters

That distinction should be decided early because it changes how the model learns.

Handle Color Images Correctly

For RGB images, you can either:

  • convert to grayscale first
  • replicate the same kernel across input channels
  • build different filters per channel

The simplest route is grayscale preprocessing if the task is texture-driven rather than color-driven.

Common Pitfalls

One common mistake is building a correct 2D kernel but forgetting TensorFlow’s 4D filter shape requirements for convolution.

Another issue is assuming a custom filter automatically becomes trainable. A constant kernel passed to tf.nn.conv2d is fixed unless you explicitly make it a variable or layer weight.

A third mistake is comparing fixed Gabor preprocessing to trainable CNN layers without keeping the preprocessing pipeline consistent.

Summary

  • TensorFlow can use Gabor filters directly through tf.nn.conv2d.
  • A custom filter is just a kernel tensor with the correct shape.
  • You can apply fixed Gabor preprocessing or initialize trainable convolution layers with Gabor kernels.
  • Filter banks with multiple orientations are often more useful than a single filter.
  • Decide early whether your custom filters are meant to stay fixed or be learned further.

Course illustration
Course illustration

All Rights Reserved.