(modelArtifacts: ModelArtifacts)
| 87 | } |
| 88 | |
| 89 | async save(modelArtifacts: ModelArtifacts): Promise<SaveResult> { |
| 90 | if (modelArtifacts.modelTopology instanceof ArrayBuffer) { |
| 91 | throw new Error( |
| 92 | 'BrowserHTTPRequest.save() does not support saving model topology ' + |
| 93 | 'in binary formats yet.'); |
| 94 | } |
| 95 | |
| 96 | const init = Object.assign({method: this.DEFAULT_METHOD}, this.requestInit); |
| 97 | init.body = new FormData(); |
| 98 | |
| 99 | const weightsManifest: WeightsManifestConfig = [{ |
| 100 | paths: ['./model.weights.bin'], |
| 101 | weights: modelArtifacts.weightSpecs, |
| 102 | }]; |
| 103 | const modelTopologyAndWeightManifest: ModelJSON = |
| 104 | getModelJSONForModelArtifacts(modelArtifacts, weightsManifest); |
| 105 | |
| 106 | init.body.append( |
| 107 | 'model.json', |
| 108 | new Blob( |
| 109 | [JSON.stringify(modelTopologyAndWeightManifest)], |
| 110 | {type: JSON_TYPE}), |
| 111 | 'model.json'); |
| 112 | |
| 113 | if (modelArtifacts.weightData != null) { |
| 114 | // TODO(mattsoulanille): Support saving models over 2GB that exceed |
| 115 | // Chrome's ArrayBuffer size limit. |
| 116 | const weightBuffer = CompositeArrayBuffer.join(modelArtifacts.weightData); |
| 117 | |
| 118 | init.body.append( |
| 119 | 'model.weights.bin', |
| 120 | new Blob([weightBuffer], {type: OCTET_STREAM_MIME_TYPE}), |
| 121 | 'model.weights.bin'); |
| 122 | } |
| 123 | |
| 124 | const response = await this.fetch(this.path, init); |
| 125 | |
| 126 | if (response.ok) { |
| 127 | return { |
| 128 | modelArtifactsInfo: getModelArtifactsInfoForJSON(modelArtifacts), |
| 129 | responses: [response], |
| 130 | }; |
| 131 | } else { |
| 132 | throw new Error( |
| 133 | `BrowserHTTPRequest.save() failed due to HTTP response status ` + |
| 134 | `${response.status}.`); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private async loadModelJSON(): Promise<ModelJSON> { |
| 139 | const modelConfigRequest = await this.fetch(this.path, this.requestInit); |
no test coverage detected