Fast Image Scanning with Deep Max-Pooling Convolutional Neural Networks

Alessandro Giusti, Dan C. Cireşan, Jonathan Masci, Luca M. Gambardella, Jürgen Schmidhuber

Introduction

Deep Max-Pooling Convolutional Neural Networks are Deep Neural Networks (DNN) with convolutional and max-pooling layers. Convolutional Neural Networks (CNN) can be traced back to the Neocognitron in 1980. They were first successfully applied to relatively small tasks such as digit recognition , image interpretation and object recognition . Back then their size was greatly limited by the low computational power of available hardware. Since 2010, however, DNN have greatly profited from Graphics Processing Units (GPU). Simple GPU-based multilayer perceptrons (MLP) establised new state of the art results on the MNIST handwritten digit dataset when made both deep and large (augmenting the training set by artificial samples helped to avoid overfitting). 2011 saw the first implementation of GPU-based DNN on the CUDA parallel computing platform . This yielded new benchmark records on multiple object detection tasks. The field of Deep Learning with Neural Networks exploded. Multi-Column DNN improved previous results by over 30% on many benchmarks including: handwritten digits (MNIST) and Latin letters (NIST SD 19) ; Chinese characters (CASIA) ; traffic signs (GTSRB) ; natural images (CIFAR 10) . Another flavor of DNN greatly improved the accuracy on a subset of ImageNet . Recently, Google parallelized a large DNN on a cluster with over 10000 CPU cores .

For image classification, the DNN returns a vector of class posterior probabilities when provided with an input patch whose fixed width and height usually does not exceed a few hundreds of pixels and depends on the network architecture. But DNN also excel at image segmentation and object detection . For segmentation, image data within a square patch of odd size is used to determine the class of its central pixel. The network is trained on patches extracted from a set of images with ground truth segmentations (i.e. the class of each pixel is known). To segment an unseen image, the trained net is used to classify all of its pixels. Object detection within an image is trivially cast as a segmentation problem: pixels close to the centroid of each object are classified differently from background pixels. Once an unseen image is segmented, the centroid of each detected object is determined using simple image processing techniques.

Solving segmentation and detection tasks requires to apply the network to every patch contained in the image, which is prohibitively expensive when implemented in the naive, straightforward way. Consider a net with a convolutional layer immediately above the input layer: when evaluating the first patch contained in the input image, the patch is convolved with a large number of kernels to compute the output maps; when evaluating the next (typically overlapping) patch, such convolutions are re-evaluated – a huge amount of redundant computation. It is better to compute each convolution only once for the whole input image: the resulting set of images (which we will refer to as extended maps) contain the maps for each patch contained in the input image.

In the particular case of a CNN without max-pooling layers, this optimization is trivially implemented by computing all convolutions in the first layer on the entire input image, then computing all convolutions in subsequent layers on the resulting extended maps. This approach yielded real time detection performance when combined with dedicated FPGA or even ASIC integrated circuits.

However, present DNN owe much of their power to max-pooling layers interleaved with convolutional layers. Max-pooling cannot be handled using the straightforward approach outlined above. For example, when we perform a 2×22\times 2 max-pooling operation on an extended map, we obtain a smaller extended map which does not contain information from all the patches contained in the input image; instead, only patches whose upper left corner lies at even coordinates of the original image are represented. Any subsequent max-pooling layer would further aggravate the problem.

Our contribution consists in an optimized forward-propagation approach which avoids such problems by fragmenting the extended maps resulting from each max-pooling layer, such that each fragment contains information independent of other fragments, and the union of all fragments contains information about all the patches in the input image. A similar approach was previously used for handling a single subsampling layer in a simple CNN for face detection. Our mechanism, however, is completely general. It handles arbitrary architectures mixing convolutional and max-pooling layers in any order, and ensures that no redundant computation is performed at any stage.

Method

We consider nets composed by four types of layers : input, convolutional, max-pooling and fully-connected. In the following, different layers are denoted by index ll. Nets are formed by an input layer (l=0l=0), followed by a set of convolutional and max-pooling layers l∈{1,…,L}l\in\{1,\ldots,L\}, followed by a number of fully-connected layers. The optimization described in this paper concerns convolutional and max-pooling layers, and allows to find the outputs of layer LL. We do not discuss forward-propagation in fully-connected layers, where a trivial approach does not suffer from redundant computations.

To simplify notation, we consider nets with square maps and square kernels, but this is trivially generalized to rectangular maps and filters. Sets are denoted by bold symbols.

We first recall how convolutional and max-pooling layers are forward-propagated at the patch level (Section 2.1). Then we extend the approach by the proposed optimization, which operates at the level of the whole image (Section 2.2). Figure 1 illustrates both approaches.

