- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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 Keras be used to extract and reuse nodes in graph of layers using Python?
Keras was developed as a part of 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
The Keras functional API helps create models that are more flexible in comparison to models created using sequential API. The functional API can work with models that have non-linear topology, can share layers and work with multiple inputs and outputs. A deep learning model is usually a directed acyclic graph (DAG) that contains multiple layers. The functional API helps build the graph of layers.
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 wherein Kera is used to extract and reuse nodes in graph of layers −
print("VGG19 model with pre-trained weights") vgg19 = tf.keras.applications.VGG19() features_list = [layer.output for layer in vgg19.layers] feat_extraction_model = keras.Model(inputs=vgg19.input, outputs=features_list) img = np.random.random((1, 224, 224, 3)).astype("float32") print("Create feature-extraction model") extracted_features = feat_extraction_model(img)
Code credit − https://www.tensorflow.org/guide/keras/functional
VGG19 model with pre-trained weights Downloading data from https://storage.googleapis.com/tensorflow/kerasapplications/vgg19/vgg19_weights_tf_dim_ordering_tf_kernels.h5 574717952/574710816 [==============================] - 6s 0us/step Create feature-extraction model
Since the graph of layers is a static data structure, it can be accessed.
This is the reason why functional models can be plotted as images.
The activations of intermediate layers (nodes) can also be accessed and reused.
This is very useful for feature extraction purposes.
We will be using the VGG19 model which has pre-trained weights with the help of ImageNet.
These intermediate activations can be obtained by querying graph data structure.
These features can be used to create a new feature-extraction model that returns values of intermediate layer activations.
- Related Articles
- How can Keras be used with Embedding layer to share layers using Python?
- Discuss how the Keras functional API can be used to create layers using Python
- How can Keras be used to extract features from only one layer of the model using Python?
- How can Tensorflow be used to compose layers using Python?
- How can Keras be used to plot the model using Python?
- How can Keras be used to train the model using Python?
- How can Keras be used to evaluate the model using Python?
- How can Keras be used to save and serialize the model using Python?
- How can Keras be used to plot the model as a graph and display input and output shapes using Python?
- How can Keras be used to plot the model using Python Program?
- How can Keras be used to train the model using Python Program?
- How can Keras be used to manually save the weights using Python?
- How can Keras be used to save the entire model using Python?
- How can Keras be used to evaluate the restored model using Python?
- How can Keras be used to save model using hdf5 format in Python?