Stereo Matching by Training a Convolutional Neural Network to Compare Image Patches

Jure Žbontar, Yann LeCun

Introduction

Consider the following problem: given two images taken by cameras at different horizontal positions, we wish to compute the disparity dd for each pixel in the left image. Disparity refers to the difference in horizontal location of an object in the left and right image—an object at position (x,y)(x,y) in the left image appears at position (xd,y)(x-d,y) in the right image. If we know the disparity of an object we can compute its depth zz using the following relation:

where ff is the focal length of the camera and BB is the distance between the camera centers. Figure 1 depicts the input to and the output from our method.

The described problem of stereo matching is important in many fields such as autonomous driving, robotics, intermediate view generation, and 3D scene reconstruction. According to the taxonomy of Scharstein and Szeliski (2002), a typical stereo algorithm consists of four steps: matching cost computation, cost aggregation, optimization, and disparity refinement. Following Hirschmüller and Scharstein (2009) we refer to the first two steps as computing the matching cost and the last two steps as the stereo method. The focus of this work is on computing a good matching cost.

We propose training a convolutional neural network (LeCun et al., 1998) on pairs of small image patches where the true disparity is known (for example, obtained by LIDAR or structured light). The output of the network is used to initialize the matching cost. We proceed with a number of post-processing steps that are not novel, but are necessary to achieve good results. Matching costs are combined between neighboring pixels with similar image intensities using cross-based cost aggregation. Smoothness constraints are enforced by semiglobal matching and a left-right consistency check is used to detect and eliminate errors in occluded regions. We perform subpixel enhancement and apply a median filter and a bilateral filter to obtain the final disparity map.

a description of two architectures based on convolutional neural networks for computing the stereo matching cost;

a method, accompanied by its source code, with the lowest error rate on the KITTI 2012, KITTI 2015, and Middlebury stereo data sets; and

experiments analyzing the importance of data set size, the error rate compared with other methods, and the trade-off between accuracy and runtime for different settings of the hyperparameters.

This paper extends our previous work (Žbontar and LeCun, 2015) by including a description of a new architecture, results on two new data sets, lower error rates, and more thorough experiments.

Related Work

Before the introduction of large stereo data sets like KITTI and Middlebury, relatively few stereo algorithms used ground truth information to learn parameters of their models; in this section, we review the ones that did. For a general overview of stereo algorithms see Scharstein and Szeliski (2002).

Kong and Tao (2004) used the sum of squared distances to compute an initial matching cost. They then trained a model to predict the probability distribution over three classes: the initial disparity is correct, the initial disparity is incorrect due to fattening of a foreground object, and the initial disparity is incorrect due to other reasons. The predicted probabilities were used to adjust the initial matching cost. Kong and Tao (2006) later extend their work by combining predictions obtained by computing normalized cross-correlation over different window sizes and centers. Peris et al. (2012) initialized the matching cost with AD-Census (Mei et al., 2011), and used multiclass linear discriminant analysis to learn a mapping from the computed matching cost to the final disparity.

Ground-truth data was also used to learn parameters of probabilistic graphical models. Zhang and Seitz (2007) used an alternative optimization algorithm to estimate optimal values of Markov random field hyperparameters. Scharstein and Pal (2007) constructed a new data set of 30 stereo pairs and used it to learn parameters of a conditional random field. Li and Huttenlocher (2008) presented a conditional random field model with a non-parametric cost function and used a structured support vector machine to learn the model parameters.

Recent work (Haeusler et al., 2013; Spyropoulos et al., 2014) focused on estimating the confidence of the computed matching cost. Haeusler et al. (2013) used a random forest classifier to combine several confidence measures. Similarly, Spyropoulos et al. (2014) trained a random forest classifier to predict the confidence of the matching cost and used the predictions as soft constraints in a Markov random field to decrease the error of the stereo method.

A related problem to computing the matching cost is learning local image descriptors (Brown et al., 2011; Trzcinski et al., 2012; Simonyan et al., 2014; Revaud et al., 2015; Paulin et al., 2015; Han et al., 2015; Zagoruyko and Komodakis, 2015). The two problems share a common subtask: to measure the similarity between image patches. Brown et al. (2011) introduced a general framework for learning image descriptors and used Powell’s method to select good hyperparameters. Several methods have been suggested for solving the problem of learning local image descriptors, such as boosting (Trzcinski et al., 2012), convex optimization (Simonyan et al., 2014), hierarchical moving-quadrant similarity (Revaud et al., 2015), convolutional kernel networks (Paulin et al., 2015), and convolutional neural networks (Zagoruyko and Komodakis, 2015; Han et al., 2015). Works of Zagoruyko and Komodakis (2015) and Han et al. (2015), in particular, are very similar to our own, differing mostly in the architecture of the network; concretely, the inclusion of pooling and subsampling to account for larger patch sizes and larger variation in viewpoint.

