当前位置: 移动技术网 > 科技>操作系统>windows > windows10下AirSimE2Edeeplearning训练与测试

windows10下AirSimE2Edeeplearning训练与测试

2020年07月30日  | 移动技术网科技  | 我要评论
windows10下AirSimE2Edeeplearning训练与测试前期准备tensorflow与keras版本notebook版本与tornado版本AirSim配置数据处理与模型训练数据处理模型训练模型测试打开AirSim环境模型测试与连接AirSim环境前期准备在配置AirSimE2Edeeplearning项目前需要保证官方InstallPackages.py中提到的包成功安装,不过有一些点需要注意。tensorflow与keras版本很明显项目中使用的是tensorflow1.x版本,

前期准备

在配置AirSimE2Edeeplearning项目前需要保证官方InstallPackages.py中提到的包成功安装,不过有一些点需要注意。

tensorflow与keras版本

很明显项目中使用的是tensorflow1.x版本,不过keras版本并没有给出,我这里使用的是tensorflow1.14-gpu、cuda10、cudnn7.5以及keras2.1.2

notebook版本与tornado版本

如果使用jupyter notebook的话需要保证notebook版本与tornado版本都是在5.0以下,不然很多步骤都无法进行,不过官方用这版本真的是太老了,现在都6.0.3了,然而不改还不行(捂脸)。

AirSim配置

严格来说不进行这步也行,前提十只能使用官方给的几个场景,不过虚化引擎还是得安的,具体过程可以参考build AirSim on Windows以及AirSim与unreal4.24配置或者直接参考博主changshen_xu的翻译教程不过需要注意的是这位博主使用的是vs2017以及虚幻4.18所以具体流程可能会有所不同。

数据处理与模型训练

这部分针对的是项目文件中的DataExplorationAndPreparation.ipynbTrainModel.ipynb

数据处理

数据集链接下载数据集后,解压到项目目录,目录如下:

  • AirSimE2EDeepLearning
    • data_raw
      • normal_1
      • normal_2
      • normal_3
      • normal_4
      • normal_5
      • normal_6
      • swerve_1
      • swerve_2
      • swerve_3

完成解压之后就可以运行DataExplorationAndPreparation.ipynb了。这里需要注意的是在生成train.h5等时,可能会报错runtime error,需要在cooking.py中做一定修改,方法如下:

  • 方法1:注释掉cooking.py代码的第130行处raise StopIteration(即注释掉第二个raise,这个方法比较简单粗暴,可能会有问题,不过目前我还没遇到)
  • 方法2:为raise StopIteration处的for循环添加一个异常抛出(即try,在原项目issue中找到的方法,没试过)

模型训练

完成train.h5eval.h5以及test.h5的生成后就可以进行模型训练了,不过有几点需要注意

  • 训练时有时报Oprogress not found的类似错误,我的解决方法是安装ipywidgets具体为
pip install  ipywidegets==5.0.0
jupyter nbextension enable --py widgetsnebextension
  • 在原项目中有earlystop的设置,如果想关的话可以按如下设置
    在这里插入图片描述

完成训练后,会自动保存训练模型以便测试时使用。

模型测试

打开AirSim环境

下载官方的Ad_book链接并解压到随便一个目录,我的话是直接放在d盘download目录下的,接着打开powershell

d:
cd download\AD_Cookbook_AirSim

之后运行

 .\AD_Cookbook_Start_AirSim.ps1 landscape -windowed

就可以连接到AirSim了,此时的控制方式一般为键盘。

模型测试与连接AirSim环境

这里的坑比较多,一步一步解决

  • 首先在连接AirSim时,TestModel.ipynb的第一个cell中的
if ('../../PythonClient/' not in sys.path):
    sys.path.insert(0, '../../PythonClient/')

需要修改一下,我们需要下载AirSim-master链接然后将对应目录修改

if (r'D:\BaiduNetdiskDownload\AirSim-master\PythonClient' not in sys.path):
    sys.path.insert(0, r'D:\BaiduNetdiskDownload\AirSim-master\PythonClient')
  • 其次是第二个cell中的连接CarClient,需要保证tornado的版本满足,原始代码的要求,如果一步一步按本教程进行的话应该就没问题了。
  • 最后也是最迷得问题,完成第二个cell,并输出
Waiting for connection: 
Connection established!

但紧接着运行第三个cell时,会发现这个cell并不还运行并且卡住不动了,解决方法是限制GPU的使用率(我这里由于设备一般,所以将gpu内存使用率设为50%),即添加

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
 
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
config = tf.ConfigProto() 
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.5
set_session(tf.Session(config=config))

但发现即使这样,jupyter中第三个cell还是无法运行,不过我将全部代码复制到spyder直接运行后是可以的,具体如下:

from keras.models import load_model
import sys
import numpy as np
import glob
import os
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
 
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
config = tf.ConfigProto() 
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.5
set_session(tf.Session(config=config))
#if ('../../PythonClient/' not in sys.path):
#    sys.path.insert(0, '../../PythonClient/')


if (r'D:\BaiduNetdiskDownload\AirSim-master\PythonClient' not in sys.path):
    sys.path.insert(0, r'D:\BaiduNetdiskDownload\AirSim-master\PythonClient')
from AirSimClient import *

# << Set this to the path of the model >>
# If None, then the model with the lowest validation loss from training will be used
MODEL_PATH = None

def get_image():
    image_response = client.simGetImages([ImageRequest(0, AirSimImageType.Scene, False, False)])[0]
    image1d = np.fromstring(image_response.image_data_uint8, dtype=np.uint8)
    image_rgba = image1d.reshape(image_response.height, image_response.width, 4)
    
    return image_rgba[76:135,0:255,0:3].astype(float)
    
    
if (MODEL_PATH == None):
    models = glob.glob('model/models/*.h5') 
    best_model = max(models, key=os.path.getctime)
    MODEL_PATH = best_model
    
print('Using model {0} for testing.'.format(MODEL_PATH))

model = load_model(MODEL_PATH)
client = CarClient()
client.confirmConnection()
client.enableApiControl(True)
car_controls = CarControls()
print('Connection established!')


car_controls.steering = 0
car_controls.throttle = 0
car_controls.brake = 0
image_buf = np.zeros((1, 59, 255, 3))
state_buf = np.zeros((1,4))

while (True):
    car_state = client.getCarState()
    
    if (car_state.speed < 5):
        car_controls.throttle = 1.0
    else:
        car_controls.throttle = 0.0
    
    image_buf[0] = get_image()
    state_buf[0] = np.array([car_controls.steering, car_controls.throttle, car_controls.brake, car_state.speed])
    model_output = model.predict([image_buf, state_buf])
    car_controls.steering = round(0.5 * float(model_output[0][0]), 2)
    
    print('Sending steering = {0}, throttle = {1}'.format(car_controls.steering, car_controls.throttle))
    
    client.setCarControls(car_controls)
    

之后就可以看到我们的模型自动控制汽车前进了。

本文地址:https://blog.csdn.net/zhangshengdong1/article/details/107653509

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网