0

I am trying to make a convolutional neural network and activate it using a single function which returns the data. I am having trouble with the asynchronous part because when I try to callback the data and log it, it returns an empty object.

const convolute = (callback) => {
let inputData = {};

  for(let p = 0; p < files.length; p++){
    let fileData = [];
    let pixels = [];
    let dimensions = [];
    let image, iterationArr;
    let FinalVector = [];
    fs.readFile("./imgs/" + files[p], "utf8", (err, data) => {
      if(err) console.log(err);

      fileData = data.split("\n");
      dimensions = fileData[0].split(" ").map(l => parseInt(l));
      pixels = fileData[1].split(" ").map(p => parseInt(p));
      pixels.splice(pixels[pixels.length-1], 1);
      pixels = MM.convertToMatrix(pixels, dimensions[0]);

      image = new ImageProcess(pixels);
      for(let i = 0; i < edges.length; i++){
        iterationArr = image.pixels;
        let iteration = 0
        while(iterationArr.length > 30){
          iteration++;
          iterationArr = image.start(iterationArr, edges[i]);
        }
        let iterationVector = MM.convertToVector(iterationArr)
        for(let i = 0; i < iterationVector.length; i++){
          FinalVector.push(iterationVector);
        }
      }//end of each file
      let fileName = files[p].substring(0, files[p].length -4);
      inputData[fileName] = FinalVector;
    });//end of reading file
  }//new file reading starts here

  callback(inputData)
}//takes roughly around 5 seconds to execute

convolute((data) => {
  console.log(data);
})

files is an array that stores all the file names. FinalVector is the array that contains 2000+ items. I've done some testing and the code works, I know this because it works when I log each files vector separately it returns the data, but there is trouble when I use a callback to return the data from the function.

PS. I've only included the relevant parts of my code that may contribute to my problem.

1 Answer 1

1

inputData is empty because readFile is asynchronous : callback(inputData) is called before the callbacks of each readFile where inputData is filled.

So callback(inputData) should be called after all the (asynchronous) readFile are processed : inside the callback of readFile once all the files are read :

const convolute = (callback) => {
    let inputData = {};
    let remainings = files.length;
    if (!remainings) {
        callback(inputData);
        return;
    }
    for (let p = 0; p < files.length; p++) {
        fs.readFile("./imgs/" + files[p], "utf8", (err, data) => {
            if (err) {
                console.log(err);
            }
            else {
                /* process data simplified */
                inputData[files[p]] = data;
            }

            if (--remainings <= 0) {
                callback(inputData);
            }
        });
    }
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.