09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Advanced Convolutional Neural Networks

def gram_matrix(input_tensor):

# image channels first

channels = int(input_tensor.shape[-1])

a = tf.reshape(input_tensor, [-1, channels])

n = tf.shape(a)[0]

gram = tf.matmul(a, a, transpose_a=True)

return gram / tf.cast(n, tf.float32)

def get_style_loss(base_style, gram_target):

# height, width, num filters of each layer

height, width, channels = base_style.get_shape().as_list()

gram_style = gram_matrix(base_style)

return tf.reduce_mean(tf.square(gram_style - gram_target))

In short, the concepts behind style transfer are simple: first, we use VGG19 as a

feature extractor and then we define two suitable function distances, one for style

and the other one for contents, which are appropriately minimized. If you want to

try this out for yourself, then TensorFlow tutorials are available online. If you are

interested in a demo of this technique, you can go to deepart.io free site where they

do style transfer.

A tutorial is available at https://colab.research.google.

com/github/tensorflow/models/blob/master/research/

nst_blogpost/4_Neural_Style_Transfer_with_Eager_

Execution.ipynb.

Next, we'll check out another interesting image-related application of CNNs.

Creating a DeepDream network

Another interesting application of CNNs is DeepDream, a computer vision program

created by Google [8], which uses a CNN to find and enhance patterns in images.

The result is a dream-like hallucinogenic effect. Similarly to the previous example,

we are going to use a pretrained network to extract features. However, in this case

we want to "enhance" patterns in images, meaning that we need to maximize some

functions. This tells us that we need to use a gradient ascent and not a descent. First,

let's see an example from Google gallery (https://photos.google.com/share/AF1

QipPX0SCl7OzWilt9LnuQliattX4OUCj_8EP65_cTVnBmS1jnYgsGQAieQUc1VQWdgQ?k

ey=aVBxWjhwSzg2RjJWLWRuVFBBZEN1d205bUdEMnhB) where the classic Seattle

landscape is "inceptioned" with hallucinogenic dreams such as birds, cards, and

strange flying objects.

[ 168 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!