1
$\begingroup$

I am working to make my first trained model for image recognition, using the programming language R.

First I am attempting to make a function that takes a PNG-image as input, resizes it to 128x128 pixels, and converts it into a row-vector of numbers representing the colours in each pixel. I then want to add this to a file that contains my training-set and labels (the image bank).

I am having some trouble with this, being a beginner in R, as the numerical values I get out from the following code does not make sense to me - often I get a large amount of 1's, and the pictures appear to be completely white if I try to export and look at the image.

Is there any obvious mistake here? I assume it must be where I convert to numerical values. Alternatively, is there an alternative approach to this task that would be better?

load_png_to_image_bank <- function(wd, input_file, label) { 

  # Remembers old working directory 
  oldwd <- wd

  # Sets input wd to file location
  setwd(wd)

  # Loads imager package
  library(imager)

  # Imports file and resizes to 128x128 pixels
  image <- load.image(input_file)
  image <- resize(image,128,128)

  # Finds numerical values for each pixel
  rasterized_vector <- as.data.frame(as.vector(image))
  rasterized_vector <- as.data.frame(t(rasterized_vector))

  # Adds label defined by input variable
  rasterized_vector <- as.data.frame((cbind(1, rasterized_vector)))
  names(rasterized_vector)[1] <- label

  # Sets wd to predetermined place for image bank file
  setwd(" ... ")

  # Writes vector to file
  write.table(rasterized_vector, 
              output_file, 
              row.names=F, 
              na="NA",
              append = T, 
              quote= FALSE, 
              sep = ";", 
              col.names = F)

  # Resets working directory to initial state
  setwd(oldwd)

  }
$\endgroup$
  • $\begingroup$ Maybe it's better to submit this kind of questions to programming forums, such as Stack Overflow or R dedicated forum. It's more an R question rather than an AI question. $\endgroup$ – rursw1 Aug 27 '17 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.