The square input patch is represented as a set P0\mathbf{P}_{0} containing one or more input maps (depending on the number of input channels). Let w0w_{0} denote the width and height of such maps (i.e., the size of the input patch). Because the input image and all kernels are assumed square, maps obtained as the output of any intermediate layer ll (i.e., contained in Pl\mathbf{P}_{l}) will be square.

Let ll denote the index of a convolutional layer. The layer’s output is a set Pl\mathbf{P}_{l} of square maps with size wlw_{l}. Pl\mathbf{P}_{l} is obtained as a function of Pl−1\mathbf{P}_{l-1} . wl=wl−1−(k−1)w_{l}=w_{l-1}-(k-1), where kk is the width of the (square) kernels of layer ll. In general, the number of maps may change after a convolutional layer, i.e.: ∣Pl∣≠∣Pl−1∣\left|\mathbf{P}_{l}\right|\neq\left|\mathbf{P}_{l-1}\right|.

1.2 Max-pooling layers

Let ll denote the index of a max-pooling layer. The layer’s output is a set Pl\mathbf{P}_{l} of square maps with size wlw_{l}. Pl\mathbf{P}_{l} is obtained as a function of Pl−1\mathbf{P}_{l-1} . wl=wl−1/kw_{l}=w_{l-1}/k, where kk is the size of the square max-pooling kernel; the architecture of the net is such that mod(wl−1,k)=0\text{mod}(w_{l-1},k)=0. The number of maps is unchanged after a max-pooling layer, i.e. ∣Pl∣=∣Pl−1∣\left|\mathbf{P}_{l}\right|=\left|\mathbf{P}_{l-1}\right|.

2 Optimized testing on images

Let us now consider a square input image of size s≥w0s\geq w_{0}. We want to compute the network outputs on all windows completely contained within it – i.e. (s−w0+1)2(s-w_{0}+1)^{2} windows (patches).

We represent the output data of a given layer ll of the net as a set containing FlF_{l} fragments. We denote each fragment in layer ll by an index f∈{1,…,Fl}f\in\{1,\ldots,F_{l}\}. Each fragment f∈{1,…,Fl}f\in\{1,\ldots,F_{l}\} is associated to a set Ilf\mathbf{I}_{l}^{f} of extended maps.

Extended maps in the same fragment all have the same size; extended maps in different fragments may have different sizes, and not all such sizes may be square even though the input image is. Let sx,lf{s_{x,l}^{f}} and sy,lf{s_{y,l}^{f}} denote width and height of the extended maps in set Ilf\mathbf{I}_{l}^{f}, respectively.

The input image is provided as a single fragment, therefore F0=1F_{0}=1. Such fragment contains a set I01\mathbf{I}_{0}^{1} of square extended maps with sizes

Let ll denote the index of a convolutional layer. Its output consists of a set of fragments, such that Fl=Fl−1F_{l}=F_{l-1}.

A given fragment f∈{1,…,Fl}f\in\{1,\ldots,F_{l}\} contains a set Ilf\mathbf{I}_{l}^{f} of extended maps; each has size

where kk is the size of the (square) kernels of layer ll. Again, note that maps in different fragments may have different sizes, but all maps in the same fragment have the same size.

Ilf\mathbf{I}_{l}^{f} is obtained as a function of Il−1f\mathbf{I}_{l-1}^{f}, using the same operations as with patch-level forward propagation. However, convolutions are performed on the (large) extended maps rather than on small maps, like in the patch-level approach.

2.2 Max-pooling layers

Let ll denote the index of a max-pooling layer. Its output consists of a set of fragments, such that Fl=k2Fl−1F_{l}=k^{2}F_{l-1} fragments, where kk is the size of the square max-pooling kernel. In particular, each input fragment fin∈{1,…,Fl−1}f_{\text{in}}\in\{1,\ldots,F_{l-1}\} generates k2k^{2} output fragments.

Consider a given input fragment finf_{\text{in}}, associated with the set Il−1fin\mathbf{I}_{l-1}^{f_{\text{in}}} containing the input extended maps. Let o\mathbf{o} be a set of k2k^{2} 2D offsets defined as the Cartesian product {0,1,…,k−1}×{0,1,…,k−1}\{0,1,\ldots,k-1\}\times\{0,1,\ldots,k-1\}. E.g., for k=2k=2:

For a given input fragment finf_{\text{in}} and for each offset o∈oo\in\mathbf{o}, o=(ox,oy)o=(o_{x},o_{y}), one output fragment ff is produced. Each of the extended maps in ff is generated by applying the max-pooling operation to the corresponding extended map in finf_{\text{in}}, by starting at the top left offset (x,y)=o(x,y)=o. Specifically, the pixel at coordinates (xˉ,yˉ)(\bar{x},\bar{y}) in the output map is computed as the maximum of all pixels in the corresponding input map at coordinates (x,y)(x,y) such that:

