Custom convolution layer can be implemented either by inheritance from the class nn.Conv2d, or through the funciton unfold. Unfortunately, unfold do not support dilation.
You may want to check these two topics:

  
    
    
    How to impelement a customized convolutional layer (conv2d) 
  
  
    Hi, 
I want to implement a customized Conv2d in which some multiplications during the convolution operation are dropped by some probability. This would happen during testing, and preferably different multiplications would drop for different kernels in a layer. Is there an easy way to do this? Do I need to implement the Conv2d function using pytorch functions from scratch? 
Thanks!
  



  
    
    
    General unfold function with dilation 
  
  
    PyTorch provide the powerful function unfold, through both torch.nn.functional.unfold and the builtin function for tensor torch.Tensor.unfold. It seems the latter is easier to use, and it is more general as it is not restricted to 4D tensor. However, the former implementation support dilation, while the latter does not. 
Does anyone has method to augment the builtin function implementation to make it support dilation?
  


Custom pooling can be implemented like this:

  
      gist.github.com
  
  
    https://gist.github.com/rwightman/f2d3849281624be7c0f11c85c87c1598 440
median_pool.py
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.utils import _pair, _quadruple


class MedianPool2d(nn.Module):
    """ Median pool (usable as median filter when stride=1) module.
    
This file has been truncated. show original