Matching Cost

A typical stereo algorithm begins by computing a matching cost at each position p\mathbf{p} for all disparities dd under consideration. A simple method for computing the matching cost is the sum of absolute differences:

where IL(p)I^{L}(\mathbf{p}) and IR(p)I^{R}(\mathbf{p}) are image intensities at position p\mathbf{p} in the left and right image and Np\mathcal{N}_{\mathbf{p}} is the set of locations within a fixed rectangular window centered at p\mathbf{p}.

We use bold lowercase letters p\mathbf{p} and q\mathbf{q} to denote image locations. A bold lowercase d\mathbf{d} denotes the disparity dd cast to a vector, that is, d=(d,0)\mathbf{d}=(d,0). We use typewriter font for the names of hyperparameters. For example, we would use patch_size to denote the size of the neighbourhood area Np\mathcal{N}_{\mathbf{p}}.

Equation (1) can be interpreted as measuring the cost associated with matching a patch from the left image, centered at position p\mathbf{p}, with a patch from the right image, centered at position pd\mathbf{p}-\mathbf{d}. We want the cost to be low when the two patches are centered around the image of the same 3D point, and high when they are not.

Since examples of good and bad matches can be constructed from publicly available data sets (for example, the KITTI and Middlebury stereo data sets), we can attempt to solve the matching problem by a supervised learning approach. Inspired by the successful application of convolutional neural networks to vision problems, we used them to assess how well two small image patches match.

We use ground truth disparity maps from either the KITTI or Middlebury stereo data sets to construct a binary classification data set. At each image position where the true disparity is known we extract one negative and one positive training example. This ensures that the data set contains an equal number of positive and negative examples. A positive example is a pair of patches, one from the left and one from the right image, whose center pixels are the images of the same 3D point, while a negative example is a pair of patches where this is not the case. The following section describes the data set construction step in detail.

Let <Pn×nL(p),Pn×nR(q)><\mathcal{P}_{n\times n}^{L}(\mathbf{p}),\mathcal{P}_{n\times n}^{R}(\mathbf{q})> denote a pair of patches, where Pn×nL(p)\mathcal{P}_{n\times n}^{L}(\mathbf{p}) is an n×nn\times n patch from the left image centered at position p=(x,y)\mathbf{p}=(x,y), Pn×nR(q)\mathcal{P}_{n\times n}^{R}(\mathbf{q}) is an n×nn\times n patch from the right image centered at position q\mathbf{q}, and dd denotes the correct disparity at position p\mathbf{p}. A negative example is obtained by setting the center of the right patch to

where onego_{\text{neg}} is chosen from either the interval [dataset_neg_low,dataset_neg_high][\texttt{dataset\_neg\_low},\texttt{dataset\_neg\_high}] or, its origin reflected counterpart, [dataset_neg_high,dataset_neg_low][-\texttt{dataset\_neg\_high},-\texttt{dataset\_neg\_low}]. The random offset onego_{\text{neg}} ensures that the resulting image patches are not centered around the same 3D point.

where oposo_{\text{pos}} is chosen randomly from the interval [dataset_pos,dataset_pos][-\texttt{dataset\_pos},\texttt{dataset\_pos}]. The reason for including oposo_{\text{pos}}, instead of setting it to zero, has to do with the stereo method used later on. In particular, we found that cross-based cost aggregation performs better when the network assigns low matching costs to good matches as well as near matches. In our experiments, the hyperparameter dataset_pos was never larger than one pixel.

2 Network Architectures

We describe two network architectures for learning a similarity measure on image patches. The first architecture is faster than the second, but produces disparity maps that are slightly less accurate. In both cases, the input to the network is a pair of small image patches and the output is a measure of similarity between them. Both architectures contain a trainable feature extractor that represents each image patch with a feature vector. The similarity between patches is measured on the feature vectors instead of the raw image intensity values. The fast architecture uses a fixed similarity measure to compare the two feature vectors, while the accurate architecture attempts to learn a good similarity measure on feature vectors.

The first architecture is a siamese network, that is, two shared-weight sub-networks joined at the head (Bromley et al., 1993). The sub-networks are composed of a number of convolutional layers with rectified linear units following all but the last layer. Both sub-networks output a vector capturing the properties of the input patch. The resulting two vectors are compared using the cosine similarity measure to produce the final output of the network. Figure 2 provides an overview of the architecture.

The network is trained by minimizing a hinge loss. The loss is computed by considering pairs of examples centered around the same image position where one example belongs to the positive and one to the negative class. Let s+s_{+} be the output of the network for the positive example, ss_{-} be the output of the network for the negative example, and let mm, the margin, be a positive real number. The hinge loss for that pair of examples is defined as max(0,m+ss+)\max(0,m+s_{-}-s_{+}). The loss is zero when the similarity of the positive example is greater than the similarity of the negative example by at least the margin mm. We set the margin to 0.2 in our experiments.

