python - TensorFlow 数据集 .map() 方法不适用于内置 tf.keras.preprocessing.image 函数
问题描述
我这样加载数据集:
import tensorflow_datasets as tfds
ds = tfds.load(
'caltech_birds2010',
split='train',
as_supervised=False)
这个功能工作正常:
import tensorflow as tf
@tf.function
def pad(image,label):
return (tf.image.resize_with_pad(image,32,32),label)
ds = ds.map(pad)
但是当我尝试映射不同的内置函数时
from tf.keras.preprocessing.image import random_rotation
@tf.function
def rotate(image,label):
return (random_rotation(image,90), label)
ds = ds.map(rotate)
我收到以下错误:
AttributeError:“张量”对象没有属性“ndim”
这不是唯一给我带来问题的功能,无论有没有@tf.function
装饰器,它都会发生。
任何帮助是极大的赞赏!
解决方案
我会尝试在这里使用 tf.py_function 进行随机旋转。例如:
def rotate(image, label):
im_shape = image.shape
[image, label,] = tf.py_function(random_rotate,[image, label],
[tf.float32, tf.string])
image.set_shape(im_shape)
return image, label
ds = ds.map(rotate)
虽然我认为他们根据tf.py_function 和 tf.function 的目的有什么区别?, tf.py_function 通过 tensorflow 执行 python 代码更直接,即使 tf.function 具有性能优势。
推荐阅读
- chart.js - 突出显示图表 JS 中的特定点
- python - 从 oracle 表中检索与 csv 文件中的 ID 有关的记录时出错
- python - 没有传递参数时如何打印使用说明?
- javascript - 如何在通过热敏打印成功完成先前的 AJAX 打印请求后执行 AJAX 请求
- apache-flink - 在 Apache Flink 的flapMap Operator 中发现“缓冲池被破坏”问题
- css - 空样式 (.css/.scss) 文件
- reactjs - google-map-react 和 react-google-maps 之间的区别
- php - Twilio 群发短信
- regex - 在 JMeter 正则表达式提取器中使用正则表达式获取值
- android - 带有 layout_weight = 0 的 Android 布局 wrap_content 不显示