forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
475 lines (420 loc) · 13.2 KB
/
types.ts
File metadata and controls
475 lines (420 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/* Type definitions for exporting and importing of models. */
/**
* A map from Tensor dtype to number of bytes per element of the Tensor.
*/
export const DTYPE_VALUE_SIZE_MAP: {[dtype: string]: number} = {
'float32': 4,
'int32': 4,
'uint16': 2,
'uint8': 1,
'bool': 1,
};
/**
* A weight manifest.
*
* The weight manifest consists of an ordered list of weight-manifest groups.
* Each weight-manifest group ("group" for short hereafter) consists of a
* number of weight values stored in a number of paths.
* See the documentation of `WeightManifestGroupConfig` below for more details.
*/
export declare type WeightsManifestConfig = WeightsManifestGroupConfig[];
/**
* A weight-manifest group.
*
* Consists of an ordered list of weight values encoded in binary format,
* stored in an ordered list of paths.
*/
export declare interface WeightsManifestGroupConfig {
/**
* An ordered list of paths.
*
* Paths are intentionally abstract in order to be general. For example, they
* can be relative URL paths or relative paths on the file system.
*/
paths: string[];
/**
* Specifications of the weights stored in the paths.
*/
weights: WeightsManifestEntry[];
}
/**
* Group to which the weight belongs.
*
* - 'optimizer': Weight from a stateful optimizer.
*/
export type WeightGroup = 'model'|'optimizer';
/**
* An entry in the weight manifest.
*
* The entry contains specification of a weight.
*/
export declare interface WeightsManifestEntry {
/**
* Name of the weight, e.g., 'Dense_1/bias'
*/
name: string;
/**
* Shape of the weight.
*/
shape: number[];
/**
* Data type of the weight.
*/
dtype: 'float32'|'int32'|'bool'|'string';
/**
* Type of the weight.
*
* Optional.
*
* The value 'optimizer' indicates the weight belongs to an optimizer
* (i.e., used only during model training and not during inference).
*/
group?: WeightGroup;
/**
* Information for dequantization of the weight.
*/
quantization?: {
scale: number, // The scaling constant to multiply by.
min: number, // The (possibly nudged) minimum weight to add.
dtype: 'uint16'|'uint8' // The dtype of the quantized weights.
};
}
/**
* Options for saving a model.
* @innamespace io
*/
export interface SaveConfig {
/**
* Whether to save only the trainable weights of the model, ignoring the
* non-trainable ones.
*/
trainableOnly?: boolean;
/**
* Whether the optimizer will be saved (if exists).
*
* Default: `false`.
*/
includeOptimizer?: boolean;
}
/**
* Result of a saving operation.
*/
export interface SaveResult {
/**
* Information about the model artifacts saved.
*/
modelArtifactsInfo: ModelArtifactsInfo;
/**
* HTTP responses from the server that handled the model-saving request (if
* any). This is applicable only to server-based saving routes.
*/
responses?: Response[];
/**
* Error messages and related data (if any).
*/
errors?: Array<{}|string>;
}
export declare interface ModelArtifactsInfo {
/**
* Timestamp for when the model is saved.
*/
dateSaved: Date;
/**
* TODO (cais,yassogba) consider removing GraphDef as GraphDefs now
* come in a JSON format and none of our IOHandlers support a non json
* format. We could conder replacing this with 'Binary' if we want to
* allow future handlers to save to non json formats (though they will
* probably want more information than 'Binary').
* Type of the model topology
*
* Type of the model topology
*
* Possible values:
* - JSON: JSON config (human-readable, e.g., Keras JSON).
* - GraphDef: TensorFlow
* [GraphDef](https://www.tensorflow.org/extend/tool_developers/#graphdef)
* protocol buffer (binary).
*/
modelTopologyType: 'JSON'|'GraphDef';
/**
* Size of model topology (Keras JSON or GraphDef), in bytes.
*/
modelTopologyBytes?: number;
/**
* Size of weight specification or manifest, in bytes.
*/
weightSpecsBytes?: number;
/**
* Size of weight value data, in bytes.
*/
weightDataBytes?: number;
}
/** Model training configuration. */
export declare interface TrainingConfig {
// TODO(cais): Tighten the typing once keras spec is available to tfjs-core.
// See
// tslint:disable-next-line:max-line-length
// https://github.com/tensorflow/tfjs-layers/blob/master/src/keras_format/training_config.ts
/** Optimizer used for the model training. */
optimizer_config: {};
// TODO(cais): Tighten the typing once keras spec is available to tfjs-core.
/** Loss function(s) for the model's output(s). */
loss: string|string[]|{[key: string]: string};
// TODO(cais): Tighten the typing once keras spec is available to tfjs-core.
/** Metric function(s) for the model's output(s). */
metrics?: string[]|{[key: string]: string};
// TODO(cais): Tighten the typing once keras spec is available to tfjs-core.
weighted_metrics?: string[];
// TODO(cais): Tighten the typing once keras spec is available to tfjs-core.
sample_weight_mode?: string;
loss_weights?: number[]|{[key: string]: number};
}
/**
* The serialized artifacts of a model, including topology and weights.
*
* The `modelTopology`, `trainingConfig`, `weightSpecs` and `weightData` fields
* of this interface are optional, in order to support topology- or weights-only
* saving and loading.
*
* Note this interface is used internally in IOHandlers. For the file format
* written to disk as `model.json`, see `ModelJSON`.
*/
export declare interface ModelArtifacts {
/**
* Model topology.
*
* For Keras-style `tf.Model`s, this is a JSON object.
* For TensorFlow-style models (e.g., `SavedModel`), this is the JSON
* encoding of the `GraphDef` protocol buffer.
*/
modelTopology?: {}|ArrayBuffer;
/**
* Serialized configuration for the model's training.
*/
trainingConfig?: TrainingConfig;
/**
* Weight specifications.
*
* This corresponds to the weightsData below.
*/
weightSpecs?: WeightsManifestEntry[];
/**
* Binary buffer for all weight values concatenated in the order specified
* by `weightSpecs`.
*/
weightData?: ArrayBuffer;
/**
* Hard-coded format name for models saved from TensorFlow.js or converted
* by TensorFlow.js Converter.
*/
format?: string;
/**
* What library is responsible for originally generating this artifact.
*
* Used for debugging purposes. E.g., 'TensorFlow.js v1.0.0'.
*/
generatedBy?: string;
/**
* What library or tool is responsible for converting the original model
* to this format, applicable only if the model is output by a converter.
*
* Used for debugging purposes. E.g., 'TensorFlow.js Converter v1.0.0'.
*
* A value of `null` means the model artifacts are generated without any
* conversion process (e.g., saved directly from a TensorFlow.js
* `tf.LayersModel` instance.)
*/
convertedBy?: string|null;
/**
* User-defined metadata about the model.
*/
userDefinedMetadata?: {};
}
/**
* The on-disk format of the `model.json` file.
*
* TF.js 1.0 always populates the optional fields when writing model.json.
* Prior versions did not provide those fields.
*/
export declare interface ModelJSON {
/**
* Model topology.
*
* For Keras-style `tf.Model`s, this is a JSON object.
* For TensorFlow-style models (e.g., `SavedModel`), this is the JSON
* encoding of the `GraphDef` protocol buffer.
*/
modelTopology: {};
/** Model training configuration. */
trainingConfig?: TrainingConfig;
/**
* Weights manifest.
*
* The weights manifest consists of an ordered list of weight-manifest
* groups. Each weight-manifest group consists of a number of weight values
* stored in a number of paths. See the documentation of
* `WeightsManifestConfig` for more details.
*/
weightsManifest: WeightsManifestConfig;
/**
* Hard-coded format name for models saved from TensorFlow.js or converted
* by TensorFlow.js Converter.
*/
format?: string;
/**
* What library is responsible for originally generating this artifact.
*
* Used for debugging purposes. E.g., 'TensorFlow.js v1.0.0'.
*/
generatedBy?: string;
/**
* What library or tool is responsible for converting the original model
* to this format, applicable only if the model is output by a converter.
*
* Used for debugging purposes. E.g., 'TensorFlow.js Converter v1.0.0'.
*
* A value of `null` means the model artifacts are generated without any
* conversion process (e.g., saved directly from a TensorFlow.js
* `tf.LayersModel` instance.)
*/
convertedBy?: string|null;
/**
* User-defined metadata about the model.
*/
userDefinedMetadata?: {};
}
/**
* Type definition for handlers of loading operations.
*/
export type LoadHandler = () => Promise<ModelArtifacts>;
/**
* Type definition for handlers of saving operations.
*/
export type SaveHandler = (modelArtifact: ModelArtifacts) =>
Promise<SaveResult>;
/**
* Interface for a model import/export handler.
*
* The `save` and `load` handlers are both optional, in order to allow handlers
* that support only saving or loading.
*/
// tslint:disable-next-line:interface-name
export interface IOHandler {
save?: SaveHandler;
load?: LoadHandler;
}
/**
* An interface for the manager of a model store.
*
* A model store is defined as a storage medium on which multiple models can
* be stored. Each stored model has a unique `path` as its identifier.
* A `ModelStoreManager` for the store allows actions including
*
* - Listing the models stored in the store.
* - Deleting a model from the store.
*/
export interface ModelStoreManager {
/**
* List all models in the model store.
*
* @returns A dictionary mapping paths of existing models to their
* model artifacts info. Model artifacts info include type of the model's
* topology, byte sizes of the topology, weights, etc.
*/
listModels(): Promise<{[path: string]: ModelArtifactsInfo}>;
/**
* Remove a model specified by `path`.
*
* @param path
* @returns ModelArtifactsInfo of the deleted model (if and only if deletion
* is successful).
* @throws Error if deletion fails, e.g., if no model exists at `path`.
*/
removeModel(path: string): Promise<ModelArtifactsInfo>;
}
/**
* Callback for the progress of a long-running action such as an HTTP
* request for a large binary object.
*
* `fraction` should be a number in the [0, 1] interval, indicating how
* much of the action has completed.
*/
export type OnProgressCallback = (fraction: number) => void;
/** @innamespace io */
export interface LoadOptions {
/**
* RequestInit (options) for HTTP requests.
*
* For detailed information on the supported fields, see
* [https://developer.mozilla.org/en-US/docs/Web/API/Request/Request](
* https://developer.mozilla.org/en-US/docs/Web/API/Request/Request)
*/
requestInit?: RequestInit;
/**
* Progress callback.
*/
onProgress?: OnProgressCallback;
/**
* A function used to override the `window.fetch` function.
*/
fetchFunc?: Function;
/**
* Strict loading model: whether extraneous weights or missing
* weights should trigger an `Error`.
*
* If `true`, require that the provided weights exactly match those
* required by the layers. `false` means that both extra weights
* and missing weights will be silently ignored.
*
* Default: `true`.
*/
strict?: boolean;
/**
* Path prefix for weight files, by default this is calculated from the
* path of the model JSON file.
*
* For instance, if the path to the model JSON file is
* `http://localhost/foo/model.json`, then the default path prefix will be
* `http://localhost/foo/`. If a weight file has the path value
* `group1-shard1of2` in the weight manifest, then the weight file will be
* loaded from `http://localhost/foo/group1-shard1of2` by default. However,
* if you provide a `weightPathPrefix` value of
* `http://localhost/foo/alt-weights`, then the weight file will be loaded
* from the path `http://localhost/foo/alt-weights/group1-shard1of2` instead.
*/
weightPathPrefix?: string;
/**
* Whether the module or model is to be loaded from TF Hub.
*
* Setting this to `true` allows passing a TF-Hub module URL, omitting the
* standard model file name and the query parameters.
*
* Default: `false`.
*/
fromTFHub?: boolean;
}
/**
* Additional options for Platform.fetch
*/
export interface RequestDetails {
/**
* Is this request for a binary file (as opposed to a json file)
*/
isBinary?: boolean;
}