Tensorflow 解码\u jpeg,编码\u jpeg类型错误

Tensorflow 解码\u jpeg,编码\u jpeg类型错误,tensorflow,jpeg,tf-slim,Tensorflow,Jpeg,Tf Slim,我正在根据自己的需要修改以下代码 我需要添加零填充并将图像大小调整为299x299(inception V3输入大小) 我这样做是为了增加一些代码行来改变原来的 image_data = tf.gfile.FastGFile(filenames[i], 'r').read() height, width = image_reader.read_image_dims(sess, image_data) class_name = os.path.b

我正在根据自己的需要修改以下代码

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

我这样做是为了增加一些代码行来改变原来的

        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)
用这个

        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)
我得到以下错误

  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'>,),)
文件“convert_dataset.py”,第236行,在
tf.app.run()
文件“/home/franco/tensorflow/local/lib/python2.7/site packages/tensorflow/python/platform/app.py”,第44行,正在运行
_系统出口(主(_sys.argv[:1]+标志_passthrough))
文件“convert_dataset.py”,第233行,主
运行(FLAGS.dataset\u dir)
文件“convert_dataset.py”,第217行,正在运行
数据集(目录)
文件“convert_dataset.py”,第165行,在_convert_dataset中
示例=dataset_utils.image_to_tfexample(image_数据,b'jpg',高度,宽度,类id)
文件“/home/franco/Desktop/dataset_originario/dataset/dataset_utils.py”,第58行,如图所示
“图像/编码”:字节\u特征(图像\u数据),
文件“/home/franco/Desktop/dataset_originario/dataset/dataset_utils.py”,第53行,以字节为单位
返回tf.train.Feature(bytes\u list=tf.train.BytesList(value=[values]))
文件“/home/franco/tensorflow/lib/python2.7/site packages/google/protobuf/internal/python_message.py”,第508行,在init中
复制.扩展(字段值)
文件“/home/franco/tensorflow/lib/python2.7/site packages/google/protobuf/internal/containers.py”,第275行,扩展
新的检查值=[self.\u type\u checker.CheckValue(elem)for elem\u seq\u iter]
CheckValue中的文件“/home/franco/tensorflow/lib/python2.7/site packages/google/protobuf/internal/type_checkers.py”,第109行
raise TYPE错误(消息)
TypeError:具有类型,但应为以下类型之一:(,),)
我只发现了这个悬而未决的问题

可能我的代码中还有其他错误

我在这一步添加了
.eval()
image\u data=tf.image.encode\u jpeg(image\u decoded)

它是这样的:

image\u data=tf.image.encode\u jpeg(image\u decoded).eval()

您的github链接不存在,因此我无法评估
dataset\u utils.image\u to\u tfexample
是否遵循与下面相同的模式,但从参数来看似乎是这样

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))
我在这个步骤中添加了
.eval()
image\u data=tf.image.encode\u jpeg(image\u decoded)

它是这样的:

image\u data=tf.image.encode\u jpeg(image\u decoded).eval()

您的github链接不存在,因此我无法评估
dataset\u utils.image\u to\u tfexample
是否遵循与下面相同的模式,但从参数来看似乎是这样

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))

你能在你的问题中包括一个完整的堆栈跟踪吗?你试过tf.image.encode_jpeg(图像,格式=“”)吗?@AllenLavoie我刚刚更新了…@hars…我试过了,但它不起作用,我得到错误“ValueError:Attr'format'of'EncodeJpeg'Op-passed string'',不在:,“grayscale”,“rgb.”中看起来问题在于
tf.train.Feature(bytes\u list=tf.train.BytesList(value=[values])
,显然是因为
values
是一个张量。也许你只需要
eval
/
运行
就可以得到张量的值,然后再从中生成协议缓冲区?你能在你的问题中包含一个完整的堆栈跟踪吗?你试过tf.image.encode\u jpeg(image,format='')?@AllenLavoie我刚刚更新了…@hars…我试过了,但它不起作用,我得到了错误ValueError:“EncodeJpeg”Op传递字符串“”的Attr“format”不在:“”、“grayscale”、“rgb”中。问题似乎出在
tf.train.Feature(bytes\u list=tf.train.BytesList(value=[values]))
,显然是因为
是一个张量。也许你只需要
求值
/
运行
就可以得到张量的值,然后再从中生成协议缓冲区?