pdfcoffee
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 ]
- Page 131 and 132: Regression2. Now, we define the fea
- Page 133 and 134: Regression2. Download the dataset:(
- Page 135 and 136: RegressionThe following is the Tens
- Page 137 and 138: RegressionIn regression the aim is
- Page 139 and 140: RegressionThe Estimator outputs the
- Page 141 and 142: RegressionThe following is the grap
- Page 143 and 144: RegressionReferencesHere are some g
- Page 145 and 146: Convolutional Neural NetworksIn thi
- Page 147 and 148: Convolutional Neural NetworksIn thi
- Page 149 and 150: Convolutional Neural NetworksIn oth
- Page 151 and 152: Convolutional Neural NetworksThen w
- Page 153 and 154: Convolutional Neural NetworksHoweve
- Page 155 and 156: Convolutional Neural NetworksPlotti
- Page 157 and 158: Convolutional Neural NetworksIn gen
- Page 159 and 160: Convolutional Neural NetworksOur ne
- Page 161 and 162: Convolutional Neural NetworksThese
- Page 163 and 164: Convolutional Neural NetworksSo, we
- Page 165 and 166: Convolutional Neural NetworksEach i
- Page 167 and 168: Convolutional Neural NetworksVery d
- Page 169 and 170: Convolutional Neural NetworksRecogn
- Page 171 and 172: Convolutional Neural NetworksIf we
- Page 173 and 174: Convolutional Neural NetworksRefere
- Page 175 and 176: Advanced Convolutional Neural Netwo
- Page 177 and 178: Advanced Convolutional Neural Netwo
- Page 179 and 180: Advanced Convolutional Neural Netwo
- Page 181: Advanced Convolutional Neural Netwo
- Page 185 and 186: Advanced Convolutional Neural Netwo
- Page 187 and 188: Advanced Convolutional Neural Netwo
- Page 189 and 190: Advanced Convolutional Neural Netwo
- Page 191 and 192: Advanced Convolutional Neural Netwo
- Page 193 and 194: Advanced Convolutional Neural Netwo
- Page 195 and 196: Advanced Convolutional Neural Netwo
- Page 197 and 198: Advanced Convolutional Neural Netwo
- Page 199 and 200: Advanced Convolutional Neural Netwo
- Page 201 and 202: Advanced Convolutional Neural Netwo
- Page 203 and 204: Advanced Convolutional Neural Netwo
- Page 205 and 206: Advanced Convolutional Neural Netwo
- Page 207 and 208: Advanced Convolutional Neural Netwo
- Page 209 and 210: Advanced Convolutional Neural Netwo
- Page 211 and 212: Advanced Convolutional Neural Netwo
- Page 213 and 214: Advanced Convolutional Neural Netwo
- Page 215 and 216: Advanced Convolutional Neural Netwo
- Page 217 and 218: Advanced Convolutional Neural Netwo
- Page 219 and 220: Advanced Convolutional Neural Netwo
- Page 221 and 222: Advanced Convolutional Neural Netwo
- Page 223 and 224: Advanced Convolutional Neural Netwo
- Page 226 and 227: GenerativeAdversarial NetworksIn th
- Page 228 and 229: [ 193 ]Chapter 6Eventually, we reac
- Page 230 and 231: [ 195 ]Chapter 6Next, we combine th
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 ]