diff --git a/src/Unmanned_vehicle_AD_DQN/README.md b/src/Unmanned_vehicle_AD_DQN/README.md index 98a95e219a..ffc39f14e5 100644 --- a/src/Unmanned_vehicle_AD_DQN/README.md +++ b/src/Unmanned_vehicle_AD_DQN/README.md @@ -6,10 +6,13 @@ 1. python版本3.7.16 2. 确保将carla在运行前配置到系统环境中 +3.environment.yml为conda导出的虚拟环境,可一键导入虚拟环境 +4.ROS封装环境为Ubuntu20.04-Linux(ros版本是1版,不是ros2) ### carla环境配置 -1. .whl文件的API请下载至py环境中 +1.Carla版本尽量选择适配Python3,例如0.9.12版本 +2. .whl文件的API请下载至py环境中 ## 使用步骤 @@ -19,12 +22,16 @@ 训练模型请运行: ``` + Main.py + ``` ### 测试 测试训练后的模型,请运行: ``` + Test.py + ``` ### 文件说明 各文件说明: @@ -49,4 +56,25 @@ Log文件夹:文件夹里的文件是TensorBoard的记录文件,主要作用 models:这些是训练好的神经网络权重,是核心成果,请谨慎删除 -``` \ No newline at end of file +``` +### ROS封装 +ROS文件夹: +``` + +launch\train_full.launch 为完整训练启动文件 +scripts\carla_train_full.py 为完整训练节点 +./start_full_training.sh 为一键启动脚本 +CMakeLists.txt 为ROS编译配置 +package.xml 为ROS包描述 + +``` +### ROS代码启用 +使用一键启动脚本/手动(此处不给出): +``` + +(请确保Carla已启用) +chmod +x start_full_training.sh +./start_full_training.sh + +``` + diff --git a/src/Unmanned_vehicle_AD_DQN/ROS/CMakeLists.txt b/src/Unmanned_vehicle_AD_DQN/ROS/CMakeLists.txt new file mode 100644 index 0000000000..c680832463 --- /dev/null +++ b/src/Unmanned_vehicle_AD_DQN/ROS/CMakeLists.txt @@ -0,0 +1,207 @@ +cmake_minimum_required(VERSION 3.0.2) +project(carla_dqn) + +## Compile as C++11, supported in ROS Kinetic and newer +# add_compile_options(-std=c++11) + +## Find catkin macros and libraries +## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) +## is used, also find other catkin packages +find_package(catkin REQUIRED COMPONENTS + cv_bridge + rospy + sensor_msgs + std_msgs +) + +## System dependencies are found with CMake's conventions +# find_package(Boost REQUIRED COMPONENTS system) + + +## Uncomment this if the package has a setup.py. This macro ensures +## modules and global scripts declared therein get installed +## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html +# catkin_python_setup() + +################################################ +## Declare ROS messages, services and actions ## +################################################ + +## To declare and build messages, services or actions from within this +## package, follow these steps: +## * Let MSG_DEP_SET be the set of packages whose message types you use in +## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...). +## * In the file package.xml: +## * add a build_depend tag for "message_generation" +## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET +## * If MSG_DEP_SET isn't empty the following dependency has been pulled in +## but can be declared for certainty nonetheless: +## * add a exec_depend tag for "message_runtime" +## * In this file (CMakeLists.txt): +## * add "message_generation" and every package in MSG_DEP_SET to +## find_package(catkin REQUIRED COMPONENTS ...) +## * add "message_runtime" and every package in MSG_DEP_SET to +## catkin_package(CATKIN_DEPENDS ...) +## * uncomment the add_*_files sections below as needed +## and list every .msg/.srv/.action file to be processed +## * uncomment the generate_messages entry below +## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...) + +## Generate messages in the 'msg' folder +# add_message_files( +# FILES +# Message1.msg +# Message2.msg +# ) + +## Generate services in the 'srv' folder +# add_service_files( +# FILES +# Service1.srv +# Service2.srv +# ) + +## Generate actions in the 'action' folder +# add_action_files( +# FILES +# Action1.action +# Action2.action +# ) + +## Generate added messages and services with any dependencies listed here +# generate_messages( +# DEPENDENCIES +# sensor_msgs# std_msgs +# ) + +################################################ +## Declare ROS dynamic reconfigure parameters ## +################################################ + +## To declare and build dynamic reconfigure parameters within this +## package, follow these steps: +## * In the file package.xml: +## * add a build_depend and a exec_depend tag for "dynamic_reconfigure" +## * In this file (CMakeLists.txt): +## * add "dynamic_reconfigure" to +## find_package(catkin REQUIRED COMPONENTS ...) +## * uncomment the "generate_dynamic_reconfigure_options" section below +## and list every .cfg file to be processed + +## Generate dynamic reconfigure parameters in the 'cfg' folder +# generate_dynamic_reconfigure_options( +# cfg/DynReconf1.cfg +# cfg/DynReconf2.cfg +# ) + +################################### +## catkin specific configuration ## +################################### +## The catkin_package macro generates cmake config files for your package +## Declare things to be passed to dependent projects +## INCLUDE_DIRS: uncomment this if your package contains header files +## LIBRARIES: libraries you create in this project that dependent projects also need +## CATKIN_DEPENDS: catkin_packages dependent projects also need +## DEPENDS: system dependencies of this project that dependent projects also need +catkin_package( +# INCLUDE_DIRS include +# LIBRARIES carla_dqn +# CATKIN_DEPENDS cv_bridge rospy sensor_msgs std_msgs +# DEPENDS system_lib +) + +########### +## Build ## +########### + +## Specify additional locations of header files +## Your package locations should be listed before other locations +include_directories( +# include + ${catkin_INCLUDE_DIRS} +) + +## Declare a C++ library +# add_library(${PROJECT_NAME} +# src/${PROJECT_NAME}/carla_dqn.cpp +# ) + +## Add cmake target dependencies of the library +## as an example, code may need to be generated before libraries +## either from message generation or dynamic reconfigure +# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) + +## Declare a C++ executable +## With catkin_make all packages are built within a single CMake context +## The recommended prefix ensures that target names across packages don't collide +# add_executable(${PROJECT_NAME}_node src/carla_dqn_node.cpp) + +## Rename C++ executable without prefix +## The above recommended prefix causes long target names, the following renames the +## target back to the shorter version for ease of user use +## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node" +# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "") + +## Add cmake target dependencies of the executable +## same as for the library above +# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) + +## Specify libraries to link a library or executable target against +# target_link_libraries(${PROJECT_NAME}_node +# ${catkin_LIBRARIES} +# ) + +############# +## Install ## +############# + +# all install targets should use catkin DESTINATION variables +# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html + +## Mark executable scripts (Python etc.) for installation +## in contrast to setup.py, you can choose the destination +# catkin_install_python(PROGRAMS +# scripts/my_python_script +# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +# ) + +## Mark executables for installation +## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html +# install(TARGETS ${PROJECT_NAME}_node +# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +# ) + +## Mark libraries for installation +## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html +# install(TARGETS ${PROJECT_NAME} +# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} +# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} +# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION} +# ) + +## Mark cpp header files for installation +# install(DIRECTORY include/${PROJECT_NAME}/ +# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} +# FILES_MATCHING PATTERN "*.h" +# PATTERN ".svn" EXCLUDE +# ) + +## Mark other files for installation (e.g. launch and bag files, etc.) +# install(FILES +# # myfile1 +# # myfile2 +# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} +# ) + +############# +## Testing ## +############# + +## Add gtest based cpp test target and link libraries +# catkin_add_gtest(${PROJECT_NAME}-test test/test_carla_dqn.cpp) +# if(TARGET ${PROJECT_NAME}-test) +# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}) +# endif() + +## Add folders to be run by python nosetests +# catkin_add_nosetests(test) diff --git a/src/Unmanned_vehicle_AD_DQN/ROS/launch/train_full.launch b/src/Unmanned_vehicle_AD_DQN/ROS/launch/train_full.launch new file mode 100644 index 0000000000..3d2ad0dfe9 --- /dev/null +++ b/src/Unmanned_vehicle_AD_DQN/ROS/launch/train_full.launch @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/Unmanned_vehicle_AD_DQN/ROS/package.xml b/src/Unmanned_vehicle_AD_DQN/ROS/package.xml new file mode 100644 index 0000000000..6dbdea489f --- /dev/null +++ b/src/Unmanned_vehicle_AD_DQN/ROS/package.xml @@ -0,0 +1,71 @@ + + + carla_dqn + 0.0.0 + The carla_dqn package + + + + + joe + + + + + + TODO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + catkin + cv_bridge + rospy + sensor_msgs + std_msgs + cv_bridge + rospy + sensor_msgs + std_msgs + cv_bridge + rospy + sensor_msgs + std_msgs + + + + + + + + diff --git a/src/Unmanned_vehicle_AD_DQN/ROS/scripts/carla_train_full.py b/src/Unmanned_vehicle_AD_DQN/ROS/scripts/carla_train_full.py new file mode 100644 index 0000000000..101c077b36 --- /dev/null +++ b/src/Unmanned_vehicle_AD_DQN/ROS/scripts/carla_train_full.py @@ -0,0 +1,326 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +CARLA DQN训练节点 - 完整使用原始代码 +""" +import rospy +import sys +import os +import time +import threading +import numpy as np + +# 设置路径 +sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'lib')) + +# ROS消息 +from std_msgs.msg import String, Float32, Int32, Bool +from sensor_msgs.msg import Image +from cv_bridge import CvBridge + +class FullCarlaTrainer: + def __init__(self): + # 初始化ROS节点 + rospy.init_node('carla_dqn_full_trainer') + + # 获取参数 - 使用Hyperparameters.py中的值作为默认值 + try: + import Hyperparameters as hp + default_episodes = hp.EPISODES + except: + default_episodes = 100 + + self.episodes = rospy.get_param('~episodes', default_episodes) + self.model_name = rospy.get_param('~model_name', 'YY_Enhanced_ObstacleAvoidance') + + # 创建发布器 + self.status_pub = rospy.Publisher('/carla/full_training/status', String, queue_size=10) + self.reward_pub = rospy.Publisher('/carla/full_training/reward', Float32, queue_size=10) + self.episode_pub = rospy.Publisher('/carla/full_training/episode', Int32, queue_size=10) + self.epsilon_pub = rospy.Publisher('/carla/full_training/epsilon', Float32, queue_size=10) + + # 图像发布器 + self.image_pub = rospy.Publisher('/carla/full_training/image', Image, queue_size=1) + self.bridge = CvBridge() + + # 导入原始代码 + self.import_modules() + + rospy.loginfo(f"✅ 完整训练节点初始化完成") + rospy.loginfo(f" 训练轮次: {self.episodes}") + rospy.loginfo(f" 模型名称: {self.model_name}") + + self.status_pub.publish(f"节点启动,准备训练{self.episodes}轮") + + def import_modules(self): + """导入所有需要的模块""" + try: + # 导入主训练函数 + import main + self.main_module = main + rospy.loginfo("✅ 导入main.py模块") + + # 导入超参数 + import Hyperparameters + self.hp = Hyperparameters + rospy.loginfo(f"✅ 导入超参数: EPISODES={Hyperparameters.EPISODES}") + + # 导入环境 + import Environment + self.Environment = Environment + rospy.loginfo("✅ 导入环境模块") + + # 导入模型 + import Model + self.Model = Model + rospy.loginfo("✅ 导入模型模块") + + self.import_success = True + + except Exception as e: + rospy.logerr(f"❌ 导入失败: {e}") + self.import_success = False + + def publish_training_status(self, episode, reward, epsilon): + """发布训练状态""" + # 发布状态 + status_msg = String() + status_msg.data = f"Episode {episode}: Reward={reward:.2f}, Epsilon={epsilon:.3f}" + self.status_pub.publish(status_msg) + + # 发布奖励 + reward_msg = Float32() + reward_msg.data = reward + self.reward_pub.publish(reward_msg) + + # 发布回合数 + episode_msg = Int32() + episode_msg.data = episode + self.episode_pub.publish(episode_msg) + + # 发布探索率 + epsilon_msg = Float32() + epsilon_msg.data = epsilon + self.epsilon_pub.publish(epsilon_msg) + + def publish_image(self, cv_image): + """发布图像""" + try: + if cv_image is not None and len(cv_image.shape) == 3: + # 确保图像是8位 + if cv_image.dtype != np.uint8: + cv_image = cv_image.astype(np.uint8) + + # 调整大小以便显示 + display_image = cv2.resize(cv_image, (640, 480)) + + # 转换为ROS消息 + ros_image = self.bridge.cv2_to_imgmsg(display_image, "bgr8") + ros_image.header.stamp = rospy.Time.now() + ros_image.header.frame_id = "carla_camera" + + self.image_pub.publish(ros_image) + except Exception as e: + rospy.logwarn(f"发布图像失败: {e}") + + def run_original_training(self): + """运行原始训练代码""" + try: + rospy.loginfo("🚗 开始原始CARLA DQN训练") + + # 这里直接调用你原来的main.py中的函数 + # 但需要稍作修改以适应ROS + + # 1. 设置GPU内存 + import tensorflow as tf + gpu_options = tf.compat.v1.GPUOptions( + per_process_gpu_memory_fraction=self.hp.MEMORY_FRACTION + ) + tf.compat.v1.keras.backend.set_session( + tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))) + + # 2. 创建目录 + for dir_name in ['models', 'expert_data', 'logs', 'training_stats']: + if not os.path.isdir(dir_name): + os.makedirs(dir_name) + + # 3. 创建智能体和环境 + rospy.loginfo("创建增强版智能体和环境...") + agent = self.Model.EnhancedDQNAgent( + use_dueling=True, + use_per=True, + use_curriculum=True, + use_multi_objective=True, + use_attention=True, + use_enhanced_model=True + ) + + env = self.Environment.CarEnv(obstacle_detection_mode='advanced') + + # 4. 设置训练策略 + agent.setup_training_strategies(env) + + # 5. 启动训练线程 + trainer_thread = threading.Thread(target=agent.train_in_loop, daemon=True) + trainer_thread.start() + + while not agent.training_initialized: + time.sleep(0.01) + + # 6. 训练循环 + rospy.loginfo(f"开始{self.episodes}轮训练...") + + success_count = 0 + scores = [] + + for episode in range(1, self.episodes + 1): + if rospy.is_shutdown(): + rospy.loginfo("ROS关闭,停止训练") + break + + # 重置环境 + if agent.curriculum_manager: + config = agent.curriculum_manager.get_current_config() + current_state = env.reset(episode, curriculum_config=config) + else: + current_state = env.reset(episode) + + # 重置统计 + score = 0 + step = 1 + done = False + + # 获取最大步数 + if agent.curriculum_manager: + config = agent.curriculum_manager.get_current_config() + max_steps = config['max_episode_steps'] + else: + max_steps = self.hp.SECONDS_PER_EPISODE * self.hp.FPS + + # 单轮训练 + while not done and step < max_steps: + # 选择动作 + if np.random.random() > self.hp.EPSILON: + qs = agent.get_qs(current_state) + action = np.argmax(qs) + else: + action = np.random.randint(0, 5) + time.sleep(1 / self.hp.FPS) + + # 执行动作 + new_state, reward, done, _ = env.step(action) + + # 判断是否为重要经验 + is_obstacle_experience = (env.last_ped_distance < 10.0) + is_success_experience = (reward > 2.0 and not done) + + # 更新经验回放 + agent.update_replay_memory( + (current_state, action, reward, new_state, done), + is_obstacle=is_obstacle_experience, + is_success=is_success_experience + ) + + score += reward + current_state = new_state + step += 1 + + # 每10步发布一次图像 + if step % 10 == 0: + self.publish_image(current_state) + + if done: + break + + # 发布本轮结果 + self.publish_training_status(episode, score, self.hp.EPSILON) + + # 记录分数 + scores.append(score) + success = score > 8 + if success: + success_count += 1 + + # 清理环境 + env.cleanup_actors() + + # 衰减探索率 + if self.hp.EPSILON > self.hp.MIN_EPSILON: + self.hp.EPSILON *= self.hp.EPSILON_DECAY + self.hp.EPSILON = max(self.hp.MIN_EPSILON, self.hp.EPSILON) + + # 每10轮保存一次检查点 + if episode % 10 == 0: + model_path = f"models/{self.model_name}_checkpoint_ep{episode}.model" + agent.save_model(model_path) + rospy.loginfo(f"检查点已保存: {model_path}") + + # 显示进度 + rospy.loginfo(f"Episode {episode}/{self.episodes}: Score={score:.2f}, " + f"Success={success_count}, Epsilon={self.hp.EPSILON:.3f}") + + # 训练完成 + rospy.loginfo("🎉 训练完成!") + + # 保存最终模型 + if scores: + final_avg = np.mean(scores[-10:]) if len(scores) >= 10 else np.mean(scores) + final_model = f"models/{self.model_name}_final_avg{final_avg:.2f}.model" + agent.save_model(final_model) + rospy.loginfo(f"最终模型已保存: {final_model}") + + # 清理 + agent.terminate = True + trainer_thread.join() + env.cleanup_actors() + + self.status_pub.publish("训练完成") + + except Exception as e: + rospy.logerr(f"训练失败: {e}") + import traceback + rospy.logerr(traceback.format_exc()) + self.status_pub.publish(f"训练失败: {str(e)}") + + def run(self): + """主运行函数""" + rospy.loginfo("=" * 60) + rospy.loginfo("CARLA DQN完整训练系统") + rospy.loginfo("=" * 60) + rospy.loginfo(f"训练轮次: {self.episodes}") + rospy.loginfo(f"使用模型: {self.model_name}") + rospy.loginfo("") + rospy.loginfo("📡 发布的话题:") + rospy.loginfo(" /carla/full_training/status - 训练状态") + rospy.loginfo(" /carla/full_training/reward - 实时奖励") + rospy.loginfo(" /carla/full_training/episode - 当前回合") + rospy.loginfo(" /carla/full_training/epsilon - 探索率") + rospy.loginfo(" /carla/full_training/image - 训练图像") + rospy.loginfo("") + rospy.loginfo("⏳ 5秒后开始训练...") + + time.sleep(5) + + # 在新线程中运行训练 + if self.import_success: + train_thread = threading.Thread(target=self.run_original_training) + train_thread.daemon = True + train_thread.start() + else: + rospy.logerr("导入失败,无法开始训练") + return + + # 保持节点运行 + rospy.spin() + +def main(): + trainer = FullCarlaTrainer() + trainer.run() + +if __name__ == '__main__': + try: + main() + except rospy.ROSInterruptException: + rospy.loginfo("训练节点被中断") + except Exception as e: + rospy.logerr(f"节点异常: {e}") diff --git a/src/Unmanned_vehicle_AD_DQN/ROS/start_full_training.sh b/src/Unmanned_vehicle_AD_DQN/ROS/start_full_training.sh new file mode 100644 index 0000000000..1f41665f85 --- /dev/null +++ b/src/Unmanned_vehicle_AD_DQN/ROS/start_full_training.sh @@ -0,0 +1,52 @@ +#!/bin/bash +echo "==========================================" +echo "🚗 CARLA DQN完整训练系统启动" +echo "==========================================" + +# 激活虚拟环境 +echo "激活虚拟环境..." +source venv/bin/activate + +# 检查Carla是否运行 +echo "检查Carla是否运行..." +if ! pgrep -f CarlaUE4 > /dev/null; then + echo "⚠️ Carla未运行,请先启动Carla!" + echo " 在另一个终端运行: ~/carla/CarlaUE4.sh" + read -p "按回车继续(如果Carla已启动)或Ctrl+C退出..." +fi + +# 进入ROS工作空间 +cd ros_ws + +# 编译(如果需要) +echo "编译ROS包..." +catkin_make + +# 设置环境 +source devel/setup.bash + +# 检查roscore是否运行 +if ! rostopic list > /dev/null 2>&1; then + echo "启动roscore..." + gnome-terminal -- bash -c "roscore; exec bash" + sleep 3 +fi + +# 启动完整训练系统 +echo "启动完整训练系统..." +echo "训练轮次: 100轮(可在launch文件中修改)" +gnome-terminal -- bash -c "source ~/projects/my_carla_project/ros_ws/devel/setup.bash && roslaunch carla_dqn train_full.launch; exec bash" + +echo "" +echo "✅ 完整训练系统启动完成!" +echo "" +echo "📊 监控训练状态:" +echo " 1. 查看训练日志: 在当前终端查看" +echo " 2. 查看图像: 自动打开image_view窗口" +echo " 3. 查看曲线: 运行: rqt_plot" +echo "" +echo "📈 查看奖励曲线:" +echo " rosrun rqt_plot rqt_plot /carla/full_training/reward" +echo "" +echo "🛑 停止系统: 按Ctrl+C关闭所有窗口" +echo "==========================================" diff --git a/src/Unmanned_vehicle_AD_DQN/environment.yml b/src/Unmanned_vehicle_AD_DQN/environment.yml new file mode 100644 index 0000000000..76ffebf0ba Binary files /dev/null and b/src/Unmanned_vehicle_AD_DQN/environment.yml differ