jetracer——自动驾驶车项目(road_following.ipynb)

First, create the model. This must match the model used in the interactive training notebook.

  • 首先,创建模型。这必须与interactive_regression.ipynb中使用的模型相匹配。
import torch
import torchvision
​
CATEGORIES = ['apex']
​
device = torch.device('cuda')
model = torchvision.models.resnet18(pretrained=False)
model.fc = torch.nn.Linear(512, 2 * len(CATEGORIES))
model = model.cuda().eval().half()

Next, load the saved model. Enter the model path you used to save.

  • 接下来,加载上一步保存的模型。输入你用于保存的模型路径。
model.load_state_dict(torch.load('road_following_model.pth'))

Convert and optimize the model using torch2trt for faster inference with TensorRT. Please see the torch2trt readme for more details.

  • 使用torch2trt转换和优化模型,使用TensorRT进行更快的推断。详情请参阅torch2trt自述文件。

This optimization process can take a couple minutes to complete.

  • 这个优化过程可能需要几分钟才能完成。这个过程是没有相应的运行和完成提示的请耐心等待。
from torch2trt import torch2trt
​
data = torch.zeros((1, 3, 224, 224)).cuda().half()
​
model_trt = torch2trt(model, [data], fp16_mode=True)

Save the optimized model using the cell below

  • 使用下面的代码保存优化的模型
torch.save(model_trt.state_dict(), 'road_following_model_trt.pth')
print("8888888888888888888888888888")
  • 输出结果:8888888888888888888888888888
    选择输出888888888888888888888是因为模型优化过后,保存成功后,使用优化后的模型进行小车自动驾驶直接加载该步保存的模型即可,并不需要再次对模型进行优化,节省时间,方便操作。

Load the optimized model by executing the cell below

  • 通过执行下面的程序进行优化后模型的加载
import torch
from torch2trt import TRTModule
​
model_trt = TRTModule()
model_trt.load_state_dict(torch.load('road_following_model_trt.pth'))

Create the racecar class

from jetracer.nvidia_racecar import NvidiaRacecar
​
car = NvidiaRacecar()

Create the camera class.

  • 创建摄像头类
from jetcam.csi_camera import CSICamera
​
camera = CSICamera(width=224, height=224, capture_fps=65)
  • 打开摄像头。众所周知,如果打开出现错误,需要使用命令行重置摄像头。

Finally, execute the cell below to make the racecar move forward, steering the racecar based on the x value of the apex.

Here are some tips,

If the car wobbles left and right, lower the steering gain
If the car misses turns, raise the steering gain
If the car tends right, make the steering bias more negative (in small increments like -0.05)
If the car tends left, make the steering bias more postive (in small increments +0.05)

  • 最后,执行下面的单元格,使赛车向前移动,根据顶点的x值控制赛车。
    这里有一些建议,
    如果汽车左右摇摆,降低转向增益STEERING_GAIN
    如果汽车没有转弯,提高转向增益STEERING_GAIN
    由于小车设计的转向最大值有限制,更大的转向增益有时并不会提高转向,有甚至可能会损坏小车,因此选择合适的转向增益即可。
    如果汽车向右,使转向偏置更负(以小的增量,如-0.05)STEERING_BIAS
    如果汽车偏左,使转向偏置更积极(用小增量,如+0.05)STEERING_BIAS
    小车做工等原因可能导致小车转向偏左或偏右,又或者转向左转和右转的转向幅度不一致。由此带来的问题是不管是增大转向偏置还是减小转向偏置,可能都不能顺利的完成一个赛道的正向行驶和反向行驶。
from utils import preprocess
import numpy as np
​
STEERING_GAIN = -0.7
STEERING_BIAS = 0.0
​
car.throttle = 0.6
car.throttle_gain =0.6
  • 上面的程序是设置初始参数的,当然,在速度属性的设置上,你也可以不设置数值,使用远程控制程序,用手柄控制小车前进,后退。
  • 由于小车的转向是靠摄像头拍到的图片做出的,因此在小车后退时,小车仍然能够按照赛道轨迹进行后退。
  • 下面的程序是死循环程序,需要远程控制的话需要提前运行相关程序。
while True:
    image = camera.read()
    image = preprocess(image).half()
    output = model_trt(image).detach().cpu().numpy().flatten()
    x = float(output[0])
    print(x)
    car.steering = x * STEERING_GAIN + STEERING_BIAS
  • x的值大概是在-1~1之间,可能会略超出边界x * STEERING_GAIN + STEERING_BIAS是小车的实际转向值,是写入转向舵机的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dunkle.T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值