tf.lookup.StaticHashTable 用法
生活随笔
收集整理的這篇文章主要介紹了
tf.lookup.StaticHashTable 用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
tf.lookup.StaticHashTable 本質是tensorflow 內置字典,在yolov3 tf代碼中多次應用
def load_tfrecord_dataset(file_pattern, class_file, size=416):LINE_NUMBER = -1 # TODO: use tf.lookup.TextFileIndex.LINE_NUMBERclass_table = tf.lookup.StaticHashTable(tf.lookup.TextFileInitializer(class_file, tf.string, 0, tf.int64, LINE_NUMBER, delimiter="\n"), -1)files = tf.data.Dataset.list_files(file_pattern)dataset = files.flat_map(tf.data.TFRecordDataset)return dataset.map(lambda x: parse_tfrecord(x, class_table, size))###### 在當前目錄下新建文件 voc2012.names
aeroplane bicycle bird boat bottle bus car cat chair cow diningtable dog horse motorbike person pottedplant sheep sofa train tvmonitor import tensorflow as tf class_table = tf.lookup.StaticHashTable(tf.lookup.TextFileInitializer(class_file, tf.string, 0, tf.int64, LINE_NUMBER, delimiter="\n"), -1) class_table.lookup(tf.constant(['cat','person'])) <tf.Tensor: shape=(2,), dtype=int64, numpy=array([ 7, 14])> class_table.export() (<tf.Tensor: shape=(20,), dtype=string, numpy=array([b'cat', b'chair', b'dog', b'person', b'bird', b'motorbike',b'bottle', b'car', b'bus', b'sheep', b'boat', b'train',b'aeroplane', b'pottedplant', b'sofa', b'tvmonitor', b'cow',b'diningtable', b'horse', b'bicycle'], dtype=object)>,<tf.Tensor: shape=(20,), dtype=int64, numpy=array([ 7, 8, 11, 14, 2, 13, 4, 6, 5, 16, 3, 18, 0, 15, 17, 19, 9,10, 12, 1])>)總結
以上是生活随笔為你收集整理的tf.lookup.StaticHashTable 用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tf.data.Dataset 用法
- 下一篇: yolov3-tf2 数据格式压缩