Transfer learning and fine-tuning

  1. Data Preprocessing

In this tutorial, you will use a dataset containing several thousand images of cats and dogs. Download and extract a zip file containing the images, then create a for training and validation using the tf.keras.preprocessing.image_dataset_from_directory utility

_URL = ''
path_to_zip = tf.keras.utils.get_file('', origin=_URL, extract=True)
PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered')

train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')

IMG_SIZE = (160, 160)

train_dataset = image_dataset_from_directory(train_dir,
validation_dataset = image_dataset_from_directory(validation_dir,                                                  shuffle=True,                                                  batch_size=BATCH_SIZE,                                                  image_size=IMG_SIZE)

2. Configure the dataset for performance

AUTOTUNE = = train_dataset.prefetch(buffer_size=AUTOTUNE)validation_dataset = validation_dataset.prefetch(buffer_size=AUTOTUNE)test_dataset = test_dataset.prefetch(buffer_size=AUTOTUNE)

3. Use data augmentation

data_augmentation = tf.keras.Sequential([  tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),  tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),])

4. Rescale pixel values

preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input
rescale = tf.keras.layers.experimental.preprocessing.Rescaling(1./127.5, offset= -1)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Start a Blog at

Up ↑

%d bloggers like this: