* Load a tfjs model. * * @return A promise of tfjs model.
()
| 43 | * @return A promise of tfjs model. |
| 44 | */ |
| 45 | async load(): Promise<tf.io.ModelArtifacts> { |
| 46 | const path = this.path; |
| 47 | |
| 48 | const fsInfo = fs.statSync(path); |
| 49 | |
| 50 | if (!fsInfo.isFile()) { |
| 51 | throw new Error( |
| 52 | 'The path to load from must be a file. Loading from a directory ' + |
| 53 | 'is not supported.'); |
| 54 | } else { |
| 55 | const file = fs.readFileSync(path, 'utf8'); |
| 56 | const modelJSON = JSON.parse(file); |
| 57 | |
| 58 | // Mapping modelJSON to modelArtifacts. |
| 59 | const modelArtifacts: tf.io.ModelArtifacts = { |
| 60 | modelTopology: modelJSON.modelTopology, |
| 61 | format: modelJSON.format, |
| 62 | generatedBy: modelJSON.generatedBy, |
| 63 | convertedBy: modelJSON.convertedBy |
| 64 | }; |
| 65 | |
| 66 | if (modelJSON.signature != null) { |
| 67 | modelArtifacts.signature = modelJSON.signature; |
| 68 | } |
| 69 | |
| 70 | if (modelJSON.userDefinedMetadata != null) { |
| 71 | modelArtifacts.userDefinedMetadata = modelJSON.userDefinedMetadata; |
| 72 | } |
| 73 | |
| 74 | if (modelJSON.modelInitializer != null) { |
| 75 | modelArtifacts.modelInitializer = modelJSON.modelInitializer; |
| 76 | } |
| 77 | |
| 78 | if (modelJSON.initializerSignature != null) { |
| 79 | modelArtifacts.initializerSignature = modelJSON.initializerSignature; |
| 80 | } |
| 81 | |
| 82 | if (modelJSON.weightsManifest != null) { |
| 83 | const [weightSpecs, weightData] = |
| 84 | this.loadWeights(modelJSON.weightsManifest, path); |
| 85 | modelArtifacts.weightSpecs = weightSpecs; |
| 86 | modelArtifacts.weightData = weightData; |
| 87 | } |
| 88 | if (modelJSON.trainingConfig != null) { |
| 89 | modelArtifacts.trainingConfig = modelJSON.trainingConfig; |
| 90 | } |
| 91 | |
| 92 | return modelArtifacts; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Load weights binary files from a local path. |