首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >decode_jpeg,encode_jpeg类型错误

decode_jpeg,encode_jpeg类型错误
EN

Stack Overflow用户
提问于 2017-05-26 21:39:59
回答 1查看 1.1K关注 0票数 1

我正在使用下面的代码https://github.com/tensorflow/models/blob/master/slim/datasets/download_and_convert_flowers.py来适应我自己的需求

我需要添加零填充并将图像大小调整为299x299 (初始V3输入大小)。

我正在添加一些代码行来更改原始的

代码语言:javascript
复制
        image_data = tf.gfile.FastGFile(filenames[i], 'r').read()
        height, width = image_reader.read_image_dims(sess, image_data)

        class_name = os.path.basename(os.path.dirname(filenames[i]))
        class_id = class_names_to_ids[class_name]

        example = dataset_utils.image_to_tfexample(
            image_data, 'jpg', height, width, class_id)

有了这个

代码语言:javascript
复制
        image_data = tf.gfile.FastGFile(filenames[i], 'r').read()
        height, width = image_reader.read_image_dims(sess, image_data)

        image_decoded = tf.image.decode_jpeg(image_data, channels=None, ratio=None, fancy_upscaling=None, try_recover_truncated=None, acceptable_fraction=None, name=None)

        M=max(width,height)

        image_decoded = tf.image.pad_to_bounding_box(image_decoded, int(math.floor((M-height)/2)), int(math.floor((M-width)/2)), M, M)

        image_decoded = tf.expand_dims(image_decoded, 0)

        image_decoded = tf.image.resize_bilinear(image_decoded, [299, 299], align_corners=None, name=None)

        image_decoded = tf.squeeze(image_decoded)

        image_decoded = tf.bitcast(image_decoded, tf.uint8)

        image_data = tf.image.encode_jpeg(image_decoded)

        class_name = os.path.basename(os.path.dirname(filenames[i]))
        class_id = class_names_to_ids[class_name]

        example = dataset_utils.image_to_tfexample(image_data, b'jpg', height, width, class_id)

我得到以下错误

代码语言:javascript
复制
  File "convert_dataset.py", line 236, in <module>
tf.app.run()
  File "/home/franco/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "convert_dataset.py", line 233, in main
run(FLAGS.dataset_dir)
  File "convert_dataset.py", line 217, in run
dataset_dir)
  File "convert_dataset.py", line 165, in _convert_dataset
example = dataset_utils.image_to_tfexample(image_data, b'jpg', height, width, class_id)
  File "/home/franco/Desktop/dataset_originario/dataset/dataset_utils.py", line 58, in image_to_tfexample
'image/encoded': bytes_feature(image_data),
  File "/home/franco/Desktop/dataset_originario/dataset/dataset_utils.py", line 53, in bytes_feature
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))
  File "/home/franco/tensorflow/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 508, in init
copy.extend(field_value)
  File "/home/franco/tensorflow/lib/python2.7/site-packages/google/protobuf/internal/containers.py", line 275, in extend
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
  File "/home/franco/tensorflow/lib/python2.7/site-packages/google/protobuf/internal/type_checkers.py", line 109, in CheckValue
raise TypeError(message)
  TypeError: <tf.Tensor 'EncodeJpeg:0' shape=() dtype=string> has type <class 'tensorflow.python.framework.ops.Tensor'>, but expected one of: ((<type 'str'>,),)

我只找到了这个开放的问题https://github.com/tensorflow/models/issues/726

也许我的代码中还存在其他错误

EN

回答 1

Stack Overflow用户

发布于 2018-09-22 00:14:26

我在此步骤image_data = tf.image.encode_jpeg(image_decoded)添加了.eval()

它是这样的:

image_data = tf.image.encode_jpeg(image_decoded).eval()

你的github链接是不存在的,所以我不能评估dataset_utils.image_to_tfexample是否遵循下面的模式,但从参数上看是这样的。

代码语言:javascript
复制
def bytes_feature(values):
    """Returns a TF-Feature of bytes.
    Args:
      values: A string.
    Returns:
     a TF-Feature.
   """
   return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))

def image_to_tfexample(image_data, image_format, height, width):
   feature={
       'image/encoded': bytes_feature(image_data.eval()),
       'image/format': bytes_feature(image_format),
       'image/height': int64_feature(height),
       'image/width': int64_feature(width),
   }
   return tf.train.Example(features=tf.train.Features(feature=feature))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44203108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档