* Save model artifacts to AsyncStorage * * @param modelArtifacts The model artifacts to be stored. * @returns An instance of SaveResult.
(modelArtifacts: io.ModelArtifacts)
| 86 | * @returns An instance of SaveResult. |
| 87 | */ |
| 88 | async save(modelArtifacts: io.ModelArtifacts): Promise<io.SaveResult> { |
| 89 | if (modelArtifacts.modelTopology instanceof ArrayBuffer) { |
| 90 | throw new Error( |
| 91 | 'AsyncStorageHandler.save() does not support saving model topology ' + |
| 92 | 'in binary format.'); |
| 93 | } else { |
| 94 | // We save three items separately for each model, |
| 95 | // a ModelArtifactsInfo, a ModelArtifacts without weights |
| 96 | // and the model weights. |
| 97 | const modelArtifactsInfo: io.ModelArtifactsInfo = |
| 98 | getModelArtifactsInfoForJSON(modelArtifacts); |
| 99 | const {weightData, ...modelArtifactsWithoutWeights} = modelArtifacts; |
| 100 | |
| 101 | try { |
| 102 | this.asyncStorage.setItem( |
| 103 | this.keys.info, JSON.stringify(modelArtifactsInfo)); |
| 104 | this.asyncStorage.setItem( |
| 105 | this.keys.modelArtifactsWithoutWeights, |
| 106 | JSON.stringify(modelArtifactsWithoutWeights)); |
| 107 | this.asyncStorage.setItem( |
| 108 | this.keys.weightData, fromByteArray(new Uint8Array(weightData))); |
| 109 | return {modelArtifactsInfo}; |
| 110 | } catch (err) { |
| 111 | // If saving failed, clean up all items saved so far. |
| 112 | this.asyncStorage.removeItem(this.keys.info); |
| 113 | this.asyncStorage.removeItem(this.keys.weightData); |
| 114 | this.asyncStorage.removeItem(this.keys.modelArtifactsWithoutWeights); |
| 115 | |
| 116 | throw new Error( |
| 117 | `Failed to save model '${this.modelPath}' to AsyncStorage. |
| 118 | Error info ${err}`); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Load a model from local storage. |
no test coverage detected