Update api_token with kaggle api key for downloading dataset
- Login to kaggle
- My Profile > Edit Profile > Createt new API Token
- Update **api_token** dict below with the values
Change Notebook runtime to TPU
- In colab notebook menu, Runtime > Change runtime type
- Select TPU in the list
Install kaggle package, download and extract zip file
In [10]:
!pip install kaggle
api_token = {"username":"xxxxx","key":"xxxxxxxxxxxxxxxxxxxxxxxx"}
import json
import zipfile
import os
os.mkdir('/root/.kaggle')
with open('/root/.kaggle/kaggle.json', 'w') as file:
json.dump(api_token, file)
!chmod 600 /root/.kaggle/kaggle.json
# !kaggle config path -p /root
!kaggle competitions download -c dogs-vs-cats
In [0]:
zip_ref = zipfile.ZipFile('/content/train.zip', 'r')
zip_ref.extractall()
zip_ref.close()
Re-arrange classes to 2 separate directories
In [0]:
!mkdir train/cat train/dog
!mv train/*cat*.jpg train/cat
!mv train/*dog*.jpg train/dog
Training configs
In [0]:
BATCH_SIZE = 64
IMG_DIM = (256, 256, 3)
NUM_EPOCHS = 1
Setup generators to provide with train and validation batches
In [15]:
import tensorflow as tf
from tensorflow import keras
print(keras.__version__)
print(tf.__version__)
datagen = keras.preprocessing.image.ImageDataGenerator(
rescale=1./255,
validation_split=0.2)
traingen = datagen.flow_from_directory(
'train',
batch_size = BATCH_SIZE,
target_size = IMG_DIM[:-1],
class_mode = 'categorical',
subset='training')
valgen = datagen.flow_from_directory(
'train',
batch_size = BATCH_SIZE,
target_size = IMG_DIM[:-1],
class_mode = 'categorical',
subset='validation')
Define LeNet model architecture
In [17]:
input = keras.layers.Input(IMG_DIM, name="input")
conv1 = keras.layers.Conv2D(20, kernel_size=(5, 5), padding='same')(input)
pool1 = keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2))(conv1)
conv2 = keras.layers.Conv2D(50, kernel_size=(5,5), padding='same')(pool1)
pool2 = keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2))(conv1)
flatten1 = keras.layers.Flatten()(pool2)
fc1 = keras.layers.Dense(500, activation='relu')(flatten1)
fc2 = keras.layers.Dense(2, activation='softmax')(fc1)
model = keras.models.Model(inputs=input, outputs=fc2)
model.compile(
loss='categorical_crossentropy',
optimizer=keras.optimizers.SGD(lr=0.01),
metrics=['accuracy'])
print(model.summary())
Check for TPU availability
In [18]:
import os
try:
device_name = os.environ['COLAB_TPU_ADDR']
TPU_ADDRESS = 'grpc://' + device_name
print('Found TPU at: {}'.format(TPU_ADDRESS))
except KeyError:
print('TPU not found')
Convert keras model to TPU model
In [19]:
tpu_model = tf.contrib.tpu.keras_to_tpu_model(
model,
strategy=tf.contrib.tpu.TPUDistributionStrategy(
tf.contrib.cluster_resolver.TPUClusterResolver(TPU_ADDRESS)))
Run training
In [21]:
tpu_model.fit_generator(
traingen,
steps_per_epoch=traingen.n//traingen.batch_size,
epochs=1,
validation_data=valgen,
validation_steps=valgen.n//valgen.batch_size)
Out[21]:
Save the model weights
In [22]:
tpu_model.save_weights('./lenet-catdog.h5', overwrite=True)
Download model weights locally
In [0]:
from google.colab import files
files.download("lenet-catdog.h5")