thomelane commented on issue #9064: How to perform 2D convolution with user 
defined kernel just like sobel filter?
URL: 
https://github.com/apache/incubator-mxnet/issues/9064#issuecomment-404347843
 
 
   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)
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to