VOC資料集用於製作tensorflow所需格式
阿新 • • 發佈:2018-11-10
VOC資料集
VOC主要有三個重要的資料夾:Annotations、ImageSets和JPEGImages。分別是標註資訊、分割資訊和原始圖片等。
公開資料集有現成的對應檔案,前人已經做好標註工作。
在初步訓練網路時,選擇該組比較完備的資料,確實是一個比較快捷的方法。
XML到CSV格式
以下是一個標準的VOC格式寫法,貼上了一個VOCtrainval_11-May-2012資料夾下的XML格式檔案。
可以看到,除了基本尺寸,目標所在位置之外,還有part等資訊,可能是用於目標特徵級聯。
我們暫時不需要,直接的XML轉CSV格式會報錯,因為存在多個xmin等資料。
以下是修改後的用於VOC格式資料集的XML轉CSV的程式碼,主要修改部分在物體所在位置的資料讀取,我在原始碼上做了修改,這部分在Python可執行。
''' import os import glob import pandas as pd import xml.etree.ElementTree as ET os.chdir('D:\\python_objectdetection\\models-master\\models-master\\research\\object_detection\\images_voc\\test') path = 'D:\\python_objectdetection\\models-master\\models-master\\research\\object_detection\\images_voc\\test' def xml_to_csv(path): xml_list = [] for xml_file in glob.glob(path + '/*.xml'): tree = ET.parse(xml_file) root = tree.getroot() for member in root.findall('object'): value = (root.find('filename').text, int(root.find('size')[0].text), int(root.find('size')[1].text), member[0].text, # 原始碼長這樣,[4][0]大概意思:第4個節點的幾個成員;從0 # int(member[4][0].text), # int(member[4][1].text), # int(member[4][2].text), # int(member[4][3].text), # # 因為是節點,所以這樣的形式訪問 int(member.find('bndbox')[0].text), int(member.find('bndbox')[1].text), int(member.find('bndbox')[2].text), int(member.find('bndbox')[3].text), ) xml_list.append(value) print(value[3]) column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax'] xml_df = pd.DataFrame(xml_list, columns=column_name) return xml_df def main(): image_path = path xml_df = xml_to_csv(image_path) xml_df.to_csv('test.csv', index=None) print('Successfully converted xml to csv.') main()
樣本類別統計
畢竟公開資料集,類別是先前標記,在初步手寫各個類別時候,存在漏寫情形,也會導致報錯。
以下檔案用於統計CSV檔案中所有的類別,C++寫的。
//統計CSV格式資料集中所有被包含的物體類別 #include<fstream> #include<map> #include<string> #include<vector> #include<iostream> using namespace std; #include<sstream> //istringstream 必須包含這個標頭檔案 int main() { ifstream fin("D:\\python_objectdetection\\models-master\\models-master\\research\\object_detection\\data\\test_voc.csv"); //開啟檔案流操作 string line; vector<string>classes; while (getline(fin, line)) //整行讀取,換行符“\n”區分,遇到檔案尾標誌eof終止讀取 { istringstream stream_in(line); //將整行字串line讀入到字串流istringstream中 vector<string> fields; //宣告一個字串向量 string field; while (getline(stream_in, field, ',')) //將字串流sin中的字元讀入到field字串中,以逗號為分隔符 { fields.push_back(field); //將剛剛讀取的字串新增到向量fields中 } vector<string>::iterator iter_class = find(classes.begin(), classes.end(), fields[3]); if (iter_class == classes.end()) { classes.push_back(fields[3]); } } fin.close(); //把各類別寫在另一個CSV格式檔案 ofstream file("D:\\python_objectdetection\\models-master\\models-master\\research\\object_detection\\data\\test_voc_classes.csv"); for (int i = 1; i < classes.size(); i++) { file << classes[i] << endl; } }
在統計完類別後,csv到tfrecord格式轉換,在類別宣告部分修改後,即可執行。
當然,也有比較工具化的,csv檔案右鍵點選篩選即可:
tensorflow下pbtxt檔案
訓練時候,資料集的配置流程:
- 新建圖片所在資料夾,儲存對應的train和test圖片;
- 新建data資料夾,存放CSV檔案和生成的record格式,以及類別宣告檔案,也就是那個pbtxt格式的檔案;
- 新建train資料夾,存放配置環境,也就是那個類似ssd_mobilenet_v1_coco.config的檔案,訓練過程中,模型可以在此資料夾下儲存。
如果你也恰好在用這組公開資料集,那偷個懶直接copy都可以的,我這邊生成的相應pbtxt格式檔案在此:
//該組資料對應pbtxt格式檔案
item {
name: "person"
id: 1
display_name: "person"
}
item {
name: "aeroplane"
id: 2
display_name: "aeroplane"
}
item {
name: "tvmonitor"
id: 3
display_name: "tvmonitor"
}
item {
name: "train"
id: 4
display_name: "train"
}
item {
name: "boat"
id: 5
display_name: "boat"
}
item {
name: "dog"
id: 6
display_name: "dog"
}
item {
name: "chair"
id: 7
display_name: "chair"
}
item {
name: "bird"
id: 8
display_name: "bird"
}
item {
name: "bicycle"
id: 9
display_name: "bicycle"
}
item {
name: "bottle"
id: 10
display_name: "bottle"
}
item {
name: "sheep"
id: 11
display_name: "sheep"
}
item {
name: "diningtable"
id: 12
display_name: "diningtable"
}
item {
name: "horse"
id: 13
display_name: "horse"
}
item {
name: "motorbike"
id: 14
display_name: "motorbike"
}
item {
name: "sofa"
id: 15
display_name: "sofa"
}
item {
name: "cow"
id: 16
display_name: "cow"
}
item {
name: "car"
id: 17
display_name: "car"
}
item {
name: "cat"
id: 18
display_name: "cat"
}
item {
name: "bus"
id: 19
display_name: "bus"
}
item {
name: "pottedplant"
id: 20
display_name: "pottedplant"
}
一切資料準備OK,等待訓練模型吧。