Skip to content

Commit 7dddaa1

Browse files
Added generateTrainingData() function
1 parent f90030f commit 7dddaa1

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

script.js

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -123,47 +123,51 @@ for (var i = 0; i < numTrainingImages; i ++) {
123123

124124
// Wait for last image (testing data) to load before continuing
125125
trainingData.images[trainingData.images.length - 1].onload = function () {
126-
// Create training data from pixels of image elements
127-
// Create a new variable to store the data
128-
var pixels;
129-
var pixelsArray;
130-
var outputValues;
131-
// Loop through each training image
132-
133-
for (var i = 0; i < numTrainingImages; i ++) {
134-
// Create a tensor with 3 (RGB) color channels from the image element
135-
pixels = tf.fromPixels(trainingData.images[i], numParameters);
136-
// Resize image to the specified dimensions with resizeBilinear()
137-
pixels = tf.image.resizeBilinear(pixels, [imageSize, imageSize]);
138-
// Get the values array from the pixels tensor
139-
pixels = pixels.dataSync();
140-
// Add new array to trainingData.pixels.input to store the pixel values for the image
141-
pixelsArray = [];
142-
// Loop through each value in the pixels array
143-
// The whole pixels array is not pushed on at once because the array format will be incompatible
144-
pixels.forEach(
145-
// Add color value to the corresponding image's trainingData.pixels.input array
146-
(element) => pixelsArray.push(element)
147-
);
148-
trainingData.pixels.input.push(pixelsArray);
149-
outputValues = new Array(numParameters).fill(1);
150-
trainingData.pixels.output.push(outputValues);
151-
152-
// Uncaught Error: Constructing tensor of shape (92160) should match the length of values (46095)
153-
const generated = generator.model.predict(parameters.display).dataSync();
154-
const generatedArray = [];
155-
generated.forEach(
156-
(element) => generatedArray.push(element)
157-
);
158-
trainingData.pixels.input.push(generatedArray);
126+
function generateTrainingData() {
127+
// Create training data from pixels of image elements
128+
// Create a new variable to store the data
129+
var pixels;
130+
var pixelsArray;
131+
var outputValues;
132+
// Loop through each training image
133+
134+
for (var i = 0; i < numTrainingImages; i ++) {
135+
// Create a tensor with 3 (RGB) color channels from the image element
136+
pixels = tf.fromPixels(trainingData.images[i], numParameters);
137+
// Resize image to the specified dimensions with resizeBilinear()
138+
pixels = tf.image.resizeBilinear(pixels, [imageSize, imageSize]);
139+
// Get the values array from the pixels tensor
140+
pixels = pixels.dataSync();
141+
// Add new array to trainingData.pixels.input to store the pixel values for the image
142+
pixelsArray = [];
143+
// Loop through each value in the pixels array
144+
// The whole pixels array is not pushed on at once because the array format will be incompatible
145+
pixels.forEach(
146+
// Add color value to the corresponding image's trainingData.pixels.input array
147+
(element) => pixelsArray.push(element)
148+
);
149+
trainingData.pixels.input.push(pixelsArray);
150+
outputValues = new Array(numParameters).fill(1);
151+
trainingData.pixels.output.push(outputValues);
152+
153+
// Uncaught Error: Constructing tensor of shape (92160) should match the length of values (46095)
154+
const generated = generator.model.predict(parameters.display).dataSync();
155+
const generatedArray = [];
156+
generated.forEach(
157+
(element) => generatedArray.push(element)
158+
);
159+
trainingData.pixels.input.push(generatedArray);
160+
161+
outputValues = new Array(numParameters).fill(-1);
162+
trainingData.pixels.output.push(outputValues);
163+
}
159164

160-
outputValues = new Array(numParameters).fill(-1);
161-
trainingData.pixels.output.push(outputValues);
165+
// Create a tensor from the pixel values of the training data and store it in trainingData.tensor.input
166+
trainingData.tensor.input = tf.tensor(trainingData.pixels.input);
167+
trainingData.tensor.output = tf.tensor(trainingData.pixels.output);
162168
}
163169

164-
// Create a tensor from the pixel values of the training data and store it in trainingData.tensor.input
165-
trainingData.tensor.input = tf.tensor(trainingData.pixels.input);
166-
trainingData.tensor.output = tf.tensor(trainingData.pixels.output);
170+
generateTrainingData();
167171

168172
// trainingData.tensor.output = tf.ones([numTrainingImages, 6]);
169173
// trainingData.tensor.output.dtype = "float32";

0 commit comments

Comments
 (0)