TFrecord创建错误

时间:2018-03-11 14:07:22

标签: python tensorflow tfrecord

尝试为输入图像文件创建tfrecords。尝试了两个不同版本的Tfrecord创建脚本,遇到了两个不同的问题。以下是详细信息。

  

Verison 1:单个图像的硬编码图像路径,高度和宽度。

结果:为单个图像文件生成Tfrecord "发生了异常,使用%tb查看完整的回溯。 SystemExit"

  

版本2:一组5个图像 - 它们的路径,高度和重量 - 作为函数调用传递给tfrecord创建。

结果:为五个图像中的第一个生成Tfrecord,然后在Jupyter的QTConsole中生成内核死亡消息。

  

版本1代码:

###
###

def create_tf_example(label_and_data_info):
  # TODO START: Populate the following variables from your example.
  height = 256 # Image height - hardcoded
  width = 256 # Image width - hardcoded
  filename = 'C:\\Users\\Sethu\\Desktop\\Encoding\\0b2e702f90aee4fff2bc6e4326308d50cf04701082e718d4f831c8959fbcda93.png' # Filename of the image - hardcoded
  filename = filename.encode()
  with tf.gfile.GFile(filename, 'rb') as fid:
        encoded_image = fid.read()
  encoded_image_data = encoded_image # Encoded image bytes
  image_format = b'png' # b'jpeg' or b'png'

  xmins = [56/256] # List of normalized left x coordinates in bounding box 
  xmaxs = [200/256] # List of normalized right x coordinates in bounding box
  ymins = [56/256] # List of normalized top y coordinates in bounding box
  ymaxs = [200/256] #List of normalized bottom y coordinates in bounding box
  classes_text = ['a'.encode()] # List of string class name of bounding box 
  classes = [1] # List of integer class id of bounding 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)

  # TODO: Write code to read in your dataset to examples variable

  tf_example = create_tf_example('e') ## passing a dummy value to the function. All function values have been hard-coded.
  writer.write(tf_example.SerializeToString())

  writer.close()

  if __name__ == '__main__':
  tf.app.run()

###
###
  

第2版代码:

def create_tf_example(h, w , p):
  # TODO START: Populate the following variables from your example.
  height = h # Image height
  width = w # Image width
  filename = p # Filename of the image. Empty if image is not from file
  filename = filename.encode()
  with tf.gfile.GFile(filename, 'rb') as fid:
        encoded_image = fid.read()
  encoded_image_data = encoded_image # Encoded image bytes
  image_format = b'png' # b'jpeg' or b'png'

  xmins = [56/256] # List of normalized left x coordinates in bounding box
  xmaxs = [200/256] # List of normalized right x coordinates in bounding box
  ymins = [56/256] # List of normalized top y coordinates in bounding box 
  ymaxs = [200/256] # List of normalized bottom y coordinates in bounding box
  classes_text = ['a'.encode()] # List of string class name of bounding box 
  classes = [1] # 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)
    for i in range(5): 
        p , h , w = Orig_images[i] # p - path, h - height, w - width of the image
    tf_example = create_tf_example(h,w,p)
    writer.write(tf_example.SerializeToString())
    writer.close()
if __name__ == '__main__':
tf.app.run()

导致'系统退出的原因是什么?和内核死了#39;错误?如何生成tfrecord?

0 个答案:

没有答案