Exploring the Power of Functional APIs using Transfer Learning

Exploring the Power of Functional APIs using Transfer Learning

Domain - Deep Learning, Image Analysis

Definition given by AI - Functional APIs in deep learning are a way to build neural networks by defining computational graphs of layers and connecting them. It provides a flexible and powerful approach to create complex models with multiple inputs and outputs. With functional APIs, you can define models with shared layers, create skip connections, and build models with branching architectures. It allows for more flexibility and customization compared to the sequential model approach.

Only if I was smart enough to understand this definition :’)

Let us understand this in very simple words, the way I prefer.

Just to brief up the basics, when you create a neural network, you try to identify features or something primitive from the data (tabular, textual or image format). For this, you construct a Neural Network.

There are mainly 2 types of Neural Networks -

  1. Sequential API

  2. Functional API

Sequential API

Sequential APIs are the most common type of neural network architecture, used for both classification and regression tasks. They consist of a linear stack of layers, with each layer taking the output of the previous layer as input to theirs.

This means that the model has a single input and output, where the input is a set of features and the output is a single prediction.

For example, this neural network uses Sequential API.

In this, there is only 1 set of inputs with few columns and on that, there is a neural network built. It is a simple one-directional neural network and predicts if a person is alcoholic or not.

The main point in this network is we have a set of inputs and we are trying to predict one output (alcoholic or not).

Note: This code is from a research paper I co-authored with two classmates. You can learn more about it in this LinkedIn post - Link

Functional API

Now, Functional APIs are a little different. They can (not necessarily) take multiple inputs, build multiple layers with respect to different inputs, have different weights and generate different outputs all in ONE SINGLE NEURAL NETWORK.

Well that’s how I define it.

Let’s take an example to understand this better (same will be taken while coding).

Say we have a photograph of a person (annotated), and we have to predict the age ‘AND’ the gender of the person. Typically, if we were to use Sequential API, we would go through the following techniques -

  1. Pre-process the dataset (EG - Normalization, reduce the number of channels, etc.).

  2. Split the data in terms of predictor and target variable (age).

  3. Train a Neural Network and predict the age.

Now we repeat the same for Gender.

So those are 2 neural networks we trained to predict to separate features.

Now, with Functional APIs you could train it in one single neural network. And the best part is, it isn’t anything fancier from the Sequential API that we just discussed. They are 95% identical. Just a few changes, and you are good to go.

Lets build the model using Transfer Learning.

vgg = VGG16(include_top=False, input_shape=(200,200,3))

vgg.trainable = False 
output = vgg.layers[-1].output

flatten = Flatten()(output)

Dense1 = Dense(512, activation='relu')(flatten) #Age
Dense2 = Dense(512, activation='relu')(flatten) #Gender

Dense3 = Dense(512, activation='relu')(Dense1) #Age
Dense4 = Dense(512, activation='relu')(Dense2) #Gender

output1 = Dense(1, activation='linear', name='Age')(Dense3)
output2 = Dense(1, activation='sigmoid', name='Gender')(Dense4)

So instead of having 1 layer connected sequentially, trained to extract features from 1 feature/column, we bifurcate the network amongst the 2 features (Age and Gender) to be predicted and accordingly assign the necessary activation functions.

This is the model architecture of the Functional API that we constructed -

As we can clearly see, we did not invent any new form of coding or haven’t even done anything cool in the slightest sense but have managed to successfully classify and predict values in the same neural network there by saving a lot of memory resources.

Easy! Isn’t it?

The example I have provided is simple and straightforward, but you can experiment with more complex models and features. The Functional API can be challenging to use, but also very rewarding, when applied to very complex models and datasets, such as VGG19 or ResNet50, to predict features from your dataset. Although these models can be very complex, complexity should not matter as long as you understand the whys, whens, and hows of your data and model explainability.

This is a model architecture of a slightly advanced usage of Functional APIs -

SUMMARY-

  1. Sequential APIs are well-suited for classification and regression tasks with a single output variable. For multiple output variables, multiple sequential APIs can be constructed.

  2. Functional APIs are more flexible and powerful, allowing for multi-output models with a single network. This is achieved by feeding in different features at different times, enabling the model to learn and predict multiple target features simultaneously.

Let's delve into the merits and demerits associated with the Functional API.

Merits -

  1. Flexibility: The Functional API is highly flexible and allows you to create complex neural network architectures with multiple inputs, multiple outputs, shared layers, skip connections, and more. This flexibility is especially useful for advanced network designs, such as siamese networks, multi-input models, and models with complex branching.

  2. Multiple Inputs and Outputs: You can easily create models that take multiple inputs or produce multiple outputs. This is beneficial for tasks like multi-modal learning, where you might have both text and image inputs, or tasks with multiple objectives.

  3. Shared Layers: The Functional API allows you to share layers between different parts of the network. This is useful for transfer learning, where you can reuse the same layer across different parts of the model.

  4. Debugging: The Functional API provides more transparency into the model's architecture, making it easier to debug and understand how data flows through the network.

Demerits -

  1. Complexity: While the Functional API is powerful, it can be more complex than the Sequential API, especially for simple models. If you're building a straightforward feedforward network, the Sequential API might be more intuitive.

  2. Compatibility: Not all layers are compatible with the Functional API, especially custom layers that don't follow the Keras Layer class pattern. This can limit the use of certain layers in your models.

We looked at one part of the code but you can refer to the entire code repository - GitHub

I hope you found this article intriguing, less complex and “at least” a little informative. Happy Learning :)

For any suggestions or just a chat,

Raj Kulkarni - LinkedIn