pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 5Figure 10: An example of image segmentationIn this section we have covered, at a somewhat high level, various networkarchitectures that are popular in computer vision. Note that all of them are composedof the same basic CNNs and fully connected architectures. This composability is oneof the most powerful features of deep learning. Hopefully, this has given you someideas for networks that could be adapted for your own computer vision use cases.Classifying Fashion-MNIST with a tf.keras -estimator modelEstimators are another set of APIs available in TensorFlow. In this section we aregoing to see how to create them. In many situations, Estimators are preferable forperformance and ease of conversion to distributed training. In our example, weare going to use Fashion-MNIST, a drop-in replacement for the MNIST datasetreleased by Zalando (more information is available at https://github.com/zalandoresearch/fashion-mnist). Each example is a 28×28 grayscale image,associated with a label from 10 classes. An example is provided in Figure 11:Figure 11: Fashion-MNIST examples[ 147 ]

Advanced Convolutional Neural NetworksLet's start by importing what we need and preparing as described in the commentsbelow:import osimport timeimport tensorflow as tfimport numpy as np# How many categories we are predicting from (0-9)LABEL_DIMENSIONS = 10(train_images, train_labels), (test_images, test_labels) =tf.keras.datasets.fashion_mnist.load_data()TRAINING_SIZE = len(train_images)TEST_SIZE = len(test_images)train_images = np.asarray(train_images, dtype=np.float32) / 255# Convert the train images and add channelstrain_images = train_images.reshape((TRAINING_SIZE, 28, 28, 1))test_images = np.asarray(test_images, dtype=np.float32) / 255# Convert the train images and add channelstest_images = test_images.reshape((TEST_SIZE, 28, 28, 1))train_labels = tf.keras.utils.to_categorical(train_labels, LABEL_DIMENSIONS)test_labels = tf.keras.utils.to_categorical(test_labels, LABEL_DIMENSIONS)# Cast the labels to floattrain_labels = train_labels.astype(np.float32)test_labels = test_labels.astype(np.float32)print (train_labels.shape)print (test_labels.shape)Now let's build a convolutional model with the tf.Keras functional API:inputs = tf.keras.Input(shape=(28,28,1))x = tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3),activation='relu')(inputs)x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)x = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3),activation='relu')(x)x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)x = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3),activation='relu')(x)x = tf.keras.layers.Flatten()(x)x = tf.keras.layers.Dense(64, activation='relu')(x)[ 148 ]

Advanced Convolutional Neural Networks

Let's start by importing what we need and preparing as described in the comments

below:

import os

import time

import tensorflow as tf

import numpy as np

# How many categories we are predicting from (0-9)

LABEL_DIMENSIONS = 10

(train_images, train_labels), (test_images, test_labels) =

tf.keras.datasets.fashion_mnist.load_data()

TRAINING_SIZE = len(train_images)

TEST_SIZE = len(test_images)

train_images = np.asarray(train_images, dtype=np.float32) / 255

# Convert the train images and add channels

train_images = train_images.reshape((TRAINING_SIZE, 28, 28, 1))

test_images = np.asarray(test_images, dtype=np.float32) / 255

# Convert the train images and add channels

test_images = test_images.reshape((TEST_SIZE, 28, 28, 1))

train_labels = tf.keras.utils.to_categorical(train_labels, LABEL_

DIMENSIONS)

test_labels = tf.keras.utils.to_categorical(test_labels, LABEL_

DIMENSIONS)

# Cast the labels to float

train_labels = train_labels.astype(np.float32)

test_labels = test_labels.astype(np.float32)

print (train_labels.shape)

print (test_labels.shape)

Now let's build a convolutional model with the tf.Keras functional API:

inputs = tf.keras.Input(shape=(28,28,1))

x = tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3),

activation='relu')(inputs)

x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)

x = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3),

activation='relu')(x)

x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)

x = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3),

activation='relu')(x)

x = tf.keras.layers.Flatten()(x)

x = tf.keras.layers.Dense(64, activation='relu')(x)

[ 148 ]

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

Saved successfully!

Ooh no, something went wrong!