Как проверить, что преобразование метки в tfrecord прошло успешно или нет?

Я преобразовал изображение и координаты прямоугольника в виде файла TFRecord. Как убедиться, что в файле, который я получил, есть необходимые данные, т.е. преобразование метки прошло успешно? Ограничительный центр, высота, ширина, идентификатор класса были сохранены в текстовом файле. Я прочитал эти данные в списки, чтобы получить xmin, xmax, ymin, ymax. А потом я попытался преобразовать его в файл tfrecord.

import base64
import tensorflow as tf
from object_detection.utils import dataset_util

flags = tf.app.flags
flags.DEFINE_string('output_path','D:\\filecord.tfrecords', 'Path to output TFRecord')
FLAGS = flags.FLAGS

def create_tf_example():
  # TODO START: Populate the following variables from your example.
  with open(r'C:\Users\SP-TestMc\Downloads\task1_folder\23324_PID_7660_000.txt', 'r') as file:
     rows = [[float(x) for x in line.split(' ')] for line in file]  
     cols = [list(col) for col in zip(*rows)]
  l=len(cols[0])
  xmin=[]
  xmax=[]
  ymin=[]
  ymax=[]
  cid=[]
  for i in range(l):
      xmin.append(cols[1][i]-cols[3][i]/2)
      xmax.append(cols[1][i]+cols[3][i]/2)
      ymin.append(cols[2][i]-cols[4][i]/2)
      ymax.append(cols[2][i]+cols[4][i]/2)
      cid.append(int(cols[0][i]))




  height = 416 # Image height
  width = 416 # Image width
  filename =r'C:\Users\SP-TestMc\Downloads\task1_folder\23324_PID_7660_000.png'  # Filename of the image. Empty if image is not from file
  filename=filename.encode()

  img_file = open(filename, 'rb')
  encoded_image_data = base64.b64encode(img_file.read())
  image_format = b'png' # b'jpeg' or b'png'

  xmins = xmin # List of normalized left x coordinates in bounding box (1 per box)
  xmaxs = xmax # List of normalized right x coordinates in bounding box
             # (1 per box)
  ymins = ymin # List of normalized top y coordinates in bounding box (1 per box)
  ymaxs = ymax # List of normalized bottom y coordinates in bounding box
             # (1 per box)
  classes_text = ['a'.encode(), 'a'.encode(),'b'.encode(),'b'.encode()] # List of string class name of bounding box (1 per box)
  classes = cid # 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={
      'image/height': dataset_util.int64_feature(height),
      'image/width': dataset_util.int64_feature(width),
      'image/filename': dataset_util.bytes_feature(filename),
      'image/source_id': dataset_util.bytes_feature(filename),
      'image/encoded': dataset_util.bytes_feature(encoded_image_data),
      'image/format': dataset_util.bytes_feature(image_format),
      'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
      'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
      'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
      'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
      'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
      'image/object/class/label': dataset_util.int64_list_feature(classes),
  }))
  return tf_label_and_data
def main(_):
  writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
  tf_example = create_tf_example()
  writer.write(tf_example.SerializeToString())

  writer.close()

a=None
main(a)

0 ответов

Другие вопросы по тегам