Skip to content

Commit c63dad3

Browse files
authored
CARLA场景下2D/3D目标检测与跟踪脚本 (#3258)
* 小车跟踪 * 提交作业:2D-CARLA-Tracking项目完成 | 集成YOLO检测+SORT跟踪+CARLA可视化
1 parent aa8402b commit c63dad3

File tree

2 files changed

+440
-0
lines changed

2 files changed

+440
-0
lines changed

src/tracking_car/README.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
```markdown
2+
# 基于CARLA的智能代理系统
3+
4+
## 项目概述
5+
6+
本项目旨在利用神经网络技术,实现CARLA模拟器中车辆和行人的全栈智能代理,涵盖感知、规划与控制三大核心模块。通过深度学习算法赋予虚拟智能体环境理解、决策规划和动态控制能力,同时包含具身人仿真、机械臂控制等扩展功能,构建多智能体协同的仿真系统。
7+
8+
## 环境配置
9+
10+
* **支持平台**:Windows 10/11,Ubuntu 20.04/22.04
11+
* **核心软件**:
12+
* Python 3.7-3.12(需兼容3.7版本)
13+
* PyTorch(优先采用,不依赖TensorFlow)
14+
* CARLA 0.9.11+(推荐0.9.13/0.9.15版本)
15+
* **依赖安装**:
16+
```bash
17+
# 基础依赖
18+
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
19+
20+
# CARLA客户端安装(需替换为对应版本)
21+
pip install carla==0.9.15
22+
23+
# 文档生成工具
24+
pip install mkdocs
25+
```
26+
27+
## 文档生成
28+
29+
1. 安装文档工具链:
30+
```bash
31+
pip install mkdocs -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
32+
pip install -r requirements.txt
33+
```
34+
35+
2. 构建并预览文档:
36+
```bash
37+
# 进入项目根目录
38+
cd nn
39+
40+
# 构建静态文档
41+
mkdocs build
42+
43+
# 启动本地文档服务
44+
mkdocs serve
45+
```
46+
47+
3. 浏览器访问 [http://127.0.0.1:8000](http://127.0.0.1:8000) 查看文档。
48+
49+
## 核心功能模块
50+
51+
1. **车辆智能代理**
52+
* **感知系统**:基于CNN的目标检测(车辆、行人、交通信号灯)、车道线识别
53+
* **规划系统**:全局路径规划(A*、RRT*算法)、局部避障
54+
* **控制系统**:强化学习车辆控制(油门、刹车、转向)、PID参数自适应调优
55+
* **手动控制**:支持键盘操作的车辆交互模式(WASD控制方向,空格/左Shift控制升降)
56+
57+
2. **具身人仿真**
58+
* **感知模块**:william开发的具身人环境感知系统(`humanoid_perception`
59+
* **运动模拟**:基于Mujoco的物理引擎实现具身人运动控制(`Mujoco_manrun`
60+
61+
3. **神经网络模型**
62+
* **CNN模型**:图像识别与目标检测(`chap05_CNN`),包含完整训练流程(Adam优化器、交叉熵损失)
63+
* **RNN模型**:序列数据处理与生成(`chap06_RNN`),基于LSTM实现唐诗生成等文本任务
64+
* **FNN模型**:基础神经网络结构(`chap04_simple_neural_network`),包含测试评估函数
65+
66+
4. **辅助系统**
67+
* **车道辅助**:Active-Lane-Keeping-Assistant实现车道偏离预警与辅助
68+
* **机械臂测试**:humantest模块提供机械臂力控仿真与交互功能
69+
70+
## 快速启动
71+
72+
1. 启动CARLA服务器:
73+
```bash
74+
# Linux
75+
./CarlaUE4.sh
76+
77+
# Windows
78+
CarlaUE4.exe
79+
```
80+
81+
2. 运行示例场景:
82+
```bash
83+
# 自动驾驶车辆(强化学习)
84+
python src/driverless_car/main.py
85+
86+
# 车辆手动控制
87+
python src/manual_control/main.py
88+
89+
# CNN模型训练
90+
python src/chap05_CNN/CNN_pytorch.py
91+
92+
# RNN文本生成
93+
python src/chap06_RNN/tangshi_for_pytorch/rnn.py
94+
```
95+
96+
## 关键代码说明
97+
98+
### 神经网络训练框架
99+
```python
100+
# CNN训练示例(chap05_CNN/CNN_pytorch.py)
101+
def train(cnn):
102+
optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate)
103+
loss_func = nn.CrossEntropyLoss()
104+
105+
for epoch in range(max_epoch):
106+
for step, (x_, y_) in enumerate(train_loader):
107+
x, y = Variable(x_), Variable(y_)
108+
output = cnn(x)
109+
loss = loss_func(output, y)
110+
optimizer.zero_grad(set_to_none=True)
111+
loss.backward()
112+
optimizer.step()
113+
114+
if step != 0 and step % 20 == 0:
115+
print(f"测试准确率: {test(cnn)}")
116+
```
117+
118+
### 强化学习智能体
119+
```python
120+
# 车辆强化学习(driverless_car/main.py)
121+
def main():
122+
env = DroneEnv()
123+
agent = Agent()
124+
episodes = 1000
125+
epsilon = 1.0 # 探索率
126+
127+
for episode in range(episodes):
128+
state = env.reset()
129+
total_reward = 0
130+
131+
while True:
132+
action = agent.get_action(state, epsilon) # 根据状态和探索率获取动作
133+
next_state, reward, done = env.step(action)
134+
agent.remember(state, action, reward, next_state, done) # 存储经验
135+
agent.train(batch_size=32) # 训练模型
136+
137+
if done:
138+
epsilon = max(0.01, epsilon * 0.995) # 衰减探索率
139+
break
140+
```
141+
142+
### RNN模型结构
143+
```python
144+
# 唐诗生成RNN(chap06_RNN/tangshi_for_pytorch/rnn.py)
145+
class RNN_model(nn.Module):
146+
def __init__(self, batch_sz, vocab_len, word_embedding, embedding_dim, lstm_hidden_dim):
147+
super(RNN_model, self).__init__()
148+
self.word_embedding_lookup = word_embedding
149+
self.rnn_lstm = nn.LSTM(input_size=embedding_dim,
150+
hidden_size=lstm_hidden_dim,
151+
num_layers=2,
152+
batch_first=False)
153+
self.fc = nn.Linear(lstm_hidden_dim, vocab_len)
154+
self.softmax = nn.LogSoftmax(dim=1)
155+
156+
def forward(self, sentence, is_test=False):
157+
batch_input = self.word_embedding_lookup(sentence).view(1, -1, self.word_embedding_dim)
158+
h0 = torch.zeros(2, batch_input.size(1), self.lstm_dim)
159+
c0 = torch.zeros(2, batch_input.size(1), self.lstm_dim)
160+
161+
output, (hn, cn) = self.rnn_lstm(batch_input, (h0, c0))
162+
out = self.fc(output.contiguous().view(-1, self.lstm_dim))
163+
return self.softmax(out)
164+
```
165+
166+
## 贡献指南
167+
168+
请在提交代码前阅读 [贡献指南](https://github.com/OpenHUTB/.github/blob/master/CONTRIBUTING.md),代码优化方向包括:
169+
170+
* 遵循 [PEP 8 代码风格](https://peps.pythonlang.cn/pep-0008/) 并完善注释
171+
* 实现神经网络在CARLA场景中的端到端应用
172+
* 撰写模块功能说明与API文档
173+
* 添加自动化测试(模型性能、场景稳定性、数据一致性)
174+
* 优化感知-规划-控制链路的实时性
175+
176+
## 参考资源
177+
178+
* [CARLA官方文档](https://carla.readthedocs.io/)
179+
* [PyTorch神经网络教程](https://pytorch.org/tutorials/)
180+
* [项目文档中心](https://openhutb.github.io/nn/)
181+
* [代理模拟器文档](https://openhutb.github.io/carla_doc/)
182+
* [神经网络原理](https://github.com/OpenHUTB/neuro)

0 commit comments

Comments
 (0)