自动驾驶汽车又称为无人驾驶车,是一种需要驾驶员辅助或者完全不需操控的车辆。
自动驾驶分级:
自动驾驶系统的组成部分:
环境感知系统:
自动驾驶系统架构:
自动驾驶数据集:
Aidlux的作用:
2D视觉感知任务:
2D物体检测算法:
自动驾驶中的物体检测和跟踪:
2D感知的局限性:
双目3D感知:
YOLOP算法:
损失函数:
模型训练:
数据集:
修改配置文件lib/config/default.py
训练:
pip install -r requirements.txt
python tools/train.py
_C.GPUS = (0,1) #v根据你实际的显卡数进行修改
_C.WORKERS = 0 #由cpu的数量确认worker是的数量,直接影响数据加载速度
_C.DATASET.DATAROOT = 'dataset/images' # the path of images folder
_C.DATASET.LABELROOT = 'dataset/det_annotations' # the path of det_annotations folder
_C.DATASET.MASKROOT = 'dataset/da_seg_annotations' # the path of da_seg_annotations folder
_C.DATASET.LANEROOT = 'dataset/ll_seg_annotations' # the path of ll_seg_annotations folder
_C.DATASET.DATASET = 'BddDataset'
_C.DATASET.TRAIN_SET = 'train'
_C.DATASET.TEST_SET = 'val'
_C.DATASET.DATA_FORMAT = 'jpg'
_C.TRAIN.BEGIN_EPOCH = 0
_C.TRAIN.END_EPOCH = 240
# if training 3 tasks end-to-end, set all parameters as True
# Alternating optimization
_C.TRAIN.SEG_ONLY = False # Only train two segmentation branchs
_C.TRAIN.DET_ONLY = False # Only train detection branch
_C.TRAIN.ENC_SEG_ONLY = False # Only train encoder and two segmentation branchs
_C.TRAIN.ENC_DET_ONLY = False # Only train encoder and detection branch
# Single task
_C.TRAIN.DRIVABLE_ONLY = False # Only train da_segmentation task
_C.TRAIN.LANE_ONLY = False # Only train ll_segmentation task
_C.TRAIN.DET_ONLY = False # Only train detection task
onnx:是开放式神经网络的简称。目前官方支持加载onnx模型的框架有:Caff2,Pytorch,MXNet等。执行命令:
python export_onnx.py --height 640 --width 640
在weigths文件夹下生成转换成功的onnx模型
onnx转换核心api:
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--height', type=int, default=640) # height
parser.add_argument('--width', type=int, default=640) # width
args = parser.parse_args()
do_simplify = True
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = MCnet(YOLOP)
checkpoint = torch.load('./weights/End-to-end.pth', map_location=device)
model.load_state_dict(checkpoint['state_dict'])
model.eval()
height = args.height
width = args.width
print("Load ./weights/End-to-end.pth done!")
onnx_path = f'./weights/yolop-{height}-{width}.onnx'
# onnx_path = f'./weights/yolop-test.onnx'
inputs = torch.randn(1, 3, height, width)
print(f"Converting to {onnx_path}")
torch.onnx.export(model, inputs, onnx_path,
verbose=False, opset_version=12, input_names=['images'],
output_names=['det_out', 'drive_area_seg', 'lane_line_seg'])
print('convert', onnx_path, 'to onnx finish!!!')
# Checks
model_onnx = onnx.load(onnx_path) # load onnx model
onnx.checker.check_model(model_onnx) # check onnx model
print(onnx.helper.printable_graph(model_onnx.graph)) # print
Aidlux平台部署推理:
找到home目录,上传YOLOP文件夹至home内。打开终端,安装pytorch环境。
智能预警:
包含三个任务:目标检测、可行驶区域检测、车道线检测。
执行python forewarning.py进行智能预警检测。
def main(source, save_path):
cap = cv2.VideoCapture(source)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #获取视频的宽度
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #获取视频的高度
fps = cap.get(cv2.CAP_PROP_FPS) #获取视频的帧率
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) #视频的编码
# fourcc = cv2.VideoWriter_fourcc(*'avc1')
#定义视频对象输出
writer = cv2.VideoWriter(save_path, fourcc, fps, (width, height))
#检查是否导入视频成功
if not cap.isOpened():
print("视频无法打开")
exit()
frame_id = 0
while True:
ret, frame = cap.read()
if not ret:
print("视频推理完毕...")
break
frame_id += 1
# if frame_id % 3 != 0:
# continue
canvas, r, dw, dh, new_unpad_w, new_unpad_h = resize_unscale(frame, (640, 640))
img = canvas.copy().astype(np.float32) # (3,640,640) RGB
img /= 255.0
img[:, :, 0] -= 0.485
img[:, :, 1] -= 0.456
img[:, :, 2] -= 0.406
img[:, :, 0] /= 0.229
img[:, :, 1] /= 0.224
img[:, :, 2] /= 0.225
img = img.transpose(2, 0, 1)
img = np.expand_dims(img, 0) # (1, 3,640,640)
#推理
img_det, boxes, color_seg, fps = infer(frame, img, r, dw, dh, new_unpad_w, new_unpad_h)
if img_det is None:
continue
color_mask = np.mean(color_seg, 2)
img_merge = canvas[dh:dh + new_unpad_h, dw:dw + new_unpad_w, :]
# merge: resize to original size
img_merge[color_mask != 0] = \
img_merge[color_mask != 0] * 0.5 + color_seg[color_mask != 0] * 0.5
img_merge = img_merge.astype(np.uint8)
img_merge = cv2.resize(img_merge, (width, height),
interpolation=cv2.INTER_LINEAR)
img_merge = cv2AddChineseText(img_merge, f'帧数:{frame_id} 帧率:{fps} 前方共有 {boxes.shape[0]} 辆车...',
(100, 50), textColor=(0, 0, 255), textSize=30)
img_merge = cv2AddChineseText(img_merge, '前方绿色区域为可行驶区域,红色为检出的车道线...',
(100, 100), textColor=(0, 0, 255), textSize=30)
for i in range(boxes.shape[0]):
x1, y1, x2, y2, conf, label = boxes[i]
x1, y1, x2, y2, label = int(x1), int(y1), int(x2), int(y2), int(label)
img_merge = cv2.rectangle(img_merge, (x1, y1), (x2, y2), (0, 255, 0), 2, 2)
# cv2.imshow('img_merge', img_merge)
# cv2.waitKey(0)
writer.write(img_merge)
cap.release() #释放摄像头
writer.release() #可以实现预览
cv2.destroyAllWindows()