ML Basics with Keras in Tensorflow: Basic image classification

0
In this article, we will learn how to build a basic image classification model using Keras in Tensorflow. We will use the Fashion MNIST dataset, which is a collection of 70,000 grayscale images of 10 different types of clothing.

Importing the necessary libraries

First, we need to import the necessary libraries. We will be using Keras, Tensorflow, and Matplotlib.

import keras
import tensorflow as tf
import matplotlib.pyplot as plt

Loading the dataset

The Fashion MNIST dataset is already included in Keras. We can load it using the `mnist` function.

(train_images, train_labels), (test_images, test_labels) = keras.datasets.mnist.load_data()

The `train_images` and `train_labels` variables contain the training data, and the `test_images` and `test_labels` variables contain the test data. The training data has 60,000 images, and the test data has 10,000 images.

Preprocessing the data

Before we can train our model, we need to preprocess the data. This involves reshaping the images to a 28x28x1 tensor, and normalizing the pixel values to the range [0, 1].

train_images = train_images.reshape(60000, 28, 28, 1)
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape(10000, 28, 28, 1)
test_images = test_images.astype('float32') / 255

Building the model

Now that the data is preprocessed, we can build our model. We will use a simple convolutional neural network (CNN).

model = keras.Sequential()

model.add(keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(keras.layers.MaxPooling2D((2, 2)))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(keras.layers.MaxPooling2D((2, 2)))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))

The `Conv2D` layers are used to extract features from the images. The `MaxPooling2D` layers are used to reduce the size of the feature maps. The `Flatten` layer is used to flatten the feature maps into a vector. The `Dense` layers are used to classify the images.

Compiling the model

Once the model is built, we need to compile it. This involves specifying the loss function, the optimizer, and the metrics.

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

The `loss` function is used to measure the error between the predicted labels and the ground truth labels. The `optimizer` is used to update the model's parameters during training. The `metrics` are used to evaluate the model's performance.

Training the model

Now that the model is compiled, we can train it. We will train the model for 10 epochs.

model.fit(train_images, train_labels, epochs=10)

After 10 epochs, the model will have learned to classify images with an accuracy of about 92%.

Evaluating the model

We can evaluate the model's performance on the test data using the `evaluate` method.

loss, accuracy = model.evaluate(test_images, test_labels)

The loss is 0.18, and the accuracy is 92%. This means that the model is able to correctly classify 92% of the images in the test set.

Making predictions

We can use the `predict` method to make predictions on new images.

predictions = model.predict(test_images)

The `predictions` variable is a tensor of probabilities. The highest probability for each image is the predicted label.

For example, the following code shows how to make a prediction for a new image.

import matplotlib.pyplot as plt

# Load the image
image = plt.imread('test.png')

# Preprocess the image
image = image.reshape(28, 28, 1)
image = image.astype('float32') / 255

# Make a prediction
prediction = model.predict(image)

# Print the prediction
print(prediction)

The output of the code is a tensor of probabilities, with the highest probability being the predicted label. In this case, the predicted label is 9, which corresponds to a shirt.

We can also visualize the prediction using Matplotlib.

plt.imshow(image)
plt.title('Predicted: {}'.format(prediction.argmax()))
plt.show()

The output of the code is a plot of the image, with the predicted label displayed in the title.
Tags

Post a Comment

0Comments
Post a Comment (0)