使用TensorFlow一步步進行目標檢測 2

2021-08-21 19:40:06 字數 3186 閱讀 2618

資料集標籤

tensorflow目標檢測api要求所有標記的訓練資料都採用tfrecord檔案格式。如果我們的資料集如pascal voc資料集那樣附帶儲存在單個.xml檔案中的標籤,那麼我們可以使用名為create_pascal_tf_record.py的檔案(可能需要稍作修改)將資料集轉換為tfrecord檔案。

不幸的是,我們必須編寫自己的指令碼以從資料集建立tfrecord檔案。bosch資料集的標籤全部儲存在單個.yaml檔案中,其片段如下所示:

-boxes:

- -

path: ./rgb/train/2015-10-05-16-02-30_bag/720654.png

-boxes:

path: ./rgb/train/2015-10-05-16-02-30_bag/720932.png

tfrecord將整個資料集的所有標籤(邊界框)和影象組合到乙個檔案中。雖然建立tfrecord檔案有點痛苦,但一旦建立了它就非常方便。

建立單個的tfrecord條目

tensorflow在檔案using_your_own_dataset.md中為我們提供了乙個示例指令碼:

def

create_tf_example

(label_and_data_info):

# todo start: populate the following variables from your example.

height = none

# image height

width = none

# image width

filename = none

# filename of the image. empty if image is not from file

encoded_image_data = none

# encoded image bytes

image_format = none

# b'jpeg' or b'png'

xmins = # list of normalized left x coordinates in bounding box (1 per box)

xmaxs = # list of normalized right x coordinates in bounding box

# (1 per box)

ymins = # list of normalized top y coordinates in bounding box (1 per box)

ymaxs = # list of normalized bottom y coordinates in bounding box

# (1 per box)

classes_text = # list of string class name of bounding box (1 per box)

classes = # list of integer class id of bounding box (1 per box)

# todo end

tf_label_and_data = tf.train.example(features=tf.train.features(feature=))

return tf_label_and_data

上面的函式給出了從.yaml檔案中提取單個影象的標籤和資料資訊。使用此資訊,您需要編寫**來填充所有給定的變數。請注意,除了邊界框和類資訊之外,還必須提供編碼影象資料,這可以使用tensorflow.gifle.gfile()函式實現。填充所有這些變數後,您就可以轉到指令碼的第二部分了。

建立整個tfrecord檔案

完成create_tf_record函式後,您只需建立乙個迴圈來為資料集中的每個標籤呼叫該函式。

import tensorflow as tf

from object_detection.utils import dataset_util

flags.define_string('output_path', '', 'path to output tfrecord')

flags = flags.flags

defcreate_tf_example

(data_and_label_info):

......

return tf_data_and_label

defmain

(_):

writer = tf.python_io.tfrecordwriter(flags.output_path)

# todo start: write code to read in your dataset to examples variable

file_loc = none

all_data_and_label_info = load(file_loc)

# todo end

for data_and_label_info in all_data_and_label_info:

tf_example = create_tf_example(data_and_label_info)

writer.write(tf_example.serializetostring())

writer.close()

if __name__ == '__main__':

完成後以上**後,您就可以執行指令碼了。如果您想檢視完整的示例,anthony sarkis對bosch資料集的tfrecord指令碼有乙個非常完整的實現。

如果之前未修改.bashrc檔案,請確保在執行此指令碼之前在終端視窗中執行export pythonpath語句。在包含tfrecord指令碼的資料夾中,並將資料(影象)放在.yaml(或包含影象路徑的其他檔案)中列出的相同位置,執行以下命令。

python tf_record.py --output_path training.record
為確保我們正確完成了所有操作,可以將建立的訓練記錄檔案的大小與包含所有訓練影象的資料夾的大小進行比較。如果它們幾乎完全相同,那就完成了!

您的資料集可能會有乙個單獨的訓練和評估資料集,請確保為每個檔案建立單獨的tfrecord檔案。

一步步學ROS

最近因為看svo的 裡面用到catkin決定要好好看ros,年前學會基本操作。啟動節點 rosrun package name executable name 檢視節點 rosnode list 注 rosout 節點是乙個特殊的節點,通過 roscore 自動啟動 檢視特定節點的資訊 rosnod...

windows Thrift c 一步步搭建

1.thrift 原始碼路徑 2.libevent原始碼路徑 3.boost路徑 安裝 conan install boost 1.68.0 conan stable 4.openssl路徑 安裝 conan install openssl 1.1.1a conan stable conan安裝bo...

一步步啟動linux

可以一步一步啟動linux.在ubantu剛一啟動時,按c健即進入grub 提示符狀態,在此狀態下輸入 我用的是ubuntu 13 grub linux vmlinuz grub ls boot grub initrd boot initrd.img 3.11.0 15 generic grub b...