-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathres_system.ets
More file actions
610 lines (561 loc) · 24 KB
/
res_system.ets
File metadata and controls
610 lines (561 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as tf from '@ohos/tfjs';
import fileio from '@ohos.fileio';
import '../flags';
import {env} from '../environment';
import {Buffer} from 'buffer';
import {assert} from '../util';
import {arrayBufferToBase64String, base64StringToArrayBuffer, getModelArtifactsForJSON,
getModelArtifactsInfoForJSON, concatenateArrayBuffers, typedArrayToBuffer} from './io_utils';
import {IORouter, IORouterRegistry} from './router_registry';
import {IOHandler, ModelArtifacts, ModelJSON, SaveResult, WeightsManifestConfig, WeightsManifestEntry} from './types';
// import * as fs from 'fs';
// import {dirname, join, resolve} from 'path';
// import {promisify} from '@ohos/util';
/*import promisify from '@ohos.util';
const stat = promisify(fs.stat);
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const mkdir = promisify(fs.mkdir);*/
function doesNotExistHandler(name: string): (e: Error) =>
never {
return e => {
throw new Error(`${name} ${e.message} does not exist: loading failed`);
};
}
export class NodeFileSystem implements IOHandler {
static readonly URL_SCHEME = '/models';
protected readonly path: string|string[];
readonly MODEL_JSON_FILENAME = 'model.json';
readonly WEIGHTS_BINARY_FILENAME = 'weights.bin';
readonly MODEL_BINARY_FILENAME = 'tensorflowjs.pb';
/**
* Constructor of the NodeFileSystem IOHandler.
* @param path A single path or an Array of paths.
* For saving: expects a single path pointing to an existing or nonexistent
* directory. If the directory does not exist, it will be
* created.
* For loading:
* - If the model has JSON topology (e.g., `tf.Model`), a single path
* pointing to the JSON file (usually named `model.json`) is expected.
* The JSON file is expected to contain `modelTopology` and/or
* `weightsManifest`. If `weightManifest` exists, the values of the
* weights will be loaded from relative paths (relative to the directory
* of `model.json`) as contained in `weightManifest`.
* - If the model has binary (protocol buffer GraphDef) topology,
* an Array of two paths is expected: the first path should point to the
* .pb file and the second path should point to the weight manifest
* JSON file.
*/
constructor(path: string|string[]) {
if (Array.isArray(path)) {
tf.util.assert(
path.length === 2,
() => 'file paths must have a length of 2, ' +
`(actual length is ${path.length}).`);
this.path = path.map(p => p);
} else {
this.path = path;
}
}
async save(modelArtifacts: tf.io.ModelArtifacts): Promise<tf.io.SaveResult> {
if (Array.isArray(this.path)) {
throw new Error('Cannot perform saving to multiple paths.');
}
await this.createOrVerifyDirectory();
if (modelArtifacts.modelTopology instanceof ArrayBuffer) {
throw new Error(
'NodeFileSystem.save() does not support saving model topology ' +
'in binary format yet.');
// TODO(cais, nkreeger): Implement this. See
// https://github.com/tensorflow/tfjs/issues/343
} else {
// const weightsBinPath = join(this.path, this.WEIGHTS_BINARY_FILENAME);
const weightsBinPath = this.path + this.WEIGHTS_BINARY_FILENAME;
const weightsManifest = [{
paths: [this.WEIGHTS_BINARY_FILENAME],
weights: modelArtifacts.weightSpecs
}];
const modelJSON: tf.io.ModelJSON = {
modelTopology: modelArtifacts.modelTopology,
weightsManifest,
format: modelArtifacts.format,
generatedBy: modelArtifacts.generatedBy,
convertedBy: modelArtifacts.convertedBy
};
if (modelArtifacts.trainingConfig != null) {
modelJSON.trainingConfig = modelArtifacts.trainingConfig;
}
if (modelArtifacts.signature != null) {
modelJSON.signature = modelArtifacts.signature;
}
if (modelArtifacts.userDefinedMetadata != null) {
modelJSON.userDefinedMetadata = modelArtifacts.userDefinedMetadata;
}
// const modelJSONPath = join(this.path, this.MODEL_JSON_FILENAME);
const modelJSONPath = this.path + this.MODEL_JSON_FILENAME;
let fd = fileio.openSync(modelJSONPath, 0o100 | 0o2, 0o666);
let num = await fileio.write(fd, JSON.stringify(modelJSON), 'utf8');
//await writeFile(modelJSONPath, JSON.stringify(modelJSON), 'utf8');
let fd1 = fileio.openSync(weightsBinPath, 0o100 | 0o2, 0o666);
let num1 = await fileio.write(fd1, Buffer.from(modelArtifacts.weightData), 'utf8');
/*await writeFile(
weightsBinPath, Buffer.from(modelArtifacts.weightData), 'binary');*/
return {
// TODO(cais): Use explicit tf.io.ModelArtifactsInfo type below once it
// is available.
// tslint:disable-next-line:no-any
modelArtifactsInfo: getModelArtifactsInfoForJSON(modelArtifacts) as any
};
}
}
async load(): Promise<tf.io.ModelArtifacts> {
return Array.isArray(this.path) ? this.loadBinaryModel() :
this.loadJSONModel();
}
protected async loadBinaryModel(): Promise<tf.io.ModelArtifacts> {
const topologyPath = this.path[0];
const weightManifestPath = this.path[1];
const topology =
await fileio.stat(topologyPath); //.catch(doesNotExistHandler('Topology Path'));
const weightManifest =
await fileio.stat(weightManifestPath)
.catch(doesNotExistHandler('Weight Manifest Path'));
// `this.path` can be either a directory or a file. If it is a file, assume
// it is model.json file.
if (!topology.isFile()) {
throw new Error('File specified for topology is not a file!');
}
if (!weightManifest.isFile()) {
throw new Error('File specified for the weight manifest is not a file!');
}
let fd = fileio.openSync(this.path[0], 0o2);
let buf = new ArrayBuffer(4096);
let res = await fileio.read(fd, buf);
const modelTopology = buf;
// const modelTopology = await readFile(this.path[0]);
let fd1 = fileio.openSync(this.path[1], 0o2);
let buf1 = new ArrayBuffer(4096);
let res1 = await fileio.read(fd1, buf1);
const weightsManifest = JSON.parse(arrayBufferToBase64String(buf1));
// const weightsManifest = JSON.parse(await readFile(this.path[1], 'utf8'));
const modelArtifacts: tf.io.ModelArtifacts = {
modelTopology,
};
const [weightSpecs, weightData] =
await this.loadWeights(weightsManifest, this.path[1]);
modelArtifacts.weightSpecs = weightSpecs;
modelArtifacts.weightData = weightData;
return modelArtifacts;
}
protected async loadJSONModel(): Promise<ModelArtifacts> {
console.info("res loadJSONModel " + this.path);
const path = this.path as string;
// let myData = await import("./common" + this.path + "_model.json");
let myData;
if(path==('/spam')){
myData = await import('@ohos/tfjs-models/models/spam/model.json');
}
else if (path==('/iris')) {
myData = await import('@ohos/tfjs-models/models/iris/model.json');
}
else if (path==('/abalone')){
myData = await import('@ohos/tfjs-models/models/abalone/model.json');
}
else if(path==('/mnist')){
myData = await import('@ohos/tfjs-models/models/mnist/model.json');
}
else if (path==('/segmentation')){
myData = await import('@ohos/tfjs-models/models/segmentation/model.json');
}
else if (path==('/handpose')){
myData = await import('@ohos/tfjs-models/models/handpose/model.json');
}
else if(path==('/fashion')){
myData = await import('@ohos/tfjs-models/models/fashion/model.json');
}
console.info("myData " + myData);
let data = JSON.parse((JSON.stringify(myData)));
console.info(data.format);
const modelTopology = data.modelTopology;
if (modelTopology == null) {
console.info("myData modelTopology null ");
return;
}
const weightsManifest = data.weightsManifest;
if (weightsManifest == null) {
console.info("myData weightsManifest null ");
return;
}
return getModelArtifactsForJSON(
data,
(weightsManifest) => this.loadWeights(weightsManifest, ""));
}
protected async loadJSONModell(): Promise<ModelArtifacts> {
console.info("res loadJSONModel " + this.path);
// const path = "./common" + this.path + "_model.json"as string;
return new Promise((resolve, reject) => {
const path = this.path as string;
if(path==('/spam')){
import('@ohos/tfjs-models/models/spam/model.json').then(({default: myData}) => {
console.info("myData " + myData);
Promise.resolve(JSON.parse((JSON.stringify(myData))))
.then(data => {
/*console.info(data);
console.info(data.modelTopology);*/
console.info(data.format);
const modelTopology = data.modelTopology;
if (modelTopology == null) {
console.info("myData modelTopology null ");
reject(new Error(`modelTopology field is missing from file `));
return;
}
const weightsManifest = data.weightsManifest;
if (weightsManifest == null) {
console.info("myData weightsManifest null ");
reject(new Error(`weightManifest field is missing from file `));
return;
}
/*const modelArtifactsPromise = getModelArtifactsForJSON(
data, (weightsManifest) => this.loadWeights(weightsManifest));
resolve(modelArtifactsPromise);*/
return getModelArtifactsForJSON(
data,
(weightsManifest) => this.loadWeights(weightsManifest, ""));
})
});
}
else if (path==('/iris')) {
import('@ohos/tfjs-models/models/iris/model.json').then(({default: myData}) => {
console.info("myData " + myData);
Promise.resolve(JSON.parse((JSON.stringify(myData))))
.then(data => {
/*console.info(data);
console.info(data.modelTopology);*/
console.info(data.format);
const modelTopology = data.modelTopology;
if (modelTopology == null) {
console.info("myData modelTopology null ");
reject(new Error(`modelTopology field is missing from file `));
return;
}
const weightsManifest = data.weightsManifest;
if (weightsManifest == null) {
console.info("myData weightsManifest null ");
reject(new Error(`weightManifest field is missing from file `));
return;
}
/*const modelArtifactsPromise = getModelArtifactsForJSON(
data, (weightsManifest) => this.loadWeights(weightsManifest));
resolve(modelArtifactsPromise);*/
return getModelArtifactsForJSON(
data,
(weightsManifest) => this.loadWeights(weightsManifest, ""));
})
});
}
else if (path==('/abalone')){
import('@ohos/tfjs-models/models/abalone/model.json').then(({default: myData}) => {
console.info("myData " + myData);
Promise.resolve(JSON.parse((JSON.stringify(myData))))
.then(data => {
/*console.info(data);
console.info(data.modelTopology);*/
console.info(data.format);
const modelTopology = data.modelTopology;
if (modelTopology == null) {
console.info("myData modelTopology null ");
reject(new Error(`modelTopology field is missing from file `));
return;
}
const weightsManifest = data.weightsManifest;
if (weightsManifest == null) {
console.info("myData weightsManifest null ");
reject(new Error(`weightManifest field is missing from file `));
return;
}
/*const modelArtifactsPromise = getModelArtifactsForJSON(
data, (weightsManifest) => this.loadWeights(weightsManifest));
resolve(modelArtifactsPromise);*/
return getModelArtifactsForJSON(
data,
(weightsManifest) => this.loadWeights(weightsManifest, ""));
})
});
}
else if (path==('/segmentation')){
import('@ohos/tfjs-models/models/segmentation/model.json').then(({default: myData}) => {
console.info("myData " + myData);
Promise.resolve(JSON.parse((JSON.stringify(myData))))
.then(data => {
/*console.info(data);
console.info(data.modelTopology);*/
console.info(data.format);
const modelTopology = data.modelTopology;
if (modelTopology == null) {
console.info("myData modelTopology null ");
reject(new Error(`modelTopology field is missing from file `));
return;
}
const weightsManifest = data.weightsManifest;
if (weightsManifest == null) {
console.info("myData weightsManifest null ");
reject(new Error(`weightManifest field is missing from file `));
return;
}
/*const modelArtifactsPromise = getModelArtifactsForJSON(
data, (weightsManifest) => this.loadWeights(weightsManifest));
resolve(modelArtifactsPromise);*/
return getModelArtifactsForJSON(
data,
(weightsManifest) => this.loadWeights(weightsManifest, ""));
})
});
}
else if (path==('/handpose')) {
import('@ohos/tfjs-models/models/handpose/model.json').then(({default: myData}) => {
console.info("myData " + myData);
Promise.resolve(JSON.parse((JSON.stringify(myData))))
.then(data => {
/*console.info(data);
console.info(data.modelTopology);*/
console.info(data.format);
const modelTopology = data.modelTopology;
if (modelTopology == null) {
console.info("myData modelTopology null ");
reject(new Error(`modelTopology field is missing from file `));
return;
}
const weightsManifest = data.weightsManifest;
if (weightsManifest == null) {
console.info("myData weightsManifest null ");
reject(new Error(`weightManifest field is missing from file `));
return;
}
/*const modelArtifactsPromise = getModelArtifactsForJSON(
data, (weightsManifest) => this.loadWeights(weightsManifest));
resolve(modelArtifactsPromise);*/
return getModelArtifactsForJSON(
data,
(weightsManifest) => this.loadWeights(weightsManifest, ""));
})
});
}
else if(path ==('/fashion')){
import('@ohos/tfjs-models/models/fashion/model.json').then(({default: myData}) => {
console.info("myData " + myData);
Promise.resolve(JSON.parse((JSON.stringify(myData))))
.then(data => {
/*console.info(data);
console.info(data.modelTopology);*/
console.info(data.format);
const modelTopology = data.modelTopology;
if (modelTopology == null) {
console.info("myData modelTopology null ");
reject(new Error(`modelTopology field is missing from file `));
return;
}
const weightsManifest = data.weightsManifest;
if (weightsManifest == null) {
console.info("myData weightsManifest null ");
reject(new Error(`weightManifest field is missing from file `));
return;
}
/*const modelArtifactsPromise = getModelArtifactsForJSON(
data, (weightsManifest) => this.loadWeights(weightsManifest));
resolve(modelArtifactsPromise);*/
return getModelArtifactsForJSON(
data,
(weightsManifest) => this.loadWeights(weightsManifest, ""));
})
});
}
});
}
private async loadWeights(
weightsManifest: WeightsManifestConfig,
pathoffile: string): Promise<[WeightsManifestEntry[], ArrayBuffer]> {
console.info("loadWeights");
const weightSpecs: WeightsManifestEntry[] = [];
const paths: string[] = [];
const buffers: ArrayBuffer[] = [];
for (const entry of weightsManifest) {
console.info("loadWeights for loop");
weightSpecs.push(...entry.weights);
paths.push(...entry.paths);
}
/*for (const group of weightsManifest) {
for (const path of group.paths) {
console.info("loadWeights for loop path " + path);
const weightFilePath = "./../common/weights.bin";
let fd = fileio.openSync('./../common/weights.bin', 0o2);
let buf = new ArrayBuffer(4096);
fileio.readSync(fd, buf);
buffers.push(buf);
}
}*/
console.info("Perform JSON to array buffer");
/*import('./../common/weights.json').then(({default: myData}) => { // JSON to ArrayBuffer
console.info("JSON to array buffer " + myData);
const arrBuffer = new Uint8Array(JSON.parse(JSON.stringify(myData))).buffer
console.info("loadWeights arrBuffer " + arrBuffer);
const arrBuffer1 = typedArrayToBuffer(new Uint8Array(JSON.parse(JSON.stringify(myData))))
console.info("loadWeights arrBuffer1 " + arrBuffer1);
// JSON.parse(String.fromCharCode.apply(null, new Uint8Array(myData)));
console.info("arrBuffer return");
return [weightSpecs, arrBuffer];
// return [weightSpecs, concatenateArrayBuffers(buffers)];
});*/
// let jsonOfWeights = await import('./../common/weights.json');
let weightsPath = String('@ohos/tfjs-models/models'+String(this.path)+'/weights.json');
const path = this.path as string;
let jsonOfWeights;
// let jsonOfWeights = await import(weightsPath);
if(path==('/spam')){
jsonOfWeights = await import('@ohos/tfjs-models/models/spam/weights.json');
}
else if (path==('/iris')) {
jsonOfWeights = await import('@ohos/tfjs-models/models/iris/weights.json');
}
else if (path==('/abalone')){
jsonOfWeights = await import('@ohos/tfjs-models/models/abalone/weights.json');
}
else if(path==('/mnist')){
jsonOfWeights = await import('@ohos/tfjs-models/models/mnist/weights.json');
}
else if (path==('/segmentation')){
jsonOfWeights = await import('@ohos/tfjs-models/models/segmentation/weights.json');
}
else if (path==('/handpose')){
jsonOfWeights = await import('@ohos/tfjs-models/models/handpose/weights.json');
}
else if(path==('/fashion')){
jsonOfWeights = await import('@ohos/tfjs-models/models/fashion/weights.json');
}
let jsonObj = JSON.parse(JSON.stringify(jsonOfWeights))
let jsonArray = []
for (var item in jsonObj) {
jsonArray.push(jsonObj[item]);
}
console.info("loadWeights jsonArray count " + jsonArray.length);
// const arrBuffer = new Uint8Array(JSON.parse(JSON.stringify(jsonOfWeights))).buffer
// the json which we converted used Float32Array to convert the binary file,
// So to get back the buffer we should use floar32array
const arrBuffer = new Float32Array(jsonArray).buffer
console.info("loadWeights jsonOfWeights arrBuffer " + arrBuffer);
return [weightSpecs, arrBuffer];
// return [weightSpecs, concatenateArrayBuffers(buffers)];
}
private async loadWeightss(
weightsManifest: WeightsManifestConfig,
path: string): Promise<[WeightsManifestEntry[], ArrayBuffer]> {
// const dirName = dirname(path);
const buffers: ArrayBuffer[] = [];
const weightSpecs: WeightsManifestEntry[] = [];
for (const group of weightsManifest) {
for (const path of group.paths) {
/*const weightFilePath = join(dirName, path);
const buffer = await readFile(weightFilePath)
.catch(doesNotExistHandler('Weight file'));*/
const weightFilePath = "/common/weights.bin";
let fd = fileio.openSync(weightFilePath, 0o2);
let buf = new ArrayBuffer(4096);
await fileio.read(fd, buf)
.catch(doesNotExistHandler('Weight file'));
buffers.push(buf);
}
weightSpecs.push(...group.weights);
}
// return [weightSpecs, base64StringToArrayBuffer(buffers)];
return [weightSpecs, concatenateArrayBuffers(buffers)];
}
/**
* For each item in `this.path`, creates a directory at the path or verify
* that the path exists as a directory.
*/
protected async createOrVerifyDirectory() {
const paths = Array.isArray(this.path) ? this.path : [this.path];
for (const path of paths) {
try {
await fileio.mkdir(path);
} catch (e) {
if (e.code === 'EEXIST') {
if ((await fileio.stat(path)).isFile()) {
throw new Error(
`Path ${path} exists as a file. The path must be ` +
`nonexistent or point to a directory.`);
}
// else continue, the directory exists
} else {
throw e;
}
}
}
}
}
export const nodeFileSystemRouter = (url: string|string[]) => {
if (Array.isArray(url)) {
if (url.every(
urlElement => urlElement.startsWith(NodeFileSystem.URL_SCHEME))) {
return new NodeFileSystem(url.map(
urlElement => urlElement.slice(NodeFileSystem.URL_SCHEME.length)));
} else {
return null;
}
} else {
if (url.startsWith(NodeFileSystem.URL_SCHEME)) {
return new NodeFileSystem(url.slice(NodeFileSystem.URL_SCHEME.length));
} else {
return null;
}
}
};
// Registration of `nodeFileSystemRouter` is done in index.ts.
/**
* Factory function for Node.js native file system IO Handler.
*
* @param path A single path or an Array of paths.
* For saving: expects a single path pointing to an existing or nonexistent
* directory. If the directory does not exist, it will be
* created.
* For loading:
* - If the model has JSON topology (e.g., `tf.Model`), a single path
* pointing to the JSON file (usually named `model.json`) is expected.
* The JSON file is expected to contain `modelTopology` and/or
* `weightsManifest`. If `weightManifest` exists, the values of the
* weights will be loaded from relative paths (relative to the directory
* of `model.json`) as contained in `weightManifest`.
* - If the model has binary (protocol buffer GraphDef) topology,
* an Array of two paths is expected: the first path should point to the
* .pb file and the second path should point to the weight manifest
* JSON file.
*/
export function fileSystem(path: string|string[]): NodeFileSystem {
return new NodeFileSystem(path);
}
export const resSystemRouter: IORouter = (path: string|string[]) => {
if (!Array.isArray(path) && path.startsWith(NodeFileSystem.URL_SCHEME)) {
console.info("resSystemRouter " + path.slice(NodeFileSystem.URL_SCHEME.length));
return fileSystem(
path.slice(NodeFileSystem.URL_SCHEME.length));
} else {
console.info("resSystemRouter returning null");
return null;
}
};
IORouterRegistry.registerLoadRouter(resSystemRouter);