tensor如何转为图像_如何在tensorflow中将jpeg图像目录转换为TFRecords文件?

我希望这有帮助:

filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) # list of files to read

reader = tf.WholeFileReader()

key, value = reader.read(filename_queue)

my_img = tf.image.decode_png(value) # use decode_png or decode_jpeg decoder based on your files.

init_op = tf.initialize_all_variables()

with tf.Session() as sess:

sess.run(init_op)

# Start populating the filename queue.

coord = tf.train.Coordinator()

threads = tf.train.start_queue_runners(coord=coord)

for i in range(1): #length of your filename list

image = my_img.eval() #here is your image Tensor :)

print(image.shape)

Image.show(Image.fromarray(np.asarray(image)))

coord.request_stop()

coord.join(threads)

为了将所有图像作为张量的数组获取,请使用以下代码示例。

ImageFlow的Github回购

更新:

在上一个答案中,我只是告诉了如何读取TF格式的图像,而不是将其保存在TFRecords中。 为此,您应该使用:

def _int64_feature(value):

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

def _bytes_feature(value):

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

# images and labels array as input

def convert_to(images, labels, name):

num_examples = labels.shape[0]

if images.shape[0] != num_examples:

raise ValueError("Images size %d does not match label size %d." %

(images.shape[0], num_examples))

rows = images.shape[1]

cols = images.shape[2]

depth = images.shape[3]

filename = os.path.join(FLAGS.directory, name + '.tfrecords')

print('Writing', filename)

writer = tf.python_io.TFRecordWriter(filename)

for index in range(num_examples):

image_raw = images[index].tostring()

example = tf.train.Example(features=tf.train.Features(feature={

'height': _int64_feature(rows),

'width': _int64_feature(cols),

'depth': _int64_feature(depth),

'label': _int64_feature(int(labels[index])),

'image_raw': _bytes_feature(image_raw)}))

writer.write(example.SerializeToString())

更多信息在这里

您将像这样读取数据:

# Remember to generate a file name queue of you 'train.TFRecord' file path

def read_and_decode(filename_queue):

reader = tf.TFRecordReader()

_, serialized_example = reader.read(filename_queue)

features = tf.parse_single_example(

serialized_example,

dense_keys=['image_raw', 'label'],

# Defaults are not specified since both keys are required.

dense_types=[tf.string, tf.int64])

# Convert from a scalar string tensor (whose single string has

image = tf.decode_raw(features['image_raw'], tf.uint8)

image = tf.reshape(image, [my_cifar.n_input])

image.set_shape([my_cifar.n_input])

# OPTIONAL: Could reshape into a 28x28 image and apply distortions

# here. Since we are not applying any distortions in this

# example, and the next step expects the image to be flattened

# into a vector, we don't bother.

# Convert from [0, 255] -> [-0.5, 0.5] floats.

image = tf.cast(image, tf.float32)

image = tf.cast(image, tf.float32) * (1. / 255) - 0.5

# Convert label from a scalar uint8 tensor to an int32 scalar.

label = tf.cast(features['label'], tf.int32)

return image, label

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值