使用图像作为输入和目标的tfRecords

时间:2019-03-12 15:50:10

标签: python tensorflow machine-learning data-analysis tfrecord

我目前正在尝试从本地存储的某些.png图像创建tf.Records。

我在此看到的大多数示例都是用于分类任务的,其中目标值是类。 我正在尝试建立VAE,所以我的目标值也应该是图片。

我发现了this生成tf.Records的示例:

# Converting the values into features
# _int64 is used for numeric values
def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

# _bytes is used for string/char values
def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

tfrecord_filename = 'something.tfrecords'

# Initiating the writer and creating the tfrecords file.
writer = tf.python_io.TFRecordWriter(tfrecord_filename)

# Loading the location of all files - image dataset
# Considering our image dataset has apple or orange
# The images are named as apple01.jpg, apple02.jpg .. , orange01.jpg .. etc.

images = glob.glob('data/*.jpg')
for image in images[:1]:
    img = Image.open(image)
    img = np.array(img.resize((32,32)))
label = 0 if 'apple' in image else 1
feature = { 'label': _int64_feature(label),'image': _bytes_feature(img.tostring()) }

# Create an example protocol buffer
example = tf.train.Example(features=tf.train.Features(feature=feature))

# Writing the serialized example.
writer.write(example.SerializeToString())

writer.close()

问题: 要将图像另存为目标值,应该做些什么改变?

它在变化吗?

feature = { 'label': _int64_feature(label),'image': _bytes_feature(img.tostring()) }

feature = { 'label': _bytes_feature(img.tostring()),'image': _bytes_feature(img.tostring()) }

预先感谢

1 个答案:

答案 0 :(得分:0)

我认为您可以在一个示例中保存两个图像。而且通常 保存图像尺寸的好主意

features=tf.train.Features(feature={'height': _int64_feature(h),
                                    'width': _int64_feature(w),
                                    'channels': _int64_feature(c)
                                    'image_1': _bytes_feature(image1)
                                    'image_2': _bytes_feature(image2)
                                    }
                          ))
example = tf.train.Example(features=tf.train.Features(feature=feature))

修改

如果我答对了:

list = np.array([image_1, image_2,...image_n])
images = np.split(np.fromstring(list.tostring()), number_of_images)