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.