【问题标题】:Colab+TPU not supporting TF 2.3.0 tf.keras.layers.experimental.preprocessingColab+TPU 不支持 TF 2.3.0 tf.keras.layers.experimental.preprocessing
【发布时间】:2020-11-27 19:54:42
【问题描述】:

我在 Colab+TPU 上基于 https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/ 使用 TF 2.3.0 更新我的模型,特别是遵循数据增强和从预训练权重段落进行迁移学习。

当我启动 model.fit 时出现此错误:

InvalidArgumentError: 9 root error(s) found.
  (0) Invalid argument: {{function_node __inference_train_function_372657}} Compilation failure: Detected unsupported operations when trying to compile graph cluster_train_function_12053586239504196919[] on XLA_TPU_JIT: ImageProjectiveTransformV2 (No registered 'ImageProjectiveTransformV2' OpKernel for XLA_TPU_JIT devices compatible with node {{node EfficientNet/img_augmentation/random_rotation_2/transform/ImageProjectiveTransformV2}}){{node EfficientNet/img_augmentation/random_rotation_2/transform/ImageProjectiveTransformV2}}
    TPU compilation failed
     [[tpu_compile_succeeded_assert/_6138790737589773377/_7]]
     [[TPUReplicate/_compile/_14198390524791994190/_6/_238]]
 

我想 TPU 仍然不支持tf.keras.layers.experimental.preprocessing,因为在available TPU operations 列表中没有preprocessing 选项。我说的对吗?

推理时有多个Benefits of doing preprocessing inside the model

我在哪里可以找到可能的实施日期?

谢谢。

大卫

【问题讨论】:

  • 我在github 上提出了这个问题,因为我找不到任何官方的说法来说明这些层是否在 TPU 上受支持,或者是否正在开发中。

标签: tensorflow google-colaboratory tf.keras tpu google-cloud-tpu


【解决方案1】:

你说对了一半。 TPU operations 的列表包括较低级别的 TF 函数,但不包括 Keras 层。从错误消息来看,您的预处理层似乎试图在图中实例化 ImageProjectiveTransformV2 操作,这是不受支持的。

作为兼容 TPU 的替代品,我建议您查看 TF 模型园中的 official EfficientNet implementation。特别是preprocessing.py 可能对您有帮助。

【讨论】:

  • 但是由于这些层是官方的(尽管是实验性的)功能,因此可以合理地预期 TPU 支持正在筹备中。我想可能有必要在github而不是here上提问。
【解决方案2】:

一种可能的解决方法是将图层合并到输入管道中。这有点骇人听闻,但我已经对其进行了简短的测试,它似乎可以在 TPU 上运行。例如,如果您正在使用tf.data.Dataset API,您可以创建一个图层对象,然后在Dataset.map() 中调用它以将增强应用到管道:

# dummy data
images = tf.random.uniform((10, 224, 224, 1))
labels = tf.zeros((10, 1))
ds = tf.data.Dataset.from_tensor_slices((images, labels))
ds = ds.batch(10)

# now incorporate the augmentation 'layer' into the pipeline
augmentor = tf.keras.layers.experimental.preprocessing.RandomRotation((-0.1, 0.1))
# augment the images, pass the labels through untouched
ds = ds.map(lambda x, y: (augmentor.call(x), y))

# assume we've compiled a model elsewhere
model.fit(ds)

这不会按照最初的预期将增强层编译到模型中,但它应该允许您在不需要第三方插件的情况下增强您的训练数据。在issue 正式解决之前,我打算将此作为一种解决方法。

【讨论】:

  • 我遇到了同样的问题。我假设将随机转换放入数据集管道中,这将在 CPU 内核而不是 TPU 上运行?我想这真的取决于你的模型有多大和数据增强的复杂程度:它对你的具体案例在训练时间方面的影响有多大?
  • @kawingkelvin 我之前在使用 Dataset API 时遇到过特定于 TPU 的问题,这让我认为它确实利用了 TPU。这并不能解释为什么这些层在数据集管道中起作用,但在模型中不起作用,所以谁知道呢?这对我来说都是一个黑匣子。
猜你喜欢
  • 2019-07-29
  • 2019-12-05
  • 2020-05-24
  • 2021-02-19
  • 2019-12-06
  • 1970-01-01
  • 2020-05-01
  • 2019-03-24
  • 2021-02-17
相关资源
最近更新 更多