Short Bytes: Compositional Pattern Producing Networks (CPPN), a kind of generative neural network, are a type of artificial neural network that differ in their set of activation patterns and application. By implementing it in Python using NumPy, we can easily generate artistic images. So, try it out and let your creativity flow.Having been introduced to AI, the next step would be to get your hands dirty by trying to actually use neural networks. There are ample resources out there by which you can get started with Machine Learning. With a visually attractive application, I intend to describe how to generate artistic images using Neural Networks.
Can Neural Networks generate data?
The general opinion about Deep Learning or Machine Learning, in general, is that they require data to learn a specific model. But there are separate classes of Deep Learning models to generate data similar to the ones which were given to them as input. These are called as generative models. So, what do they learn? They learn the hidden structures within the data without relying on external labels. Therefore, these models are categorized under Unsupervised Learning. Here, we shall discuss the most elementary form of a generative neural network known as Compositional Pattern Producing Network.
Compositional Pattern Producing Networks –
Artificial Neural Networks can learn to approximate any function due to their architecture. Basically, we input data along with its output (or label), and the network tunes some parameters (weights) to fit the function represented by the data. In essence, any function can be mapped to a set of numbers for a specific architecture of neural networks.
By setting random numbers to the weights, we can generate some patterns. This type of Artificial Neural Network is called as a Compositional Pattern Producing Network or CPPN for short. But, there is more to this story.
Activation Function(s) –
Activation functions are the foremost controlling parameters for the pattern of the generated image. It is common to use a composite of activation functions, i.e., function of functions. The basal activation functions used in this project are –
- Tanh Activation –
- Sigmoid Activation-
- Sinusoidal Activation-
- Softplus Activation-
The combination of these activation functions is the basis for producing patterns. That is why this is called as a “compositional” network.
Creating Art –
Using these CPPNs, let’s create an AI Artist (more of a “Modern” Artist). An important feature of this network is that we don’t have to train using any dataset. The neural network generates these images automatically based on some random input. This may sound more like Artificial Creativity.
So, if the network generates based on random data, then we would expect something like this —
But the actual images produced by our CPPN are –
This is due to the fact that instead of generating each pixel independently, the network generates them based on their location on the image.
Note: There are plenty of frameworks like Tensorflow, Theano, Torch, Caffe to get started in Deep Learning. But, they often require a special understanding of the framework and have a slightly steeper learning curve–but totally worth it! Therefore, we shall build the network in Python programming language using only NumPy.
Building the Network –
The network has a set of inputs (z, x, y, r) and a combination of activation functions to generate an image. In this case, we build a fully connected network with four intermediate layers with a combination of the basal activation functions.
Running the Network –
We will provide an extra input to the network (represented by z) which controls the pattern. This is called the latent/hidden input vector. It is similar to a set of keys for generating a particular image. It can be generated by a uniformly distributed random sequence. By providing this input, the network produces a tensor which we display as an image.
Get your hands dirty –
This is my GitHub repo for the CPPN code. To generate an image of your choice, modify the following function call in the code –
Generate_Art(batch_size = 1, net_size = 16, h_size = 8, x_res = 512, y_res= 512, scaling = 10.0, RGB = True, seed = None)
Try changing the following parameters to generate awesome images –
- Net Size – To resize the net
- Hidden Dimensions – Length of the z vector
- Resolution of the image – Size of the image
- Scale of image – For zoomed image
- Colour – RGB or Grayscale
- Random Seed – To generate repeated patterns
- Activation functions and their combinations – To generate interesting patterns (Refer function build_CPPN() )
So, I hope you have fun playing around with these parameters. You can even create amazing wallpapers for your computer. Share your thoughts in the comments.