(
handler: io.IOHandler, customObjects?: serialization.ConfigDict,
options?: io.LoadOptions)
| 280 | * Default: `true`. |
| 281 | */ |
| 282 | export async function loadLayersModelFromIOHandler( |
| 283 | handler: io.IOHandler, customObjects?: serialization.ConfigDict, |
| 284 | options?: io.LoadOptions): Promise<LayersModel> { |
| 285 | if (options == null) { |
| 286 | options = {}; |
| 287 | } |
| 288 | if (handler.load == null) { |
| 289 | throw new ValueError( |
| 290 | 'Cannot proceed with model loading because the IOHandler provided ' + |
| 291 | 'does not have the `load` method implemented.'); |
| 292 | } |
| 293 | const artifacts = await handler.load(); |
| 294 | let modelTopology = artifacts.modelTopology as PyJsonDict; |
| 295 | if (modelTopology['model_config'] != null) { |
| 296 | modelTopology = modelTopology['model_config'] as PyJsonDict; |
| 297 | } |
| 298 | |
| 299 | const strict = options.strict == null ? true : options.strict; |
| 300 | // If weights are provided and the weight-loading mode is strict, use |
| 301 | // fast weight initialization. This skips costly initializers such as |
| 302 | // 'orthogonal' and saves unnecessary computation in cases where |
| 303 | // the initialized weight values will immediately be overwritten by |
| 304 | // loaded weight values. |
| 305 | const fastWeightInit = |
| 306 | artifacts.weightData != null && artifacts.weightSpecs != null && strict; |
| 307 | const model = |
| 308 | deserialize( |
| 309 | convertPythonicToTs(modelTopology) as serialization.ConfigDict, |
| 310 | customObjects, fastWeightInit) as LayersModel; |
| 311 | |
| 312 | const trainingConfig = artifacts.trainingConfig as TrainingConfig; |
| 313 | if (trainingConfig != null) { |
| 314 | model.loadTrainingConfig(trainingConfig); |
| 315 | } |
| 316 | if (artifacts.userDefinedMetadata != null) { |
| 317 | model.setUserDefinedMetadata(artifacts.userDefinedMetadata); |
| 318 | } |
| 319 | |
| 320 | // If weightData is present, load the weights into the model. |
| 321 | if (artifacts.weightData != null) { |
| 322 | // Loading weights requires weightSpecs. |
| 323 | if (artifacts.weightSpecs == null) { |
| 324 | throw new ValueError( |
| 325 | 'LayersModel artifacts contains weight data, but not weight specs. ' + |
| 326 | 'Therefore loading of weights cannot proceed.'); |
| 327 | } |
| 328 | |
| 329 | const {modelWeights, optimizerWeights} = decodeModelAndOptimizerWeights( |
| 330 | artifacts.weightData, artifacts.weightSpecs); |
| 331 | model.loadWeights(modelWeights, strict); |
| 332 | |
| 333 | if (model.optimizer != null && optimizerWeights.length > 0) { |
| 334 | await model.optimizer.setWeights(optimizerWeights); |
| 335 | } |
| 336 | |
| 337 | // Dispose temporary weight values. |
| 338 | dispose(modelWeights); |
| 339 | dispose(optimizerWeights.map(w => w.tensor)); |
no test coverage detected
searching dependent graphs…