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.

Advanced Convolutional Neural Networks

Keras applications

Keras applications include models for image classification with weights

trained on ImageNet (Xception, VGG16, VGG19, ResNet, ResNetV2, ResNeXt,

InceptionV3, InceptionResNetV2, MobileNet, MobileNetV2, DenseNet, and

NASNet). In addition, there are a few other reference implementations from the

community for object detection and segmentation, sequence learning (see Chapter

8, Recurrent Neural Networks), reinforcement learning (see Chapter 11, Reinforcement

Learning), and GANs (see Chapter 6, Generative Adversarial Networks).

TensorFlow Hub

TensorFlow Hub (https://www.tensorflow.org/hub) is an alternative collection

of pretrained models. However, Hub is not fully integrated with TensorFlow 2.0

as of September 2019, but this issue will surely be fixed with the final version of

2.0 (however, as of July 2019, eager computation is also supported). TensorFlow

Hub includes modules for Text Classification, Sentence Encoding (see Chapter 9,

Autoencoders), Image Classification, Feature Extraction, Image Generation with GANs

(see Chapter 6, Generative Adversarial Networks), and Video Classification. Currently,

both Google and DeepMind contribute to publishing.

See the issue about eager execution at https://github.com/

tensorflow/hub/issues/124 and the issue for full integration

at https://github.com/tensorflow/tensorflow/

issues/25362.

Let's look at an example of using TF.Hub. In this case, we have a simple image

classifier using MobileNetV2:

import matplotlib.pylab as plt

import tensorflow as tf

import tensorflow_hub as hub

import numpy as np

import PIL.Image as Image

classifier_url ="https://tfhub.dev/google/tf2-preview/mobilenet_v2/

classification/2" #@param {type:"string"}

IMAGE_SHAPE = (224, 224)

# wrap the hub to work with tf.keras

classifier = tf.keras.Sequential([

hub.KerasLayer(classifier_url, input_shape=IMAGE_SHAPE+(3,))

])

grace_hopper = tf.keras.utils.get_file('image.jpg','https://storage.

googleapis.com/download.tensorflow.org/example_images/grace_hopper.

jpg')

grace_hopper = Image.open(grace_hopper).resize(IMAGE_SHAPE)

[ 158 ]

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

Saved successfully!

Ooh no, something went wrong!