library(imager) library(dplyr) library(caret) library(nnet) library(ggplot2) library(lattice)
ase_path <- “C:\Users\ASUS\Desktop\Car_Dataset” folders <- c(“Genesis”, “hudson”, “lincoln”, “MG”, “Oldmobile”, “Studebaker”)
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 }
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”)
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
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)
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(123)
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
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 } }))
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)
train_data\(label <- as.factor(train_data\)label)
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]
if (length(zero_variance_column_names) > 0) { cat(“Zero variance columns found:”, paste(zero_variance_column_names, collapse = “,”), “”)
# Remove zero variance columns train_data <- train_data[, !(names(train_data) %in% zero_variance_column_names)] } else { cat(“No zero variance columns found.”) }
str(train_data)