def decaf_convolution(input_size, dtype, num_kernels, ksize, stride, mode, iternum): bottom = base.Blob((1, ) + input_size, dtype=dtype) layer = convolution.ConvolutionLayer(name='conv', num_kernels=num_kernels, ksize=ksize, stride=stride, mode=mode) top = base.Blob() # run a forward pass first to initialize everything. layer.forward([bottom], [top]) top.init_diff() top.diff().flat = 1. print '*****' print 'input shape:', bottom.data().shape[1:] start = time.time() for i in range(iternum): layer.forward([bottom], [top]) print 'forward runtime:', (time.time() - start) / iternum print 'output shape:', top.data().shape[1:] start = time.time() for i in range(iternum): layer.backward([bottom], [top], True) print 'backward runtime:', (time.time() - start) / iternum print '*****'
def __init__(self, **kwargs): """Initializes the convolution layer. Strictly, this is a correlation layer since the kernels are not reversed spatially as in a classical convolution operation. kwargs: name: the name of the layer. group: the number of groups that should be carried out for the block group convolution. Note that the number of channels of the incoming block should be divisible by the number of groups, otherwise we will have an error produced. num_kernels: the number of kernels PER GROUP. As a result, the output would have (num_kernels * group) channels. Also the layer should be provided all the appropriate parameters for the underlying convolutional layer. """ base.Layer.__init__(self, **kwargs) self._group = self.spec['group'] self._conv_args = dict(self.spec) self._conv_args['name'] = self.spec['name'] + '_sub' del self._conv_args['group'] self._bottom_sub = [base.Blob() for _ in range(self._group)] self._top_sub = [base.Blob() for _ in range(self._group)] self._conv_layers = None self._blocksize = 0 self._num_kernels = self.spec['num_kernels'] # create the convolution layers self._conv_layers = [ convolution.ConvolutionLayer(**self._conv_args) for i in range(self._group)] self._param = sum((layer.param() for layer in self._conv_layers), []) return
""" from decaf import base from decaf.util import smalldata from decaf.layers import convolution, fillers import numpy as np from skimage import io """The main demo code.""" img = np.asarray(smalldata.lena()) img = img.reshape((1, ) + img.shape).astype(np.float64) # wrap the img in a blob input_blob = base.Blob() input_blob.mirror(img) # create a convolutional layer layer = convolution.ConvolutionLayer( name='convolution', num_kernels=1, ksize=15, stride=1, mode='same', filler=fillers.ConstantFiller(value=1. / 15 / 15 / 3)) # run the layer output_blob = base.Blob() layer.forward([input_blob], [output_blob]) out = np.multiply(output_blob.data()[0, :, :, 0], 256).astype(np.uint8) io.imsave('out.png', out) print('Convolution result written to out.png')