The hyperparameters of this architecture are the number of convolutional layers in each sub-network (num_conv_layers), the size of the convolution kernels (conv_kernel_size), the number of feature maps in each layer (num_conv_feature_maps), and the size of the input patch (input_patch_size).

2.2 Accurate Architecture

The second architecture is derived from the first by replacing the cosine similarity measure with a number of fully-connected layers (see Figure 3). This architectural change increased the running time, but decreased the error rate. The two sub-networks comprise a number of convolutional layers, with a rectified linear unit following each layer. The resulting two vectors are concatenated and forward-propagated through a number of fully-connected layers followed by rectified linear units. The last fully-connected layer produces a single number which, after being transformed with the sigmoid nonlinearity, is interpreted as the similarity score between the input patches.

We use the binary cross-entropy loss for training. Let ss denote the output of the network for one training example and tt denote the class of that training example; t=1t=1 if the example belongs to the positive class and t=0t=0 if the example belongs to the negative class. The binary cross-entropy loss for that example is defined as tlog(s)+(1t)log(1s)t\log(s)+(1-t)\log(1-s).

The decision to use two different loss functions, one for each architecture, was based on empirical evidence. While we would have preferred to use the same loss function for both architectures, experiments showed that the binary cross-entropy loss performed better than the hinge loss on the accurate architecture. On the other hand, since the last step of the fast architecture is the cosine similarity computation, a cross-entropy loss was not directly applicable.

The hyperparameters of the accurate architecture are the number of convolutional layers in each sub-network (num_conv_layers), the number of feature maps in each layer (num_conv_feature_maps), the size of the convolution kernels (conv_kernel_size), the size of the input patch (input_patch_size), the number of units in each fully-connected layer (num_fc_units), and the number of fully-connected layers (num_fc_layers).

3 Computing the Matching Cost

The output of the network is used to initialize the matching cost:

where s(<PL(p),PR(pd)>)s(<\mathcal{P}^{L}(\mathbf{p}),\mathcal{P}^{R}(\mathbf{p}-\mathbf{d})>) is the output of the network when run on input patches PL(p)\mathcal{P}^{L}(\mathbf{p}) and PR(pd)\mathcal{P}^{R}(\mathbf{p}-\mathbf{d}). The minus sign converts the similarity score to a matching cost.

To compute the entire matching cost tensor CCNN(p,d)C_{\text{CNN}}(\mathbf{p},d) we would, naively, have to perform the forward pass for each image location and each disparity under consideration. The following three implementation details kept the running time manageable:

The outputs of the two sub-networks need to be computed only once per location, and do not need to be recomputed for every disparity under consideration.

The output of the two sub-networks can be computed for all pixels in a single forward pass by propagating full-resolution images, instead of small image patches. Performing a single forward pass on the entire w×hw\times h image is faster than performing whw\cdot h forward passes on small patches because many intermediate results can be reused.

The output of the fully-connected layers in the accurate architecture can also be computed in a single forward pass. This is done by replacing each fully-connected layer with a convolutional layer with 1×11\times 1 kernels. We still need to perform the forward pass for each disparity under consideration; the maximum disparity dd is 228 for the KITTI data set and 400 for the Middlebury data set. As a result, the fully-connected part of the network needs to be run dd times, and is a bottleneck of the accurate architecture.

To compute the matching cost of a pair of images, we run the sub-networks once on each image and run the fully-connected layers dd times, where dd is the maximum disparity under consideration. This insight was important in designing the architecture of the network. We could have chosen an architecture where the two images are concatenated before being presented to the network, but that would imply a large cost at runtime because the whole network would need to be run dd times. This insight also led to the development of the fast architecture, where the only layer that is run dd times is the dot product of the feature vectors.

Stereo Method

The raw outputs of the convolutional neural network are not enough to produce accurate disparity maps, with errors particularly apparent in low-texture regions and occluded areas. The quality of the disparity maps can be improved by applying a series of post-processing steps referred to as the stereo method. The stereo method we used was influenced by Mei et al. (2011) and comprises cross-based cost aggregation, semiglobal matching, a left-right consistency check, subpixel enhancement, a median, and a bilateral filter.

Information from neighboring pixels can be combined by averaging the matching cost over a fixed window. This approach fails near depth discontinuities, where the assumption of constant depth within a window is violated. We might prefer a method that adaptively selects the neighborhood for each pixel, so that support is collected only from pixels of the same physical object. In cross-based cost aggregation (Zhang et al., 2009) we build a local neighborhood around each location comprising pixels with similar image intensity values with the hope that these pixels belong to the same object.

