自動(dòng)駕駛汽車(chē)又稱為無(wú)人駕駛車(chē),是一種需要駕駛員輔助或者完全不需操控的車(chē)輛自点。
自動(dòng)駕駛分級(jí):
自動(dòng)駕駛系統(tǒng)的組成部分:
環(huán)境感知系統(tǒng):
自動(dòng)駕駛系統(tǒng)架構(gòu):
自動(dòng)駕駛數(shù)據(jù)集:
Aidlux的作用:
2D視覺(jué)感知任務(wù):
2D物體檢測(cè)算法:
自動(dòng)駕駛中的物體檢測(cè)和跟蹤:
2D感知的局限性:
雙目3D感知:
YOLOP算法:
損失函數(shù):
模型訓(xùn)練:
數(shù)據(jù)集:
修改配置文件lib/config/default.py
訓(xùn)練:
pip install -r requirements.txt
python tools/train.py
_C.GPUS = (0,1) ? ? #v根據(jù)你實(shí)際的顯卡數(shù)進(jìn)行修改
_C.WORKERS = 0 ? ? ?#由cpu的數(shù)量確認(rèn)worker是的數(shù)量遂赠,直接影響數(shù)據(jù)加載速度
_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:是開(kāi)放式神經(jīng)網(wǎng)絡(luò)的簡(jiǎn)稱久妆。目前官方支持加載onnx模型的框架有:Caff2,Pytorch跷睦,MXNet等筷弦。執(zhí)行命令:
python export_onnx.py --height 640 --width 640
在weigths文件夾下生成轉(zhuǎn)換成功的onnx模型
onnx轉(zhuǎn)換核心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平臺(tái)部署推理:
找到home目錄,上傳YOLOP文件夾至home內(nèi)。打開(kāi)終端烂琴,安裝pytorch環(huán)境爹殊。
智能預(yù)警:
包含三個(gè)任務(wù):目標(biāo)檢測(cè)、可行駛區(qū)域檢測(cè)奸绷、車(chē)道線檢測(cè)梗夸。
執(zhí)行python forewarning.py進(jìn)行智能預(yù)警檢測(cè)。
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')
#定義視頻對(duì)象輸出
? ? writer = cv2.VideoWriter(save_path, fourcc, fps, (width, height))
#檢查是否導(dǎo)入視頻成功
? ? if not cap.isOpened():
print("視頻無(wú)法打開(kāi)")
? ? ? ? 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'幀數(shù):{frame_id} ?幀率:{fps} 前方共有 {boxes.shape[0]} 輛車(chē)...',
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (100, 50), textColor=(0, 0, 255), textSize=30)
img_merge = cv2AddChineseText(img_merge, '前方綠色區(qū)域?yàn)榭尚旭倕^(qū)域号醉,紅色為檢出的車(chē)道線...',
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (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() ?#可以實(shí)現(xiàn)預(yù)覽
? ? cv2.destroyAllWindows()