Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

How to perform 2D convolution with user defined kernel just like sobel filter? #9064

Closed
chowkamlee81 opened this issue Dec 14, 2017 · 3 comments
Labels

Comments

@chowkamlee81
Copy link

How to perform 2D convolution with user defined kernel just like sobel filter? Kindly provide example

@ZiyueHuang
Copy link
Member

You can fix the weight of convolution.

@thomelane
Copy link
Contributor

With Gluon API you can set_data on a parameter. So for a convolutional layer you can select the weight (which will be the kernel) and call set_data on this with the kernel of your choosing. You must make sure you have the correct shape, with the dimensions being (filter, channel, height, width). So if we wanted a single sobel kernel applied to a single channel input we could for the following:

import mxnet as mx

# kernel of your choosing (e.g. a sobel filter)
kernel = mx.nd.array(((1,0,-1),
                      (2,0,-2),
                      (1,0,-1)))
# add dimensions for filters, and input channel
# both of which will have shape 1, since single filter and single input channel
weight = kernel.expand_dims(0).expand_dims(0)

conv = mx.gluon.nn.Conv2D(channels=1, kernel_size=(3,3), padding=(1,1))
conv._in_channels = 1
conv.initialize()
conv.weight.set_data(weight)

@sojiadeshina
Copy link
Contributor

@marcoabreu can you close out this issue since the question has been answered with example by @thomelane

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants