bevfusion_distillation
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (7.2%) to scientific vocabulary
Repository
Basic Info
- Host: GitHub
- Owner: Fahad16301139
- License: apache-2.0
- Language: Python
- Default Branch: main
- Size: 5.18 MB
Statistics
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
BEVFusion Distillation Training Workflow
When you run the training script (finalworkingdistillation.py), here's the execution flow:
1. Initialization Phase
```python
First, the main function is called
def main(): # Parse arguments and setup
# Create the distillation trainer
trainer = FinalDistillationTrainer(
teacher_config_path=args.teacher_config,
student_config_path=args.student_config,
teacher_checkpoint=args.teacher_checkpoint
)
```
2. Trainer Initialization
```python
Inside FinalDistillationTrainer.init
INTEGRATION POINT 1: BEVFusion models are loaded and wrapped
self.teacherwrapper, self.studentwrapper, self.distillcriterion = buildbevfusiondistillation( teacherconfigpath=teacherconfigpath, studentconfigpath=studentconfigpath, teachercheckpoint=teachercheckpoint, ndata=1000, device=self.device ) ```
3. Model Building (in buildbevfusiondistillation)
```python
Load configs and build BEVFusion models
teacherconfig = Config.fromfile(teacherconfigpath) studentconfig = Config.fromfile(studentconfigpath)
INTEGRATION POINT 2: BEVFusion teacher model created
teachermodel = MODELS.build(teacherconfig.model)
Load teacher checkpoint and freeze weights
INTEGRATION POINT 3: BEVFusion student model created
studentmodel = MODELS.build(studentconfig.model)
INTEGRATION POINT 4: BEVFusion models are wrapped for feature extraction
teacherwrapper = BEVFusionDistillationWrapper(teachermodel, isstudent=False) studentwrapper = BEVFusionDistillationWrapper(studentmodel, isstudent=True)
INTEGRATION POINT 5: Create distillation loss for BEVFusion features
distillcriterion = BEVFusionDistillationLoss( studentchannels=studentchannels, teacherchannels=teacher_channels, # CRD parameters... ) ```
4. Training Loop
```python
Loop through dataset and epochs
for epoch in range(startepoch, numepochs): for batchidx, databatch in enumerate(dataloader): # Process each batch with trainstep losses = trainer.trainstep(data_batch) ```
5. Training Step (Single Batch)
```python
Inside train_step method of FinalDistillationTrainer
def trainstep(self, databatch): # Validate and fix data format databatch = self.validateandfixdata(data_batch)
# INTEGRATION POINT 6: Get features from BEVFusion teacher model
with torch.no_grad():
teacher_loss, teacher_features = self.teacher_wrapper(batch_inputs_dict, batch_data_samples, mode='loss')
# INTEGRATION POINT 7: Get features from BEVFusion student model
student_loss, student_features = self.student_wrapper(batch_inputs_dict, batch_data_samples, mode='loss')
# Calculate task loss from student model's output
task_loss = sum(v for k, v in student_loss.items() if 'loss' in k)
# INTEGRATION POINT 8: Compute CRD distillation loss using BEVFusion features
indices = torch.randint(0, 1000, (batch_size,), device=self.device)
distill_losses = self.distill_criterion(student_features, teacher_features, indices)
distill_loss = distill_losses.get('total_distill', torch.tensor(0.0, device=self.device))
# Combine losses and backpropagate
total_loss = task_loss + alpha_distill * distill_loss
total_loss.backward()
# Update student model parameters
self.optimizer.step()
```
6. Under the Hood: Feature Extraction
During both teacherwrapper and studentwrapper forward passes:
```python
In BEVFusionDistillationWrapper.forward
The underlying BEVFusion model is called
if mode == 'loss': result = self.model.loss(batchinputsdict, batchdatasamples) else: result = self.model.predict(batchinputsdict, batchdatasamples)
Meanwhile, registered hooks capture features during the forward pass
def getactivation_hook(self, name): def hook(module, input, output): # Store output features in self.features dictionary self.features[name] = output ```
7. Under the Hood: Distillation Loss Computation
When the distill_criterion is called:
```python
In BEVFusionDistillationLoss.forward
for layername in studentfeatures.keys(): if layername in teacherfeatures and layername in self.crdlosses: # Get features for this layer from both models sfeat = studentfeatures[layername] # From student BEVFusion tfeat = teacherfeatures[layername] # From teacher BEVFusion
# INTEGRATION POINT 9: Call CRD loss for this specific layer's features
crd_loss = self.crd_losses[layer_name](s_feat, t_feat, indices)
weighted_loss = self.alpha_crd * crd_loss
losses[f'{layer_name}_crd'] = weighted_loss
total_loss += weighted_loss
```
8. Under the Hood: CRD Loss Computation
Inside each CRDLoss module's forward call:
```python
In CRDLoss.forward
Project features to embedding space
fs = self.embeds(fs) # Student features ft = self.embedt(ft) # Teacher features
Compute contrastive loss using memory bank
outs, outt = self.contrast(fs, ft, idx) sloss = self.criterions(outs) tloss = self.criteriont(outt) loss = sloss + tloss ```
9. Model Saving & Evaluation
```python
After certain epochs or batches
if epoch % saveinterval == 0: # Save model checkpoint ckptpath = os.path.join(workdir, f'epoch{epoch}.pth') save_checkpoint(...)
# Optionally evaluate model
# ...
```
This workflow shows how BEVFusion models are wrapped, how features are extracted during forward passes, and how these features are fed into the CRD distillation framework to calculate the distillation loss that guides the student's training.
I'll teach you about knowledge distillation and CRD (Contrastive Representation Distillation) from the basics, then explain how it's been adapted for BEVFusion. Let me start by exploring the original RepDistiller framework. Read file: RepDistiller-master/README.md Read file: RepDistiller-master/distiller_zoo/CRD.py Ran tool Ran tool Read file: RepDistiller-master/crd Read file: RepDistiller-master/crd/criterion.py Read file: RepDistiller-master/crd/memory.py Now let me create a comprehensive tutorial explaining the concepts and adaptations: Ran tool
📚 Complete Guide: Knowledge Distillation & CRD for BEVFusion
Let me teach you from the basics to the advanced BEVFusion adaptation!
🔰 1. Knowledge Distillation Basics
What is Knowledge Distillation? - Transfer knowledge from a large, accurate teacher model to a small, efficient student model - The student learns to mimic the teacher's behavior - Goal: Get small model performance close to large model performance
Traditional KD Process:
1. Teacher Model: Large, pre-trained, high accuracy
2. Student Model: Small, lightweight, needs training
3. Loss Function: Compare teacher and student outputs
4. Training: Student learns from both ground truth AND teacher
🎯 2. CRD (Contrastive Representation Distillation) - Advanced Technique
Read file: bevfusion_distillation.py CRD Key Innovation: Instead of just comparing final outputs, CRD uses contrastive learning on intermediate features:
🧠 CRD Core Concepts:
- Contrastive Learning: Learn by comparing positive and negative examples
- Memory Bank: Store feature representations from previous training samples
- Negative Sampling: For each student-teacher feature pair, sample many negative examples
- Mutual Information: Maximize agreement between student and teacher features
🔍 How CRD Works:
```python
Original RepDistiller CRD (simplified)
class CRDLoss(nn.Module):
def init(self, opt):
self.embeds = Embed(opt.sdim, opt.featdim) # Student projection
self.embedt = Embed(opt.tdim, opt.featdim) # Teacher projection
self.contrast = ContrastMemory(...) # Memory bank
def forward(self, f_s, f_t, idx):
f_s = self.embed_s(f_s) # Project student features
f_t = self.embed_t(f_t) # Project teacher features
out_s, out_t = self.contrast(f_s, f_t, idx) # Contrastive scores
loss = contrastive_loss(out_s) + contrastive_loss(out_t)
return loss
```
🚗 3. BEVFusion Adaptation - Key Changes
the major adaptations needed for BEVFusion: Read file: bevfusion_distillation.py
🔧 Major Adaptations for BEVFusion:
A. Model Architecture Differences
Original RepDistiller (for 2D image classification): ```python
Simple CNN features - single tensor output
teachermodel = ResNet32x4() studentmodel = ResNet8x4() features = model(images) # [batch, channels, height, width] ```
BEVFusion Adaptation (for 3D object detection): ```python
Complex 3D detection pipeline with multiple modules
teachermodel = BEVFusion(largeconfig) # Multi-modal: camera + lidar studentmodel = BEVFusion(lightweightconfig) # Smaller version
Features from different modules:
features = {
'ptsbackbone': [...], # LiDAR backbone features
'ptsneck': [...], # Neck fusion features ← Main focus
'bbox_head': [...] # Detection head features
}
```
B. Feature Extraction - Hook System
The biggest challenge was extracting intermediate features from BEVFusion:
python
class BEVFusionDistillationWrapper(nn.Module):
def _register_hooks(self):
"""Register forward hooks ONLY for specified features"""
if 'pts_neck' in self.extract_features:
self.model.pts_neck.register_forward_hook(
self._get_activation_hook('pts_neck')
)
Why hooks? BEVFusion's complex architecture doesn't easily expose intermediate features, so we "hook" into the forward pass to capture them.
C. Multi-Scale Feature Handling
Original CRD: Simple 2D features
python
def forward(self, x):
x = x.view(x.shape[0], -1) # Flatten to [batch, features]
x = self.linear(x)
return x
BEVFusion Adaptation: Handle complex 3D shapes
python
def forward(self, x):
if len(x.shape) == 4: # [B, C, H, W] - 2D features
x = F.adaptive_avg_pool2d(x, (1, 1))
x = x.view(x.size(0), -1)
elif len(x.shape) == 5: # [B, C, D, H, W] - 3D features
x = F.adaptive_avg_pool3d(x, (1, 1, 1))
x = x.view(x.size(0), -1)
elif len(x.shape) == 3: # [B, N, C] - Point cloud features
x = x.mean(dim=1)
D. Layer-wise CRD Application
Key Innovation: Apply CRD to multiple layers simultaneously
```python class BEVFusionDistillationLoss(nn.Module): def init(self, studentchannels, teacherchannels, ...): self.crd_losses = nn.ModuleDict()
# Create separate CRD loss for each layer
for layer_name in student_channels.keys():
if layer_name in teacher_channels:
opt = CRDOpt(
s_dim=student_channels[layer_name], # e.g., 256
t_dim=teacher_channels[layer_name], # e.g., 512
feat_dim=128 # Projection dimension
)
self.crd_losses[layer_name] = CRDLoss(opt)
```
E. Memory and Device Management
Major Issue: BEVFusion uses much more GPU memory than simple CNNs
Solution: Added extensive memory management: ```python def forward(self, v1, v2, y, idx=None): try: # Clear cache before memory operations if torch.cuda.isavailable(): torch.cuda.emptycache()
# ... CRD computation ...
# Synchronize before/after operations
if torch.cuda.is_available():
torch.cuda.synchronize()
except Exception as e:
# Clear cache on error
if torch.cuda.is_available():
torch.cuda.empty_cache()
```
🔄 4. Complete Distillation Pipeline
Ran tool
🎯 5. Key Differences Summary
| Aspect | Original RepDistiller | BEVFusion Adaptation | |--------|----------------------|---------------------| | Domain | 2D Image Classification | 3D Object Detection | | Models | Simple CNNs (ResNet) | Complex Multi-modal (BEVFusion) | | Features | Single tensor [B,C,H,W] | Multiple modules with complex outputs | | Channels | Teacher: 128, Student: 64 | Teacher: 512, Student: 256 | | Memory | Basic GPU usage | Extensive memory management | | Hooks | Direct feature access | Forward hooks for intermediate features | | Validation | Simple prediction format | Complex 3D detection validation |
🚀 6. Why This Adaptation Works
- Focused Distillation: Using only
pts_neckfeatures (most informative) - Proper Scaling: Handles different feature dimensions (512→256)
- Memory Efficient: Careful GPU memory management
- Robust Hooks: Safe feature extraction from complex models
- Layer-wise Learning: Each layer learns independently
💡 7. The Magic of CRD for BEVFusion
Traditional KD: "Make student output similar to teacher output" CRD: "Make student features similar to teacher features using contrastive learning"
This is especially powerful for 3D detection because:
- Rich Feature Learning: 3D spatial features are complex
- Better Generalization: Contrastive learning improves feature quality
- Multi-scale Knowledge: Can distill different layers simultaneously
- Robust Training: Memory bank provides stable negative examples
The key insight is that for complex tasks like 3D detection, intermediate feature alignment (CRD) is more effective than just output alignment (traditional KD).
Owner
- Name: Fahad Hossain
- Login: Fahad16301139
- Kind: user
- Repositories: 1
- Profile: https://github.com/Fahad16301139
CSE graduate
Citation (CITATION.cff)
cff-version: 1.2.0 message: "If you use this software, please cite it as below." authors: - name: "MMDetection3D Contributors" title: "OpenMMLab's Next-generation Platform for General 3D Object Detection" date-released: 2020-07-23 url: "https://github.com/open-mmlab/mmdetection3d" license: Apache-2.0
GitHub Events
Total
- Push event: 1
- Create event: 1
Last Year
- Push event: 1
- Create event: 1
Dependencies
- actions/checkout v2 composite
- actions/setup-python v2 composite
- actions/checkout v2 composite
- actions/setup-python v2 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- codecov/codecov-action v1.0.14 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- codecov/codecov-action v1.0.14 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- pytorch/pytorch ${PYTORCH}-cuda${CUDA}-cudnn${CUDNN}-devel build
- pytorch/pytorch ${PYTORCH}-cuda${CUDA}-cudnn${CUDNN}-devel build
- pytorch/pytorch ${PYTORCH}-cuda${CUDA}-cudnn${CUDNN}-devel build
- docutils ==0.16.0
- markdown >=3.4.0
- myst-parser *
- sphinx ==4.0.2
- sphinx-tabs *
- sphinx_copybutton *
- sphinx_markdown_tables >=0.0.16
- tabulate *
- urllib3 <2.0.0
- mmcv >=2.0.0rc4,<2.2.0
- mmdet >=3.0.0,<3.3.0
- mmengine >=0.7.1,<1.0.0
- black ==20.8b1
- typing-extensions *
- waymo-open-dataset-tf-2-6-0 *
- mmcv >=2.0.0rc4
- mmdet >=3.0.0
- mmengine >=0.7.1
- torch *
- torchvision *
- lyft_dataset_sdk *
- networkx >=2.5
- numba *
- numpy *
- nuscenes-devkit *
- open3d *
- plyfile *
- scikit-image *
- tensorboard *
- trimesh *
- codecov * test
- flake8 * test
- interrogate * test
- isort * test
- kwarray * test
- parameterized * test
- pytest * test
- pytest-cov * test
- pytest-runner * test
- ubelt * test
- xdoctest >=0.10.0 test
- yapf * test