diff --git a/bias/undergraduate/.gitignore b/bias/undergraduate/.gitignore new file mode 100644 index 00000000..c89dc294 --- /dev/null +++ b/bias/undergraduate/.gitignore @@ -0,0 +1,246 @@ +## Core latex/pdflatex auxiliary files: +*.aux +*.lof +*.log +*.lot +*.fls +*.out +*.toc +*.fmt +*.fot +*.cb +*.cb2 +.*.lb +clean.bat +elegant*-cn.pdf +elegant*-en.pdf +*.dates + +## Intermediate documents: +*.dvi +*.xdv +*-converted-to.* +# these rules might exclude image files for figures etc. +# *.ps +# *.eps +# *.pdf + +## Generated if empty string is given at "Please type another file name for output:" +.pdf + +## Bibliography auxiliary files (bibtex/biblatex/biber): +*.bbl +*.bcf +*.blg +*-blx.aux +*-blx.bib +*.run.xml + +## Build tool auxiliary files: +*.fdb_latexmk +*.synctex +*.synctex(busy) +*.synctex.gz +*.synctex.gz(busy) +*.pdfsync + +## Auxiliary and intermediate files from other packages: +# algorithms +*.alg +*.loa + +# achemso +acs-*.bib + +# amsthm +*.thm + +# beamer +*.nav +*.pre +*.snm +*.vrb + +# changes +*.soc + +# cprotect +*.cpt + +# elsarticle (documentclass of Elsevier journals) +*.spl + +# endnotes +*.ent + +# fixme +*.lox + +# feynmf/feynmp +*.mf +*.mp +*.t[1-9] +*.t[1-9][0-9] +*.tfm + +#(r)(e)ledmac/(r)(e)ledpar +*.end +*.?end +*.[1-9] +*.[1-9][0-9] +*.[1-9][0-9][0-9] +*.[1-9]R +*.[1-9][0-9]R +*.[1-9][0-9][0-9]R +*.eledsec[1-9] +*.eledsec[1-9]R +*.eledsec[1-9][0-9] +*.eledsec[1-9][0-9]R +*.eledsec[1-9][0-9][0-9] +*.eledsec[1-9][0-9][0-9]R + +# glossaries +*.acn +*.acr +*.glg +*.glo +*.gls +*.glsdefs + +# gnuplottex +*-gnuplottex-* + +# gregoriotex +*.gaux +*.gtex + +# htlatex +*.4ct +*.4tc +*.idv +*.lg +*.trc +*.xref + +# hyperref +*.brf + +# knitr +*-concordance.tex +# TODO Comment the next line if you want to keep your tikz graphics files +*.tikz +*-tikzDictionary + +# listings +*.lol + +# makeidx +*.idx +*.ilg +*.ind +*.ist + +# minitoc +*.maf +*.mlf +*.mlt +*.mtc[0-9]* +*.slf[0-9]* +*.slt[0-9]* +*.stc[0-9]* + +# minted +_minted* +*.pyg + +# morewrites +*.mw + +# nomencl +*.nlg +*.nlo +*.nls + +# pax +*.pax + +# pdfpcnotes +*.pdfpc + +# sagetex +*.sagetex.sage +*.sagetex.py +*.sagetex.scmd + +# scrwfile +*.wrt + +# sympy +*.sout +*.sympy +sympy-plots-for-*.tex/ + +# pdfcomment +*.upa +*.upb + +# pythontex +*.pytxcode +pythontex-files-*/ + +# thmtools +*.loe + +# TikZ & PGF +*.dpth +*.md5 +*.auxlock + +# todonotes +*.tdo + +# easy-todo +*.lod + +# xmpincl +*.xmpi + +# xindy +*.xdy + +# xypic precompiled matrices +*.xyc + +# endfloat +*.ttt +*.fff + +# Latexian +TSWLatexianTemp* + +## Editors: +# WinEdt +*.bak +*.sav + +# Texpad +.texpadtmp + +# Kile +*.backup + +# KBibTeX +*~[0-9]* + +# auto folder when using emacs and auctex +./auto/* +*.el + +# expex forward references with \gathertags +*-tags.tex + +# standalone packages +*.sta + +# generated if using elsarticle.cls +*.spl diff --git a/bias/undergraduate/README.md b/bias/undergraduate/README.md new file mode 100644 index 00000000..74029dba --- /dev/null +++ b/bias/undergraduate/README.md @@ -0,0 +1,38 @@ +# 本科毕业设计模板 + +湖南工商大学本科毕业设计模板。 + +# 使用 +推荐使用 [texstudio](https://pan.baidu.com/s/1Is2-VR1z-tMYvmdinsVY_g?pwd=hutb) 或 [overleaf](https://cn.overleaf.com/) 进行论文撰写。 + +注:在 latex 2023 中编译成功,latex 2016 编译失败(需要更高的版本,其他版本的 latex 没试过)。 + +# 贡献 +有任何对模板格式进行调整的可以提交 [Issues](https://github.com/OpenHUTB/undergraduate/issues) 或 [Pull Request](https://github.com/OpenHUTB/undergraduate/pulls) 。 + +# 选题 +[选题列表](https://github.com/OpenHUTB/undergraduate/wiki/%E6%AF%95%E8%AE%BE%E9%80%89%E9%A2%98) + + +## 要求 +1. 格式化、注释、文档翻译 +2. 运行 +3. 测试(例子通过) + + +## 初始化 + +1. 新建github工程 + +2. 运行`init_proj('PROJECT_NAME');` + +3. 分配开发者 + + +## 使用 +克隆仓库 +```shell +git clone --recursive https://github.com/OpenHUTB/undergraduate.git +``` + +# 贡献者 diff --git a/bias/undergraduate/cmdel.bat b/bias/undergraduate/cmdel.bat new file mode 100644 index 00000000..c9642e0a --- /dev/null +++ b/bias/undergraduate/cmdel.bat @@ -0,0 +1,2 @@ +del /a /f *.xml *.bcf *.out *.log *.sav *.gz *.aux *.bbl *.bak *.dvi *.blg + diff --git a/bias/undergraduate/content/abstracten.tex b/bias/undergraduate/content/abstracten.tex new file mode 100644 index 00000000..bb96d2d5 --- /dev/null +++ b/bias/undergraduate/content/abstracten.tex @@ -0,0 +1,11 @@ +%!TEX root = ../csuthesis_main.tex +\keywordsen{Deep learning \ \ Virtual simulation \ \ Model training \ \ Model evaluation} +\begin{abstracten} + + As deep learning technology advances rapidly, its application in complex environments is expanding. However, traditional model training and evaluation methods face significant challenges in these scenarios. This study explores a deep learning training and evaluation method based on virtual simulation technology to enhance model performance and reliability in complex environments. The advanced Carla virtual simulation platform was chosen for its high realism and customization, providing rich training resources and diverse testing scenarios for deep learning models. + This paper reveals the potential of virtual simulation technology in deep learning training and evaluation, offering reference experience for future research. Despite the limitation that differences between virtual and real environments may affect model generalization, the findings provide valuable references for future exploration. Future research directions may focus on developing training strategies that better integrate virtual and real environments to enhance the practical application of deep learning models. + + + + +\end{abstracten} \ No newline at end of file diff --git a/bias/undergraduate/content/abstractzh.tex b/bias/undergraduate/content/abstractzh.tex new file mode 100644 index 00000000..a5eb17df --- /dev/null +++ b/bias/undergraduate/content/abstractzh.tex @@ -0,0 +1,13 @@ +%!TEX root = ../csuthesis_main.tex +% 设置中文摘要 +\keywordscn{深度学习\quad 虚拟仿真\quad 模型训练\quad 模型评测} +%\categorycn{TP391} +\begin{abstractzh} + + + 随着深度学习技术的飞速进步,其在复杂环境下的应用范围不断扩大。然而,传统的模型训练与评测手段在处理这些复杂场景时遭遇了重重困难。本研究致力于探讨一种基于虚拟仿真技术的深度学习训练与评测方法,旨在提高模型在复杂环境下的表现和信赖度。本研究选取了Carla这一先进的虚拟仿真平台,借助其高度真实感和定制化能力,为深度学习模型提供了丰富的训练资源和多样化的测试场景。 + 本文揭示了虚拟仿真技术在深度学习训练与评测领域的应用潜力,并为后续研究提供参考经验。尽管存在虚拟环境与实际环境差异可能对模型泛化力造成影响的局限性,但本研究的发现仍为未来的探索提供了重要的参考。未来的研究方向可能包括深化虚拟与现实结合的训练策略,以进一步增强深度学习模型在现实世界中的应用效果。 + + + +\end{abstractzh} \ No newline at end of file diff --git a/bias/undergraduate/content/acknowledgements.tex b/bias/undergraduate/content/acknowledgements.tex new file mode 100644 index 00000000..5b7a3d87 --- /dev/null +++ b/bias/undergraduate/content/acknowledgements.tex @@ -0,0 +1,18 @@ +%!TEX root = ../csuthesis_main.tex +\begin{acknowledgements} + +感谢制作出中南大学本科学位论文 LaTeX 模板的edwardzcn。 + +感谢制作出中南大学博士学位论文 LaTeX 模板的郭大侠@CSGrandeur。 + +感谢添加本科学位论文样式支持的@BlurryLight。 + +感谢帮助重构项目并进行测试的@burst-bao以及为独立使用LaTeX进行毕业论文写作提供宝贵经验的16级的姜析阅学长。 + +感谢 CTeX-kit 提供了 LaTeX 的中文支持。 + +感谢上海交通大学学位论文 LaTeX 模板的维护者们 @sjtug 和清华大学学位论文 LaTeX 模板的维护者们 @tuna 给予的宝贵设计经验。 + +感谢所有为模板贡献过代码的同学们! + +\end{acknowledgements} diff --git a/bias/undergraduate/content/appendix.tex b/bias/undergraduate/content/appendix.tex new file mode 100644 index 00000000..d952b542 --- /dev/null +++ b/bias/undergraduate/content/appendix.tex @@ -0,0 +1,109 @@ +%!TEX root = ../csuthesis_main.tex +% \begin{appendixs} % 无章节编号 +\chapter{附录代码} + +附录部分用于存放这里用来存放不适合放置在正文的大篇幅内容、典型如代码、图纸、完整数学证明过程等内容。 + +\section{堆溢出检测算法} + +\begin{algorithm}[h] + \caption{堆溢出检测算法}\label{alg:ovf} + \begin{algorithmic}[1] + \IF {$\beta \in \mathbb{N^{*}} \land \Delta_\beta = \Delta_{\beta - 1} \land \beta < S$} + \STATE 正常写入 + \ELSIF {$\beta \in \mathbb{N^{*}} \land \Delta_\beta \neq \Delta_{\beta - 1} \land \beta \geq S$} + \STATE 发生堆溢出 + \ENDIF + \end{algorithmic} +\end{algorithm} + +\section{KMP算法C++描述} + +% \begin{minted}[linenos]{c} +\begin{lstlisting} + const int maxn=2e5+5; + int nt[maxn]; + int aa[maxn],bb[maxn]; + int a[maxn],b[maxn]; + int n; + //参数为模板串和next数组 + //字符串均从下标0开始 + void kmpGetNext(int *s,int *Next) + { + Next[0]=0; + // int len=strlen(s); + for(int i=1,j=0;i>n) + { + memset(a,0,sizeof(a)); + memset(b,0,sizeof(b)); + rep(i,0,n) cin>>aa[i]; + rep(i,0,n) cin>>bb[i]; + sort(aa,aa+n); + sort(bb,bb+n); + rep(i,0,n-1){ + a[i]=aa[i+1]-aa[i]; + b[i]=bb[i+1]-bb[i]; + } + a[n-1]=360000+aa[0]-aa[n-1]; + // rep(i,0,n) cout<= episodes: + break + + return calculate_safety_scores(metrics) + +# 模型量化 +def quantize_model(model): + model.qconfig = torch.ao.quantization.get_default_qconfig('fbgemm') + torch.ao.quantization.prepare(model, inplace=True) + torch.ao.quantization.convert(model, inplace=True) + return model + +# 控制信号平滑 +class ControlFilter: + def __init__(self, alpha=0.8): + self.prev_control = None + self.alpha = alpha + + def smooth(self, current_control): + if self.prev_control is None: + self.prev_control = current_control + return current_control + + smoothed = self.alpha * self.prev_control + (1 - self.alpha) * current_control + self.prev_control = smoothed + return smoothed + +# 导出模型 +def export_model(model, output_path): + model.eval() + traced_model = torch.jit.trace(model, torch.randn(1, 3, 600, 800)) + traced_model.save(output_path) + +# 加载模型 +def load_deployed_model(model_path): + model = AutonomousDriver() + model.load_state_dict(torch.load(model_path)) + return model + +# 自动驾驶主循环 +def autonomous_driving_loop(world): + model = load_deployed_model('deployed_model.pt') + vehicle = spawn_vehicle(world) + control_filter = ControlFilter() + + while True: + # 获取相机图像 + image = get_camera_image(world, vehicle) + preprocessed = preprocess_image(image) + + # 模型推理 + with torch.no_grad(): + control = model(preprocessed) + + # 控制信号平滑 + smoothed_control = control_filter.smooth(control) + + # 执行控制 + vehicle.apply_control(smoothed_control) + + # 安全监控 + if detect_critical_situation(vehicle): + trigger_emergency_stop(vehicle) + +# 辅助函数:获取相机图像 +def get_camera_image(world, vehicle): + # 这里需要根据你的传感器设置获取最新的相机图像 + # 示例代码: + # for actor in world.get_actors(): + # if actor.type_id == 'sensor.camera.rgb' and actor.parent.id == vehicle.id: + # return actor.queue.get() + pass + +# 辅助函数:预处理图像 +def preprocess_image(image): + # 图像预处理逻辑 + img_array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8")) + img_array = np.reshape(img_array, (image.height, image.width, 4)) + img_array = img_array[:, :, :3] # 仅保留RGB通道 + img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) + img_array = cv2.resize(img_array, (800, 600)) + return torch.from_numpy(img_array.transpose(2, 0, 1).astype(np.float32) / 255.0).unsqueeze(0) + +# 辅助函数:检测紧急情况 +def detect_critical_situation(vehicle): + # 紧急情况检测逻辑 + # 示例:检测车辆是否静止超过一定时间 + velocity = vehicle.get_velocity() + if velocity.x == 0 and velocity.y == 0 and velocity.z == 0: + return True + return False + +# 辅助函数:触发紧急停止 +def trigger_emergency_stop(vehicle): + # 紧急停止逻辑 + control = carla.VehicleControl() + control.brake = 1.0 + control.hand_brake = True + vehicle.apply_control(control) + +# 辅助函数:计算安全分数 +def calculate_safety_scores(metrics): + # 安全分数计算逻辑 + total_score = 0 + total_score -= metrics['collision_rate'] * 10 + total_score += metrics['route_completion'] * 5 + total_score -= metrics['traffic_violations'] * 3 + total_score += metrics['control_smoothness'] * 2 + return total_score + +if __name__ == '__main__': + # 启动Carla服务器 + server_thread = Thread(target=launch_carla_server) + server_thread.start() + time.sleep(5) # 等待服务器启动 + + # 连接Carla客户端 + world = connect_carla() + + # 加载YOLOv5模型 + model = load_yolov5_model() + + # 训练模型 + dataset = DrivingDataset('expert_data') + dataloader = DataLoader(dataset, batch_size=32, shuffle=True) + autodrive_model = AutonomousDriver() + train_model(autodrive_model, dataloader, epochs=50) + + # 导出模型 + export_model(autodrive_model, 'deployed_model.pt') + + # 量化模型 + quantized_model = quantize_model(autodrive_model) + export_model(quantized_model, 'quantized_model.pt') + + # 启动自动驾驶 + autonomous_driving_loop(world) + + # 评估模型 + metrics = evaluate_model(autodrive_model, world) + print(f'Model Safety Score: {metrics}') \ No newline at end of file diff --git a/bias/undergraduate/reference.bib b/bias/undergraduate/reference.bib new file mode 100644 index 00000000..5fa2d85c --- /dev/null +++ b/bias/undergraduate/reference.bib @@ -0,0 +1,32 @@ +@book{knuth1984texbook, + title={The texbook}, + author={Knuth, Donald Ervin and Bibby, Duane}, + volume={15}, + year={1984}, + publisher={Addison-Wesley Reading} +} +@book{lamport1994latex, + title={LATEX: a document preparation system: user's guide and reference manual}, + author={Lamport, Leslie}, + year={1994}, + publisher={Addison-wesley} +} +@article{pritchard1969statistical, + title={Statistical bibliography or bibliometrics}, + author={Pritchard, Alan and others}, + journal={Journal of documentation}, + volume={25}, + number={4}, + pages={348--349}, + year={1969}, + publisher={New York} +} +@article{Pawlak1982Rough, + title={Rough sets}, + author={Pawlak, Zdzisław}, + journal={International Journal of Computer and Information Sciences}, + volume={11}, + number={5}, + pages={341-356}, + year={1982}, +} \ No newline at end of file diff --git a/bias/undergraduate/training_code.py b/bias/undergraduate/training_code.py new file mode 100644 index 00000000..f9893046 --- /dev/null +++ b/bias/undergraduate/training_code.py @@ -0,0 +1,456 @@ +import carla +import numpy as np +import cv2 +import random +from collections import deque +import tensorflow as tf +from tensorflow.keras.models import Sequential +from tensorflow.keras.layers import Dense, Conv2D, Flatten +from tensorflow.keras.optimizers import Adam +import os +import threading +from queue import Queue +class CarlaSyncMode(object): + def __init__(self, world, *sensors, **kwargs): + self.world = world + self.sensors = sensors + self.frame = None + self._queues = [] + self._settings = None + + def __enter__(self): + self._settings = self.world.get_settings() + self.frame = self.world.apply_settings(carla.WorldSettings( + no_rendering_mode=False, + synchronous_mode=True, + fixed_delta_seconds=0.05)) + def make_queue(register_event): + q = deque() + register_event(q.append) + self._queues.append(q) + make_queue(self.world.on_tick) + for sensor in self.sensors: + make_queue(sensor.listen) + return self + + def tick(self, timeout): + self.frame = self.world.wait_for_tick(self.frame + 1, timeout) + + def __exit__(self, *args, **kwargs): + self.world.apply_settings(self._settings) + for q in self._queues: + q.clear() +# 初始化CARLA客户端 +def init_carla_client(): + client = carla.Client('localhost', 2000) + client.set_timeout(10.0) + return client +def main(): + client = init_carla_client() + world = setup_world(client) + bp_lib = get_blueprint_library(world) + vehicle = spawn_vehicle(world, bp_lib) + camera = spawn_rgb_camera(world, bp_lib, vehicle) + semantic_camera = spawn_semantic_camera(world, bp_lib, vehicle) + lidar = spawn_lidar(world, bp_lib, vehicle) + create_output_dir() + state_size, action_size, batch_size, episodes = setup_experiment_parameters() + agent = initialize_training(state_size, action_size) + actions = get_action_space() + try: + with CarlaSyncMode(world, camera, semantic_camera, lidar) as sync_mode: + spawn_points = world.get_map().get_spawn_points() + for e in range(episodes): + spawn_point = random.choice(spawn_points) + vehicle.set_transform(spawn_point) + sync_mode.tick(3.0) + image = sync_mode._queues[1][-1] + state = np.array(image.raw_data).reshape(200, 200, 3) + state = np.expand_dims(state, axis=0) + total_reward = 0 + done = False + steps = 0 + while not done and steps < 1000: + action_idx = agent.act(state) + action = actions[action_idx] + vehicle.apply_control(carla.VehicleControl( + steer=action[0], + throttle=action[1], + brake=action[2] + )) + sync_mode.tick(3.0) + next_image = sync_mode._queues[1][-1] + next_state = np.array(next_image.raw_data).reshape(200, 200, 3) + next_state = np.expand_dims(next_state, axis=0) + reward = calculate_reward(vehicle, action) + total_reward += reward + steps += 1 + done = steps >= 1000 + agent.remember(state, action_idx, reward, next_state, done) + state = next_state + if len(agent.memory) > batch_size: + agent.replay(batch_size) + print(f"Episode: {e}, Total Reward: {total_reward}, Steps: {steps}") + finally: + save_model(agent) + cleanup(vehicle, camera, semantic_camera, lidar) +# 加载地图并设置天气 +def setup_world(client, map_name='Town01'): + world = client.load_world(map_name) + weather = carla.WeatherParameters.ClearNoon + world.set_weather(weather) + return world + +# 获取蓝图库 +def get_blueprint_library(world): + return world.get_blueprint_library() + +# 添加车辆 +def spawn_vehicle(world, bp_lib, vehicle_name='vehicle.tesla.model3'): + vehicle_bp = bp_lib.find(vehicle_name) + spawn_points = world.get_map().get_spawn_points() + spawn_point = random.choice(spawn_points) + vehicle = world.spawn_actor(vehicle_bp, spawn_point) + vehicle.set_autopilot(False) + return vehicle + +# 添加RGB相机 +def spawn_rgb_camera(world, bp_lib, vehicle): + camera_bp = bp_lib.find('sensor.camera.rgb') + camera_bp.set_attribute('image_size_x', '200') + camera_bp.set_attribute('image_size_y', '200') + camera_bp.set_attribute('fov', '110') + camera_location = carla.Location(2, 0, 1) + camera_rotation = carla.Rotation(0, 0, 0) + camera_transform = carla.Transform(camera_location, camera_rotation) + camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle) + return camera + +# 添加语义分割相机 +def spawn_semantic_camera(world, bp_lib, vehicle): + semantic_camera_bp = bp_lib.find('sensor.camera.semantic_segmentation') + semantic_camera_bp.set_attribute('image_size_x', '200') + semantic_camera_bp.set_attribute('image_size_y', '200') + semantic_camera_bp.set_attribute('fov', '110') + semantic_camera_location = carla.Location(2, 0, 1) + semantic_camera_rotation = carla.Rotation(0, 0, 0) + semantic_camera_transform = carla.Transform(semantic_camera_location, semantic_camera_rotation) + semantic_camera = world.spawn_actor(semantic_camera_bp, semantic_camera_transform, attach_to=vehicle) + return semantic_camera + +# 添加激光雷达 +def spawn_lidar(world, bp_lib, vehicle): + lidar_bp = bp_lib.find('sensor.lidar.ray_cast') + lidar_bp.set_attribute('channels', '32') + lidar_bp.set_attribute('range', '5000') + lidar_location = carla.Location(0, 0, 2.5) + lidar_rotation = carla.Rotation(0, 0, 0) + lidar_transform = carla.Transform(lidar_location, lidar_rotation) + lidar = world.spawn_actor(lidar_bp, lidar_transform, attach_to=vehicle) + return lidar + +# 经验回放缓冲区 +class ReplayBuffer: + def __init__(self, capacity): + self.buffer = deque(maxlen=capacity) + + def add(self, experience): + self.buffer.append(experience) + + def sample(self, batch_size): + return random.sample(self.buffer, batch_size) + + def __len__(self): + return len(self.buffer) + +# DQN模型构建 +def build_model(input_shape, action_size): + model = Sequential() + model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape)) + model.add(Flatten()) + model.add(Dense(64, activation='relu')) + model.add(Dense(action_size, activation='linear')) + model.compile(loss='mse', optimizer=Adam(learning_rate=0.001)) + return model + +# PERDQN代理类 +class PERDQNAgent: + def __init__(self, state_size, action_size): + self.state_size = state_size + self.action_size = action_size + self.memory = ReplayBuffer(2000) + self.gamma = 0.95 + self.epsilon = 1.0 + self.epsilon_min = 0.01 + self.epsilon_decay = 0.995 + self.model = build_model(state_size, action_size) + + def act(self, state): + if np.random.rand() <= self.epsilon: + return random.randrange(self.action_size) + act_values = self.model.predict(state) + return np.argmax(act_values[0]) + + def remember(self, state, action, reward, next_state, done): + self.memory.add((state, action, reward, next_state, done)) + + def replay(self, batch_size): + minibatch = self.memory.sample(batch_size) + for state, action, reward, next_state, done in minibatch: + target = reward + if not done: + target = reward + self.gamma * np.amax(self.model.predict(next_state)[0]) + target_f = self.model.predict(state) + target_f[0][action] = target + self.model.fit(state, target_f, epochs=1, verbose=0) + if self.epsilon > self.epsilon_min: + self.epsilon *= self.epsilon_decay + +# 创建输出目录 +def create_output_dir(): + if not os.path.exists('output'): + os.makedirs('output') + +# 定义奖励函数 +def calculate_reward(vehicle, action): + velocity = vehicle.get_velocity() + speed = np.linalg.norm([velocity.x, velocity.y, velocity.z]) + transform = vehicle.get_transform() + lane_offset = transform.location.y + reward = 0.0 + if abs(lane_offset) > 2.0: + reward -= 10.0 + else: + reward += 5.0 + if speed < 5 or speed > 15: + reward -= 2.0 + else: + reward += 1.0 + if transform.location.x > 100.0: + reward += 10.0 + if abs(transform.rotation.yaw) > 15.0: + reward -= 1.0 + return reward + +# 设置实验参数 +def setup_experiment_parameters(): + state_size = (200, 200, 3) + action_size = 5 + batch_size = 32 + episodes = 300 + return state_size, action_size, batch_size, episodes + +# 初始化训练过程 +def initialize_training(state_size, action_size): + agent = PERDQNAgent(state_size, action_size) + return agent + +# 定义动作空间 +def get_action_space(): + steer_space = [-0.5, 0.0, 0.5] + throttle_space = [0.3, 0.6] + brake_space = [0.0, 0.3] + actions = [] + action_idx = agent.act(state) + action = actions[action_idx] + for steer in steer_space: + for throttle in throttle_space: + for brake in brake_space: + actions.append((steer, throttle, brake)) + return actions +def spawn_collision_sensor(world, bp_lib, vehicle): + collision_bp = bp_lib.find('sensor.other.collision') + collision_location = carla.Location(0, 0, 0) + collision_rotation = carla.Rotation(0, 0, 0) + collision_transform = carla.Transform(collision_location, collision_rotation) + collision_sensor = world.spawn_actor(collision_bp, collision_transform, attach_to=vehicle) + return collision_sensor + +def main(): + client = init_carla_client() + world = setup_world(client) + bp_lib = get_blueprint_library(world) + vehicle = spawn_vehicle(world, bp_lib) + camera = spawn_rgb_camera(world, bp_lib, vehicle) + semantic_camera = spawn_semantic_camera(world, bp_lib, vehicle) + lidar = spawn_lidar(world, bp_lib, vehicle) + collision_sensor = spawn_collision_sensor(world, bp_lib, vehicle) + create_output_dir() + state_size, action_size, batch_size, episodes = setup_experiment_parameters() + agent = initialize_training(state_size, action_size) + actions = get_action_space() + try: + with CarlaSyncMode(world, camera, semantic_camera, lidar, collision_sensor) as sync_mode: + spawn_points = world.get_map().get_spawn_points() + for e in range(episodes): + spawn_point = random.choice(spawn_points) + vehicle.set_transform(spawn_point) + sync_mode.tick(3.0) + image = sync_mode._queues[1][-1] + state = np.array(image.raw_data).reshape(200, 200, 3) + state = np.expand_dims(state, axis=0) + total_reward = 0 + done = False + steps = 0 + while not done and steps < 1000: + action_idx = agent.act(state) + action = actions[action_idx] + vehicle.apply_control(carla.VehicleControl( + steer=action[0], + throttle=action[1], + brake=action[2] + )) + sync_mode.tick(3.0) + next_image = sync_mode._queues[1][-1] + next_state = np.array(next_image.raw_data).reshape(200, 200, 3) + next_state = np.expand_dims(next_state, axis=0) + reward = calculate_reward(vehicle, action) + total_reward += reward + steps += 1 + # 检查碰撞 + if len(sync_mode._queues[4]) > 0: + done = True + agent.remember(state, action_idx, reward, next_state, done) + state = next_state + if len(agent.memory) > batch_size: + agent.replay(batch_size) + print(f"Episode: {e}, Total Reward: {total_reward}, Steps: {steps}") + finally: + save_model(agent) + cleanup(vehicle, camera, semantic_camera, lidar, collision_sensor) +# 训练循环 +def training_loop(world, vehicle, camera, semantic_camera, lidar, agent, actions, batch_size, episodes): + spawn_points = world.get_map().get_spawn_points() + max_steps_per_episode = 1000 + for e in range(episodes): + spawn_point = random.choice(spawn_points) + vehicle.set_transform(spawn_point) + image = camera.get() + image = sync_mode._queues[1][-1] # 假设第二个传感器是RGB相机 + state = np.array(image.raw_data).reshape(200, 200, 3) + state = np.expand_dims(state, axis=0) + total_reward = 0 + done = False + steps = 0 + while not done and steps < max_steps_per_episode: + action_idx = agent.act(state) + action = actions[action_idx] + vehicle.apply_control(carla.VehicleControl( + steer=action[0], + throttle=action[1], + brake=action[2] + )) + next_image = camera.get() + next_state = np.array(next_image.raw_data).reshape(200, 200, 3) + next_state = np.expand_dims(next_state, axis=0) + reward = calculate_reward(vehicle, action) + total_reward += reward + steps += 1 + if steps >= max_steps_per_episode: + done = True + agent.remember(state, action_idx, reward, next_state, done) + state = next_state + if len(agent.memory) > batch_size: + agent.replay(batch_size) + print(f"Episode: {e}, Total Reward: {total_reward}, Steps: {steps}") + +# 保存模型 +def save_model(agent, model_path='perdqn_model.h5'): + agent.model.save(model_path) + +# 清理环境 +def cleanup(vehicle, camera, semantic_camera, lidar, collision_sensor): + vehicle.destroy() + camera.destroy() + semantic_camera.destroy() + lidar.destroy() + collision_sensor.destroy() + +# 同步模式辅助类 +class CarlaSyncMode(object): + def __init__(self, world, *sensors, **kwargs): + self.world = world + self.sensors = sensors + self.frame = None + self._queues = [] + self._settings = None + + def __enter__(self): + self._settings = self.world.get_settings() + self.frame = self.world.apply_settings(carla.WorldSettings( + no_rendering_mode=False, + synchronous_mode=True, + fixed_delta_seconds=0.05)) + def make_queue(register_event): + q = deque() + register_event(q.append) + self._queues.append(q) + make_queue(self.world.on_tick) + for sensor in self.sensors: + make_queue(sensor.listen) + return self + + def tick(self, timeout): + self.frame = self.world.wait_for_tick(self.frame + 1, timeout) + + def __exit__(self, *args, **kwargs): + self.world.apply_settings(self._settings) + for q in self._queues: + q.clear() + +# 主函数 +def main(): + client = init_carla_client() + world = setup_world(client) + bp_lib = get_blueprint_library(world) + vehicle = spawn_vehicle(world, bp_lib) + camera = spawn_rgb_camera(world, bp_lib, vehicle) + semantic_camera = spawn_semantic_camera(world, bp_lib, vehicle) + lidar = spawn_lidar(world, bp_lib, vehicle) + create_output_dir() + state_size, action_size, batch_size, episodes = setup_experiment_parameters() + agent = initialize_training(state_size, action_size) + actions = get_action_space() + try: + with CarlaSyncMode(world, camera, semantic_camera, lidar) as sync_mode: + spawn_points = world.get_map().get_spawn_points() + for e in range(episodes): + spawn_point = random.choice(spawn_points) + vehicle.set_transform(spawn_point) + sync_mode.tick(3.0) + image = sync_mode._queues[1][-1] + state = np.array(image.raw_data).reshape(200, 200, 3) + state = np.expand_dims(state, axis=0) + total_reward = 0 + done = False + steps = 0 + while not done and steps < 1000: + action_idx = agent.act(state) + action = actions[action_idx] + vehicle.apply_control(carla.VehicleControl( + steer=action[0], + throttle=action[1], + brake=action[2] + )) + sync_mode.tick(3.0) + next_image = sync_mode._queues[1][-1] + next_state = np.array(next_image.raw_data).reshape(200, 200, 3) + next_state = np.expand_dims(next_state, axis=0) + reward = calculate_reward(vehicle, action) + total_reward += reward + steps += 1 + done = steps >= 1000 + agent.remember(state, action_idx, reward, next_state, done) + state = next_state + if len(agent.memory) > batch_size: + agent.replay(batch_size) + print(f"Episode: {e}, Total Reward: {total_reward}, Steps: {steps}") + finally: + save_model(agent) + cleanup(vehicle, camera, semantic_camera, lidar) + +if __name__ == '__main__': + main() +# 检查碰撞 +if len(sync_mode._queues[4]) > 0: # 假设第五个传感器是碰撞传感器 + done = True