R Markdown

Load required libraries

library(imager) library(dplyr) library(caret) library(nnet) library(ggplot2) library(lattice)

Define paths to each folder containing car images

ase_path <- “C:\Users\ASUS\Desktop\Car_Dataset” folders <- c(“Genesis”, “hudson”, “lincoln”, “MG”, “Oldmobile”, “Studebaker”)

Function to load images from a specified folder

load_images <- function(folder) { folder_path <- file.path(ase_path, folder) # Construct full path to the folder images <- list.files(folder_path, pattern = “\.jpg\(|\\.png\)”, full.names = TRUE) # List all images

# Standard dimensions for resizing standard_width <- 64 standard_height <- 64

image_list <- lapply(images, function(img_path) { img <- load.image(img_path) # Load the image

# Convert to grayscale if the image has 3 channels (RGB)
if (spectrum(img) == 3) {
  img <- grayscale(img)
}

# Resize the image to standard dimensions (64x64)
img <- resize(img, standard_width, standard_height)

# Flatten the image into a single vector for further processing
as.vector(img)

})

# Combine image vectors into a matrix and add corresponding labels image_data <- do.call(rbind, image_list) labels <- rep(folder, length(images)) # Create a label for each image return(data.frame(image_data, label = labels, stringsAsFactors = FALSE)) # Return as data frame }

Load images from all specified car folders

Genesis_data <- load_images(“Genesis”) hudson_data <- load_images(“hudson”) lincoln_data <- load_images(“lincoln”) MG_data <- load_images(“MG”) Oldmobile_data <- load_images(“Oldsmobile”) Studebaker_data <- load_images(“Studebaker”)

Preprocess the data to convert it to numeric and scale it

convert_to_numeric <- function(data) { data_numeric <- as.data.frame(lapply(data[, 1:4097], function(x) suppressWarnings(as.numeric(x)))) / 255 data_numeric <- data_numeric[, 1:4096] # Ensure only 4096 features return(data_numeric) } Genesis_data_numeric <- as.data.frame(lapply(Genesis_data[, 1:4097], function(x) suppressWarnings(as.numeric(x)))) / 255 hudson_data_numeric <- as.data.frame(lapply(hudson_data[, 1:4097], function(x) suppressWarnings(as.numeric(x)))) / 255 lincoln_data_numeric <- as.data.frame(lapply(lincoln_data[, 1:4097], function(x) suppressWarnings(as.numeric(x)))) / 255 MG_data_numeric <- as.data.frame(lapply(MG_data[, 1:4097], function(x) suppressWarnings(as.numeric(x)))) / 255 Oldmobile_data_numeric <- as.data.frame(lapply(Oldmobile_data[, 1:4097], function(x) suppressWarnings(as.numeric(x)))) / 255 Studebaker_data_numeric <-as.data.frame(lapply(Studebaker_data[, 1:4097], function(x) suppressWarnings(as.numeric(x)))) / 255

Rename columns for consistency across datasets

colnames(Genesis_data_numeric) <- paste0(“X”, 1:4097) colnames(hudson_data_numeric) <- paste0(“X”, 1:4097) colnames(lincoln_data_numeric) <- paste0(“X”, 1:4097) colnames(MG_data_numeric) <- paste0(“X”, 1:4097) colnames(Oldmobile_data_numeric) <- paste0(“X”, 1:4097) colnames(Studebaker_data_numeric) <- paste0(“X”, 1:4097)

Combine all datasets into one data frame and add labels

combined_data <- rbind( Genesis_data_numeric, hudson_data_numeric, lincoln_data_numeric, MG_data_numeric, Oldmobile_data_numeric, Studebaker_data_numeric )

combined_data$label <- factor(c(rep(“Genesis”, nrow(Genesis_data_numeric)), rep(“hudson”, nrow(hudson_data_numeric)), rep(“lincoln”, nrow(lincoln_data_numeric)), rep(“MG”, nrow(MG_data_numeric)), rep(“Oldmobile”, nrow(Oldmobile_data_numeric)), rep(“Studebaker”, nrow(Studebaker_data_numeric))))

Set seed for reproducibility of the random sample

set.seed(123)

Split the combined data into training and test sets (70% training, 30% testing)

train_indices <- sample(1:nrow(combined_data), size = 0.7 * nrow(combined_data)) train_data <- combined_data[train_indices, ] # Training data test_data <- combined_data[-train_indices, ] # Test data

Convert any character columns in training data to factors

train_data <- as.data.frame(lapply(train_data, function(x) { if (is.character(x)) { return(factor(x)) # Convert to factor if x is character } else { return(x) # Keep the column unchanged if not character } }))

Ensure numeric predictors are in the correct format (excluding label)

train_data[, -which(names(train_data) == “label”)] <- lapply(train_data[, -which(names(train_data) == “label”)], as.numeric) train_data\(label <- as.factor(train_data\)label)

Ensure the label is a factor

train_data\(label <- as.factor(train_data\)label)

Perform Principal Component Analysis (PCA) on training data

Check for zero variance columns

zero_variance_cols <- sapply(train_data[, -which(names(train_data) == “label”)], function(x) var(x) == 0) zero_variance_column_names <- names(train_data)[-which(names(train_data) == “label”)][zero_variance_cols]

Check the structure after removal

str(train_data)