()
| 136 | } |
| 137 | |
| 138 | private async loadModelJSON(): Promise<ModelJSON> { |
| 139 | const modelConfigRequest = await this.fetch(this.path, this.requestInit); |
| 140 | |
| 141 | if (!modelConfigRequest.ok) { |
| 142 | throw new Error( |
| 143 | `Request to ${this.path} failed with status code ` + |
| 144 | `${modelConfigRequest.status}. Please verify this URL points to ` + |
| 145 | `the model JSON of the model to load.`); |
| 146 | } |
| 147 | let modelJSON: ModelJSON; |
| 148 | try { |
| 149 | modelJSON = await modelConfigRequest.json(); |
| 150 | } catch (e) { |
| 151 | let message = `Failed to parse model JSON of response from ${this.path}.`; |
| 152 | // TODO(nsthorat): Remove this after some time when we're comfortable that |
| 153 | // .pb files are mostly gone. |
| 154 | if (this.path.endsWith('.pb')) { |
| 155 | message += ' Your path contains a .pb file extension. ' + |
| 156 | 'Support for .pb models have been removed in TensorFlow.js 1.0 ' + |
| 157 | 'in favor of .json models. You can re-convert your Python ' + |
| 158 | 'TensorFlow model using the TensorFlow.js 1.0 conversion scripts ' + |
| 159 | 'or you can convert your.pb models with the \'pb2json\'' + |
| 160 | 'NPM script in the tensorflow/tfjs-converter repository.'; |
| 161 | } else { |
| 162 | message += ' Please make sure the server is serving valid ' + |
| 163 | 'JSON for this request.'; |
| 164 | } |
| 165 | throw new Error(message); |
| 166 | } |
| 167 | |
| 168 | // We do not allow both modelTopology and weightsManifest to be missing. |
| 169 | const modelTopology = modelJSON.modelTopology; |
| 170 | const weightsManifest = modelJSON.weightsManifest; |
| 171 | if (modelTopology == null && weightsManifest == null) { |
| 172 | throw new Error( |
| 173 | `The JSON from HTTP path ${this.path} contains neither model ` + |
| 174 | `topology or manifest for weights.`); |
| 175 | } |
| 176 | |
| 177 | return modelJSON; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Load model artifacts via HTTP request(s). |
no test coverage detected