Image data augmentation Keras M1 Mac

Hi there,

I am trying to augment some image data in TensorFlow. I am running TensorFlow-macos version 2.6.0 and Tensorflow-metal version 0.2.0.

When I run the following lines of code:

data_augmentation = keras.Sequential( [ layers.RandomFlip("horizontal"), layers.RandomRotation(0.1), layers.RandomZoom(0.2), ] )

for images, _ in train_dataset.take(1):
for i in range(9): augmented_images = data_augmentation(images)

I get the following error message:

NotFoundError: No registered 'RngReadAndSkip' OpKernel for 'GPU' devices compatible with node {{node RngReadAndSkip}} . Registered: device='XLA_CPU_JIT' device='CPU' [Op:RngReadAndSkip]

I have searched the internet extensively for a workaround, however have not found one. Any help would be greatly appreciated.

Edie

Post not yet marked as solved Up vote post of Edie_Bishop Down vote post of Edie_Bishop
3.2k views
  • Work fine for me, with with this function "layers.experimental.preprocessing":

    tf.device('/cpu:0'): data_augmentation = keras.Sequential( [ layers.experimental.preprocessing.RandomFlip("horizontal"), layers.experimental.preprocessing.RandomRotation(0.05), ] )

Add a Comment

Replies

Hi @Edie_Bishop,

Thanks for reporting this issue. The RngReadAndSkip Op has not yet been added to the tensorflow-metal plugin hence the error. We are taking a look into this, I'll add an update here once this is resolved.

In the meanwhile the workaround would be to try and run the portion of the code requiring this op inside a block that uses the CPU explicitly with

with tf.device('/cpu:0'):
    #Your code here
  • Thanks for your comment. Unfortunately the code is still not working even with this workaround. If you would let me know when there is a solution that would be very much appreciated.

    Edie

  • How long would it take to make it work. Is there a new update on tensorflow metal?

  • with tf.device('/cpu:0'): #code

    instead use this: with tf.device('/CPU:0'): #code

    changing the cpu from smalls to caps actually worked for me on m1 mac air

Same issue. This code is part of Francois Chollet's book, "Deep Learning with Python" so I expect many are having this issue.

The suggested solution does not work, either when I wrap the whole Jupyter cell in the with tf.device statement or when I wrap the specific line "augmented_images = data_augmentation(images)" in the tf.device statement.

This code segment is a prerequisite for further exercises in a later chapter, which amplifies the desire for a solution.

Thanks, Brett

Please use Tensor Flow TensorFlow Version 2.7.0 As mentioned by Frameworks Engineer & chaitanya_ambaruk, please use the with tf.device('/CPU:0'):

PS: Need to place this piece of code in the correct line of code. For the above example please place it before you start the augmentation process

with tf.device('/CPU:0'):
    data_augmentation = keras.Sequential(
    [
     layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical", input_shape = (img_height, img_width,3)),
     layers.experimental.preprocessing.RandomRotation(0.2),
     layers.experimental.preprocessing.RandomZoom(0.2)
    ]
)

plt.figure(figsize=(10,10))
for images, _ in train_ds.take(5):
  for i in range(9):
    augmented_images = data_augmentation(images)
    ax = plt.subplot(3,3,i+1)
    plt.imshow(augmented_images[0].numpy().astype('uint8'))
    plt.axis("off");
  • Not true CPU should be added as: `inputs = keras.Input(shape=(180, 180, 3)) with tf.device('/CPU:0'): x = data_augmentation(inputs)

    x = layers.Rescaling(1./255)(x) x = layers.Conv2D(filters=32, kernel_size=5, use_bias=False)(x) `

  • thanks!!!! Your solution here helped! my tf version is up-to-date, and simply put a with tf.device('/CPU:0'): doesn't work in my case.

  • Thanks! Downgrading tensorflow from 2.8 to 2.7, then using with tf.device('/CPU:0'): worked for me!

Add a Comment

RngReadAndSkip is registered on the GPU in tensorflow-metal==0.5.1.

  • Indeed it's working, but it's very slow (even slower than google Colab).

  • it's working, very good.

Add a Comment

I have the same issue with layers.RandomZoom(0.2). I used the "with tf.device..." and it didnt work for me...