112 lines
2.6 KiB
Markdown
112 lines
2.6 KiB
Markdown
|
|
# ✅ 最终修复 - 预训练模型加载问题
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🎯 问题根源
|
|||
|
|
|
|||
|
|
即使注释掉配置文件中的`init_cfg`,`bevfusion.py`的`init_weights`方法仍然会调用Swin Transformer的`init_weights`,导致尝试从GitHub下载预训练模型。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## ✅ 解决方案
|
|||
|
|
|
|||
|
|
### 修改 `bevfusion.py` 的 `init_weights` 方法
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def init_weights(self) -> None:
|
|||
|
|
# ✅ 如果从checkpoint加载,跳过预训练模型初始化
|
|||
|
|
# 只有当backbone配置了init_cfg时才初始化
|
|||
|
|
if "camera" in self.encoders:
|
|||
|
|
backbone = self.encoders["camera"]["backbone"]
|
|||
|
|
# 检查是否有init_cfg配置
|
|||
|
|
if hasattr(backbone, 'init_cfg') and backbone.init_cfg is not None:
|
|||
|
|
backbone.init_weights()
|
|||
|
|
else:
|
|||
|
|
# 没有init_cfg,说明从checkpoint加载,跳过初始化
|
|||
|
|
print("[BEVFusion] ⚪ Skipping camera backbone init_weights (will load from checkpoint)")
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**逻辑**:
|
|||
|
|
- ✅ 如果backbone有`init_cfg`配置 → 调用`init_weights()`加载预训练模型
|
|||
|
|
- ✅ 如果backbone没有`init_cfg` → 跳过初始化,等待从checkpoint加载
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📊 修复位置
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
文件: mmdet3d/models/fusion_models/bevfusion.py
|
|||
|
|
行数: 第159-169行
|
|||
|
|
修改: 添加init_cfg检查逻辑
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## ✅ 现在的行为
|
|||
|
|
|
|||
|
|
### 配置文件中 (已注释)
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
backbone:
|
|||
|
|
type: SwinTransformer
|
|||
|
|
...
|
|||
|
|
# ✅ 从checkpoint加载,无需预训练模型
|
|||
|
|
# init_cfg:
|
|||
|
|
# type: Pretrained
|
|||
|
|
# checkpoint: pretrained/swint-nuimages-pretrained.pth
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 代码中 (已修复)
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 检查init_cfg
|
|||
|
|
if hasattr(backbone, 'init_cfg') and backbone.init_cfg is not None:
|
|||
|
|
backbone.init_weights() # 有配置才初始化
|
|||
|
|
else:
|
|||
|
|
print("Skipping init_weights") # 无配置,跳过
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 启动时 (使用checkpoint)
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
--load_from epoch_5.pth
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**结果**:
|
|||
|
|
- ✅ 不会尝试加载预训练模型
|
|||
|
|
- ✅ 直接从checkpoint加载所有权重
|
|||
|
|
- ✅ task_gca随机初始化
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🚀 现在可以正常启动了!
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
docker exec -it bevfusion bash
|
|||
|
|
cd /workspace/bevfusion
|
|||
|
|
bash START_PHASE4A_TASK_GCA.sh
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## ✅ 启动后应该看到
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
[BEVFusion] ⚪ Skipping camera backbone init_weights (will load from checkpoint)
|
|||
|
|
[BEVFusion] ✨✨ Task-specific GCA mode enabled ✨✨
|
|||
|
|
[object] GCA: params: 131,072
|
|||
|
|
[map] GCA: params: 131,072
|
|||
|
|
|
|||
|
|
load checkpoint from .../epoch_5.pth
|
|||
|
|
|
|||
|
|
The following keys in model are not found in checkpoint:
|
|||
|
|
task_gca.* (正常,随机初始化)
|
|||
|
|
|
|||
|
|
Epoch [1][50/xxx] ...
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
**🎉 预训练模型加载问题已彻底解决!现在可以正常启动训练了!**
|
|||
|
|
|