【问题标题】:Python Tensorflow:UnimplementedError: Cast string to int32 is not supportedPython Tensorflow:UnimplementedError:不支持将字符串转换为 int32
【发布时间】:2018-02-11 08:15:03
【问题描述】:

每个人。 我是 TensorFlow 的新手 当我在这部分写这样的代码时:

def get_batch(image,label,new_height,new_width,batch_size,capacity):
image=tf.cast(image,tf.string)
label=tf.cast(image,tf.int32)

input_queue= tf.train.slice_input_producer([image,label])
label=input_queue[1]
image_contents=tf.read_file(input_queue[0])
image=tf.image.decode_jpeg(image_contents,channels=3)

image=tf.image.resize_images(image,(new_height,new_width))
image=tf.image.per_image_standardization(image)
image_batch,label_batch=tf.train.batch([image,label],batch_size=batch_size,capacity=capacity,num_threads=8)

label_batch=tf.reshape(label_batch,[batch_size])
return image_batch,label_batch

顺便说一句。 Args: images ,labels 是从另一个函数返回的,该函数读取存储图像和标签的文件。当我运行此代码时,我将 new_height 和 new_width 定义为常量

 I met the error like this:

UnimplementedError (see above for traceback): Cast string to int32 is not supported
         [[Node: Cast_1 = Cast[DstT=DT_INT32, SrcT=DT_STRING, _device="/job:localhost/replica:0/task:0/cpu:0"](Cast/x)]]

Traceback (most recent call last):

  File "<ipython-input-1-5c65685872d1>", line 1, in <module>
    runfile('C:/Users/yanghang/ugthesis/mean subtraction.py', wdir='C:/Users/yanghang/ugthesis')

  File "C:\Users\yanghang\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\Users\yanghang\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/yanghang/ugthesis/mean subtraction.py", line 102, in <module>
    coord.join(threads)

  File "C:\Users\yanghang\Anaconda3\lib\site-packages\tensorflow\python\training\coordinator.py", line 389, in join
    six.reraise(*self._exc_info_to_raise)

  File "C:\Users\yanghang\Anaconda3\lib\site-packages\six.py", line 686, in reraise
    raise value

  File "C:\Users\yanghang\Anaconda3\lib\site-packages\tensorflow\python\training\queue_runner_impl.py", line 234, in _run
    sess.run(enqueue_op)

  File "C:\Users\yanghang\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 778, in run
    run_metadata_ptr)

  File "C:\Users\yanghang\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 982, in _run
    feed_dict_string, options, run_metadata)

  File "C:\Users\yanghang\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1032, in _do_run
    target_list, options, run_metadata)

  File "C:\Users\yanghang\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1052, in _do_call
    raise type(e)(node_def, op, message)`

你能告诉我如何解决这个问题吗? 提前致谢

【问题讨论】:

  • 此错误取决于您的图像对象的内容,因此请调试图像对象并提供结果,以便我们为您提供帮助。
  • 不支持将字符串转换为 int32 ?我猜,因为字符串就像"C:\\Users\\yanghang\\ugthesis\\data\\b\\normal.1.jpg'" 所以不能转换成int32,对吧?
  • 您应该使用tf.data 库而不是旧队列:这里是guide。另外,看看这个answer 从图像中导入数据。
  • 嗨,奥利维尔,我试试dataset=tf.data.TFRecordDataset(filenames) def _parse_function(record): features={"image":tf.FixedLenFeature((),tf.string,default_value='') "label":tf.FixedLenFeature((),tf.int32,default_value='')} # use tf.parse_single_example() function to extract the data parsed=tf.parse_single_example(record,features) image_decoded=tf.image.decoded_image(parsed["image"]) image_resize=tf.image.reshape(image_decoded,[200,200,1]) label=tf.cast(parse["label"],tf.int32) return {"image":image,"label":label}
  • 请编辑您的帖子,提出明确的问题以及您已有的代码。

标签: python-3.x tensorflow tensorflow-datasets


【解决方案1】:

正如@mrry 所指出的,代码中有一个错字:

label=tf.cast(image,tf.int32)

应该是:

label=tf.cast(label,tf.int32)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2014-11-15
    • 2019-07-16
    相关资源
    最近更新 更多