The method begins by constructing an upright cross at each position; this cross is used to define the local support region. The left arm pl\mathbf{p}_{l} at position p\mathbf{p} extends left as long as the following two conditions hold:

I(p)I(pl)<cbca_intensity|I(\mathbf{p})-I(\mathbf{p}_{l})|<\texttt{cbca\_intensity}; the image intensities at positions p\mathbf{p} and pl\mathbf{p}_{l} should be similar, their difference should be less than cbca_intensity.

ppl<cbca_distance\|\mathbf{p}-\mathbf{p}_{l}\|<\texttt{cbca\_distance}; the horizontal distance (or vertical distance in case of top and bottom arms) between positions p\mathbf{p} and pl\mathbf{p}_{l} is less than cbca_distance pixels.

The right, bottom, and top arms are constructed analogously. Once the four arms are known, we can compute the support region U(p)U(\mathbf{p}) as the union of horizontal arms of all positions q\mathbf{q} laying on p\mathbf{p}’s vertical arm (see Figure 4).

Zhang et al. (2009) suggest that aggregation should consider the support regions of both images in a stereo pair. Let ULU^{L} and URU^{R} denote the support regions in the left and right image. We define the combined support region UdU_{d} as

The matching cost is averaged over the combined support region:

where ii is the iteration number. We repeat the averaging a number of times. Since the support regions are overlapping, the results can change at each iteration. We skip cross-based cost aggregation in the fast architecture because it is not crucial for achieving a low error rate and because it is relatively expensive to compute.

2 Semiglobal Matching

We refine the matching cost by enforcing smoothness constraints on the disparity image. Following Hirschmüller (2008), we define an energy function E(D)E(D) that depends on the disparity image DD:

where 1{}1\{\cdot\} denotes the indicator function. The first term penalizes disparities with high matching costs. The second term adds a penalty P1P_{1} when the disparity of neighboring pixels differ by one. The third term adds a larger penalty P2P_{2} when the neighboring disparities differ by more than one.

Rather than minimizing E(D)E(D) in all directions simultaneously, we could perform the minimization in a single direction with dynamic programming. This solution would introduce unwanted streaking effects, since there would be no incentive to make the disparity image smooth in the directions we are not optimizing over. In semiglobal matching we minimize the energy in a single direction, repeat for several directions, and average to obtain the final result. Although Hirschmüller (2008) suggested choosing sixteen direction, we only optimized along the two horizontal and the two vertical directions; adding the diagonal directions did not improve the accuracy of our system. To minimize E(D)E(D) in direction r\mathbf{r}, we define a matching cost Cr(p,d)C_{\mathbf{r}}(\mathbf{p},d) with the following recurrence relation:

The second term is subtracted to prevent values of Cr(p,d)C_{\mathbf{r}}(\mathbf{p},d) from growing too large and does not affect the optimal disparity map.

The penalty parameters P1P_{1} and P2P_{2} are set according to the image gradient so that jumps in disparity coincide with edges in the image. Let D1=IL(p)IL(pr)D_{1}=|I^{L}(\mathbf{p})-I^{L}(\mathbf{p}-\mathbf{r})| and D2=IR(pd)IR(pdr)D_{2}=|I^{R}(\mathbf{p}-\mathbf{d})-I^{R}(\mathbf{p}-\mathbf{d}-\mathbf{r})| be the difference in image intensity between two neighboring positions in the direction we are optimizing over. We set P1P_{1} and P2P_{2} according to the following rules:

The hyperparameters sgm_P1 and sgm_P2 set a base penalty for discontinuities in the disparity map. The base penalty is reduced by a factor of sgm_Q1 if one of D1D_{1} or D2D_{2} indicate a strong image gradient or by a larger factor of sgm_Q2 if both D1D_{1} and D2D_{2} indicate a strong image gradient. The value of P1P_{1} is further reduced by a factor of sgm_V when considering the two vertical directions; in the ground truth, small changes in disparity are much more frequent in the vertical directions than in the horizontal directions and should be penalised less.

The final cost CSGM(p,d)C_{\text{SGM}}(\mathbf{p},d) is computed by taking the average across all four directions:

After semiglobal matching we repeat cross-based cost aggregation, as described in the previous section. Hyperparameters cbca_num_iterations_1 and cbca_num_iterations_2 determine the number of cross-based cost aggregation iterations before and after semiglobal matching.

3 Computing the Disparity Image

The disparity image D(p)D(\mathbf{p}) is computed by the winner-takes-all strategy, that is, by finding the disparity dd that minimizes C(p,d)C(\mathbf{p},d),

