How can Keras be used to extract features from only one layer of the model using Python?


Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications, and much more. It is used in research and for production purposes. It has optimization techniques that help in performing complicated mathematical operations quickly. This is because it uses NumPy and multi-dimensional arrays. These multi-dimensional arrays are also known as ‘tensors’. The framework supports working with deep neural networks.

The ‘TensorFlow’ package can be installed on Windows using the below line of code −

pip install tensorflow

Tensor is a data structure used in TensorFlow. It helps connect edges in a flow diagram. This flow diagram is known as the ‘Data flow graph’. Tensors are nothing but a multidimensional array or a list.

Keras means ‘horn’ in Greek. Keras was developed as a part of the research for the project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System). Keras is a deep learning API, which is written in Python. It is a high-level API that has a productive interface that helps solve machine learning problems. It runs on top of the Tensorflow framework. It was built to help experiment in a quick manner. It provides essential abstractions and building blocks that are essential in developing and encapsulating machine learning solutions.

It is highly scalable and comes with cross-platform abilities. This means Keras can be run on TPU or clusters of GPUs. Keras models can also be exported to run in a web browser or a mobile phone as well.

Keras is already present within the Tensorflow package. It can be accessed using the below line of code.

import tensorflow
from tensorflow import keras

We are using 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. Following is the code snippet −

Example

print("Feature extraction from one layer only")
initial_model = keras.Sequential(
   [
      keras.Input(shape=(250, 250, 3)),
      layers.Conv2D(32, 5, strides=2, activation="relu"),
      layers.Conv2D(32, 3, activation="relu", name="my_intermediate_layer"),
      layers.Conv2D(32, 3, activation="relu"),
   ]
)
print("Feature extraction from the model")
feature_extractor = keras.Model(
   inputs=initial_model.inputs,
   outputs=initial_model.get_layer(name="my_intermediate_layer").output,
)
print("The feature extractor method is called on test data")
x = tf.ones((1, 250, 250, 3))
features = feature_extractor(x)

Code credit − https://www.tensorflow.org/guide/keras/sequential_model

Output

Feature extraction form one layer only 
Feature extraction form the model
The feature extractor method is called on test data

Explanation

  • Once the architecture of the model is ready, it is trained.

  • Once the training is completed, it is evaluated.

  • This model is saved to disk.

  • This can be restored when required.

  • Multiple GPU’s can be used to speed up the training of the model.

  • Once a model has been built, it behaves like a functional API model.

  • This indicates that every layer has an input and output.

  • Here, features are extracted from one single layer explicitly.

Updated on: 18-Jan-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements