Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TF / Keras error: InputLayer not a Checkpointable

I am trying out the newly added TPU support on Google Colab with the simple cats vs dogs dataset.

After creating a simple CNN, I tried to export the model to TPU. But it failed with error

TypeError: Checkpointable._track_checkpointable() passed type <class 'keras.engine.topology.InputLayer'>, not a Checkpointable.

Here's the code that I wrote on Colab.

model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()

train_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(train_dir, target_size=(150,150), batch_size=20, class_mode='binary')

tpu_model = tf.contrib.tpu.keras_to_tpu_model(model, strategy=tf.contrib.tpu.TPUDistributionStrategy(tf.contrib.cluster_resolver.TPUClusterResolver(tpu="grpc://" + os.environ['COLAB_TPU_ADDR'])))

My guess is I am doing something wrong in train_generator. But I am not sure what it is. Any help would be highly appreciated.

like image 878
Mahendra Kariya Avatar asked Oct 07 '18 17:10

Mahendra Kariya


1 Answers

If you're using or import layers from Keras instead of TensorFlow like this:

from keras import layers,models
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf

You will get error like you mention above :

TypeError: Checkpointable._track_checkpointable() passed type <class 'keras.engine.topology.InputLayer'>, not a Checkpointable.

So, you can import layers directly from TensorFlow like my code below:

from tensorflow.keras import layers,models
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf

or you can see my full code here: https://gist.github.com/ilmimris/8218e397dd35ab693404e95db32dc574

like image 118
Muhammad Rafiul Ilmi S Avatar answered Sep 27 '22 19:09

Muhammad Rafiul Ilmi S