Then the size of the extended maps in Ilf\mathbf{I}_{l}^{f} is given by:

where div denotes the integer division operation. The max-pooling operation thus ignores the following parts of the input extended maps:

mod((sx,l−1fin−ox),k)\text{mod}\left(\left(s_{x,l-1}^{f_{\text{in}}}-o_{x}\right),k\right) rightmost columns;

mod((sx,l−1fin−ox),k)\text{mod}\left(\left(s_{x,l-1}^{f_{\text{in}}}-o_{x}\right),k\right) bottom rows.

Discussion and Results

Convolutional layers do not alter the number of fragments (and operate on each fragment independently), whereas each max-pooling layer produces k2k^{2} times the number of fragments given at its input. Therefore, the final number of fragments generated by a net is equal to the product of the squares of the kernel sizes of all its max-pooling layers; for example, the net in Table 1 produces 22⋅22⋅22⋅22=2562^{2}\cdot 2^{2}\cdot 2^{2}\cdot 2^{2}=256 fragments at the output of layer 8. Note that for all layer types, including fully-connected layers, data in a fragment at layer ll only depends on data in a single fragment at layer l−1l-1.

Let wlw_{l} be the size of the map at layer ll when using the patch-based approach. Now consider our image-based approach, and the set Ilf\mathbf{I}_{l}^{f} of extended maps at a given fragment ff for layer ll. Any wl×wlw_{l}\times w_{l} subimage cropped from such extended maps corresponds to the contents of the corresponding maps for some patch contained in the original image. A single fragment contains data for a subset of the patches contained in the original image. Collectively, all fragments at a given layer contain data for the whole set of all patches contained in the original image.

We now discuss the speedup of our image-based approach in comparison to separate evaluation of all patches contained in the input image. We consider as an example the largest network (Table 1) used in for neuronal membrane segmentation . The image size (one slice with neuronal tissue data) is 512×512512\times 512 pixels (see Figure 2). Its edges are mirrored, to get enough pixels for applying the network to all positions. We limit our analysis to convolutional layers, which are by far the most computationally intensive part of a DNN. Conversely, max-pooling layers are simple and fast, requiring less than 1% of the computing time in most practical DNN.

For the patch-based approach, the required amount of floating-point operations (FLOPS) for computing the convolutions in layer ll when scanning an image by a DNN obeys the following formula:

where s2s^{2} is the number of pixels in the input image, and, for each convolutional layer ll, ∣Pl∣\left|\mathbf{P}_{l}\right| denotes the number of maps, wl2w_{l}^{2} the number of pixels of the map, and kl2k_{l}^{2} the number of kernel pixels. The factor ”2” reflects that we have one addition and one multiplication for each component of the dot product.

For the image-based approach, the FLOPS can be computed using the following formula:

where sx,l⋅sy,ls_{x,l}\cdot s_{y,l} represents the size of a fragment in layer ll (to simplify the computation, we assume all fragments have the same size, although they may differ in size by at most one pixel). For the input layer (l=0l=0), mirroring the borders implies that sx,0=sy,0=s+(w0−1)/2s_{x,0}=s_{y,0}=s+(w_{0}-1)/2.

Table 2 reports such computations for all convolutional layers in the network of Table 1. The patch-based approach requires 779.8 times more FLOPS than the image-based approach.

2 Experimental speedup

In Table 3 we report computation times of the DNN in Table 1, when used to segment a 512×512512\times 512 image using three different implementations:

a plain MATLAB implementation of the patch-based approach;

a heavily optimized implementation of the patch-based approach running on a GTX-580 graphics card using CUDA;

a plain MATLAB implementation of the image-based approach.

Results clearly show that the image-based implementation yields a dramatic speedup over patch-based approaches. In particular, matlab-image yields a 3232-fold speedup when compared to the highly-optimized GPU-patch implementation, despite the former being implemented in a slower environment and without attention to low-level optimizations. The impact of GPU and low-level optimizations is obvious as the GPU-patch approach is 5050 times faster than matlab-patch.

Conclusions

We greatly sped up forward-propagating deep neural networks on sliding windows. Our approach handles the complications due to max-pooling layers interleaved with convolutional layers, avoiding unnecessary computations. This is important for fast object detection and image segmentation. For huge nets such as those winning the ISBI Electron Microscopy Segmentation Challenge (see Figure 2), our approach is in theory almost three orders of magnitude faster than a straightforward patch-based forward-propagation approach. In practice, a simple MATLAB implementation yields a 32-fold speedup over a highly optimized patch-based GPU implementation.

Acknowledgment

This work was partially supported by the Supervised Deep / Recurrent Nets SNF grant, Project Code 140399.

References