3

I was updating my model using TF 2.3.0 on Colab+TPU based on https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/, specifically following the Data augmentation and Transfer learning from pre-trained weights paragraphs.

When I launch model.fit I got this error:

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

I suppose the TPU still does not support tf.keras.layers.experimental.preprocessing because in the list of available TPU operations there is not the preprocessing option. Am I right?

There are multiple Benefits of doing preprocessing inside the model at inference time.

Where could I find a possible implementation date?

Thanks.

Davide

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
Daviddd
  • 761
  • 2
  • 12
  • 37
  • 1
    I have raised this as an issue on [github](https://github.com/tensorflow/tensorflow/issues/42454), as I cannot find anything offical to say if these layers are supported on TPU or not, or if it is in the pipeline. – knuckles Aug 18 '20 at 10:58

3 Answers3

3

A possible workaround is to incorporate the layers into the input pipeline. It's a bit of a hack, but I've tested it briefly and it seems to work on a TPU. For example, if you are using the tf.data.Dataset API, you can create a layer object and then call it within Dataset.map() to apply the augmentation to the pipeline:

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

This doesn't compile the augmentation layers into the model as originally intended, but it should allow you to augment your training data without requiring a third party plugin. I intend to use this as a workaround until the issue is officially resolved.

knuckles
  • 388
  • 2
  • 9
  • I hit this same issue. I assume putting the random transform into the Dataset pipeline, this would run on CPU cores instead of TPU? I guess this really depends on how large your model and complicated the data augmentation is: how much does it impact your specific case in terms of time to train? – kawingkelvin Sep 10 '20 at 17:01
  • @kawingkelvin I have previously experienced TPU-specific issues with the Dataset API, which leads me to think that it does utilise the TPU. That wouldn't explain why these layers work in the Dataset pipeline but not in the model though, so who knows? It's all a black box to me. – knuckles Sep 18 '20 at 10:20
1

You are half right. The list of TPU operations includes lower-level TF functions, but not Keras layers. From the error message, it looks like your preprocessing layer tries to instantiate a ImageProjectiveTransformV2 op in the graph, which is not supported.

As a TPU-compatible alternative, I recommend you look at the official EfficientNet implementation in the TF model garden. In particular, preprocessing.py may be helpful to you.

Will Cromar
  • 179
  • 1
  • 7
  • 1
    But since these layers are an official (albeit experimental) feature, it is reasonable to expect that TPU support is in the pipeline. I guess it may be necessary to ask on github rather than on here. – knuckles Aug 17 '20 at 15:57
0

I got same problem after that i found issue with data pipeline so, i added preprocessing layers and it's working example:

Example: example

Markus Meyer
  • 3,327
  • 10
  • 22
  • 35
Guber
  • 1
  • 3