The interpolation steps attempt to resolve conflicts between the disparity map predicted for the left image and the disparity map predicted for the right image. Let DLD^{L} denote the disparity map obtained by treating the left image as the reference image—this was the case so far, that is, DL(p)=D(p)D^{L}(\mathbf{p})=D(\mathbf{p})—and let DRD^{R} denote the disparity map obtained by treating the right image as the reference image. DLD^{L} and DRD^{R} sometimes disagree on what the correct disparity at a particular position should be. We detect these conflicts by performing a left-right consistency check. We label each position p\mathbf{p} by applying the following rules in turn:

For positions marked as occlusion, we want the new disparity value to come from the background. We interpolate by moving left until we find a position labeled correct and use its value. For positions marked as mismatch, we find the nearest correct pixels in 16 different directions and use the median of their disparities for interpolation. We refer to the interpolated disparity map as DINTD_{\text{INT}}.

3.2 Subpixel Enhancement

Subpixel enhancement provides an easy way to increase the resolution of a stereo algorithm. We fit a quadratic curve through the neighboring costs to obtain a new disparity image:

where d=DINT(p)d=D_{\text{INT}}(\mathbf{p}), C=CSGM(p,d1)C_{-}=C_{\text{SGM}}(\mathbf{p},d-1), C=CSGM(p,d)C=C_{\text{SGM}}(\mathbf{p},d), and C+=CSGM(p,d+1)C_{+}=C_{\text{SGM}}(\mathbf{p},d+1).

3.3 Refinement

The final steps of the stereo method consist of a 5×55\times 5 median filter and the following bilateral filter:

where g(x)g(x) is the probability density function of a zero mean normal distribution with standard deviation blur_sigma and W(p)W(\mathbf{p}) is the normalizing constant,

The role of the bilateral filter is to smooth the disparity map without blurring the edges. DBFD_{\text{BF}} is the final output of our stereo method.

Experiments

We used three stereo data sets in our experiments: KITTI 2012, KITTI 2015, and Middlebury. The test set error rates reported in Tables 1, 2, and 4 were obtained by submitting the generated disparity maps to the online evaluation servers. All other error rates were computed by splitting the data set in two, using one part for training and the other for validation.

The KITTI stereo data set (Geiger et al., 2013; Menze and Geiger, 2015) is a collection of rectified image pairs taken from two video cameras mounted on the roof of a car, roughly 54 centimeters apart. The images were recorded while driving in and around the city of Karlsruhe, in sunny and cloudy weather, at daytime. The images were taken at a resolution of 1240×3761240\times 376. A rotating laser scanner mounted behind the left camera recorded ground truth depth, labeling around 30 % of the image pixels.

The ground truth disparities for the test set are withheld and an online leaderboard is provided where researchers can evaluate their method on the test set. Submissions are allowed once every three days. Error is measured as the percentage of pixels where the true disparity and the predicted disparity differ by more than three pixels. Translated into distance, this means that, for example, the error tolerance is 3 centimeters for objects 2 meters from the camera and 80 centimeters for objects 10 meters from the camera.

Two KITTI stereo data sets exist: KITTI 2012The KITTI 2012 scoreboard: http://www.cvlibs.net/datasets/kitti/eval_stereo_flow.php?benchmark=stereo and, the newer, KITTI 2015The KITTI 2015 scoreboard: http://www.cvlibs.net/datasets/kitti/eval_scene_flow.php?benchmark=stereo. For the task of computing stereo they are nearly identical, with the newer data set improving some aspects of the optical flow task. The 2012 data set contains 194 training and 195 testing images, while the 2015 data set contains 200 training and 200 testing images. There is a subtle but important difference introduced in the newer data set: vehicles in motion are densely labeled and car glass is included in the evaluation. This emphasizes the method’s performance on reflective surfaces.

The best performing methods on the KITTI 2012 data set are listed in Table 1. Our accurate architecture ranks first with an error rate of 2.43 %. Third place on the leaderboard is held by our previous work (Žbontar and LeCun, 2015) with an error rate of 2.61 %. The two changes that reduced the error from 2.61 % to 2.43 % were augmenting the data set (see Section 5.4) and doubling the number of convolution layers while reducing the kernel size from 5×55\times 5 to 3×33\times 3. The method in second place (Güney and Geiger, 2015) uses the matching cost computed by our previous work (Žbontar and LeCun, 2015). The test error rate of the fast architecture is 2.82 %, which would be enough for fifth place had the method been allowed to appear in the public leaderboard. The running time for processing a single image pair is 67 seconds for the accurate architecture and 0.8 seconds for the fast architecture. Figure 5 contains a pair of examples from the KITTI 2012 data set, together with the predictions of our method.

Table 2 presents the frontrunners on the KITTI 2015 data sets. The error rates of our methods are 3.89 % for the accurate architecture and 4.46 % for the fast architecture, occupying first and second place on the leaderboard. Since one submission per paper is allowed, only the result of the accurate architecture appears on the public leaderboard. See Figure 6 for the disparity maps produced by our method on the KITTI 2015 data set.

