Practical ApplicationsSolving an image classification problem with TensorFlowLet’s consider the application of TensorFlow to solve a classic computer vision problem — image classification.
As data, let’s take one of the popular datasets — MNIST, containing 70 000 images of handwritten digits 0–9.
Let’s build a simple convolutional neural network in TensorFlow:
- A convolution layer for feature extraction
- Pooling layer for dimensionality reduction
- Full-link layer for classification
model = tf.keras.Sequential()model.add(tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)))model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))model.add(tf.keras.layers.Flatten())model.add(tf.keras.layers.Dense(10, activation='softmax'))Let’s train the model on MNIST data:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])model.fit(train_images, train_labels, epochs=5, batch_size=64)Now the network is ready to classify handwritten digits with quite high accuracy!
Image classification is one of the most common examples of applying neural networks in practice with TensorFlow.