February 12, 2021

Coding16 - Introduce Tensorflow

Learn machine learning through TensorFlow, and achieve a simple NST work.

What is TensorFlow?

Tensorflow is a machine learning framework, created by Google; it can be used to design, build and train deep.

What is tensor?

We can compare a tensor to a video file. A video consists of a series of frames, and each frame includes many individual color values. The collection of frames can be regarded as a tensor. Each individual color value can be regarded as a scalar, and a group of color values can be regarded as a vector, for example, RGBA can be regarded as a 4D vector (xyzw). Vector blocks can be thought as tensors.

Another simple explanation: tensors are data. They can be lists of numbers or vectors of any dimensionality. They can be multidimensional data blocks, such as a set of images or videos. Tensorflow essentially allows users to construct a large amount of digital data, and then use CPU, GPU and even TPU to process the data, but in essence it is still a large number of multiplications and additions.

This is a small exercise to implement NST using Python and tensorflow, you also can find it in colab:

Import our libraries and modules.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import tensorflow as tf
import IPython.display as display

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (12, 12)
mpl.rcParams['axes.grid'] = False

import numpy as np
import PIL.Image
import time
import functools

def tensor_to_image(tensor):
tensor = tensor * 255
tensor = np.array(tensor, dtype = np.uint8)
if np.ndim(tensor) > 3:
assert tensor.shape[0] == 1
tensor = tensor[0]
return PIL.Image.fromarray(tensor)

Choose our style image and content image.

1
2
3
content_path = tf.keras.utils.get_file('content.jpg', 'https://mywowo.net/media/images/cache/londra_tower_bridge_01_introduzione_jpg_1200_630_cover_85.jpg')
style_path = tf.keras.utils.get_file('style.jpg', 'https://resources.matcha-jp.com/resize/720x2000/2017/10/14-38660.jpeg')
# style_path = tf.keras.utils.get_file('style.jpg', 'https://pic3.zhimg.com/80/v2-fe222a9c96b35f9d86da39122777c8d2_1440w.jpg')

Define a function to load images, and limit the max size under 512px.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def load_img(path_to_img):
max_dim = 512
img = tf.io.read_file(path_to_img)
img = tf.image.decode_image(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)

shape = tf.cast(tf.shape(img)[:-1], tf.float32)
long_dim = max(shape)
scale = max_dim / long_dim

new_shape = tf.cast(shape * scale, tf.int32)

img = tf.image.resize(img, new_shape)
img = img[tf.newaxis, :]
return img

Create a simple function to display image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def imshow(image, title=None):
if len(image.shape) > 3:
image = tf.squeeze(image, axis=0)

plt.imshow(image)
if title:
plt.title(title)

content_image = load_img(content_path)
style_image = load_img(style_path)

plt.subplot(1, 2, 1)
imshow(content_image, 'Content Image')
plt.subplot(1, 2, 2)
imshow(style_image, 'Style Image')

Use TF-Hub to make Fast Neural Style Transfer.

1
2
3
4
import tensorflow_hub as hub
hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/1')
stylized_image = hub_module(tf.constant(content_image), tf.constant(style_image))[0]
tensor_to_image(stylized_image)

We can use intermediate layers to get content and style representations of the image; we can use VGG19 to define content and style representations.

1
2
3
4
5
x = tf.keras.applications.vgg19.preprocess_input(content_image*255)
x = tf.image.resize(x, (224, 224))
vgg = tf.keras.applications.VGG19(include_top=True, weights='imagenet')
prediction_probabilities = vgg(x)
prediction_probabilities.shape
1
2
predicted_top_5 = tf.keras.applications.vgg19.decode_predictions(prediction_probabilities.numpy())[0]
[(class_name, prob) for (number, class_name, prob) in predicted_top_5]

Loading a VGG19 without the classification head, and list the layer names.

1
2
3
4
5
vgg = tf.keras.applications.VGG19(include_top=False, weights='imagenet')

print()
for layer in vgg.layers:
print(layer.name)

Choose intermediate layers from the network to represent the style and content of the image.

