本文共 2640 字,大约阅读时间需要 8 分钟。
由于SeaShip数据集的格式不是标准的voc格式需要转换一下:
voc_seaship.py
import xml.etree.ElementTree as ETimport pickleimport osfrom os import listdir, getcwdfrom os.path import joinsets=[('SeaShip', 'train'), ('SeaShip', 'val')]classes = ["ore carrier", "general cargo ship", "bulk cargo carrier", "container ship", "fishing boat", "passenger ship"]def convert(size, box): dw = 1./size[0] dh = 1./size[1] x = (box[0] + box[1])/2.0 y = (box[2] + box[3])/2.0 w = box[1] - box[0] h = box[3] - box[2] x = x*dw w = w*dw y = y*dh h = h*dh return (x,y,w,h)def convert_annotation(year, image_id): in_file = open('Annotations/%s.xml'%(image_id)) out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, image_id), 'w') tree=ET.parse(in_file) root = tree.getroot() size = root.find('size') w = int(size.find('width').text) h = int(size.find('height').text) for obj in root.iter('object'): #difficult = obj.find('difficult').text cls = obj.find('name').text #if cls not in classes or int(difficult) == 1: if cls not in classes: continue cls_id = classes.index(cls) xmlbox = obj.find('bndbox') b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) bb = convert((w,h), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')wd = getcwd()for year, image_set in sets: if not os.path.exists('VOCdevkit/VOC%s/labels/'%(year)): os.makedirs('VOCdevkit/VOC%s/labels/'%(year)) image_ids = open('ImageSets/Main/%s.txt'%(image_set)).read().strip().split() list_file = open('%s_%s.txt'%(year, image_set), 'w') for image_id in image_ids: list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id)) convert_annotation(year, image_id) list_file.close()
目录结构:
. ├── Annotations ├── ImageSets ├── JPEGImages ├── SeaShip_train.txt ├── SeaShip_val.txt ├── VOCdevkit └── voc_seaship.py
生成好后把label里面的.txt文件放进./VOCdevkit/VOCSeaShip/JPEGImages 里面这步很重要
e.g. 我用的是cuda10.1 和 pytorch 1.7
conda create -n yolov5 python=3.8
官方网址:
cd yolov5
pip install -r requirements.txt
我们用yolov5s做测试因为这个速度最快:
复制 data/voc.yaml文件并改为自己的名字,只修改nc: 和names:
只需要修改model/yolov5s.yaml里面的nc:即可
在yolov5的根目录下
python train.py --epochs 10 --cfg models/Shipyolov5s.yaml --data data/myshipvoc.yaml --weights yolov5s.pt --batch-size 32
测试
python detect.py --source data/images/ship-1.jpg --weights runs/train/exp14/weights/best.pt --conf 0.25
转载地址:http://ubbnn.baihongyu.com/