Here’s a practice exercise using TensorFlow:
Exercise: Build a Neural Network for Flower Classification
In this exercise, you’ll use TensorFlow to build a neural network for classifying images of flowers into different species. The dataset you’ll be using is the Flower Recognition Dataset from Kaggle, which contains 4,100 images of 5 different flower species.
Step 1: Import the Required Libraries
First, you’ll need to import the necessary libraries, including TensorFlow, NumPy, and Pandas. You can use the following code:
pythonCopy codeimport tensorflow as tf
import numpy as np
import pandas as pd
Step 2: Load and Preprocess the Data
Next, you’ll need to load the dataset and preprocess the images by resizing them and normalizing the pixel values. You can use the following code:
sqlCopy codefrom tensorflow.keras.preprocessing.image import ImageDataGenerator
data_generator = ImageDataGenerator(rescale=1./255, validation_split=0.2)
train_data = data_generator.flow_from_directory(
'flower_data/train',
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='training')
val_data = data_generator.flow_from_directory(
'flower_data/train',
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='validation')
Step 3: Build the Model
Now you’re ready to build your neural network model. For this example, we’ll use a pre-trained model called MobileNetV2 as a feature extractor, followed by a dense layer with 5 output units for the 5 flower species. You can define the model with the following code:
pythonCopy codebase_model = tf.keras.applications.MobileNetV2(input_shape=(224, 224, 3),
include_top=False,
weights='imagenet')
for layer in base_model.layers:
layer.trainable = False
x = base_model.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(5, activation='softmax')(x)
model = tf.keras.models.Model(inputs=base_model.input, outputs=x)
Step 4: Compile and Train the Model
After building the model, you’ll need to compile it with an optimizer, a loss function, and a metric for evaluation. You can use the Adam optimizer, the categorical crossentropy loss function, and the accuracy metric with the following code:
pythonCopy codemodel.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Now you’re ready to train the model with the following code:
bashCopy codehistory = model.fit(train_data, epochs=5, validation_data=val_data)
Step 5: Evaluate the Model
Finally, you can evaluate the model on the test dataset to see how well it performs on unseen data. You can use the following code:
pythonCopy codetest_data = data_generator.flow_from_directory(
'flower_data/test',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
model.evaluate(test_data)
And that’s it! With these five steps, you’ve built a neural network model for flower classification using TensorFlow. You can experiment with different pre-trained models, optimization algorithms, and hyperparameters to improve the performance of your model.