Monocular Depth Estimation
Module: Computer Vision | Difficulty: Advanced
Depth Prediction
Scale-Invariant Loss
where .
Self-Supervised Photometric Loss
where is the warped pixel using predicted depth and pose.
Depth Completion
Evaluation Metrics
import torch
import torch.nn as nn
class DepthNet(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Sequential(
nn.Conv2d(3, 64, 7, 2, 3), nn.BatchNorm2d(64), nn.ReLU(True),
nn.Conv2d(64, 128, 3, 2, 1), nn.BatchNorm2d(128), nn.ReLU(True),
nn.Conv2d(128, 256, 3, 2, 1), nn.BatchNorm2d(256), nn.ReLU(True),
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(256, 128, 4, 2, 1), nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4, 2, 1), nn.ReLU(True),
nn.ConvTranspose2d(64, 1, 4, 2, 1), nn.Softplus(),
)
def forward(self, x):
return self.decoder(self.encoder(x)).squeeze(1)
def scale_invariant_loss(pred, target):
diff = torch.log(pred) - torch.log(target)
return torch.mean(diff**2) - 0.5 * torch.mean(diff)**2
Key Takeaways
- Self-supervised depth uses photometric consistency as supervision
- Scale-invariant loss handles scale ambiguity
- Depth estimation enables 3D scene understanding from single images