0% found this document useful (0 votes)
10 views2 pages

SVM Classifier for Rice Image Dataset

Uploaded by

rk800deviant3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

SVM Classifier for Rice Image Dataset

Uploaded by

rk800deviant3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

17/03/2024, 22:20 BAI1120_Assessment.

ipynb - Colaboratory

import os
import numpy as np
import tensorflow as tf
from sklearn import svm
from sklearn.model_selection import train_test_split
from [Link] import ImageDataGenerator
from [Link] import vgg16
from [Link] import LabelEncoder
from [Link] import classification_report, accuracy_score

# Define the path to the dataset


dataset_path = '/content/drive/MyDrive/DeppL/riceone/Sampled_Rice_Image_Dataset'

# Create an instance of ImageDataGenerator for data augmentation and to load images in batches
datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)

# Set the batch size


batch_size = 32

# Load images in batches from directory and apply data augmentation


train_batches = datagen.flow_from_directory(
dataset_path,
target_size=(224, 224),
batch_size=batch_size,
class_mode='sparse',
subset='training'
)

validation_batches = datagen.flow_from_directory(
dataset_path,
target_size=(224, 224),
batch_size=batch_size,
class_mode='sparse',
subset='validation'
)

Found 312 images belonging to 5 classes.


Found 78 images belonging to 5 classes.

# Load a pre-trained VGG16 model without the top classification layer


pretrained_model = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# Disable training on the pre-trained model


pretrained_model.trainable = False

# Function to extract features from batches of images


def extract_features(generator, sample_count):
features = [Link](shape=(sample_count, 7, 7, 512)) # This shape is specific to VGG16
labels = [Link](shape=(sample_count))
i = 0
for inputs_batch, labels_batch in generator:
features_batch = pretrained_model.predict(inputs_batch)
features[i * batch_size : (i + 1) * batch_size] = features_batch
labels[i * batch_size : (i + 1) * batch_size] = labels_batch
i += 1
if i * batch_size >= sample_count:
break
return features, labels

# Extract features from the training and validation sets


train_features, train_labels = extract_features(train_batches, train_batches.samples)
validation_features, validation_labels = extract_features(validation_batches, validation_batches.samples)

1/1 [==============================] - 21s 21s/step


1/1 [==============================] - 19s 19s/step
1/1 [==============================] - 20s 20s/step
1/1 [==============================] - 21s 21s/step
1/1 [==============================] - 21s 21s/step
1/1 [==============================] - 21s 21s/step
1/1 [==============================] - 22s 22s/step
1/1 [==============================] - 19s 19s/step
1/1 [==============================] - 20s 20s/step
1/1 [==============================] - 15s 15s/step
1/1 [==============================] - 19s 19s/step
1/1 [==============================] - 21s 21s/step
1/1 [==============================] - 8s 8s/step

[Link] 1/2
17/03/2024, 22:20 BAI1120_Assessment.ipynb - Colaboratory
# Flatten the features to fit into the SVM classifier
train_features = [Link](train_features, (train_features.shape[0], 7 * 7 * 512))
validation_features = [Link](validation_features, (validation_features.shape[0], 7 * 7 * 512))

# Encode labels to integers


le = LabelEncoder()
train_labels_encoded = le.fit_transform(train_labels)
validation_labels_encoded = [Link](validation_labels)

# Train an SVM classifier on the training data


svm_classifier = [Link](kernel='linear', C=1)
svm_classifier.fit(train_features, train_labels_encoded)

▾ SVC
SVC(C=1, kernel='linear')

# Predict and evaluate the SVM classifier on the validation set


predictions = svm_classifier.predict(validation_features)
print(classification_report(validation_labels_encoded, predictions))
print('Validation Accuracy:', accuracy_score(validation_labels_encoded, predictions))

precision recall f1-score support

0 1.00 1.00 1.00 26


1 1.00 1.00 1.00 26
2 1.00 1.00 1.00 26

accuracy 1.00 78
macro avg 1.00 1.00 1.00 78
weighted avg 1.00 1.00 1.00 78

Validation Accuracy: 1.0

[Link] 2/2

Common questions

Powered by AI

Data augmentation helps in creating variations of the existing images by applying transformations such as rescaling, which aids in improving the model's ability to generalize to previously unseen data. It increases the diversity of the dataset, thus reducing overfitting and enhancing model robustness, especially when training on limited dataset volumes .

Benefits include reduced training time and computational load, as the model has pre-learned features on a large dataset. It enhances performance by using sophisticated features already tuned for image recognition. Drawbacks might include limited adaptability to domain-specific nuances not present in the pre-trained dataset and potential overfitting if pretrained weights do not generalize well to new datasets .

Using a batch size of 32 is advantageous as it balances between efficient memory usage and speed of convergence. Smaller batches ensure adequate stochasticity in learning, preventing local minima traps, while allowing exploitation of vectorized operations, hastening training. It also fits conveniently into memory, facilitating smoother feature extraction and model updates .

Encoding labels to integers using LabelEncoder is essential because SVM and many other machine learning algorithms require numerical labels to function correctly. The encoding process translates categorical labels into a numerical format that can be processed by the algorithm, ensuring the correct application of the learning procedure .

The process involves several technical steps: 1) Importing necessary libraries and modules such as TensorFlow, sklearn, and VGG16 from Keras. 2) Defining the dataset path and using ImageDataGenerator for data augmentation and splitting into training and validation sets. 3) Loading a pre-trained VGG16 model, disabling training for transfer learning. 4) Extracting features using the pre-trained model by predicting on batches and storing them in a specified shape. 5) Flattening the feature arrays to a suitable shape for SVM input and encoding the labels. 6) Training an SVM classifier on the extracted features with a linear kernel. 7) Evaluating the model's performance using precision, recall, f1-score, and accuracy metrics on validation data .

The SVM classifier, particularly with a linear kernel, is chosen due to its effectiveness in smaller, feature-rich datasets, where it can achieve comparable if not superior results to complex neural networks without excessive computational burden. In this framework, the VGG16 model already extracts meaningful features reducing the need for a further complex model. Alternatives like deep learning models involve more intricate architectures and longer training times without necessarily improving accuracy for these feature spaces .

The SVM classifier achieves perfect validation accuracy (1.0), suggesting excellent class distinction in the dataset. This remarkable performance could be attributed to the effective feature extraction by the VGG16, which captures essential image patterns. Additionally, a linear SVM might be well-suited to the dataset's inherent structure, and the separability of classes might be particularly high, possibly due to the quality and diversity of the dataset or effective preprocessing such as augmentation .

Using a pre-trained VGG16 model enhances the image classification task by providing a robust feature extraction process. The model is already trained on a large dataset (Imagenet), which allows it to efficiently capture complex patterns and features present in the images without needing to train a new model from scratch. This transfer learning approach reduces the training time and computational resources required while maintaining high accuracy in feature extraction .

Challenges include potential mismatch in feature space when model image input specifications differ from the dataset, leading to suboptimal learning. It might also struggle with domain transfer if the image characteristics differ from those learned by VGG16. Mitigations include fine-tuning the model on a subset of the dataset specific to the task, preprocessing images to match input specifications, or using a more specialized model if domain differences are significant .

A linear kernel SVM is chosen likely because the feature space extracted by the VGG16 model is already highly informative and linearly separable due to the nature of the transfer learning approach. Linear SVMs are computationally cheaper and perform well when the datasets can naturally be distinguished in a linear manner, such as when features are detailed and distinct, as provided by the robust VGG16 feature extraction .

You might also like