Building an Image Recognition Model using TensorFlow and Keras Libraries in Python

Image recognition models are extremely useful in a wide range of applications, from autonomous vehicles and medical diagnosis to social media analysis and e-commerce. By teaching a computer to identify and classify images based on certain features, such as color, shape, and texture, we can automate tasks that would be difficult or impossible for humans to do at scale. For example, an image recognition model can be used to detect objects in images, recognize faces and emotions, identify text in images, and even diagnose medical conditions based on medical images. In e-commerce, image recognition models can be used to recommend products based on visual similarity, allowing for more personalized and relevant product recommendations.
Pretty cool, right? Let’s give it a try…
Step 1. Install the required libraries:
First, you need to install TensorFlow and Keras libraries in Python. You can install them using pip command in the terminal.
pip install tensorflow
pip install keras
Step 2. Import the required libraries:
Once the libraries are installed, you need to import them in your Python script.
import tensorflow as tf
from tensorflow import keras
Step 3. Load the dataset:
Next, you need to load a dataset of images that you will use to train your model. For this example, we will use the CIFAR-10 dataset, which contains 60,000 32×32 color images in 10 classes. You can load the dataset using the load_data() function from keras.datasets module.
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
Step 4. Preprocess the data:
Once the dataset is loaded, you need to preprocess the data so that it can be used for training. This involves tasks such as resizing the images to a consistent size, normalizing the pixel values, and splitting the data into training and validation sets.
# Resize the images to 224x224
x_train = tf.image.resize(x_train, (224, 224))
x_test = tf.image.resize(x_test, (224, 224))
# Normalize the pixel values to be between 0 and 1
x_train = x_train / 255.0
x_test = x_test / 255.0
# Split the data into training and validation sets
x_train, x_val = x_train[:45000], x_train[45000:]
y_train, y_val = y_train[:45000], y_train[45000:]
Step 5. Define the model:
Once the data is preprocessed, you need to define the architecture of the model. For this example, we will use a pre-trained ResNet50V2 model from Keras, which has been trained on the ImageNet dataset.
# Load the ResNet50V2 model
base_model = keras.applications.ResNet50V2(
input_shape=(224, 224, 3),
include_top=False,
weights='imagenet'
)
# Freeze the pre-trained layers
for layer in base_model.layers:
layer.trainable = False
# Add a new classification layer on top of the pre-trained layers
inputs = keras.Input(shape=(224, 224, 3))
x = base_model(inputs, training=False)
x = keras.layers.GlobalAveragePooling2D()(x)
outputs = keras.layers.Dense(10, activation='softmax')(x)
# Define the model
model = keras.Model(inputs, outputs)
Step 6. Train the model:
Once the model is defined, you need to train it using the preprocessed data.
# Compile the model with a categorical cross-entropy loss function and Adam optimizer
model.compile(
loss='sparse_categorical_crossentropy',
optimizer=keras.optimizers.Adam(lr=0.001),
metrics=['accuracy']
)
# Train the model for 10 epochs
model.fit(
x_train, y_train,
batch_size=32,
epochs=10,
validation_data=(x_val, y_val)
)
Step 7. Evaluate the model:
After training the model, you need to evaluate its performance on the validation set.
# Evaluate the model on the validation set
loss, accuracy = model.evaluate(x_val, y_val)
print('Validation accuracy:', accuracy)
Step 8. Test the model:
Once you are satisfied with the model’s performance on the validation set, you can test it on a new set of images to see how well it generalizes to unseen data.
# Evaluate the model on the test set
loss, accuracy = model.evaluate(x_test, y_test)
print('Test accuracy:', accuracy)
Step 9. Save the model:
If you want to use the model in a real-world application, you can save it as a file.
# Save the model as a file
model.save('image_recognition_model.h5')
Super cool, right? Image recognition models have the potential to revolutionize many industries and improve the efficiency and accuracy of a wide range of tasks. If you want to learn more, check out the book: A.I. & Machine Learning by Lyron Foster.
Lyron Foster is a Hawai’i based African American Author, Musician, Actor, Blogger, Philanthropist and Multinational Serial Tech Entrepreneur.