- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can Tensorflow be used with a pre-trained network, i.e. transfer learning to load the data?
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.