如何以与FSNS数据集相同的格式创建数据集?

Jia*_*ang 9 dataset tensorflow

我正在基于TensorFlow 开展这个项目.

我只想基于我自己的数据集通过attention_ocr训练OCR模型,但我不知道如何以与FSNS数据集相同的格式存储我的图像和基础事实.

有没有人也在这个项目上工作或知道如何解决这个问题?

Ale*_*ban 18

存储培训/测试的数据格式在FSNS文件https://arxiv.org/pdf/1702.03970.pdf(表4)中定义.

要使用tf.Example protos存储tfrecord文件,您可以使用tf.python_io.TFRecordWriter.有一个很好的教程,stackoverflow上的现有答案和一个简短的要点.

假设你有一个numpy的ndarray img其具有num_of_views所存储的图像并排侧(参照图3中的.): 在此输入图像描述 和变量中的相应文本text.您需要定义一些函数来将unicode字符串转换为填充到固定长度且未填充的字符ID列表.例如:

char_ids_padded, char_ids_unpadded = encode_utf8_string(
   text='abc', 
   charset={'a':0, 'b':1, 'c':2},
   length=5,
   null_char_id=3)
Run Code Online (Sandbox Code Playgroud)

结果应该是:

char_ids_padded = [0,1,2,3,3]
char_ids_unpadded = [0,1,2]
Run Code Online (Sandbox Code Playgroud)

如果您使用gist中定义的函数_int64_feature,_bytes_feature则可以使用以下代码段创建与FSNS兼容的tf.Example proto:

char_ids_padded, char_ids_unpadded = encode_utf8_string(
   text, charset, length, null_char_id)
example = tf.train.Example(features=tf.train.Features(
  feature={
    'image/format': _bytes_feature("PNG"),
    'image/encoded': _bytes_feature(img.tostring()),
    'image/class': _int64_feature(char_ids_padded),
    'image/unpadded_class': _int64_feature(char_ids_unpadded),
    'height': _int64_feature(img.shape[0]),
    'width': _int64_feature(img.shape[1]),
    'orig_width': _int64_feature(img.shape[1]/num_of_views),
    'image/text': _bytes_feature(text)
  }
))
Run Code Online (Sandbox Code Playgroud)