Real-time style transfer in unity using deep neural networks
Deep learning and machine learning are slowly becoming ubiquitous. They are used in almost every device, they are no longer confined to just supercomputers and advanced applications but also are making their place in real-time.
Real-time in deep learning often deals with replicating a particular scenario inside the machine through image optimization techniques. This optimization has widely been used in gaming consoles to increase the quality of the user interface.
Several platforms have started to embed neural network learning in their system. One such platform is the Unity3d game development. Some features that these neural networks provide like style sharing, illumination, and so on are still in their early stages of integration with games. But a developer can integrate several technologies inside the game.
Let us look at one such rendering application style sharing in detail and understand the ins and outs.
What is style sharing?
As the name vividly suggests, style sharing is the process of transferring the style of one image to the other using various techniques.
If one tries to explain it in simple words, style sharing is like capturing an image and turning it into digitized data using a neural network. This digitized data is in the form of computer language so that machines can easily recognize it. The captured image is then converted to stylized versions.
What is a deep neural network?
A deep neural network is a neural network having more than two layers to it. It embeds a certain kind of complexity to it. They usually collect data and form the links between them, these links are called neural networks. Hundreds and thousands of information culminate together to form a particular neural network. As the complexity of the task increases the deeper the network goes.
Where is a deep neural network used?
Deep learning has become quite ubiquitous and is used in several applications like:
- Stock rendering
- Risk management
- Face recognition
- Speech recognition
- Signature recognition
- And the list is never-ending.
How to train the neural network?
Training a neural network is a quite complex task and required various steps and procedures:
1] To get started with the initial steps of the training select a style transfer module neural network of your choice. This network performs two important steps firstly they create a compact outline of the given image. This helps you get a rough image of what needs to be created.
Once the compact network is created it is passed through an actual network to convert into a stylized image. This image can be used in real-time unity 3d game development that is the image can be implemented in the Unity platform.
2] The image to be trained goes through several downsampling and upsampling. These have five residual blocks in between them. Each residual block performs various functions and can be used to create a different styling environment.
When the content image goes through these steps a balance is created between the content and the style to be used. During the overlap of both and training these images, weights are taken into consideration. This weighing helps in maintaining the fidelity of the original image and gives it the style required.
Integration with unity
Unity labs along with Baracuda has created a cross-platform interface of neural networks. The pre-trained models on the original library can be readily saved in the disk of Unity using Barracuda. The Barracuda uses the ONNX import path to render any libraries. The import can be easily performed by drag and drop into the Unity library.
As the module is successfully imported inside Unity, the asset inspector gives further information about the input of the network, and also the layers that are present inside the network. As a user, you only need to provide the system with input and verify the stylized output. Each stage of the network is copied to the system in the form of an image that is displayed.
The Barracuda of Unity runs on GPU and CPU and they run on any version of Unity.
Performance of the system
Neural networks have heavy computations and the system needs to have a high optimization power to carry out all the performance aesthetically and smoothly.
Using a suitable GPU can increase the overall speed of optimization and the performance can be highly increased. GPU’s like NVidia RTX 2080 GPU can carry out the task in 23ms and some more highly curated and high-performance GPU can wrap the task in 20ms or less.
The time is often divided in terms of rendering and inference and both tasks are completed within 10 ms. During the inference stage, you can readily add styles to the content image making it different in styles. The style switch is relatively smoother and does not contain any lag, as the inference is carried out only once.
As the Barracuda Unity platform is supported by the cross-platform the styles can be easily shared to PS4 as well. The time required for this hardware is competitively higher than that for any GPU platform.
To render the images relatively faster on the device several techniques can be used like reducing the neural size and also by level optimization. This can reduce the time and increase the speed of the system.
Ultimately, the real-time style transfer helps the developers to create an immersive environment in the games. The style sharing technique is quite complicated and needs several procedures and practice for its work. It is sometimes a tedious task to understand all the technicalities that are fed to the network.
This can lead to confusion. Hence a better option is to follow a particular example step by step to understand all the required materials for the same.