'Error in path.expand(path) : invalid 'path' argument in R

I wrote a function to convert image to array in Rscript. When I perform test_x <- image_prep(val_data$file_name) I have an error saying: Error in path.expand(path) : invalid 'path' argument in R.

Here the function:

val_data <- data.frame(file_name = paste0("/run/media/invasive_60000/", val_image_array_gen$filenames)) %>% 
  mutate(class = str_extract(file_name, "class0|class1"))
# Function to convert image to array
image_prep <- function(x) {
  arrays <- lapply(x, function(path) {
    img <- image_load(path, target_size = target_size, 
                      grayscale = F # Set FALSE if image is RGB
    )
    
    x <- image_to_array(img)
    x <- array_reshape(x, c(1, dim(x)))
    x <- x/255 # rescale image pixel
  })
  do.call(abind::abind, c(arrays, list(along = 1)))
}

I did a verification if the directory exists: dir.exists("/run/media/invasive_60000/") and it returns TRUE

Values of val_image_array_gen are coming from:

# Validation Dataset
val_image_array_gen <- flow_images_from_directory(directory = "/run/media/invasive_60000/",
                                                  target_size = target_size, 
                                                  color_mode = "rgb", 
                                                  batch_size = batch_size ,
                                                  seed = 123,
                                                  subset = "validation", # declare that this is the validation data
                                                  generator = train_data_gen
                                                  )


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source