Keras Conv2d own filters

前端 未结 2 1578
自闭症患者
自闭症患者 2021-01-14 02:38

it is possible to set as param filter array with own filters instead of number of filters in Conv2D

filters = [[[1,0,0],[1,0,0],[1,0,0]],
     [[1,0,0],[0,1,         


        
相关标签:
2条回答
  • 2021-01-14 03:14

    You must have in mind that the purpose of a Conv2D network is to train these filters values. I mean, in a traditional image processing task using morphological filters we are supposed to design the filter kernels and then iterate them through the whole image (convolution).

    In a deep learning approach we are trying to do the same task. But here instead we assume we don't know which filters should be used, although we know exactly what we are looking for (the labeled images). When we are training a convolutional neural network we are showing to it what we want and asking it to find out its own weights, i.e. the filters values.

    So, in this context, we should just define how many filters we want to train (in your case, 4 filters) and how they will be initialized. Their weights will be set when training the network.

    There are many ways to initialize your filters weights (e.g. setting them all to zero or one; or using a random function to guarantee that distinct image characteristics would be catched by them). The Keras Conv2D function uses as default the 'glorot uniform' algorithm, as specified in https://keras.io/layers/convolutional/#conv2d.

    If you really want to initialize your filters weights in the way you have showed, you can write your own function (take a look at https://keras.io/initializers/) and pass it via kernel_initializer parameter:

    model.add(Conv2D(number_of_filters, (3, 3), activation='relu', input_shape=(3, 1024, 1024), kernel_initializer=your_function, data_format='channels_first'))
    
    0 讨论(0)
  • 2021-01-14 03:20

    The accepted answer is right but it would certainly be more useful with a complete example, similar to the one provided in this excellent tensorflow example showing what Conv2d does.

    For keras, this is,

    from keras.models import Sequential
    from keras.layers import Conv2D
    
    import numpy as np
    
    # Keras version of this example:
    # https://stackoverflow.com/questions/34619177/what-does-tf-nn-conv2d-do-in-tensorflow
    # Requires a custom kernel initialise to set to value from example
    # kernel = [[1,0,1],[2,1,0],[0,0,1]]
    # image = [[4,3,1,0],[2,1,0,1],[1,2,4,1],[3,1,0,2]]
    # output = [[14, 6],[6,12]] 
    
    #Set Image
    image = [[4,3,1,0],[2,1,0,1],[1,2,4,1],[3,1,0,2]]
    
    # Pad to "channels_last" format 
    # which is [batch, width, height, channels]=[1,4,4,1]
    image = np.expand_dims(np.expand_dims(np.array(image),2),0)
    
    #Initialise to set kernel to required value
    def kernel_init(shape):
        kernel = np.zeros(shape)
        kernel[:,:,0,0] = np.array([[1,0,1],[2,1,0],[0,0,1]])
        return kernel
    
    #Build Keras model
    model = Sequential()
    model.add(Conv2D(1, [3,3], kernel_initializer=kernel_init, 
                     input_shape=(4,4,1), padding="valid"))
    model.build()
    
    # To apply existing filter, we use predict with no training
    out = model.predict(image)
    print(out[0,:,:,0])
    

    which outputs

    [[14, 6]
     [6, 12]]
    

    as expected.

    0 讨论(0)
提交回复
热议问题