项目地址
https://github.com/Tianxiaomo/pytorch-YOLOv4
yolov4_new all processes
下面是可能遇到的各种问题
当然现在还有一些问题需要解决
模型需要的训练和测试的标注文件不是json,而是txt文件,文件格式在上面的项目中有介绍,作者也提供了一个代码tool/dataset.py 把yolo转txt
Cfg.train_label = os.path.join(_BASE_DIR, 'train_yolov4_4_355.txt')
Cfg.val_label = os.path.join(_BASE_DIR, 'train_yolov4_4_355.txt')
classes=4
Cfg.use_darknet_cfg = False
hsv=list(hsv)
hsv[1] *= dsat
hsv[2] *= dexp
hsv[0] += 179 * dhue
up = self.upsample1(x7, downsample4.size(), self.inference) * 0.5
if self.inference:
# y1 = self.yolo1(x2)
# y2 = self.yolo2(x10)
# y3 = self.yolo3(x18)
#
# return get_region_boxes([y1, y2, y3])
return [x18, x10, x2]
else:
return [x2, x10, x18]
parser.add_argument('-interval', dest='interval', type=int, default=10, help='interval between train and val')
if (epoch + 1) % config.interval == 0:
if cfg.use_darknet_cfg:
eval_model = Darknet(cfg.cfgfile, inference=True)
else:
eval_model = Yolov4(cfg.pretrained, n_classes=cfg.classes, inference=True)
# eval_model = Yolov4(yolov4conv137weight=None, n_classes=config.classes, inference=True)
if torch.cuda.device_count() > 1:
eval_model.load_state_dict(model.module.state_dict())
else:
eval_model.load_state_dict(model.state_dict())
eval_model.to(device)
evaluator = evaluate(eval_model, val_loader, config, device)
del eval_model
stats = evaluator.coco_eval['bbox'].stats
writer.add_scalar('train/AP', stats[0], global_step)
writer.add_scalar('train/AP50', stats[1], global_step)
writer.add_scalar('train/AP75', stats[2], global_step)
writer.add_scalar('train/AP_small', stats[3], global_step)
writer.add_scalar('train/AP_medium', stats[4], global_step)
writer.add_scalar('train/AP_large', stats[5], global_step)
writer.add_scalar('train/AR1', stats[6], global_step)
writer.add_scalar('train/AR10', stats[7], global_step)
writer.add_scalar('train/AR100', stats[8], global_step)
writer.add_scalar('train/AR_small', stats[9], global_step)
writer.add_scalar('train/AR_medium', stats[10], global_step)
writer.add_scalar('train/AR_large', stats[11], global_step)
pred_ious = bboxes_iou(pred[b].contiguous().view(-1, 4), truth_box, xyxy=False)
logging.info(f'''Starting training:
Epochs: {epochs}
Batch size: {config.batch}
Subdivisions: {config.subdivisions}
Learning rate: {config.learning_rate}
Training size: {n_train}
Validation size: {n_val}
Checkpoints: {save_cp}
Device: {device.type}
Images size: {config.width}
Optimizer: {config.TRAIN_OPTIMIZER}
Dataset classes: {config.classes}
Train label path:{config.train_label}
Val label path: {config.val_label}
Pretrained:
''')
train.py line 454 model.module.state_dict() to save torch.nn.DataParallel model
if isinstance(model, torch.nn.DataParallel):
torch.save(model.module.state_dict(), save_path)
else:
torch.save(model.state_dict(), save_path)
parser.add_argument('-f', '--load', dest='load', type=str, default=None, help='Load model from a .pth file')
if cfg.use_darknet_cfg:
model = Darknet(cfg.cfgfile)
else:
model = Yolov4(cfg.pretrained, n_classes=cfg.classes)
####################### 就是这er
if cfg.load is not None:
model.load_state_dict(torch.load(cfg.load))
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.107
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.269
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.057
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.107
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.032
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.106
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.151
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.151
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000
python train.py -l 0.001 -g 0,1 -pretrained pth/yolov4.conv.137.pth -classes 4 -dir . -interval 10
项目地址(项目地址里面有介绍文档)
但是大多数都是转成tensorrt等,而不是转成onnx这一种中间格式
github.com/open-mmlab/mmdeploy
转成onnx中间格式的方法在:
https://github.com/open-mmlab/mmdeploy/blob/master/tools/torch2onnx.py
里面有这么一句代码:
parser.add_argument(‘deploy_cfg’, help=‘deploy config path’)
这一段需要输入的是configs里面的文件
mmdeploy需要转换目标模型环境(比如转换mmdetection模型就要相应的环境)
知乎介绍和解读(共六篇,里面说的很详尽)
https://zhuanlan.zhihu.com/p/486914187