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

# Display an image

def show(img):

plt.figure(figsize=(12,12))

plt.grid(False)

plt.axis('off')

plt.imshow(img)

# https://commons.wikimedia.org/wiki/File:Flickr_-_Nicholas_T_-_Big_#

# Sky_(1).jpg

url = 'https://storage.googleapis.com/applied-dl/clouds.jpg'

img = preprocess(download(url))

show(deprocess(img))

Now let's use the Inception pretrained network for extracting features. We use

several layers and the goal is to maximize their activations. tf.keras functional

API is our friend here:

# We'll maximize the activations of these layers

names = ['mixed2', 'mixed3', 'mixed4', 'mixed5']

layers = [inception_v3.get_layer(name).output for name in names]

# Create our feature extraction model

feat_extraction_model = tf.keras.Model(inputs=inception_v3.input,

outputs=layers)

def forward(img):

# Create a batch

img_batch = tf.expand_dims(img, axis=0)

# Forward the image through Inception, extract activations

# for the layers we selected above

return feat_extraction_model(img_batch)

The loss function is the mean of all the activation layers considered, normalized by

the number of units in the layer itself:

def calc_loss(layer_activations):

total_loss = 0

[ 170 ]

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

Saved successfully!

Ooh no, something went wrong!