Vehicle Identification using VGG19: A Comprehensive Guide
The provided code implements vehicle identification using a pre-trained VGG19 model. Here's a breakdown of the key steps involved:
-
Data Preparation: Essential packages are imported, and the paths to the training and validation datasets are defined. Parameters like batch size, image dimensions, and number of epochs are also set.
-
Data Preprocessing: The
ImageDataGeneratoris employed to preprocess images by rescaling them and applying data augmentation techniques like horizontal flipping. -
Model Building: The VGG19 model is loaded and fine-tuned by adding extra layers. The model is compiled using an optimizer, a loss function, and evaluation metrics.
-
Model Training: The model is trained using the training data generator and validated using the validation data generator.
-
Plotting Training Process: The accuracy and loss curves are visualized for both the training and validation sets.
-
Model Testing: The model is evaluated using a test data generator. The accuracy is calculated, and a random sample of test images is displayed along with their true and predicted labels.
Note: The code assumes the dataset is organized into a specific directory structure, with separate folders for each class of vehicles in the training, validation, and test sets.
import os
import random
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D, BatchNormalization, GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
# Set parameters
train_dir = 'C:/Users/27546/Desktop/Vehicle Identification/train' # Training dataset directory
val_dir = 'C:/Users/27546/Desktop/Vehicle Identification/test' # Validation dataset directory
classes = os.listdir(train_dir) # Get list of classes
batch_size = 64 # Batch size
IMG_HEIGHT = 150 # Image height
IMG_WIDTH = 150 # Image width
epochs = 10 # Number of training epochs
# Define a function to display images
def plotImages(img_arr):
fig, axes = plt.subplots(1, 10, figsize=(20, 20))
axes = axes.flatten()
for img, ax in zip(img_arr, axes):
ax.imshow(img)
ax.axis('off')
plt.tight_layout()
plt.show()
# Create image generators
train_image_generator = ImageDataGenerator(
rescale=1./255, # Normalization
horizontal_flip=True # Horizontal flipping
)
val_image_generator = ImageDataGenerator(
rescale=1./255 # Normalization
)
train_data_gen = train_image_generator.flow_from_directory(
batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='categorical'
)
val_data_gen = val_image_generator.flow_from_directory(
batch_size=batch_size,
directory=val_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='categorical'
)
# Print the number of images found
print('Found {} images belonging to {} classes.'.format(train_data_gen.samples, len(train_data_gen.classes)))
print('Found {} images belonging to {} classes.'.format(val_data_gen.samples, len(val_data_gen.classes)))
total_train = train_data_gen.samples # Number of training samples
total_val = val_data_gen.samples # Number of validation samples
print('Total training data samples:', total_train)
print('Total validation data samples:', total_val)
# Display some images
training_images, _ = next(train_data_gen)
plotImages(training_images[:10])
# Load and fine-tune the VGG19 model
input_tensor = tf.keras.layers.Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3))
base_model = tf.keras.applications.VGG19(input_tensor=input_tensor, weights='imagenet', include_top=False)
print('Total number of layers in the base model:', len(base_model.layers))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
x = Dense(64, activation='relu')(x)
predictions = Dense(len(classes), activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
# Compile the model
opt = tf.keras.optimizers.Nadam(learning_rate=0.00001)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
# Print the model summary
model.summary()
# Train the model
history = model.fit(
train_data_gen,
steps_per_epoch=total_train // batch_size,
epochs=epochs,
validation_data=val_data_gen,
validation_steps=total_val // batch_size
)
# Plot the accuracy and loss curves
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
# Plot training and validation loss
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Cross Entropy')
plt.ylim([0, 1.0])
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()
# Plot training and validation accuracy
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()), 1])
plt.title('Training and Validation Accuracy')
plt.xlabel('epoch')
plt.show()
from sklearn.metrics import accuracy_score
# Test the model
# Load the test data generator
test_image_generator = ImageDataGenerator(
rescale=1. / 255 # Normalization
)
test_data_gen = test_image_generator.flow_from_directory(
batch_size=1, # Batch size of 1, one sample at a time
directory=val_dir,
shuffle=False,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='categorical'
)
# Get the total number of test samples
total_test = len(test_data_gen)
print('Total testing data batches:', total_test)
# Predict and calculate accuracy
y_pred = model.predict(test_data_gen)
y_pred_class = np.argmax(y_pred, axis=1)
y_true_class = test_data_gen.classes
class_names = list(test_data_gen.class_indices.keys())
accuracy = accuracy_score(y_true_class, y_pred_class)
print('Accuracy: {:.2f}%'.format(accuracy * 100))
# Display some random test images with true and predicted labels
for i in range(5):
rand_num = random.randint(0, total_test - 1)
img, label = test_data_gen[rand_num]
img = img[0]
label_name = class_names[np.argmax(label)]
pred_name = class_names[y_pred_class[rand_num]]
plt.title('true label: {}, predicted label: {}'.format(label_name, pred_name))
plt.imshow(img)
plt.show()
原文地址: https://www.cveoy.top/t/topic/kSG 著作权归作者所有。请勿转载和采集!