Breaking News

A guide to TensorLayer for efficient deep learning development – Analytics India Magazine

Creating a functional deep learning system is a time-consuming and hard task. It entails time-consuming tasks like building sophisticated neural networks, coordinating many network models, data processing, creating a succinct workflow, and handling a significant volume of training-related data. There are currently tools available to aid in this development process, such as Keras and TFLearn, which provide flexibility and abstraction for multiple connected modalities. In this post, we’ll look at TensorLayer, a Python-based machine learning tool. Below are the major points listed that are to be discussed in this post.

Table of contents

Understanding the need of the library What is TensorLayer?Technical details Implementation with TensorLayer

Let’s first understand the need for this tool.

Understanding the need of the library 

The increasing interaction challenges the development of deep learning. Many cycles must be spent by developers integrating components for experimenting with neural networks, handling intermediate training stages, organizing training-related data, and enabling hyperparameter adjustment in reaction to various events. 

To reduce the number of cycles necessary, an integrative development method is used, in which complex operations on neural networks, states, data, and hyper-parameters are abstracted and given in complementing modules. This results in a single environment in which developers may efficiently explore concepts via high-level module operations and apply changes to modules only when necessary.

This strategy is not intended to create module lock-in. Modules are instead modelled as simple single-function blocks that share an interaction interface, allowing for easy plug-ins of user-defined modules.

What is TensorLayer?

TensorLayer is a collaborative effort to realize this objective. It is a modular Python toolkit that provides simple modules to help academics and engineers build complicated deep learning systems. The TensorLayer implementation is designed to be fast and scalable. TensorFlow is used as the distributed training and inference engine. 

The overhead associated with delegation into TensorFlow is small. TensorLayer also makes use of MongoDB as a storage backend. For managing unbounded training data, this backend is supplemented with an efficient stream controller. This controller can batch results from a dataset query and generates batch training tasks as needed to support automation.

TensorLayer employs GridFS as a blob backend and MongoDB as a sample indexer to efficiently handle huge data items such as videos. Finally, TensorLayer employs an agent pub-sub architecture in order to achieve an asynchronous training workflow. Agents can be installed on several types of devices and subscribe to separate task queues. These queues are kept in dependable storage so that failed tasks can be automatically replayed.

TensorLayer, unlike other TensorFlow-based tools like Keras and TFLearn, allows for simple low-level control over the execution of layers and neural networks. It also includes additional dataset and workflow modules, which relieve users of time-consuming data pre-processing, post-processing, module serving, and data administration duties. Its non-invasive unified module interaction interface accepts layers and networks imported from Keras and TFLearn.

Technical details

Helper functions include providing and importing layer implementations, establishing neural networks, handling states involved in model life-cycles, producing online or offline datasets, and developing a parallel training plan. Layer, model, dataset, and workflow are the four modules that contain these functions. These modules are described in turn in the sections that follow. We’ll go over them one by one.

Layer module

TensorLayer features a layer module with reference implementations of a variety of layers, including CNN, RNN, dropout, batch normalization, and many more. Similar to the widely used Lasagne, layers are built to form a neural network in a declarative manner. To aid developers with parameter sharing, each layer is given its own key. TensorFlow is in charge of the networks. TensorLayer is a hybrid and distributed platform that inherits from TensorFlow.

Model module

Models are logical representations of self-contained functional units that can be trained, assessed, and deployed in the field. Every model has its own network structure. Various versions or states of the model can exist throughout training (i.e., weights). Persisted, cached, and reloaded states are all possible. 

User-defined model events can be recorded with TensorLayer. Training steps, learning speed, and accuracy are all reflected in traditional competitions. They are frequently used to diagnose a training process in order to enable model versioning and interactive learning, for example.

Dataset module

The dataset module is where you keep track of your training samples and predictions. They’re saved in MongoDB as documents. A unique key, sample, label, and user-defined tags are all included in each document.

Declarative queries that carry requirements to tag fields are used to define datasets. Queries create views of the underlying data and do not require additional storage.

General streaming datasets are used to model the data. A stream controller is assigned to each dataset, which constantly monitors the availability of samples and predictions and subsequently triggers the appropriate training activities for that dataset.

Workflow module

The workflow module makes it easier to construct model group operations and learning systems that use asynchronous feedback loops. It is also effective for complicated cognitive systems with components that require training. For example, the creator of an image captioning system [28] trained a CNN to grasp the context of images before training an RNN decoder to generate descriptions based on the detected context. This example builds a two-stage asynchronous training plan that TensorLayer can support.

Implementation with TensorLayer

In this section, we’ll perform the image classification using transfer learning. The model used here is VGG16. To perform this classification we just need to install a package of tensor layers and the rest of the things are managed by the package. 

Let’s now quickly install and import dependencies.

! pip install tensorlayer

# interdependencies required for image pre-processing
! pip install scipy==1.2.1

import numpy as np
import tensorflow as tf

import tensorlayer as tl
from tensorlayer.models.imagenet_classes import class_names

Now first thing first, the model can be imported from tensorlayer.model package. After loading the model the code outputs the model summary.

# get the whole model
vgg = tl.models.vgg16(pretrained=True)

Here is a summary of the model.

Now next we have to load and pre-process the image as the model runs on predefined image settings. 

#image loading, pre-processing
img = tl.vis.read_image(‘/content/steam-train-rides-1570200690.jpg’)
img = tl.prepro.imresize(img, (224, 224)).astype(np.float32) / 255

Here is the image that we are feeding.

Now we’ll process the image for prediction.

#process the image to the model and get probas
output = vgg(img, is_train=False)
probs = tf.nn.softmax(output)[0].numpy()

The results are obtained in the form of probability of classes that are identified in the image and are arranged in decreasing order.

# print result
preds = (np.argsort(probs)[::-1])[0:5]
for q in preds:
print(class_names[q], probs[q])

Final words

Through this article, we have discussed TensorLayer, a python based library that acts as a bridge. TensorLayer not only provides a high-level layer abstraction like other libraries, but also an end-to-end workflow that includes rich data pre-processing, training, post-processing, serving modules, and database management, allowing developers to build a complete learning system from the experimental phase to the final product.

References
Source: https://analyticsindiamag.com/a-guide-to-tensorlayer-for-efficient-deep-learning-development/