MCPcopy Index your code
hub / github.com/tensorflow/tfjs-examples / init

Function init

mnist-acgan/index.js:162–249  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

160}
161
162async function init() {
163 // Load MNIST data for display in webpage.
164 status.textContent = 'Loading MNIST data...';
165 await loadMnistData();
166
167 const LOCAL_MEATADATA_PATH = 'generator/acgan-metadata.json';
168 const LOCAL_MODEL_PATH = 'generator/model.json';
169
170 // Hosted, pre-trained generator model.
171 const HOSTED_MODEL_URL =
172 'https://storage.googleapis.com/tfjs-examples/mnist-acgan/dist/generator/model.json';
173
174 // Attempt to load locally-saved model. If it fails, activate the
175 // "Load hosted model" button.
176 let model;
177 try {
178 status.textContent = 'Loading metadata';
179 const metadata =
180 await (await fetch(LOCAL_MEATADATA_PATH, {cache: 'no-cache'})).json();
181
182 status.textContent = `Loading model from ${LOCAL_MODEL_PATH}...`;
183 model = await tf.loadLayersModel(
184 tf.io.browserHTTPRequest(LOCAL_MODEL_PATH, {cache: 'no-cache'}));
185 await showGeneratorInitially(model);
186
187 if (metadata.completed) {
188 status.textContent =
189 `Training of ACGAN in Node.js (${metadata.totalEpochs} epochs) ` +
190 `is completed. `;
191 } else {
192 status.textContent = `Training of ACGAN in Node.js is ongoing (epoch ` +
193 `${metadata.currentEpoch + 1}/${metadata.totalEpochs})... `;
194 }
195 if (metadata.currentEpoch < 10) {
196 status.textContent +=
197 '(Note: generator results may be bad during the first few epochs ' +
198 'of training, but should get better as training progresses.) '
199 }
200 if (metadata.lastUpdated != null) {
201 status.textContent +=
202 ` (Saved model was last updated ` +
203 `${ta.ago(new Date(metadata.lastUpdated))}). `;
204 }
205 status.textContent +=
206 'Loaded locally-saved model! Now click "Generate" or ' +
207 'adjust the z-space sliders.';
208 } catch (err) {
209 console.error(err);
210 status.textContent =
211 'Failed to load locally-saved model and/or metadata. ' +
212 'Please click "Load Hosted Model"';
213 }
214
215 loadHostedModel.addEventListener('click', async () => {
216 try {
217 status.textContent = `Loading hosted model from ${HOSTED_MODEL_URL} ...`;
218 model = await tf.loadLayersModel(HOSTED_MODEL_URL);
219 loadHostedModel.disabled = true;

Callers 1

index.jsFile · 0.70

Calls 5

loadMnistDataFunction · 0.90
showGeneratorInitiallyFunction · 0.85
generateLatentVectorFunction · 0.85
drawRealsFunction · 0.85

Tested by

no test coverage detected