2 Middlebury Stereo Data Set

The image pairs of the Middlebury stereo data set are indoor scenes taken under controlled lighting conditions. Structured light was used to measure the true disparities with higher density and precision than in the KITTI data set. The data sets were published in five separate works in the years 2001, 2003, 2005, 2006, and 2014 (Scharstein and Szeliski, 2002, 2003; Scharstein and Pal, 2007; Hirschmüller and Scharstein, 2007; Scharstein et al., 2014). In this paper, we refer to the Middlebury data set as the concatenation of all five data sets; a summary of each is presented in Table 3.

Each scene in the 2005, 2006, and 2014 data sets was taken under a number of lighting conditions and shutter exposures, with a typical image pair taken under four lighting conditions and seven exposure settings for a total of 28 images of the same scene.

An online leaderboardThe Middlebury scoreboard: http://vision.middlebury.edu/stereo/eval3/, similar to the one provided by KITTI, displays a ranked list of all submitted methods. Participants have only one opportunity to submit their results on the test set to the public leaderboard. This rule is stricter than the one on the KITTI data set, where submissions are allowed every three days. The test set contains 15 images borrowed from the 2005 and 2014 data sets.

The data set is provided in full, half, and quarter resolution. The error is computed at full resolution; if the method outputs half or quarter resolution disparity maps, they are upsampled before the error is computed. We chose to run our method on half resolution images because of the limited size of the graphic card’s memory available.

Rectifying a pair of images using standard calibration procedures, like the ones present in the OpenCV library, results in vertical disparity errors of up to nine pixels on the Middlebury data set (Scharstein et al., 2014). Each stereo pair in the 2014 data set is rectified twice: once using a standard, imperfect approach, and once using precise 2D correspondences for perfect rectification (Scharstein et al., 2014). We train the network on imperfectly rectified image pairs, since only two of the fifteen test images (Australia and Crusade) are rectified perfectly.

The error is measured as the percentage of pixels where the true disparity and the predicted disparity differ by more than two pixels; this corresponds to an error tolerance of one pixel at half resolution. The error on the evaluation server is, by default, computed only on non-occluded pixels. The final error reported online is the weighted average over the fifteen test images, with weights set by the authors of the data set.

Table 4 contains a snapshot of the third, and newest, version of the Middlebury leaderboard. Our method ranks first with an error rate of 8.29 % and a substantial lead over the second placed MeshStereo method, whose error rate is 13.4 %. See Figure 7 for disparity maps produced by our method on one image pair from the Middlebury data set.

3 Details of Learning

We construct a binary classification data set from all available image pairs in the training set. The data set contains 25 million examples on the KITTI 2012, 17 million examples on the KITTI 2015, and 38 million examples on the Middlebury data set.

At training time, the input to the network was a batch of 128 pairs of image patches. At test time, the input was the entire left and right image. We could have used entire images during training as well, as it would allow us to implement the speed optimizations described in Section 3.3. There were several reasons why we preferred to train on image patches: it was easier to control the batch size, the examples could be shuffled so that one batch contained patches from several different images, and it was easier to maintain the same number of positive and negative examples within a batch.

We minimized the loss using mini-batch gradient descent with the momentum term set to 0.9. We trained for 14 epochs with the learning rate initially set to 0.003 for the accurate architecture and 0.002 for the fast architecture. The learning rate was decreased by a factor of 10 on the 11th{}^{\text{th}} epoch. The number of epochs, the initial learning rate, and the learning rate decrease schedule where treated as hyperparameters and were optimized with cross-validation. Each image was preprocessed by subtracting the mean and dividing by the standard deviation of its pixel intensity values. The left and right image of a stereo pair were preprocessed separately. Our initial experiments suggested that using color information does not improve the quality of the disparity maps; therefore, we converted all color images to grayscale.

The post-processing steps of the stereo method were implemented in CUDA (Nickolls et al., 2008), the network training was done with the Torch environment (Collobert et al., 2011) using the convolution routines from the cuDNN library (Chetlur et al., 2014). The OpenCV library (Bradski, 2000) was used for the affine transformation in the data augmentation step.

The hyperparameters where optimized with manual search and simple scripts that helped automate the process. The hyperparameters we selected are shown in Table 5.

4 Data Set Augmentation

Augmenting the data set by repeatedly transforming the training examples is a commonly employed technique to reduce the network’s generalization error. The transformations are applied at training time and do not affect the runtime performance. We randomly rotate, scale and shear the training patches; we also change their brightness and contrast. Since the transformations are applied to patches after they have been extracted from the images, the data augmentation step does not alter the ground truth disparity map or ruin the rectification.

