MCPcopy
hub / github.com/tensorflow/tfjs-examples / generateAndVisualizeImages

Function generateAndVisualizeImages

mnist-acgan/index.js:84–110  ·  view source on GitHub ↗

* Generate a set of examples using the generator model of the ACGAN. * * @param {tf.Model} generator The generator part of the ACGAN.

(generator)

Source from the content-addressed store, hash-verified

82 * @param {tf.Model} generator The generator part of the ACGAN.
83 */
84async function generateAndVisualizeImages(generator) {
85 tf.util.assert(
86 generator.inputs.length === 2,
87 `Expected model to have exactly 2 symbolic inputs, ` +
88 `but there are ${generator.inputs.length}`);
89
90 const combinedFakes = tf.tidy(() => {
91 const latentVectors = getLatentVectors(10);
92
93 // Generate one fake image for each digit.
94 const sampledLabels = tf.tensor2d([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 1]);
95 // The output has pixel values in the [-1, 1] interval. Normalize it
96 // to the unit interval ([0, 1]).
97 const t0 = tf.util.now();
98 const generatedImages =
99 generator.predict([latentVectors, sampledLabels]).add(1).div(2);
100 generatedImages.dataSync(); // For accurate timing benchmark.
101 const elapsed = tf.util.now() - t0;
102 fakeImagesSpan.textContent =
103 `Fake images (generation took ${elapsed.toFixed(2)} ms)`;
104 // Concatenate the images horizontally into a single image.
105 return tf.concat(tf.unstack(generatedImages), 1);
106 });
107
108 await tf.browser.toPixels(combinedFakes, fakeCanvas);
109 tf.dispose(combinedFakes);
110}
111
112/** Refresh examples of real MNIST images. */
113async function drawReals() {

Callers 3

createSlidersFunction · 0.85
showGeneratorInitiallyFunction · 0.85
initFunction · 0.85

Calls 2

getLatentVectorsFunction · 0.85
predictMethod · 0.45

Tested by

no test coverage detected