为每个图像创建TFRecord

时间:2017-06-04 16:08:35

标签: python arrays numpy tensorflow

目前,我有一个程序,它创建一个TFRecord,包含来自目录的所有图像,如数组字符串。而不是所有图像数组数据都包含在一个TFRecord中,我如何在单个TFRecord文件中包含每个图像数组数据? E.X.我有30个图像,我转换为数组(numpy) - >我得到30个TFRecord文件。

这是我的代码:

from PIL import Image
import numpy as np
import tensorflow as tf
from tqdm import tqdm
import os


def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))


tfrecords_filename = 'image001.tfrecords'

writer = tf.python_io.TFRecordWriter(tfrecords_filename)

path_to_images = 'images_animation'
#List of images - method of accessing images
filenum = len([name for name in os.listdir(path_to_images) if os.path.isfile(os.path.join(path_to_images, name))])
#Collect the real images to later on compare
#to the reconstructed ones
original_images = []

for p in range(1, filenum):
    fname = "images_animation/image%03d.png" % p
    img = np.array(Image.open(fname))

    # Put in the original images into array
    # Just for future check for correctness
    original_images.append((img))

    img_raw = img.tostring()

    example = tf.train.Example(features=tf.train.Features(feature={
        'image_raw': _bytes_feature(img_raw)}))

    writer.write(example.SerializeToString())

writer.close()

0 个答案:

没有答案