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.

Example

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 = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', 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')
BATCH_SIZE = 32
IMG_SIZE = (160, 160)

Code credit −https://www.tensorflow.org/tutorials/images/transfer_learning

Output

Downloading data from https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip
68608000/68606236 [==============================] - 1s 0us/step
Downloading the data

Explanation

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

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

  • A tf.data.Dataset 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.

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