Я пытаюсь перекодировать сверточную сеть с помощью пакета Magick R, но когда я выполнил сценарий trainData <- extract_feature("/run/media/rico/078622bc-841f-48e6-8bf5-0058f14a09fa/dogvscat/dogs-vs-cats/train/", width, height)
, он останавливается с ошибкой, которую я не могу объяснить:
Error in names(feature_matrix) <- paste0("pixel", c(1:img_size)) :
'names' attribute [2500] must be the same length as the vector [0]
Вот код:
rm(list=ls())
library(keras)
library(stringr)
library(pbapply)
library(magick)
image_dir <- "/run/media/rico/078622bc-841f-48e6-8bf5-0058f14a09fa/dogvscat/dogs-vs-cats/train/"
# Set image size
width <- 50
height <- 50
extract_feature <- function(dir_path, width, height, labelsExist = T) {
img_size <- width * height
## List images in path
images_names <- list.files(dir_path)
if(labelsExist){
## Select only cats or dogs images
catdog <- str_extract(images_names, "^(cat|dog)")
# Set cat == 0 and dog == 1
key <- c("cat" = 0, "dog" = 1)
y <- key[catdog]
}
print(paste("Start processing", length(images_names), "images"))
## This function will resize an image, turn it into greyscale
feature_list <- pblapply(images_names, function(imgname) {
## Read image
img <- image_read(file.path(dir_path, imgname))
## Resize image - use Magick
img_resized <-image_resize(img, "50x50")
## Set to grayscale (normalized to max)
grayimg <- image_data(img_resized, channels ="gray",frame=1)
## Get the image as a matrix
img_matrix <- grayimg[0]
## Coerce to a vector (row-wise)
img_vector <- as.vector(t(img_matrix))
return(img_vector)
})
## bind the list of vector into matrix
feature_matrix <- do.call(rbind, feature_list)
feature_matrix <- as.data.frame(feature_matrix)
## Set names
names(feature_matrix) <- paste0("pixel", c(1:img_size))
if(labelsExist){
return(list(X = feature_matrix, y = y))
}else{
return(feature_matrix)
}
}
trainData <- extract_feature("/run/media/rico/078622bc-841f-48e6-8bf5-0058f14a09fa/dogvscat/dogs-vs-cats/train/", width, height)
trainData достиг 100%, а затем ошибка, описанная выше. Есть ли у вас какие-либо идеи ? Спасибо.