The parameters of the transformation are chosen randomly for each pair of patches, and after one epoch of training, when the same example is being presented to the network for the second time, new random parameters are selected. We choose slightly different transformation parameters for the left and right image; for example, we would rotate the left patch by 10 degrees and the right by 14. Different data sets benefited from different types of transformations and, in some cases, using the wrong transformations increased the error.

On the Middlebury data set we took advantage of the fact that the images were taken under different lighting conditions and different shutter exposures by training on all available images. The same data set augmentation parameters were used for the KITTI 2012 and KITTI 2015 data sets.

The Middlebury test data sets contains two images worth mentioning: Classroom, where the right image is underexposed and, therefore, darker than the left; and Djembe, where the left and right images were taken under different light conditions. To handle these two cases we train, 20 % of the time, on images where either the shutter exposure or the arrangements of lights are different for the left and right image.

We combat imperfect rectification on the Middlebury data set by including a small vertical disparity between the left and right image patches.

Before describing the steps of data augmentation, let us introduce some notation: in the following, a word in typewriter is used to denote the name of a hyperparameter defining a set, while the same word in italic is used to denote a number drawn randomly from that set. For example, rotate is a hyperparameter defining the set of possible rotations and rotate is a number drawn randomly from that set. The steps of data augmentation are presented in the following list:

Rotate the left patch by rotate degrees and the right patch by rotate+rotate_diff\textit{rotate}+\textit{rotate\_diff} degrees.

Scale the left patch by scale and the right patch by scalescale_diff\textit{scale}\cdot\textit{scale\_diff}.

Scale the left patch in the horizontal direction by horizontal_scale and the right patch by horizontal_scalehorizontal_scale_diff\textit{horizontal\_scale}\cdot\textit{horizontal\_scale\_diff}.

Shear the left patch in the horizontal direction by horizontal_shear and the right patch by horizontal_shear+horizontal_shear_diff\textit{horizontal\_shear}+\textit{horizontal\_shear\_diff}.

Translate the right patch in the vertical direction by vertical_disparity.

Adjust the brightness and contrast by setting the left and right image patches to:

with addition and multiplication carried out element-wise where appropriate.

Table 6 contains the hyperparameters used and measures how each data augmentation step affected the validation error.

Data augmentation reduced the validation error from 2.73 % to 2.61 % on the KITTI 2012 data set and from 8.75 % to 7.91 % on the Middlebury data set.

5 Runtime

We measure the runtime of our implementation on a computer with a NVIDIA Titan X graphics processor unit. Table 7 contains the runtime measurements across a range of hyperparameter settings for three data sets: KITTI, Middlebury half resolution, and a new, fictitious data set, called Tiny, which we use to demonstrate the performance of our method on the kind of images typically used for autonomous driving or robotics. The sizes of images we measured the runtime on were: 1242 ×\times 350 with 228 disparity levels for the KITTI data set, 1500 ×\times 1000 with 200 disparity levels for the Middlebury data set, and 320 ×\times 240 with 32 disparity levels for the Tiny data set.

Table 7 reveals that the fast architecture is up to 90 times faster than the accurate architecture. Furthermore, the running times of the fast architecture are 0.78 seconds on KITTI, 2.03 seconds on Middlebury, and 0.06 seconds on the Tiny data set. We can also see that the fully-connected layers are responsible for most of the runtime in the accurate architecture, as the hyperparameters controlling the number of convolutional layer and the number of feature maps have only a small effect on the runtime.

Training times depended on the size of the data set and the architecture, but never exceeded two days.

6 Matching Cost

We argue that the low error rate of our method is due to the convolutional neural network and not a superior stereo method. We verify this claim by replacing the convolutional neural network with three standard approaches for computing the matching cost:

The sum of absolute differences computes the matching cost according to Equation (1), that is, the matching cost between two image patches is computed by summing the absolute differences in image intensities between corresponding locations. We used 9×99\times 9 patches.

The census transform (Zabih and Woodfill, 1994) represents each image position as a bit vector. The size of this vector is a hyperparameter whose value, after examining several, we set to 81. The vector is computed by cropping a 9×99\times 9 image patch centered around the position of interest and comparing the intensity values of each pixel in the patch to the intensity value of the pixel in the center. When the center pixel is brighter the corresponding bit is set. The matching cost is computed as the hamming distance between two census transformed vectors.

Normalized cross-correlation is a window-based method defined with the following equation:

The normalized cross-correlation matching cost computes the cosine similarity between the left and right image patch, when the left and right image patches are viewed as vectors instead of matrices. This is the same function that is computed in the last two layers of the fast architecture (normalization and dot product). The neighbourhood Np\mathcal{N}_{\mathbf{p}} was set to a square 11×1111\times 11 window around p\mathbf{p}.

The “sad”, “cens”, and “ncc” columns of Table 8 contain the results of the sum of absolute differences, the census transform, and normalized cross-correlation on the KITTI 2012, KITTI 2015, and Middlebury data sets. The validation errors in the last rows of Table 8 should be used to compare the five methods. On all three data sets the accurate architecture performs best, followed by the fast architecture, which in turn is followed by the census transform. These are the three best performing methods on all three data sets. Their error rates are 2.61 %, 3.02 %, and 4.90 % on KITTI 2012; 3.25 %, 3.99 %, and 5.03 % on KITTI 2015; and 7.91 %, 9.87 %, and 16.72 % on Middlebury. The sum of absolute differences and the normalized cross-correlation matching costs produce disparity maps with larger errors. For a visual comparison of our method and the census transform see Figures 5, 6, and 7.

7 Stereo Method

The stereo method includes a number of post-processing steps: cross-based cost aggregation, semiglobal matching, interpolation, subpixel enhancement, a median, and a bilateral filter. We ran a set of experiments in which we excluded each of the aforementioned steps and recorded the validation error (see Table 8).

The last two rows of Table 8 allude to the importance of the post-processing steps of the stereo method. We see that, if all post-processing steps are removed, the validation error of the accurate architecture increases from 2.61 % to 13.49 % on KITTI 2012, from 3.25 % to 13.38 % on KITTI 2015, and from 7.91 % to 28.33 % on Middlebury.

Out of all post-processing steps of the stereo method, semiglobal matching affects the validation error the strongest. If we remove it, the validation error increases from 2.61 % to 4.26 % on KITTI 2012, from 3.25 % to 4.51 % on KITTI 2015, and from 7.91 % to 11.99 % on Middlebury.

We did not use the left-right consistency check to eliminate errors in occluded regions on the Middlebury data set. The error rate increased from 7.91 % to 8.22 % using the left-right consistency check on the accurate architecture, which is why we decided to remove it.

8 Data Set Size

We used a supervised learning approach to measure the similarity between image patches. It is, therefore, natural to ask how does the size of the data set affect the quality of the disparity maps. To answer this question, we retrain our networks on smaller training sets obtained by selecting a random set of examples (see Table 9).

We observe that the validation error decreases as we increase the number of training examples. These experiments suggest a simple strategy for improving the results of our stereo method: collect a larger data set.

9 Transfer Learning

Up to this point the training and validation sets were created from the same stereo data set, either KITTI 2012, KITTI 2015, or Middlebury. To evaluate the performance of our method in the transfer learning setting, we run experiments where the validation error is computed on a different data set than the one used for training. For example, we would use the Middlebury data set to train the matching cost neural network and evaluate its performance on the KITTI 2012 data set. These experiments give us some idea of the expected performance in a real-world application, where it isn’t possible to train a specialized network because no ground truth is available. The results of these experiments are shown in Table 10.

Some results in Table 10 were unexpected. For example, the validation error on KITTI 2012 is lower when using the Middlebury training set compared to the KITTI 2015 training set, even though the KITTI 2012 data set is obviously more similar to KITTI 2015 than Middlebury. Furthermore, the validation error on KITTI 2012 is lower when using the fast architecture instead of the accurate architecture when training on KITTI 2015.

The matching cost neural network trained on the Middlebury data set transfers well to the KITTI data sets. Its validation error is similar to the validation errors obtained by networks trained on the KITTI data sets.

10 Hyperparameters

Searching for a good set of hyperparameters is a daunting task—with the search space growing exponentially with the number of hyperparameters and no gradient to guide us. To better understand the effect of each hyperparameter on the validation error, we conduct a series of experiments where we vary the value of one hyperparameter while keeping the others fixed to their default values. The results are shown in Table 11 and can be summarized by observing that increasing the size of the network improves the generalization performance, but only up to a point, when presumably, because of the size of the data set, the generalization performance starts do decrease.

Note that the num_conv_layers hyperparameter implicitly controls the size of the image patches. For example, a network with one convolutional layer with 3×33\times 3 kernels compares image patches of size 3×33\times 3, while a network with five convolutional layers compares patches of size 11×1111\times 11.

Conclusion

We presented two convolutional neural network architectures for learning a similarity measure on image patches and applied them to the problem of stereo matching.

The source code of our implementation is available at https://github.com/jzbontar/mc-cnn. The online repository contains procedures for computing the disparity map, training the network, as well as the post-processing steps of the stereo method.

The accurate architecture produces disparity maps with lower error rates than any previously published method on the KITTI 2012, KITTI 2015, and Middlebury data sets. The fast architecture computes the disparity maps up to 90 times faster than the accurate architecture with only a small increase in error. These results suggest that convolutional neural networks are well suited for computing the stereo matching cost even for applications that require real-time performance.

The fact that a relatively simple convolutional neural network outperformed all previous methods on the well-studied problem of stereo is a rather important demonstration of the power of modern machine learning approaches.

References