1
2
3
4
5
6
7
8
9
10
content_layers = ['block5_conv2']

style_layers = ['block1_conv1',
'block2_conv1',
'block3_conv1',
'block4_conv1',
'block5_conv1']

num_content_layers = len(content_layers)
num_style_layers = len(style_layers)

Extract the intermediate layer values using the Keras functional API tf.keras.applications.

1
2
3
4
5
6
7
8
9
10
def vgg_layers(layer_names):
"""Creates a vgg model that returns a list of intermediate output values."""
# Load our model and pretrained VGG, trained on tmagenet data
vgg = tf.keras.applications.VGG19(include_top=False, weights='imagenet')
vgg.trainable = False

outputs = [vgg.get_layer(name).output for name in layer_names]

model = tf.keras.Model([vgg.input], outputs)
return model

Create the model.

1
2
3
4
5
6
7
8
9
10
11
style_extractor = vgg_layers(style_layers)
style_outputs = style_extractor(style_image*255)

# Look at the statistics of each layer's output
for name, output in zip(style_layers, style_outputs):
print(name)
print(" shape: ", output.numpy().shape)
print(" min: ", output.numpy().min())
print(" max: ", output.numpy().max())
print(" mean: ", output.numpy().mean())
print()

The style of images can be described by the means and correlations across the different feature maps. We can tf.linalg.einsum function to achieve that.

1
2
3
4
5
def gram_matrix(input_tensor):
result = tf.linalg.einsum('bijc,bijd->bcd', input_tensor, input_tensor)
input_shape = tf.shape(input_tensor)
num_locations = tf.cast(input_shape[1]*input_shape[2], tf.float32)
return result/(num_locations)

Build a model that returns the style and content tensors, extract style and content.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class StyleContentModel(tf.keras.models.Model):
def __init__(self, style_layers, content_layers):
super(StyleContentModel, self).__init__()
self.vgg = vgg_layers(style_layers + content_layers)
self.style_layers = style_layers
self.content_layers = content_layers
self.num_style_layers = len(style_layers)
self.vgg.trainable = False

def call(self, inputs):
"Expects float input in [0, 1]"
inputs = inputs*255.0
preprocessed_input = tf.keras.applications.vgg19.preprocess_input(inputs)
outputs = self.vgg(preprocessed_input)
style_outputs, content_outputs = (outputs[:self.num_style_layers],
outputs[self.num_style_layers:])

style_outputs = [gram_matrix(style_output)
for style_output in style_outputs]

content_dict = {content_name: value
for content_name, value
in zip(self.content_layers, content_outputs)}

style_dict = {style_name: value
for style_name, value
in zip(self.style_layers, style_outputs)}

return {'content': content_dict, 'style': style_dict}

This model returns the gram matrix(style) of the style_layers and content of the content_layers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
extractor = StyleContentModel(style_layers, content_layers)

results = extractor(tf.constant(content_image))

print('Styles:')
for name, output in sorted(results['style'].items()):
print(" ", name)
print(" shape: ", output.numpy().shape)
print(" min: ", output.numpy().min())
print(" max: ", output.numpy().max())
print(" mean: ", output.numpy().mean())
print()

print("Contents:")
for name, output in sorted(results['content'].items()):
print(" ", name)
print(" shape: ", output.numpy().shape)
print(" min: ", output.numpy().min())
print(" max: ", output.numpy().max())
print(" mean: ", output.numpy().mean())

Calculating the mean square error for image’s output relative to each target, then take the weighted sum of these losses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
style_targets = extractor(style_image)['style']
content_targets = extractor(content_image)['content']

# Define a `tf.Variable` to contain the image to optimize.
image = tf.Variable(content_image)

# Define a function to keep the pixel values between 0 and 1.
def clip_0_1(image):
return tf.clip_by_value(image, clip_value_min = 0.0, clip_value_max = 1.0)

# Create an optimizer with `Adam`.
opt = tf.optimizers.Adam(learning_rate=0.02, beta_1=0.99, epsilon=1e-1)

# Use a weighted combination of the two losses to get the total loss.
style_weight = 1e-2
content_weight=1e4

