繁体   English   中英

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

[英]tfRecords with images as inputs and targets

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

我在此看到的大多数示例都是针对分类任务的,其中目标值是类。 我正在尝试构建VAE,因此我的目标值也应该是图像。

我在生成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()) }

提前致谢

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM