Search code examples
conv-neural-networkchainer

Calculating number and size of filters for given input size


I have an autoencoder to regenerate input images. The image shape is (10, 1308608). 1308608 is 4*644*508.

class AutoEncoder(chainer.Chain):
    def __init__(self, input_size, n_filters, n_units, filter_size, 
activation):
        self.activation = activation#{'relu': F.relu, 'sigmoid': 
F.sigmoid}[activation]
        self.n_filters = n_filters
        self.n_units = n_units
        self.filter_size = filter_size
        self.dim1 = input_size - filter_size + 1

        super(AutoEncoder, self).__init__(
            conv1 = L.Convolution2D(1, n_filters, filter_size),
            lenc1 = L.Linear(n_filters*self.dim1*self.dim1, n_units),
            ldec1 = L.Linear(n_units, n_filters*self.dim1*self.dim1),
            deconv1 = L.Deconvolution2D(n_filters, 1, filter_size)
        )

    def __call__(self, x):
        h1 = self.activation(self.conv1(x))
        h2 = F.dropout(self.activation(self.lenc1(h1)))
        h3 = F.reshape(self.activation(self.ldec1(h2)), (x.data.shape[0], 
self.n_filters, self.dim1, self.dim1))
        h4 = self.activation(self.deconv1(h3))
        return h4

class Regression(chainer.Chain):
    def __init__(self, predictor):
        super(Regression, self).__init__(predictor=predictor)

    def __call__(self, x, t):
        y = self.predictor(x)
        self.loss = F.mean_squared_error(y, t)
        report({'loss': self.loss}, self)
        return self.loss

    def dump(self, x):
        return self.predictor(x, False)

For mnist of size (1,28,28) they have used input_size=28, n_filters = 10, n_units = 20, filter_size = 9. I want to understand how to calculate n_filters, n_units, filter_size according to the input_size.


Solution

  • You can refer Chainer official document for convolution_2d.

    Output height can be calculated as

    hO = (hI+2 * hP − hK) / sY+1
    

    where

    • hO: output height
    • hI: input height
    • hP: padding size
    • hK: kernel size
    • sY: stride size

    same applies for width.