yolov3 -tf 解析数据

https://pan.baidu.com/s/19n-l9hg9v0pfdBAEhS5E3A
提取码: r7ec

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun  9 12:08:34 2021

@author: ledi
"""

import tensorflow as tf


def transform_images(x_train, size):
    x_train = tf.image.resize(x_train, (size, size))
    x_train = x_train / 255
    return x_train


# https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/using_your_own_dataset.md#conversion-script-outline-conversion-script-outline
# Commented out fields are not required in our project
IMAGE_FEATURE_MAP = {
    # 'image/width': tf.io.FixedLenFeature([], tf.int64),
    # 'image/height': tf.io.FixedLenFeature([], tf.int64),
    # 'image/filename': tf.io.FixedLenFeature([], tf.string),
    # 'image/source_id': tf.io.FixedLenFeature([], tf.string),
    # 'image/key/sha256': tf.io.FixedLenFeature([], tf.string),
    'image/encoded': tf.io.FixedLenFeature([], tf.string),
    # 'image/format': tf.io.FixedLenFeature([], tf.string),
    'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),
    'image/object/bbox/ymin': tf.io.VarLenFeature(tf.float32),
    'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),
    'image/object/bbox/ymax': tf.io.VarLenFeature(tf.float32),
    'image/object/class/text': tf.io.VarLenFeature(tf.string),
    # 'image/object/class/label': tf.io.VarLenFeature(tf.int64),
    # 'image/object/difficult': tf.io.VarLenFeature(tf.int64),
    # 'image/object/truncated': tf.io.VarLenFeature(tf.int64),
    # 'image/object/view': tf.io.VarLenFeature(tf.string),
}


def parse_tfrecord(tfrecord, class_table, size):
    x = tf.io.parse_single_example(tfrecord, IMAGE_FEATURE_MAP)
    x_train = tf.image.decode_jpeg(x['image/encoded'], channels=3)
    x_train = tf.image.resize(x_train, (size, size))
    print( x_train)

    class_text = tf.sparse.to_dense(
        x['image/object/class/text'], default_value='')
    labels = tf.cast(class_table.lookup(class_text), tf.float32)
    y_train = tf.stack([tf.sparse.to_dense(x['image/object/bbox/xmin']),
                        tf.sparse.to_dense(x['image/object/bbox/ymin']),
                        tf.sparse.to_dense(x['image/object/bbox/xmax']),
                        tf.sparse.to_dense(x['image/object/bbox/ymax']),
                        labels], axis=1)
    # print('FLAGS.yolo_max_boxes=',FLAGS.yolo_max_boxes)
    paddings = [[0, 100 - tf.shape(y_train)[0]], [0, 0]]
    # paddings = [[0, FLAGS.yolo_max_boxes - tf.shape(y_train)[0]], [0, 0]]
    y_train = tf.pad(y_train, paddings)

    return x_train, y_train


"""
count=0
for k in files:
    if count<10:
        print(k)
    count+=1 
"""


def load_tfrecord_dataset(file_pattern, class_file, size=416):
    #file_pattern, class_file, size='./data/voc2012_train.tfrecord','./data
    LINE_NUMBER = -1  # TODO: use tf.lookup.TextFileIndex.LINE_NUMBER
    class_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))


train_dataset = load_tfrecord_dataset(
       './data/voc2012_train.tfrecord','./data/voc2012.names', 416)

count=0
for k in train_dataset:
    if count<3:
        print(k)
    count+=1 

输出结果如下
(<tf.Tensor: shape=(416, 416, 3), dtype=float32, numpy=
array([[[255.      , 255.      , 255.      ],
        [255.      , 255.      , 255.      ],
        [255.      , 255.      , 255.      ],
        ...,
        [201.51099 , 204.51099 , 247.51099 ],
        [202.67535 , 205.67535 , 248.67535 ],
        [202.96875 , 205.96875 , 248.96875 ]],

       [[255.      , 255.      , 255.      ],
        [255.      , 255.      , 255.      ],
        [255.      , 255.      , 255.      ],
        ...,
        [202.375   , 205.375   , 248.375   ],
        [202.30965 , 205.30965 , 248.17892 ],
        [202.19696 , 205.19696 , 248.00946 ]],

       [[255.      , 255.      , 255.      ],
        [255.      , 255.      , 255.      ],
        [255.      , 255.      , 255.      ],
        ...,
        [205.84375 , 209.      , 251.21875 ],
        [205.36447 , 208.52072 , 249.56303 ],
        [204.39767 , 207.55392 , 248.08517 ]],


        ...,
        [ 79.83946 ,  75.82988 ,  70.3127  ],
        [ 75.77214 ,  72.77214 ,  65.72856 ],
        [ 80.510895,  77.510895,  70.448395]]], dtype=float32)>, <tf.Tensor: shape=(100, 5), dtype=float32, numpy=
array([[ 0.106     ,  0.19683258,  0.942     ,  0.95022625, 12.        ],
       [ 0.316     ,  0.09954751,  0.578     ,  0.37782806, 14.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
  ....
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ]],
      dtype=float32)>)

posted @ 2022-08-19 22:49  luoganttcc  阅读(18)  评论(0)    收藏  举报