python - Keras kernel initializer with gabor filter -


i trying create custom initializer gabor kernels according keras documentation.

i writing example easy run , debug code.

import random cv2.cv2 import cv_64f import keras.backend k import numpy np keras.layers import conv2d sklearn.model_selection import train_test_split sklearn.utils import shuffle keras.utils import np_utils keras.layers.convolutional import maxpooling2d keras.layers.core import dense, dropout, activation, flatten keras.models import sequential import cv2 theano import shared   images_size = 100 img_rows = 200 img_cols = 200 nb_channel = 3   def custom_gabor(shape, dtype=none):     total_ker = []     in xrange(shape[3]):         kernels = []         j in xrange(shape[2]):             kernels.append(             cv2.getgaborkernel(ksize=(shape[0], shape[1]), sigma=1,              theta=1, lambd=0.5, gamma=0.3, psi=(3.14) * 0.5,              ktype=cv_64f))         total_ker.append(kernels)     np_tot = shared(np.array(total_ker))     return k.variable(np_tot, dtype=dtype)   def build_model():     model = sequential()     # layer 1     model.add(conv2d(32, (3, 3), kernel_initializer=custom_gabor,                                       input_shape=(nb_channel, img_rows, img_cols)))     model.add(activation('relu'))     # layer 2     model.add(maxpooling2d(pool_size=(2, 2)))     model.add(conv2d(32, (3, 3), kernel_initializer=custom_gabor))     model.add(activation('relu'))     model.add(maxpooling2d(pool_size=(2, 2)))      # layer 3     model.add(conv2d(32, (3, 3), kernel_initializer=custom_gabor))     model.add(activation('relu'))     model.add(maxpooling2d(pool_size=(2, 2)))      model.add(dropout(0.25))     model.add(flatten())     model.add(dense(64))     model.add(activation('relu'))     model.add(dropout(0.5))      model.add(dense(2))     model.add(activation('softmax'))      model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])     return model   def make_dummy_data_set():     img_matrix = []     in xrange(images_size):         img_matrix.append([random.random() _ in xrange(img_rows*img_cols*nb_channel)])      img_matrix = np.array(img_matrix)     label = np.array([random.randint(0, 1) _ in xrange(images_size)])      data, label = shuffle(img_matrix, label, random_state=7)  # random_state=2     x_train, x_test, y_train, y_test = train_test_split(data, label, test_size=0.2, random_state=7)      # reshape data     x_train = x_train.reshape(x_train.shape[0], nb_channel, img_rows, img_cols)     x_test = x_test.reshape(x_test.shape[0], nb_channel, img_rows, img_cols)      x_train = x_train.astype('float32')     x_test = x_test.astype('float32')      # convert class vectore binary class matrices     y_train = np_utils.to_categorical(y_train, 2)     y_test = np_utils.to_categorical(y_test, 2)      return x_train, x_test, y_train, y_test   def train_model(model, x_train, x_test, y_train, y_test):     model.fit(x_train,               y_train,               batch_size=32,               epochs=5,               verbose=1,               validation_data=(x_test, y_test))   if __name__ == "__main__":     model = build_model()     x_train, x_test, y_train, y_test = make_dummy_data_set()     train_model(model, x_train, x_test, y_train, y_test) 

when run error "input dimension mis-match":

using theano backend. train on 80 samples, validate on 20 samples epoch 1/5 traceback (most recent call last): file "/home/naor/desktop/workspace/reflux_analyze/core/tests/test.py",  line 104, in <module> train_model(model, x_train, x_test, y_train, y_test) file "/home/naor/desktop/workspace/reflux_analyze/core/tests/test.py", line 98, in train_model validation_data=(x_test, y_test)) file "/home/naor/desktop/workspace/reflux_analyze/local/lib/python2.7/site-packages/keras/models.py", line 867, in fit initial_epoch=initial_epoch) file "/home/naor/desktop/workspace/reflux_analyze/local/lib/python2.7/site-packages/keras/engine/training.py", line 1598, in fit validation_steps=validation_steps) file "/home/naor/desktop/workspace/reflux_analyze/local/lib/python2.7/site-packages/keras/engine/training.py", line 1183, in _fit_loop outs = f(ins_batch) file "/home/naor/desktop/workspace/reflux_analyze/local/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 1222, in __call__ return self.function(*inputs) file "/home/naor/desktop/workspace/reflux_analyze/local/lib/python2.7/site-packages/theano/compile/function_module.py", line 898, in __call__ storage_map=getattr(self.fn, 'storage_map', none)) file "/home/naor/desktop/workspace/reflux_analyze/local/lib/python2.7/site-packages/theano/gof/link.py", line 325, in raise_with_op reraise(exc_type, exc_value, exc_trace) file "/home/naor/desktop/workspace/reflux_analyze/local/lib/python2.7/site-packages/theano/compile/function_module.py", line 884, in __call__ self.fn() if output_subset none else\ valueerror: input dimension mis-match. (input[0].shape[1] = 3, input[1].shape[1] = 32) apply node caused error: elemwise{add}[(0, 0)](corrmm{valid,  (1, 1), (1, 1)}.0, inplacedimshuffle{x,0,x,x}.0) toposort index: 73 inputs types: [tensortype(float32, 4d), tensortype(float32, (true,  false, true, true))] inputs shapes: [(32, 3, 169, 198), (1, 32, 1, 1)] inputs strides: [(401544, 133848, 792, 4), (128, 4, 4, 4)] inputs values: ['not shown', 'not shown'] outputs clients: [[elemwise{composite{(i0 * (i1 + abs(i1)))}} (tensorconstant{(1, 1, 1, 1) of 0.5}, elemwise{add}[(0, 0)].0),  elemwise{composite{((i0 * i1) + (i0 * i1 * sgn(i2)))}}[(0, 1)] (tensorconstant{(1, 1, 1, 1) of 0.5}, maxpoolgrad{ignore_border=true,  mode='max', ndim=2}.0, elemwise{add}[(0, 0)].0)]]  hint: re-running theano optimization disabled give back-trace of when node created. can done setting theano flag 'optimizer=fast_compile'. if not work, theano optimizations can disabled 'optimizer=none'. hint: use theano flag 'exception_verbosity=high' debugprint , storage map footprint of apply node.  process finished exit code 1 

i know kernel change cnn learning phase want start thows gabor kernel (only real part) , data pictures , not random... :)

thanks.

from input shapes in error message, can identify (32,3,169,198) input images.

  • they 32 images (batch_size), 3 channels, , sides 169,198.

then assume other shape (1,32,1,1) filter shape.

the output shape of filters must 32. ok, there 32 there, i'm not sure it's in right place. (as print of shape asked in other comment, can update order of these dimensions -- now, i'm running important code, can't change settings)

the other dimensions wrong, though. filters should shaped as:

  • 32, relating 32 cells (output channels) in convolutional layer
  • 3 (nb_channel), relating input channels coming layer
  • 3, relating kernel size 1
  • 3, relating kernel size 2

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -