A colour photo of a hotel room and a colour photo of a bedroom can look the same. A depth camera measures the room in metres, and the tie breaks.
Pick what the classifier may see: colour, depth, or both. Then press Next room. Then we build, piece by piece, the metres, the height and angle channels, the two networks, the fusion rule, the sensor's failures and the honest score.
You need what a probability is and a little Python. It helps to know that a network can output one label for a whole image. We build the rest from zero.
Part of the Scene Classification family. It builds on Visual Scene Classification, which classifies rooms from colour alone.
Illustrative toy. We made up the room sizes and the two networks' probabilities. The colour + depth answer multiplies the two votes and rescales them, the late fusion rule of Chapter 6.
Chapter 0
See why a bigger colour model cannot split two rooms that only differ in size
A service robot rolls through a doorway. Its scene classifier must name the room in one word. A bedroom is private: knock and wait. A hotel room has other rules. The colour classifier says bedroom 0.49, hotel room 0.46.
Look at the photo and you see why. Designers make a hotel room look like a bedroom: the same bed, lamp and beige walls. Worse, a small room and a large room with larger furniture can give the same photo. Colour has no units, so it cannot tell them apart.
Room B is room A made 1.44 times bigger in every direction. Switch from the colour view to the depth view.
Illustrative toy. In the depth view, brighter blue means nearer. The same camera sees both rooms from the doorway.
A depth camera reports, for each pixel, the distance to the surface in metres. Its output is a depth map: an image the same size as the photo, with a distance in each pixel. RGB-D scene classification names the room from both: the three colour channels (RGB) and the depth map (D).
The symptom: a colour model splits close to 50/50 between two room types, and a bigger colour model does not help. To measure it, read the confusion matrix and find the pairs that swap most. The fix is a sensor that measures what differs between them. For bedroom and hotel room, that is size.
Chapter 1
State the input and output, then count how few training images each room type gets
The input is a pair. The colour image has shape (3, H, W). The depth map has shape (1, H, W). The output is a probability for each of K room types. The prediction is the most probable type.
The pair must be registered: pixel (i, j) shows the same point in both images. The two sensors sit a few centimetres apart, so software warps one stream onto the other. The public datasets ship registered.
Two datasets define the field. NYU Depth v2 (Silberman, Hoiem, Kohli and Fergus, 2012) has 1,449 labelled pairs from a Microsoft Kinect, in 464 scenes from 3 cities. SUN RGB-D (Song, Lichtenberg and Xiao, 2015) has 10,335 pairs from four different depth sensors.
| Dataset | Pairs | Scene split | Train / test |
|---|---|---|---|
| NYU Depth v2 | 1,449, one Kinect | 27 types, grouped into 10 classes (9 common + other) | 795 / 654 |
| SUN RGB-D | 10,335, four sensors | the 19 most common types | 4,845 / 4,659 |
Count the training images per class. SUN RGB-D gives 4,845 / 19 = about 255. NYU Depth v2 gives 795 / 10 = about 80. ImageNet, the usual pretraining set, gives 1,281,167 / 1,000 = about 1,281. RGB-D work lives in a small-data regime.
Each dot is 10 training images of one class. Pick a dataset and compare it with ImageNet.
Counts from the dataset pages and the standard splits (as used by Song, Herranz and Jiang, 2018). Per-class values are averages; real classes are unbalanced.
The symptom: training accuracy near 100 percent, test accuracy far lower. That is overfitting, and 255 images per class invite it. Measure the gap between train and test accuracy. The fix is to spend data wisely. Pretrain where it transfers, hand the network ready-made geometry (Chapter 4), and keep the model small.
Chapter 2
Back-project a pixel with its depth and read off where it sits in the room
A colour pixel says only "something is in this direction". It could be a near toy or a far wall. Depth adds the distance, so the pixel becomes a point in metres. Then you can measure the room.
Use a pinhole camera model: every ray passes through one point, the camera centre. Call the distance from the centre to the image plane, in pixels, the focal length f. Call a pixel's offset from the image centre, in pixels, u. Call the depth the sensor reports there Z, in metres, measured along the camera's viewing axis. We want the sideways position X of that point, in metres.
Capital X equals capital Z times u, divided by f.
Divide the pixel offset u by f. That is the slope of the ray. Walk out along the ray to depth Z. The sideways distance you reach is X. The same rule with the row offset gives the vertical position.
With made-up numbers: u = 200 px, Z = 3.0 m, f = 500 px. X = 3.0 × 200 / 500 = 1.2 m right of the axis. A wall pixel at −2.0 m and another at +2.0 m mean the room is 4.0 m wide.
The two triangles from the camera centre (one to the pixel, one to the point) have the same shape, so X / Z = u / f. That is the whole derivation. Without Z, every point on the ray fits the pixel. Try it below.
The teal camera looks up the page. Slide the pixel and the depth. Then switch on the millimetre bug.
Illustrative camera: f = 500 px, an image 640 px wide. Many sensors store depth as whole millimetres in 16-bit integers.
The symptom: a room comes out 4,000 m wide. The sensor stored millimetres and the code read metres. To measure it, back-project two wall pixels and check the width against a tape measure. The fix: convert units once, at load time, and assert that depths fall between 0.3 m and 10 m.
Chapter 3
Turn each pixel's depth into its height, and read a room's layout from the heights
A kitchen has a counter at about 0.9 m. A dining room has a table at about 0.75 m. Every room has a floor at 0 m and a ceiling at 2.4 to 3 m. If each pixel knew its height, the layout would be plain to see.
Mount the camera level, at height c above the floor. Call a pixel's row offset below the image centre v, in pixels (rows above the centre are negative). With depth Z and focal length f, Chapter 2's rule gives the drop below the camera: Z · v / f. Call the pixel's height above the floor h.
h equals c minus capital Z times v, divided by f.
Find how far the point sits below the camera: Z times v, divided by f. Subtract that drop from the camera height c. The result is h. A point above the camera has negative v, so its drop is negative and h comes out larger than c.
With made-up numbers: c = 1.2 m, f = 500 px. A pixel at v = 150 with Z = 4.0 m: h = 1.2 − 4.0 × 150 / 500 = 0.0 m, the floor. At v = 90, Z = 2.5 m: h = 0.75 m, a table. At v = −140, Z = 5.0 m: h = 2.6 m, the ceiling.
The teal camera looks right. Slide the row to aim the ray at the floor, the table or the ceiling. Then tilt the camera and leave the formula as it is.
Illustrative room: camera 1.2 m up, f = 500 px. Table top 0.75 m high, 2.0 m to 2.8 m away. Ceiling 2.6 m, back wall 5.5 m away.
The symptom: the floor reads 0.4 m high near the back of the room. The camera tilts down, but the formula assumes it is level. To measure it, check that floor pixels give h near 0 at every distance. The fix is to find the true "up" direction first. HHA estimates it from the depth map by looking for the many floor and wall normals that line up (Chapter 4).
A histogram of all pixel heights is already a layout fingerprint: a big floor peak, a table peak, a ceiling peak. Build one below.
Chapter 4
Turn one depth channel into disparity, height and angle, so the network does not have to learn geometry
A network fed raw depth must learn "this pixel is floor" from about 255 images per class. HHA (Gupta, Girshick, Arbeláez and Malik, 2014) computes the geometry first: Horizontal disparity, Height above ground and Angle with gravity. It scales each channel to 0 to 255, so the result looks like a colour image.
Two viewpoints a distance b apart (the baseline) see a point shifted sideways. Call that shift the disparity d, in pixels. With focal length f and depth Z:
d equals f times b, divided by capital Z.
Multiply f by the baseline b. Divide by the depth Z. Near points shift a lot, far points shift a little. So the disparity channel is bright near the camera and dark far away.
With Kinect-like numbers (illustrative): f = 580 px, b = 0.075 m, so f · b = 43.5. At Z = 1 m, d = 43.5 px. At Z = 4 m, d = 10.9 px. Four times farther, a quarter of the shift.
The second channel is the height h from Chapter 3. For the third, fit a tiny plane to each pixel's neighbouring 3D points. Call the unit arrow sticking straight out of that plane the surface normal n. Call the unit arrow pointing up, against gravity, g. Call the angle between them θ.
Theta equals the arc cosine of n dot g.
Take the dot product of the normal n and the up arrow g. That is the cosine of the angle. Undo the cosine to get theta. Floors give 0°, walls 90°, ceilings 180°.
Write arrows as (sideways, up, forward), so g = (0, 1, 0). A floor has n = (0, 1, 0): n · g = 1, θ = 0°. A wall has n = (1, 0, 0): n · g = 0, θ = 90°. A ramp with n = (0, 0.94, 0.34) gives n · g = 0.94 and θ = about 20°.
Pick a channel. Then tap any pixel to read its depth, disparity, height and angle.
Illustrative room, ray-cast at 64 by 48 pixels. Camera 1.2 m up and level; ceiling 2.6 m; back wall 5.5 m away; two side walls and a table. The HHA view puts disparity, height and angle in the red, green and blue channels.
In the height view the table and the floor separate at once. In the angle view the table's top and front split; raw depth shows one smooth blob.
The symptom: the angle channel looks like static, because normals from single pixels amplify depth noise. Measure the spread of θ on a flat floor patch. The fix: fit each normal over several pixels, and fill holes first (Chapter 7).
Chapter 5
Give colour and depth their own networks, and see that the gain comes from different mistakes
Colour reads materials and objects. Depth reads shape and size. Their pixels have different statistics: depth has edges and smooth slopes, and no texture. So most RGB-D classifiers use a two-stream network. One encoder reads colour, a second reads depth (often as HHA), and a fusion step joins them.
Follow the shapes. The colour image (3, 224, 224) goes through the colour encoder, which averages it down to a descriptor, a vector (512,). The HHA image (3, 224, 224) goes through the depth encoder to its own (512,). Fusion combines the two, and a final layer outputs K probabilities.
The usual move is to start the depth encoder from a network pretrained on colour images, then fine-tune it. Song, Herranz and Jiang (AAAI 2017) found a better way on NYU Depth v2 and SUN RGB-D. They trained a smaller depth network from scratch: first on small patches, then on whole images. Its depth features scored higher than fine-tuned colour features, and added more to the colour stream.
Their explanation: the colour network's first layers detect textures and fine patterns that HHA images do not have. Fine-tuning with little data barely changes those first layers. The lesson is general: pretraining helps when its data looks like yours.
Take 100 test images. The colour stream is right on 70. Suppose depth is right on 20 of the 30 that colour gets wrong: the size-decided ones. A perfect fusion could then reach 70 + 20 = 90. If depth failed only where colour fails, the ceiling would stay at 70.
Each square is one test image. Set each stream's accuracy and how much their mistakes overlap.
Illustrative counts. Overlap 0 means the mistakes are independent; overlap 1 means depth is right only where colour is right too. "Best fusion" counts images at least one stream gets right, an upper bound.
With independent mistakes, both streams are wrong on 0.30 × 0.35 = 0.105 of the images, so the ceiling is 89.5 percent. Slide the overlap to 1 and the depth stream adds nothing.
The symptom: the two-stream model scores no higher than colour alone. To measure it, count the test images where depth is right and colour is wrong. If that set is tiny, the streams learned the same cues. The fix: give the depth stream its own input form (HHA) and its own training, not copied colour weights.
Chapter 6
Choose where the two streams meet, then fuse two votes by adding log-probabilities
The streams must meet somewhere. Where they meet is the fusion strategy. There are three main choices.
| Strategy | Where they meet | Good when | Risk |
|---|---|---|---|
| Early | At the input: stack colour and depth, one network | The channels align well and low-level mixes matter | Shared first filters suit neither sensor |
| Late | At the end: two full networks, two votes | Little data; you want it simple and robust | Misses cues that need both at once |
| Attention | In the middle: each stream's features attend to the other's | Much data; joint cues matter | Many weights; overfits small sets |
Late fusion is the first thing to try. Each stream keeps its own best training, and a dead sensor leaves the other vote standing. How should two votes combine? Call the colour stream's probability for room type k prgb(k) and the depth stream's pdep(k). Call the fused score s(k).
s of k equals log of p-rgb of k, plus log of p-dep of k.
Take the log of each stream's probability for room type k and add them. Do this for every type. The type with the largest s wins. Adding logs is the same as multiplying probabilities, so a type wins only if both streams find it plausible.
With made-up numbers for (bedroom, hotel, kitchen): prgb = (0.50, 0.45, 0.05) and pdep = (0.20, 0.70, 0.10). Bedroom: −0.693 + (−1.609) = −2.303. Hotel: −0.799 + (−0.357) = −1.155. Kitchen: −2.996 + (−2.303) = −5.298. Hotel wins. Rescaled to sum to 1, the fused vote is (0.24, 0.75, 0.01).
Why multiply and not average? If the streams err independently, multiplying is how probability combines two pieces of evidence. It also lets one stream veto a type. Colour (kitchen 0.70, bedroom 0.25, hotel 0.05) and depth (kitchen 0.02, bedroom 0.48, hotel 0.50) average to a near three-way tie. The product picks bedroom at 0.75.
Set how sure each stream is of "hotel". Compare the average with the log sum. The weight tilts the log sum toward depth.
Illustrative votes. Colour keeps kitchen at 0.05 and depth keeps it at 0.10; bedroom takes the rest. The weighted rule is 2(1 − w) log prgb + 2w log pdep, rescaled to sum to 1. At w = 0.5 it is the plain sum.
The symptom: the fused model is wrong with 99 percent confidence. One stream gave a near-zero probability to the true type and vetoed it. To measure it, list test images where the true type got under 1 percent from either stream. The fix: clip each probability to a small floor, such as 0.01, or learn the weight w on held-out data.
Chapter 7
Mark missing pixels as missing, and see why depth error grows with the square of distance
A real depth map has gaps. Glass, mirrors, shiny metal and black screens send back no usable signal. Surfaces past the sensor's range do the same. The sensor writes a 0 in those pixels. That 0 means no measurement, not "a surface at 0 m". Call these pixels holes.
Where a value exists, it is noisier far away. Many depth sensors find depth from a disparity, as in Chapter 4: Z = f · b / d. Call the sensor's error in disparity σd, in pixels. Call the resulting error in depth σZ. Differentiate Z with respect to d and replace d with f · b / Z:
Sigma-Z equals capital Z squared times sigma-d, divided by f times b.
Square the depth Z. Multiply by the disparity error sigma-d. Divide by f times b. Far points have a small disparity, so the same pixel error moves them much more.
With illustrative Kinect-like numbers: f · b = 43.5 and σd = 0.1 px. At Z = 1 m, σZ = 1 × 0.1 / 43.5 = 2.3 mm. At Z = 4 m, σZ = 16 × 0.1 / 43.5 = 3.7 cm. Four times farther, sixteen times the error.
The Kinect's useful range is about 0.5 m to 5.5 m, with accuracy falling with distance (Song, Herranz and Jiang, 2018). So the far wall of a big room is the least trustworthy part of the map. Yet it is the cue that tells a hall from a bedroom.
Pink is the true depth across one image row: a near side wall, a table, a back wall with a window. Blue dots are measurements. Add noise and holes, then choose how to treat the holes.
Illustrative scan line with f · b = 43.5. The window always returns holes. "Mask and fill" draws a straight line between the nearest valid neighbours; real pipelines use smarter fills.
Repair comes before geometry. NYU Depth v2 ships depth maps with holes filled by the colorization method of Levin and colleagues. The colour image guides which neighbours to borrow from. A learned depth-completion network does the same job with more skill.
The symptom: the height channel shows a patch at 1.2 m, the camera's own height, right where the window is. A hole read as Z = 0 gives h = c − 0 = c. To measure it, count pixels with Z = 0 before you compute anything. The fix: keep a validity mask, fill the holes, and pass the mask to the network as an extra input.
Chapter 8
Drive a toy two-stream classifier, watch depth settle the tie, then corrupt the depth
Here is the whole pipeline as a toy. The colour stream reads three object cues: a bed, cooking surfaces, a desk. The depth stream reads two size cues: room scale and ceiling height. Each stream turns its cues into five probabilities. Late fusion adds their logs.
Start from a preset. Switch depth on and off. Then raise the depth noise and watch the gain melt.
Colour cues
Depth cues
Illustrative toy: each stream is a hand-set linear score per room type, then a softmax. Noise adds random scores to the depth stream, scaled by the noise slider.
Three things to try:
The pattern to take away: depth helps exactly where size decides the room, and only as much as the depth can be trusted. A kitchen barely moves when depth turns on, because the colour stream already sees the stove.
The symptom: a robot's RGB-D model beats colour alone in the lab and loses to it in a glass-walled office. To measure it, score the model on test images grouped by their hole rate. The fix: give the fusion a per-image trust signal, such as the share of valid depth pixels, and lower the depth weight when it drops.
Chapter 9
Score each class equally, then report where depth helped and where it did not
RGB-D datasets are unbalanced: many bedrooms, few bathrooms. Overall accuracy, the share of all test images that are right, lets the big classes hide a broken small one. So RGB-D papers on SUN RGB-D report mean class accuracy: the accuracy within each class, averaged with equal weight.
Call the number of room types K. For class k, call the number of test images nk and the number classified right ck. Call the mean class accuracy mA.
m-A equals one over capital K, times the sum over k of c-k over n-k.
For each room type k, divide the right answers c-k by the images n-k. Add these K accuracies. Divide by K. A type with 10 images counts as much as a type with 600.
With made-up numbers: bedroom 54 of 60, kitchen 24 of 30, bathroom 2 of 10. Overall: 80 / 100 = 80%. Per class: 90%, 80%, 20%. mA = (0.90 + 0.80 + 0.20) / 3 = 63.3%. The overall number hides a broken bathroom class.
Each bar's width is the number of test images in that class; its height is the class accuracy. Drag the sliders and watch the two lines part.
Illustrative counts. The kitchen stays at 24 of 30.
The second report is the per-class depth gain: colour-only accuracy, RGB-D accuracy, and their difference, one row per room type. Expect large gains where size decides the type and near zero where objects decide it. Then compare the two confusion matrices: the bedroom and hotel cells should shrink.
| Room type | Colour only | RGB-D | Gain |
|---|---|---|---|
| Hotel room | 41% | 68% | +27 |
| Bedroom | 62% | 74% | +12 |
| Kitchen | 88% | 89% | +1 |
| Bathroom | 85% | 83% | −2 |
Illustrative numbers, not from any paper. The shape is typical: a negative row can appear where noisy depth (mirrors, glass shower doors) misleads.
The symptom: a paper reports "+5 points from depth", and the robot still confuses bathrooms. To measure it, rebuild the per-class gain table. The fix: report mean class accuracy, the per-class gain and both confusion matrices, never one aggregate number alone.
Chapter 10
Keep the terms, the numbers, the code and the next steps in one place
An RGB-D scene classifier reads a registered colour image and depth map. Depth turns pixels into metres, so you can compute each pixel's height and surface angle. HHA packs disparity, height and angle into an image. Two streams read colour and HHA; late fusion adds their log-probabilities. Holes are missing values, depth error grows with Z2, and the honest score is mean class accuracy with a per-class gain.
| Term | Rule | Number to remember |
|---|---|---|
| Back-projection | X = Z · u / f | u 200, Z 3 m, f 500: 1.2 m |
| Height | h = c − Z · v / f | c 1.2, v 90, Z 2.5: 0.75 m |
| Disparity | d = f · b / Z | f · b 43.5: 43.5 px at 1 m, 10.9 px at 4 m |
| Angle | θ = arccos(n · g) | floor 0°, wall 90°, ceiling 180° |
| Late fusion | s(k) = log prgb(k) + log pdep(k) | (0.50, 0.45) and (0.20, 0.70): hotel 0.75 |
| Depth noise | σZ = Z2 σd / (f b) | 2.3 mm at 1 m, 3.7 cm at 4 m |
| Mean class accuracy | mA = (1/K) Σ ck / nk | 80% overall can be 63.3% mA |
| SUN RGB-D | 10,335 pairs, 4 sensors | 19 classes, 4,845 / 4,659 |
| NYU Depth v2 | 1,449 pairs, Kinect | 10 classes, 795 / 654 |
python import numpy as np def hha(Z, f, cx, cy, cam_h, fb=43.5): # Z: (H, W) depth in metres, 0 = hole. Level camera assumed. H, W = Z.shape valid = Z > 0 u = np.arange(W)[None, :] - cx; v = np.arange(H)[:, None] - cy X, Y = Z * u / f, Z * v / f # back-project (Y points down) disp = np.where(valid, fb / np.maximum(Z, 1e-6), 0) # d = f b / Z height = cam_h - Y # h = c - Z v / f P = np.stack([X, -Y, Z], -1) # (sideways, up, forward) n = np.cross(np.gradient(P, axis=1), np.gradient(P, axis=0)) n /= np.linalg.norm(n, axis=-1, keepdims=True) + 1e-9 n = np.where((n * P).sum(-1, keepdims=True) > 0, -n, n) # point normals toward the camera angle = np.degrees(np.arccos(np.clip(n[..., 1], -1, 1))) # theta = arccos(n . up) return np.stack([disp, height, angle], 0), valid # (3, H, W) plus mask def late_fuse(p_rgb, p_dep, floor=0.01): s = np.log(np.maximum(p_rgb, floor)) + np.log(np.maximum(p_dep, floor)) e = np.exp(s - s.max()); return e / e.sum() def mean_class_accuracy(pred, label, K): accs = [(pred[label == k] == k).mean() for k in range(K) if (label == k).any()] return float(np.mean(accs))
Sources: N. Silberman, D. Hoiem, P. Kohli and R. Fergus, "Indoor Segmentation and Support Inference from RGBD Images", ECCV 2012 (NYU Depth v2). S. Song, S. Lichtenberg and J. Xiao, "SUN RGB-D: A RGB-D Scene Understanding Benchmark Suite", CVPR 2015 (SUN RGB-D). S. Gupta, R. Girshick, P. Arbeláez and J. Malik, "Learning Rich Features from RGB-D Images for Object Detection and Segmentation", ECCV 2014 (arXiv). X. Song, L. Herranz and S. Jiang, "Depth CNNs for RGB-D Scene Recognition: Learning from Scratch Better than Transferring from RGB-CNNs", AAAI 2017 (arXiv), and "Learning Effective RGB-D Representations for Scene Recognition", IEEE TIP 2018 (arXiv).
Now press Present or Teach and explain from memory: back-projection, height, HHA, two streams, the log sum, the Z2 noise and mean class accuracy. Then go back to the room classifier, pick colour only, and say why the hotel room fails.