def style_content_loss(outputs):
style_outputs = outputs['style']
content_outputs = outputs['content']
style_loss = tf.add_n([tf.reduce_mean((style_outputs[name]-style_targets[name])**2)
for name in style_outputs.keys()])
style_loss *= style_weight / num_style_layers

content_loss = tf.add_n([tf.reduce_mean((content_outputs[name]-content_targets[name])**2)
for name in content_outputs.keys()])
content_loss *= content_weight / num_content_layers
loss = style_loss + content_loss
return loss

Use tf.GradientTape to update the image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@tf.function()
def train_step(image):
with tf.GradientTape() as tape:
outputs = extractor(image)
loss = style_content_loss(outputs)

grad = tape.gradient(loss, image)
opt.apply_gradients([(grad, image)])
image.assign(clip_0_1(image))

# Run steps to test by 5 times

train_step(image)
train_step(image)
train_step(image)
train_step(image)
train_step(image)
tensor_to_image(image)

Perform a longer optimization.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import time
start = time.time()

epochs = 10
steps_per_epoch = 50

step = 0
for n in range(epochs):
for m in range(steps_per_epoch):
step += 1
train_step(image)
print(".", end='')
display.clear_output(wait=True)
display.display(tensor_to_image(image))
print("Train step: {}".format(step))

end = time.time()
print("Total time: {:.1f}".format(end-start))

It produces alot of high frequency artifacts. Using an explicit regularization term to decrease those.

1
2
3
4
5
def high_pass_x_y(image):
x_var = image[:,:,1:,:] - image[:,:,:-1,:]
y_var = image[:,1:,:,:] - image[:,:-1,:,:]

return x_var, y_var
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
x_deltas, y_deltas = high_pass_x_y(content_image)

plt.figure(figsize=(14,10))
plt.subplot(2,2,1)
imshow(clip_0_1(2*y_deltas+0.5), "Horizontal Deltas: Original")

plt.subplot(2,2,2)
imshow(clip_0_1(2*x_deltas+0.5), "Vertical Deltas: Original")

x_deltas, y_deltas = high_pass_x_y(image)

plt.subplot(2,2,3)
imshow(clip_0_1(2*y_deltas+0.5), "Horizontal Deltas: Styled")

plt.subplot(2,2,4)
imshow(clip_0_1(2*x_deltas+0.5), "Vertical Deltas: Styled")

1
2
3
4
5
6
7
plt.figure(figsize=(14,10))

sobel = tf.image.sobel_edges(content_image)
plt.subplot(1,2,1)
imshow(clip_0_1(sobel[...,0]/4+0.5), "Horizontal Sobel-edges")
plt.subplot(1,2,2)
imshow(clip_0_1(sobel[...,1]/4+0.5), "Vertical Sobel-edges")

The regularization loss associated with this is the sum of the squares of the values.

1
2
3
def total_variation_loss(image):
x_deltas, y_deltas = high_pass_x_y(image)
return tf.reduce_sum(tf.abs(x_deltas)) + tf.reduce_sum(tf.abs(y_deltas))
1
total_variation_loss(image).numpy()
1
tf.image.total_variation(image).numpy()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Rerun the Optimization.
total_variation_weight = 30

@tf.function()
def train_step(image):
with tf.GradientTape() as tape:
outputs = extractor(image)
loss = style_content_loss(outputs)
loss += total_variation_weight*tf.image.total_variation(image)

grad = tape.gradient(loss, image)
opt.apply_gradients([(grad, image)])
image.assign(clip_0_1(image))

# Reinitialize the optimization variable.
image = tf.Variable(content_image)

# Rerun the optimization.
import time
start = time.time()

epochs = 10
steps_per_epoch = 50

step = 0
for n in range(epochs):
for m in range(steps_per_epoch):
step += 1
train_step(image)
print(".", end='')
display.clear_output(wait=True)
display.display(tensor_to_image(image))
print("Train step: {}".format(step))

end = time.time()
print("Total time: {:.1f}".format(end-start))

About this Post

This post is written by Siqi Shu, licensed under CC BY-NC 4.0.