【发布时间】:2023-03-17 19:58:01
【问题描述】:
我已经阅读了许多与我类似的问题,但所有这些问题都与我的不同。
for itr in xrange(MAX_ITERATION):
train_images, train_annotations = train_dataset_reader.next_batch(batch_size)
# train_images=tf.image.convert_image_dtype(train_images,np.float32)
# train_annotations=tf.image.convert_image_dtype(train_annotations,np.float32)
# print(train_images_.get_shape(),train_annotations_.get_shape())
# train_images=tf.cast(train_images,tf.float32)
# train_images = tf.to_float(train_images)
# train_annotations = tf.to_float(train_annotations)
#train_images, train_annotations = py_func(selftrans, [train_images_, train_annotations_], [tf.float32], grad=None)
print(train_images.dtype)
# train_annotations=tf.cast(train_annotations,tf.float32)
# train_images,train_annotations = sess.run([train_images,train_annotations])
# train_images = train_images.astype('float32')
# train_annotations = train_annotations.astype('float32')
# print(train_annotations.dtype)
feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 1}
sess.run(train_op, feed_dict=feed_dict)
问题是:我使用自己的数据(血管分割,属于两分类问题)后,使用next_batch读取的数据是float64,而feed_dict()需要float32。通常,tf.cast or tf.tp_float 之类的代码可用于将我的数据转换为 float32。但是,使用这些转换代码后,我发现错误变为 TypeError: The value of a feed cannot be a tf.Tensor object。可接受的提要值包括 Python 标量、字符串、列表、numpy ndarray 或 TensorHandles。
最后,我尝试使用 sess.run 来偏移它的 tf.Tensor 形式。 Erorr 更改为原来的,这意味着我的数据被转换回 float64。
因此,似乎是一个死循环。 "不转换,float64不被接受。转换后,Tf.Tensor也不会被接受。使用sess来偏移tf.Tensor形式运行,数据将同时转换回float64“我该如何解决这个问题?我期待您的回复!类似的问题在这里, TensorFlow TypeError: Value passed to parameter input has DataType uint8 not in list of allowed values: float16, float32
【问题讨论】:
-
train_images和train_annotations的类型是什么?它们是numpy数组吗?如果是这样,只需使用numpy函数转换它们的类型:feed_dict = {image: train_images.astype(np.float32), ...} -
@dm0_ 我认为它们是 tf.float64。我已经在最后的第四行和第五行尝试了
.astype(np.float32)。错误是:AttributeError: 'Tensor' object has no attribute 'astype'。我尝试了很多转换的方法,都被注释掉了,大家可以参考一下,如上图。
标签: python tensorflow deep-learning image-segmentation