Git怎么管理软件版本(代码,模型,配置等) - 教程

下面我将用一个真实的智能驾驶项目案例,详细展示如何使用Git管理代码、模型和配置文件。

项目背景:交通信号灯识别系统

项目名称: TLR-System (Traffic Light Recognition System)
项目结构:

.code/:存放源代码

.models/:存放机器学习模型文件

.configs/:存放配置文件

.data/: 存放数据(注意:大文件不直接放在Git中,而是用其他方式管理)

我们将使用Git作为版本控制工具,并结合Git LFS(大文件存储)来管理模型等二进制文件。

```
tlr-system/
├── code/
│   ├── perception/
│   │   ├── traffic_light_detector.py
│   │   └── traffic_light_classifier.py
│   ├── planning/
│   │   └── behavior_planner.py
│   └── common/
│       └── config_loader.py
├── models/
│   ├── yolov5_traffic_light.pt
│   └── efficientnet_classifier.pth
├── configs/
│   ├── camera_params.yaml
│   ├── model_config.json
│   └── system_config.yaml
├── docker/
│   └── Dockerfile
└── scripts/
    └── train_model.py
```

---

步骤:

1初始化Git仓库,并设置Git LFS

2 创建.gitigmore文件,忽略不需要版本控制的文件(如临时文件、本地配置文件等)

3 使用Git Flow分支模型进行开发。

4 使用标签(Tag)来标记版本。

实战演示:从开发到发布的完整流程

1. 初始化和基础设置

# 初始化Git仓库并设置Git LFS(用于管理大文件)
git init tlr-system
cd tlr-system
# 设置Git LFS跟踪大文件(.pt #PyTorch 模型;.pth #Keras模型;.bin #二进制模型文件 .onnx #模型)
git lfs install
git lfs track "*.pt" "*.pth" "*.pkl" "*.onnx"
git lfs track "models/**"
git lfs track "data/processed/**"
# 添加.gitattributes文件到版本控制
git add .gitattributes

2. 创建.gitignore文件

例如

#临时文件
*.tmp
*.log
#本地配置(不要上传敏感信息)
configs/local.yaml
#数据目录(因为数据太大,用其它方式管理)
data/

3. 功能开发:添加新的检测模型

场景: 开发新的YOLOv5交通灯检测模型来替代原有模型。

# 从develop分支创建功能分支
git checkout develop
git checkout -b feature/yolov5-detector
# 开发过程中的提交示例

3.1 代码文件提交 (code/perception/traffic_light_detector.py):

# 新开发的YOLOv5检测器
class YOLOv5TrafficLightDetector:
    def __init__(self, model_path, config):
        self.model = load_yolov5_model(model_path)
        self.confidence_threshold = config['detection_confidence']
    def detect(self, image):
        # 新的检测逻辑
        detections = self.model(image)
        return self._filter_detections(detections)

# 提交代码
git add code/perception/traffic_light_detector.py
git commit -m "feat: add YOLOv5 traffic light detector implementation"

3.2 模型文件提交 (models/yolov5_traffic_light.pt):

# 训练好的模型文件(85MB)- 由Git LFS管理
git add models/yolov5_traffic_light.pt
git commit -m "feat: add trained YOLOv5 model weights"

3.3 配置文件提交 (configs/model_config.json):

```json
{
  "detection_model": {
    "type": "yolov5",
    "path": "models/yolov5_traffic_light.pt",
    "confidence_threshold": 0.7,
    "input_size": [640, 640]
  },
  "classification_model": {
    "type": "efficientnet",
    "path": "models/efficientnet_classifier.pth"
  }
}

```bash

git add configs/model_config.json
git commit -m "config: update model config for YOLOv5 integration"

3.4 Docker环境配置 (docker/Dockerfile):

```dockerfile

FROM nvcr.io/nvidia/pytorch:21.08-py3

# 复制代码和模型
COPY code/ /app/code/
COPY models/ /app/models/
COPY configs/ /app/configs/

# 安装特定版本的YOLOv5
RUN pip install torch==1.9.0 torchvision==0.10.0
RUN pip install opencv-python pyyaml tensorboard

WORKDIR /app
```

```bash

git add docker/Dockerfile
git commit -m "docker: update environment for YOLOv5 dependencies"

4. 完成功能开发并合并

# 推送到远程仓库
git push origin feature/yolov5-detector
# 在GitLab/GitHub上创建Merge Request到develop分支
# 经过代码审查和CI测试后合并

CI流水线 (.gitlab-ci.yml) 自动执行:

```yaml

stages:
  - test
  - build
  - integration

test_model:
  stage: test
  script:
    - python -m pytest tests/test_detector.py -v
    - python scripts/validate_model.py models/yolov5_traffic_light.pt

build_docker:
  stage: build
  script:
    - docker build -t tlr-system:feature-yolov5 -f docker/Dockerfile .

