How can Tensorflow be used with a pre-trained network, i.e. transfer learning to load the data?

TensorflowServer Side ProgrammingProgramming

Tensorflow can be used with a pre-trained network, for transfer learning, to load the data by using the ‘get_file’ method present in Keras package. A google API holds the dataset, which can be passed as parameter to the ‘get_file’ method to download the dataset in the current environment.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We will understand how to classify images of cats and dogs with the help of transfer learning from a pre-trained network.

The intuition behind transfer learning for image classification is, if a model is trained on a large and general dataset, this model can be used to effectively serve as a generic model for the visual world. It would have learned the feature maps, which means the user won’t have to start from scratch by training a large model on a large dataset.

Read More: How can a customized model be pre-trained?

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.


import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
from tensorflow.keras.preprocessing import image_dataset_from_directory
_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')
print("Downloading the data")
train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')
IMG_SIZE = (160, 160)

Code credit −


Downloading data from
68608000/68606236 [==============================] - 1s 0us/step
Downloading the data


  • In this dataset, we have thousands of images of cats and dogs.

  • They are downloaded, and extracted from a zip file.

  • A is created, that is used for training and validation purposes.

  • This is done with the help of tf.keras.preprocessing.image_dataset_from_directory utility.

Published on 12-Feb-2021 13:09:13