09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Convolutional Neural Networks

If we run the code we get 820 as a result, which is the image net code for "steaming

train". Equally important, all the other classes have very weak support, as shown in

the following figure:

To conclude this section, note that VGG16 is only one of the modules that is prebuilt

in tf.Keras. A full list of pretrained models is available online (https://www.

tensorflow.org/api_docs/python/tf/keras/applications).

Recycling prebuilt deep learning models

for extracting features

One very simple idea is to use VGG16, and more generally DCNN, for feature

extraction. This code implements the idea by extracting features from a specific layer.

Note that we need to switch to the functional API since the sequential model only

accepts layers:

import tensorflow as tf

from tensorflow.keras.applications.vgg16 import VGG16

from tensorflow.keras import models

from tensorflow.keras.preprocessing import image

from tensorflow.keras.applications.vgg16 import preprocess_input

import numpy as np

import cv2

# prebuild model with pre-trained weights on imagenet

base_model = VGG16(weights='imagenet', include_top=True)

print (base_model)

for i, layer in enumerate(base_model.layers):

print (i, layer.name, layer.output_shape)

# extract features from block4_pool block

model = models.Model(inputs=base_model.input,

outputs=base_model.get_layer('block4_pool').output)

[ 136 ]

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

Saved successfully!

Ooh no, something went wrong!