前言
1.前面的博文大概講了官方的VOC2007的內容結構與各個目錄的作用,接下來要講的是如何制作自己的VOC2007數據,并用于訓練。 2.制作VOC2007數據集的前準備是必須有包含要訓練的樣本的圖像,和LabelImg,LabelImg是用來標注數據用的。
一、創建文件目錄
1.創建VOC2007目錄,在VOC2007目錄下再創建三個空目錄,分別是Annotations、ImageSets、JPEGImages,此時VOC2007目錄下只有三個空的目錄。 2.在ImageSets目錄創建一個Main的目錄。 3.把所有要標注的圖像全部放進行JPEGImages目錄下。 4.對JPEGImages下的圖像進行重命名。 用python對整個文件下的圖像以遞增的方式進行命名,以下是python代碼,路徑改成自己的路徑,在終端運行就可以了。 在home目錄新建一個python腳本:
vim rename.py
輸入以下代碼,把路徑改成自己的路徑
import os
def rename():path="/home/matt/data/VOC2007/JPEGImages/"ex = 0filelist = os.listdir(path)count = 1for file in filelist:Olddir = os.path.join(path,file)if os.path.isdir(Olddir):continuefilename = os.path.splitext(file)[0]filetype = ".jpg"p = str(count).zfill(5)Newdir = os.path.join(path,str(ex)+p+filetype)os.rename(Olddir,Newdir)count += 1
rename()
保存,退出,在終端運行:
sudo python ./rename.py
完成之后,文件名字如下圖:
二、使用LabelImag標注數據
1.打開LabelImag標注工具,導入JPEGImages下的所有圖像。 在LabelImg目錄下,用終端運行
./labelImg.py
打開LabelImg工具,選擇打開目錄,選擇VOC2007/JPEGImages/。 LabelImg把所有圖像數據都讀入進來
2.選擇保存xml文件的路徑,這里要選擇VOC2007目錄下的Annotations文件夾,選擇要標注成的數據數據格式,這里選VOC。 3.開始標注數據。 打開一張圖像,創始區塊,然后用鼠標把要訓練的物體框選進去,框選完成之后會跳出一個標示框,輸入物體的名字,如果在整個圖像場景下比較難識別到該物體,則選擇有難度的,點OK。 然后保存 在VOC2007/Annotations目錄下會有一個與該文件名字相同的xml文件 打開文件可以看到里面的內容
<annotation><folder>JPEGImages</folder><filename>000000.jpg</filename><path>/home/linux/caffe/caffe_ssd/data/VOCdevkit/VOC2007/JPEGImages/000000.jpg</path><source><database>Unknown</database></source><size><width>700</width><height>504</height><depth>1</depth></size><segmented>0</segmented><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>156</xmin><ymin>109</ymin><xmax>168</xmax><ymax>130</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>177</xmin><ymin>150</ymin><xmax>191</xmax><ymax>170</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>220</xmin><ymin>134</ymin><xmax>243</xmax><ymax>144</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>270</xmin><ymin>101</ymin><xmax>291</xmax><ymax>113</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>317</xmin><ymin>100</ymin><xmax>336</xmax><ymax>112</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>486</xmin><ymin>127</ymin><xmax>499</xmax><ymax>153</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>551</xmin><ymin>143</ymin><xmax>573</xmax><ymax>157</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>591</xmin><ymin>162</ymin><xmax>603</xmax><ymax>182</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>521</xmin><ymin>163</ymin><xmax>535</xmax><ymax>181</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>82</xmin><ymin>213</ymin><xmax>96</xmax><ymax>234</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>128</xmin><ymin>228</ymin><xmax>148</xmax><ymax>240</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>188</xmin><ymin>247</ymin><xmax>205</xmax><ymax>266</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>243</xmin><ymin>281</ymin><xmax>267</xmax><ymax>292</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>391</xmin><ymin>241</ymin><xmax>407</xmax><ymax>270</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>416</xmin><ymin>214</ymin><xmax>427</xmax><ymax>233</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>457</xmin><ymin>236</ymin><xmax>482</xmax><ymax>250</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>577</xmin><ymin>292</ymin><xmax>598</xmax><ymax>304</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>616</xmin><ymin>306</ymin><xmax>632</xmax><ymax>327</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>543</xmin><ymin>306</ymin><xmax>559</xmax><ymax>331</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>211</xmin><ymin>296</ymin><xmax>227</xmax><ymax>322</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>282</xmin><ymin>297</ymin><xmax>298</xmax><ymax>319</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>115</xmin><ymin>313</ymin><xmax>136</xmax><ymax>343</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>523</xmin><ymin>254</ymin><xmax>535</xmax><ymax>277</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>416</xmin><ymin>372</ymin><xmax>429</xmax><ymax>393</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>414</xmin><ymin>414</ymin><xmax>428</xmax><ymax>435</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>349</xmin><ymin>438</ymin><xmax>373</xmax><ymax>452</ymax></bndbox></object><object><name>R</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>352</xmin><ymin>358</ymin><xmax>372</xmax><ymax>366</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>118</xmin><ymin>115</ymin><xmax>136</xmax><ymax>130</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>137</xmin><ymin>88</ymin><xmax>152</xmax><ymax>103</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>323</xmin><ymin>78</ymin><xmax>341</xmax><ymax>93</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>345</xmin><ymin>118</ymin><xmax>359</xmax><ymax>129</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>466</xmin><ymin>258</ymin><xmax>485</xmax><ymax>274</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>466</xmin><ymin>111</ymin><xmax>479</xmax><ymax>126</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>589</xmin><ymin>188</ymin><xmax>605</xmax><ymax>202</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>459</xmin><ymin>422</ymin><xmax>479</xmax><ymax>436</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>462</xmin><ymin>367</ymin><xmax>478</xmax><ymax>384</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>330</xmin><ymin>411</ymin><xmax>346</xmax><ymax>426</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>135</xmin><ymin>247</ymin><xmax>153</xmax><ymax>262</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>171</xmin><ymin>225</ymin><xmax>187</xmax><ymax>240</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>371</xmin><ymin>224</ymin><xmax>387</xmax><ymax>240</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>100</xmin><ymin>297</ymin><xmax>117</xmax><ymax>314</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>280</xmin><ymin>327</ymin><xmax>299</xmax><ymax>340</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>211</xmin><ymin>327</ymin><xmax>229</xmax><ymax>340</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>504</xmin><ymin>236</ymin><xmax>518</xmax><ymax>252</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>330</xmin><ymin>368</ymin><xmax>348</xmax><ymax>383</ymax></bndbox></object><object><name>J</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>187</xmin><ymin>82</ymin><xmax>254</xmax><ymax>131</ymax></bndbox></object><object><name>J</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>390</xmin><ymin>92</ymin><xmax>447</xmax><ymax>141</ymax></bndbox></object><object><name>J</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>532</xmin><ymin>84</ymin><xmax>587</xmax><ymax>133</ymax></bndbox></object><object><name>J</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>225</xmin><ymin>220</ymin><xmax>284</xmax><ymax>266</ymax></bndbox></object><object><name>J</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>557</xmin><ymin>226</ymin><xmax>614</xmax><ymax>280</ymax></bndbox></object><object><name>D</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>259</xmin><ymin>386</ymin><xmax>278</xmax><ymax>404</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>378</xmin><ymin>413</ymin><xmax>394</xmax><ymax>427</ymax></bndbox></object><object><name>C</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>377</xmin><ymin>369</ymin><xmax>395</xmax><ymax>383</ymax></bndbox></object>
</annotation>
關于這個文件的內容說明,請看之前關于官方數據說明的那部分,這里就不重新再說明了。 然后點下一個圖像,繼續以上的操作直到所有的圖像都標示完成。
三、生成相關的txt文件
1.把所有的圖像都標注完成之后,在main目錄下使用python腳本生成存放訓練與測試信息的相關txt文件,路徑改成自己的路徑。
import os
import random xmlfilepath=r'/home/matt/data/VOC2007/Annotations/'
saveBasePath=r"/home/matt/data/" trainval_percent=0.8
train_percent=0.8
total_xml = os.listdir(xmlfilepath)
num=len(total_xml)
list=range(num)
tv=int(num*trainval_percent)
tr=int(tv*train_percent)
trainval= random.sample(list,tv)
train=random.sample(trainval,tr) print("train and val size",tv)
print("traub suze",tr)
ftrainval = open(os.path.join(saveBasePath,'VOC2007/ImageSets/Main/trainval.txt'), 'w')
ftest = open(os.path.join(saveBasePath,'VOC2007/ImageSets/Main/test.txt'), 'w')
ftrain = open(os.path.join(saveBasePath,'VOC2007/ImageSets/Main/train.txt'), 'w')
fval = open(os.path.join(saveBasePath,'VOC2007/ImageSets/Main/val.txt'), 'w') for i in list: name=total_xml[i][:-4]+'\n' if i in trainval: ftrainval.write(name) if i in train: ftrain.write(name) else: fval.write(name) else: ftest.write(name) ftrainval.close()
ftrain.close()
fval.close()
ftest .close()
2.運行上面的python腳本,在main目錄下生成四個txt文件
結語
1.以上的操作完成之后,就得到了VOC2007格式的數據集,接下來要做的事是把數據集轉換成lmdb數據格式,步驟就跟處理之前處理VOC2007數據一樣了。 2.我使用的圖像數據是我同學收集和整理的,所以我就不上傳上來了,如果有需要的話,可以加這個群(487350510)。
總結
以上是生活随笔 為你收集整理的Ubuntu 16.04下Caffe-SSD的应用(七)——制作自己的VOC2007数据集 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。