【⽬标检测】Yolov5训练Crowdhuman数据集
这篇⽂章的⽬的,1.解析Crowdhuman数据集;2.准备Crowdhuman相应配置,训练yolov5。
其中我的是将273271,,全部写为273271_
1.环境
ubuntu16.04
cuda10.1
cudnn7
python3.6
Cython
matplotlib>=3.2.2
numpy>=1.18.5
opencv-python>=4.1.2
Pillow
PyYAML>=5.3
scipy>=1.4.1
tensorboard>=2.2
torch>=1.7.0(my1.7.1)
torchvision>=0.8.1(my0.8.2)
tqdm>=4.41.0
aborn>=0.11.0
easydict
thop
pycocotools>=2.0
2.下载
(1)crowdhuman数据集:
(2)yolov5的⼯程
(3)yolov5预训练模型下载
放到yolov5/weights⽬录下
3.解析Crowdhuman数据集
这⾥有两个参考:参考的处理crowdhuman数据集的源码,另⼀个是⼀篇应⽤的博客。
由于我要的图⽚格式和作者的稍微有点不同,但是还是需要花点时间调试⼀下。⽐如⽬录结构,以及odgt中图⽚名称等。下⾯进⼊正题。
(1)⾸先将下载的源码YOLOv5-Tools/CrowHuman2YOLO/data⽬录下的数据,放到yolov5/data⽬录下:
(2)将下载完成的数据,放到raw⽬录下
(3)修改脚本和
#prepare_47-48inrtthis:
cd../crowdhuman-$1/
forfilein`ls|`
do
newfile=`echo$file|d's/,/_/g'`
mv$file$newfile
echoRenamefile:$newfile...
done
#gen_geline80:
ID=anno['ID']#e.g.'273271,c9db000d5146c15'
#to
ID=anno['ID'].replace(',','_')#e.g.'273271,c9db000d5146c15'
完整代码和:
#!/bin/bash
#prepare_
t-e
#checkargument
if[[-z$1||!$1=~[[:digit:]]x[[:digit:]]]];then
echo"ERROR:Thisscriptrequires1argument,"inputdimension"oftheYOLOmodel."
echo"Theinputdimensionshouldbe{width}x{height}suchas608x608or416x256.".
exit1
fi
ifwhichpython3>/dev/null;then
PYTHON=python3
el
PYTHON=python
fi
pushd$(dirname$0)/raw>/dev/null
get_file()
{
#dodownloadonlyifthefiledoesnotexist
if[[-f$2]];then
echoSkipping$2
el
echoDownloading$2...
$1
fi
}
echo"**Downloaddatatfiles"
#unzipimagefiles(ignoreCrowdHuman_now)
echo"**Unzipdatatfiles"
forfinCrowdHuman_wdHuman_wdHuman_wdHuman_;do
unzip-n${f}
done
echo"**Createthecrowdhuman-$1/subdirectory"
rm-rf../crowdhuman-$1/
mkdir../crowdhuman-$1/
lnImages/*.jpg../crowdhuman-$1/
#thecrowdhuman/subdirectorynowcontainsalltrain/valjpgimages
#changefile','with'_'
cd../crowdhuman-$1/
forfilein`ls|`
do
newfile=`echo$file|d's/,/_/g'`
mv$file$newfile
echoRenamefile:$newfile...
done
echo"**Generateyolotxtfiles"
cd..
${PYTHON}gen_$1
popd>/dev/null
echo"**Done."
"""gen_
TogenerateYOLOtxtfilesfromtheoriginalCrowdHumanannotations.
isdirectory.
Inputs:
*raw/annotation_
*raw/annotation_
*crowdhuman-{width}x{height}/[IDs].jpg
Outputs:
*crowdhuman-{width}x{height}
*crowdhuman-{width}x{height}/
*crowdhuman-{width}x{height}/[IDs].txt(oneannotationforeachimageinthetrainingortestt)
"""
importjson
frompathlibimportPath
fromargparimportArgumentParr
fromargparimportArgumentParr
importnumpyasnp
importcv2
#inputimagewidth/heightoftheyolov4model,tbycommand-lineargument
INPUT_WIDTH=0
INPUT_HEIGHT=0
#Minimumwidth/heightofobjectsfordetection(don'tlearnfrom
#objectssmallerthanthe
MIN_W=5
MIN_H=5
#DoK-Meansclusteringinordertodetermine"anchor"sizes
DO_KMEANS=True
KMEANS_CLUSTERS=9
BBOX_WHS=[]#keeptrackofbboxwidth/heightwithrespectto608x608
defimage_shape(ID,image_dir):
asrtimage_dirisnotNone
jpg_path=image_dir/('%'%ID)
img=(jpg__posix())
deftxt_line(cls,bbox,img_w,img_h):
"""Generate1lineinthetxtfile."""
asrtINPUT_WIDTH>0andINPUT_HEIGHT>0
x,y,w,h=bbox
x=max(int(x),0)
y=max(int(y),0)
w=min(int(w),img_w-x)
h=min(int(h),img_h-y)
w_rescaled=float(w)*INPUT_WIDTH/img_w
h_rescaled=float(h)*INPUT_HEIGHT/img_h
ifw_rescaled
return''
el:
ifDO_KMEANS:
globalBBOX_WHS
BBOX_((w_rescaled,h_rescaled))
cx=(x+w/2.)/img_w
cy=(y+h/2.)/img_h
nw=float(w)/img_w
nh=float(h)/img_h
return'%d%.6f%.6f%.6f%.6fn'%(cls,cx,cy,nw,nh)
defprocess(t_='test',annotation_filename='raw/annotation_',
output_dir=None):
"""Procesither'train'or'test't."""
asrtoutput_dirisnotNone
output_(exist_ok=True)
jpgs=[]
withopen(annotation_filename,'r')asfanno:
forraw_nes():
anno=(raw_anno)
ID=anno['ID'].raplace(',','_')#e.g.'273271_c9db000d5146c15'
print('ProcessingID:%s'%ID)
img_h,img_w,img_c=image_shape(ID,output_dir)
asrtimg_c==3#shouldbeaBGRimage
txt_path=output_dir/('%'%ID)
#writeatxtforeachimage
#writeatxtforeachimage
withopen(txt__posix(),'w')asftxt:
forobjinanno['gtboxes']:
ifobj['tag']=='mask':
continue#ignorenon-human
asrtobj['tag']=='person'
if'hbox'():#head
line=txt_line(0,obj['hbox'],img_w,img_h)
ifline:
(line)
if'fbox'():#fullbody
line=txt_line(1,obj['fbox'],img_w,img_h)
ifline:
(line)
('data/%s/%'%(output_dir,ID))
#writethe'data/crowdhuman/'or'data/crowdhuman/'
t_path=output_dir/('%'%t_)
withopen(t__posix(),'w')asft:
forjpginjpgs:
('%sn'%jpg)
defrm_txts(output_dir):
"""Removetxtfilesinoutput_dir."""
fortxtinoutput_('*.txt'):
_file():
()
defmain():
globalINPUT_WIDTH,INPUT_HEIGHT
parr=ArgumentParr()
_argument('dim',help='inputwidthandheight,e.g.608x608')
args=_args()
dim_split=('x')
iflen(dim_split)!=2:
raiSystemExit('ERROR:badspecofinputdim(%s)'%)
INPUT_WIDTH,INPUT_HEIGHT=int(dim_split[0]),int(dim_split[1])
ifINPUT_WIDTH%32!=0orINPUT_HEIGHT%32!=0:
raiSystemExit('ERROR:badspecofinputdim(%s)'%)
output_dir=Path('crowdhuman-%s'%)
ifnotoutput__dir():
raiSystemExit('ERROR:%sdoesnotexist.'%output__posix())
rm_txts(output_dir)
process('test','raw/annotation_',output_dir)
process('train','raw/annotation_',output_dir)
withopen('crowdhuman-%'%,'w')asf:
("""class=2
train=data/crowdhuman-%s/
valid=data/crowdhuman-%s/
names=data/
backup=backup/n"""%(,))
ifDO_KMEANS:
try:
rimportKMeans
exceptModuleNotFoundError:
print('WARNING:nosklearn,skippinganchorclustering...')
el:
X=(BBOX_WHS)
kmeans=KMeans(n_clusters=KMEANS_CLUSTERS,random_state=0).fit(X)
kmeans=KMeans(n_clusters=KMEANS_CLUSTERS,random_state=0).fit(X)
centers=r_centers_
centers=centers[centers[:,0].argsort()]#sortbybboxw
print('n**foryolov5-%dx%d,'%(INPUT_WIDTH,INPUT_HEIGHT),end='')
print('resizedbboxwidth/heightclustersare:',end='')
print(''.join(['(%.2f,%.2f)'%(c[0],c[1])forcincenters]))
print('nanchors=',end='')
print(','.join(['%d,%d'%(int(c[0]),int(c[1]))forcincenters]))
if__name__=='__main__':
main()
(4)运⾏数据处理脚本
bash./prepare_608x608
4.训练
添加⼀个yaml配置⽂件在data⽬录下:
#
train:"./data/crowdhuman-608x608/"
val:"./data/crowdhuman-608x608/"
nc:2
names:["head","person"]
进⼊yolov5⽬录下,运⾏以下脚本(其中models中选择的模型⼤⼩配置⽂件,与weights中的预训练模型应该是⼀致):
--datadata/--cfgmodels/--weightsweights/--device0
这⾥就欧克了。
本文发布于:2022-12-29 14:28:25,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/53630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |