0

For making a train set for TensorFlow Object Detection model training. I need to generate a tfrecord. Here tfrecord consists of TensorFlow examples where each example is a tf.train.Example object. Following code shows how a TensorFlow example object is defined:

tf_example = 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_jpg),
        '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),
    }))

But the problem is that I have to pass each image as bytes_feature. Here encoded_jpg is the image read from file and it is in _io.BytesIO format. It is not a big deal when I am reading image from disk. But as I am augmenting my image data set hence I am getting each of the augmented image as numpy.ndarray. So either I have to cast this numpy.ndarray image to _io.BytesIO or I have to save the image first such that I can read it in _io.BytesIO which makes no sense. So how I can easily convert the image to the desired format without requiring to save it to disk.

hafiz031
  • 2,236
  • 3
  • 26
  • 48
  • Can you use other libraries? You could convert the ndarray to a PIL and then save that as io.BytesIO – Mntfr Jun 28 '20 at 16:14
  • @Mntfr I don't want to save it. I want to pass the numpy.ndarray image directly to it (with casting to desired format). – hafiz031 Jun 28 '20 at 16:30
  • Check this question [how to store numpy arrays as tfrecord?](https://stackoverflow.com/questions/47861084/how-to-store-numpy-arrays-as-tfrecord) – Richard X Jun 28 '20 at 17:10

0 Answers0