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 tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import vgg16
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics 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 = np.zeros(shape=(sample_count, 7, 7, 512)) # This shape is specific to VGG16
labels = np.zeros(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
https://colab.research.google.com/drive/1MNTv5uyY6snHRluB7Vbc-tiJc5DqWQL0?authuser=0#scrollTo=arpKjXEgUhO-&printMode=true 1/2
17/03/2024, 22:20 BAI1120_Assessment.ipynb - Colaboratory
# Flatten the features to fit into the SVM classifier
train_features = np.reshape(train_features, (train_features.shape[0], 7 * 7 * 512))
validation_features = np.reshape(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 = le.transform(validation_labels)
# Train an SVM classifier on the training data
svm_classifier = svm.SVC(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
https://colab.research.google.com/drive/1MNTv5uyY6snHRluB7Vbc-tiJc5DqWQL0?authuser=0#scrollTo=arpKjXEgUhO-&printMode=true 2/2