无法将张量流图像保存到文件 [英] Can't save tensorflow image to file

查看:45
本文介绍了无法将张量流图像保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像调整到特定大小并将其保存到文件中,所以我选择了 tf.image.resize_image_with_crop_or_pad 函数:

I need to resize an image to certain size and save it to file, so I chose tf.image.resize_image_with_crop_or_pad function:

import tensorflow as tf

image_decoded = tf.image.decode_jpeg(tf.read_file('1.jpg'), channels=3)
cropped       = tf.image.resize_image_with_crop_or_pad(image_decoded, 200, 200)
tf.write_file('2.jpg', cropped)

因错误而失败:

Traceback (most recent call last):
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 490, in apply_op
    preferred_dtype=default_dtype)
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 669, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 583, in _TensorTensorConversionFunction
    % (dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype string for Tensor with dtype uint8: 'Tensor("control_dependency_3:0", shape=(200, 200, 3), dtype=uint8)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 15, in <module>
    tf.write_file('2.jpg', cropped)
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/gen_io_ops.py", line 694, in write_file
    contents=contents, name=name)
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 508, in apply_op
    (prefix, dtypes.as_dtype(input_arg.type).name))
TypeError: Input 'contents' of 'WriteFile' Op has type uint8 that does not match expected type of string.

我尝试使用 tf.as_string() 将张量转换为字符串,但崩溃了:

I tried to convert the Tensor to string using tf.as_string() but crashed with:

TypeError: DataType uint8 for attr 'T' not in list of allowed values: int32, int64, complex64, float32, float64, bool, int8

我在 Linux Mint 上使用 Tensorflow v0.12.0-rc0

I use Tensorflow v0.12.0-rc0 on Linux Mint

推荐答案

您首先需要将图像从张量编码为 jpeg,然后保存.此外,您应该执行一个会话来评估您的代码:

You first need to encode the image from a tensor to a jpeg and then save it. Moreover, you should execute a session to evaluate your code:

import tensorflow as tf

image_decoded = tf.image.decode_jpeg(tf.read_file('1.jpg'), channels=3)
cropped       = tf.image.resize_image_with_crop_or_pad(image_decoded, 200, 200)
enc = tf.image.encode_jpeg(cropped)
fname = tf.constant('2.jpg')
fwrite = tf.write_file(fname, enc)

sess = tf.Session()
result = sess.run(fwrite)

与 TensorFlow 2 相同(兼容模式)

Same thing with TensorFlow 2 (compatibility mode)

fname = '2.jpg'
with tf.compat.v1.Session() as sess:
    image_decoded = tf.image.decode_jpeg(tf.io.read_file('1.jpg'), channels=3)
    cropped = tf.image.resize_with_crop_or_pad(image_decoded, 200, 200)
    enc = tf.image.encode_jpeg(cropped)
    fwrite = tf.io.write_file(tf.constant(fname), enc)
    result = sess.run(fwrite)

这篇关于无法将张量流图像保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