Я хочу использовать фильтры Габора в качестве ядра в CNN, но не могу найти решение. Что-то я нахожу, но не работает.
import tensorflow as tf
import cv2
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, BatchNormalization
from tensorflow.keras.layers import Activation, Flatten, Dropout, Conv2DTranspose, LeakyReLU, Concatenate, Lambda
from tensorflow.keras import backend as K
def get_gabor_tensor(ksize, sigmas, thetas, lambdas, gammas, psis):
n_kernels = len(sigmas) * len(thetas) * len(lambdas) * len(gammas) * len(psis)
gabors = []
for sigma in sigmas:
for theta in thetas:
for lambd in lambdas:
for gamma in gammas:
for psi in psis:
params = {'ksize': ksize, 'sigma': sigma,
'theta': theta, 'lambd': lambd,
'gamma': gamma, 'psi': psi}
gf = cv2.getGaborKernel(**params, ktype=cv2.CV_32F)
gf = K.expand_dims(gf, -1)
gabors.append(gf)
assert len(gabors) == n_kernels
print(f"Created {n_kernels} kernels.")
return K.stack(gabors, axis=-1)
def convolve_tensor(x, kernel_tensor=None):
return K.conv2d(x, kernel_tensor, padding='same')
def gabor_layer(layer, n_filters=16, kernel_size=3):
ksize=(3, 3)
sigmas = [1, 2, 3, 4]
thetas = np.linspace(0, np.pi, 4, endpoint=False)
lambdas=[8, 16, 32, 64]
psis = np.linspace(0, 2*np.pi, 2, endpoint=False)
gammas = np.linspace(1, 0, 2, endpoint=False)
tensor = get_gabor_tensor(ksize, sigmas, thetas, lambdas, gammas, psis)
x = Lambda(convolve_tensor, arguments={'kernel_tensor': tensor})(layer)
c1 = Conv2D(filters=16, kernel_size=(3, 3), padding='same')(layer)
p1 = MaxPooling2D((2, 2))(c1)
output = Dropout(0.1)(p1)
return output
И ошибка
ValueError: Depth of output (256) is not a multiple of the number of groups (3) for 'lambda/convolution' (op: 'Conv2D') with input shapes: [?,96,96,3], [3,3,1,256].
Решение от https://github.com/bdevans/GaborNet/blob/master/gabornet.py