赞
踩
mirrors / WongKinYiu / yolov7 · GitCode
在主目录下新建mydata目录,目录分布如下图所示
注:转换脚本自行查找,网上很多,如果找不到,私信我发
图 train.txt
图 val.txt
test的话也是一样
- train: ./mydata/train.txt
- val: ./mydata/val.txt
- test: ./mydata/test.txt
-
- # number of classes
- nc: 3
-
- # class names
- names: ['a', 'b', 'c']
https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt
注:预训练权重可以自己选择,此处我选择的是最小的一个
- parser = argparse.ArgumentParser()
- parser.add_argument('--weights', type=str, default='./weights/yolov7.pt', help='initial weights path')
- parser.add_argument('--cfg', type=str, default='yolov7.yaml', help='model.yaml path')
- parser.add_argument('--data', type=str, default='mydata.yaml', help='data.yaml path')
- parser.add_argument('--hyp', type=str, default='data/hyp.scratch.p5.yaml', help='hyperparameters path')
- parser.add_argument('--epochs', type=int, default=300)
- parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs')
- parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='[train, test] image sizes')
- parser.add_argument('--rect', action='store_true', help='rectangular training')
- parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
- parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
- parser.add_argument('--notest', action='store_true', help='only test final epoch')
- parser.add_argument('--noautoanchor', action='store_true', help='disable autoanchor check')
- parser.add_argument('--evolve', action='store_true', help='evolve hyperparameters')
- parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
- parser.add_argument('--cache-images', action='store_true', help='cache images for faster training')
- parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
- parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
- parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
- parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
- parser.add_argument('--adam', action='store_true', help='use torch.optim.Adam() optimizer')
- parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
- parser.add_argument('--local_rank', type=int, default=-1, help='DDP parameter, do not modify')
- parser.add_argument('--workers', type=int, default=8, help='maximum number of dataloader workers')
- parser.add_argument('--project', default='runs/train', help='save to project/name')
- parser.add_argument('--entity', default=None, help='W&B entity')
- parser.add_argument('--name', default='exp', help='save to project/name')
- parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
- parser.add_argument('--quad', action='store_true', help='quad dataloader')
- parser.add_argument('--linear-lr', action='store_true', help='linear LR')
- parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
- parser.add_argument('--upload_dataset', action='store_true', help='Upload dataset as W&B artifact table')
- parser.add_argument('--bbox_interval', type=int, default=-1, help='Set bounding-box image logging interval for W&B')
- parser.add_argument('--save_period', type=int, default=-1, help='Log model after every "save_period" epoch')
- parser.add_argument('--artifact_alias', type=str, default="latest", help='version of dataset artifact to be used')
- parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone of yolov7=50, first3=0 1 2')
- parser.add_argument('--v5-metric', action='store_true', help='assume maximum recall as 1.0 in AP calculation')
- opt = parser.parse_args()
因为我的训练是在云服务器ubuntu上进行,因此进入主目录,直接
pip install -r requirements.txt
此外,有时候会碰到错误
ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
这是因为linux系统缺少这个库依赖文件,所以按照下述方法安装即可
sudo apt-get install libglib2.0-dev
因为修改了大量文件,所以无需指定命令行参数
python train.py
其实第7步已经是可以结束的了,如果用于训练,这一步仅提供给需要科研的伙伴
将训练结果,即runs/train/exp/weights/best.pt复制到主目录文件夹下,使用linux命令
cp -r runs/train/exp/weights/best.pt -r ./
修改test.py以下行
- parser = argparse.ArgumentParser(prog='test.py')
- parser.add_argument('--weights', nargs='+', type=str, default='best.pt', help='model.pt path(s)')
- parser.add_argument('--data', type=str, default='mydata.yaml', help='*.data path')
- parser.add_argument('--batch-size', type=int, default=1, help='size of each image batch')
- parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
- parser.add_argument('--conf-thres', type=float, default=0.001, help='object confidence threshold')
- parser.add_argument('--iou-thres', type=float, default=0.65, help='IOU threshold for NMS')
- parser.add_argument('--task', default='val', help='train, val, test, speed or study')
- parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
- parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset')
- parser.add_argument('--augment', action='store_true', help='augmented inference')
- parser.add_argument('--verbose', action='store_true', help='report mAP by class')
- parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
- parser.add_argument('--save-hybrid', action='store_true', help='save label+prediction hybrid results to *.txt')
- parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
- parser.add_argument('--save-json', action='store_true', help='save a cocoapi-compatible JSON results file')
- parser.add_argument('--project', default='runs/test', help='save to project/name')
- parser.add_argument('--name', default='exp', help='save to project/name')
- parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
- parser.add_argument('--no-trace', action='store_true', help='don`t trace model')
- parser.add_argument('--v5-metric', action='store_true', help='assume maximum recall as 1.0 in AP calculation')
- opt = parser.parse_args()
- opt.save_json |= opt.data.endswith('coco.yaml')
- opt.data = check_file(opt.data) # check file
得到模型效果如图,此处打码,牵扯到我的实验数据
- parser.add_argument('--weights', nargs='+', type=str, default='best.pt', help='model.pt path(s)')
- parser.add_argument('--source', type=str, default='inference/img.jpg', help='source')
只需修改这行即可,要测试的图像放在inference/下,命名为img.jpg即可
如果是批量测试,则直接写成
parser.add_argument('--source', type=str, default='inference/', help='source')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。