integration_test:
  stage: integration
  script:
    - python scripts/run_simulation.py --config configs/model_config.json

5. 发布准备阶段

# 从develop创建发布分支
git checkout develop
git checkout -b release/v2.1.0

在release分支上进行最后调整:

5.1. 更新版本号 (configs/system_config.yaml):

```yaml
system:
  version: "2.1.0"
  release_date: "2024-03-20"
  model_version: "yolov5-v3"

performance:
  detection_fps: 30
  accuracy: 0.94
  inference_time: 33.2

```

5.2. 修复发布前发现的Bug:

# 发现并修复一个模型加载问题
git add code/perception/traffic_light_detector.py
git commit -m "fix: resolve model loading issue on edge devices"
# 同步修复到develop分支
git checkout develop
git cherry-pick 

6. 正式发布

# 合并到main分支并打标签
git checkout main
git merge --no-ff release/v2.1.0
git tag -a v2.1.0 -m "Release v2.1.0: YOLOv5 traffic light detection with 94% accuracy"
# 推送标签和更改
git push origin main
git push origin v2.1.0

7. 紧急热修复案例

场景: 上线后发现YOLOv5模型在特定光照条件下误检率高。

# 从main分支创建热修复分支
git checkout main
git checkout -b hotfix/improve-low-light-detection
# 快速修复:调整模型参数和后处理

更新模型配置 (configs/model_config.json):

```json
{
  "detection_model": {
    "type": "yolov5",
    "path": "models/yolov5_traffic_light.pt",
    "confidence_threshold": 0.8,  // 从0.7提高到0.8
    "input_size": [640, 640],
    "nms_threshold": 0.6,        // 新增NMS参数
    "low_light_boost": true      // 新增低光增强
  }
}
```

更新代码逻辑 (code/perception/traffic_light_detector.py):

def detect(self, image):
    # 添加低光条件检测
    if self._is_low_light(image):
        image = self._enhance_low_light(image)
    detections = self.model(image)
    return self._filter_detections(detections)
def _is_low_light(self, image):
    """检测低光照条件"""
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return np.mean(gray) < 50  # 平均像素值低于50认为是低光
# 提交热修复
git add configs/model_config.json code/perception/traffic_light_detector.py
git commit -m "fix: improve detection in low-light conditions by adjusting thresholds and adding pre-processing"
# 合并到main并打标签
git checkout main
git merge --no-ff hotfix/improve-low-light-detection
git tag -a v2.1.1 -m "Hotfix v2.1.1: Improved low-light detection performance"
# 同步到develop分支
git checkout develop
git merge --no-ff hotfix/improve-low-light-detection

版本管理效果展示

版本历史清晰可见

$ git log --oneline --graph
*   d8e4f5f (HEAD -> main, tag: v2.1.1) Merge branch 'hotfix/improve-low-light-detection'
|\
| * c3a8b2f fix: improve detection in low-light conditions
|/
*   1a2b3c4 (tag: v2.1.0) Merge branch 'release/v2.1.0'
|\
| * 8d7e6f5 fix: resolve model loading issue
| * 5f4g3h2 config: update system version to 2.1.0
|/
*   9a8b7c6 Merge branch 'feature/yolov5-detector'
|\
| * 3c2b1a9 docker: update environment for YOLOv5 dependencies
| * 2b1a9c8 config: update model config for YOLOv5 integration
| * 1a9c8d7 feat: add trained YOLOv5 model weights
| * 9c8d7e6 feat: add YOLOv5 traffic light detector implementation
|/
*   e5f4d3c (tag: v2.0.0) Previous release

精确的版本可追溯性

任何时候都可以复现任意版本:

# 复现v2.1.0版本进行问题排查
git checkout v2.1.0
docker build -t tlr-system:v2.1.0 -f docker/Dockerfile .
# 运行特定版本的仿真
python scripts/run_simulation.py --config configs/system_config.yaml

模型与代码版本一致性

每个Git标签都对应着:

· ✅ 特定版本的代码
· ✅ 特定版本的模型文件
· ✅ 特定版本的配置文件
· ✅ 特定版本的Docker环境

---

关键优势总结

1. 原子性提交: 代码、模型、配置的更改一起提交,保证一致性
2. 完整可复现: 任何版本都可以精确复现开发、测试、生产环境
3. 安全审计: 满足ISO 26262对版本可追溯性的要求
4. 高效协作: 功能分支隔离不同开发任务,release分支确保发布质量
5. 紧急响应: hotfix分支提供快速修复通道而不影响主开发线

这种管理方式确保了智能驾驶软件在复杂的开发、测试、部署流程中始终保持版本的一致性和可追溯性。

posted @ 2025-11-12 11:29  yangykaifa  阅读(24)  评论(0)    收藏  举报