-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathsaved_model_test.ts
431 lines (403 loc) · 16.4 KB
/
saved_model_test.ts
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
/**
* @license
* Copyright 2019 Google Inc. 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.
* =============================================================================
*/
import {NamedTensorMap, test_util} from '@tensorflow/tfjs-core';
import * as tf from './index';
import {nodeBackend} from './nodejs_kernel_backend';
import {getEnumKeyFromValue, getInputAndOutputNodeNameFromMetaGraphInfo, readSavedModelProto} from './saved_model';
// tslint:disable-next-line:no-require-imports
const messages = require('./proto/api_pb');
describe('SavedModel', () => {
it('deserialize SavedModel pb file', async () => {
/**
* The SavedModel has one MetaGraph (tag is serve). Part of the MetaGraph
* look like:
* {
* MetaInfoDef: {tags: [`serve`]},
* signatureDef: {
* __saved_model_init_op: {
* 'inputsMap': {},
* 'outputsMap': {'__saved_model_init_op': {'name': 'NoOp', 'dtype':
* 0}},
* },
* serving_default: {
* 'inputs': {
* x: {
* 'name': 'serving_default_x:0',
* 'dtype': 1,
* }
* },
* 'outputs': {
* output_0: {
* 'name': 'StatefulPartitionedCall:0',
* 'dtype': 1,
* }
* },
* }
* }
* }
*/
const modelMessage = await readSavedModelProto(
'./test_objects/saved_model/times_three_float');
// This SavedModel has one MetaGraph with tag serve
expect(modelMessage.getMetaGraphsList().length).toBe(1);
expect(modelMessage.getMetaGraphsList()[0]
.getMetaInfoDef()
.getTagsList()
.length)
.toBe(1);
expect(
modelMessage.getMetaGraphsList()[0].getMetaInfoDef().getTagsList()[0])
.toBe('serve');
// Validate the SavedModel has signatureDef serving_default
const signatureDefMapMessage =
modelMessage.getMetaGraphsList()[0].getSignatureDefMap();
expect(signatureDefMapMessage.has('serving_default'));
// The input op of signature serving_default is serving_default_x, DataType
// is DT_FLOAT
const inputsMapMessage =
signatureDefMapMessage.get('serving_default').getInputsMap();
expect(inputsMapMessage.getLength()).toBe(1);
const inputsMapKeys = inputsMapMessage.keys();
const inputsMapKey1 = inputsMapKeys.next();
expect(inputsMapKey1.done).toBe(false);
expect(inputsMapKey1.value).toBe('x');
const inputTensorMessage = inputsMapMessage.get(inputsMapKey1.value);
expect(inputTensorMessage.getName()).toBe('serving_default_x:0');
expect(
getEnumKeyFromValue(messages.DataType, inputTensorMessage.getDtype()))
.toBe('DT_FLOAT');
// The output op of signature serving_default is StatefulPartitionedCall,
// DataType is DT_FLOAT
const outputsMapMessage =
signatureDefMapMessage.get('serving_default').getOutputsMap();
expect(outputsMapMessage.getLength()).toBe(1);
const outputsMapKeys = outputsMapMessage.keys();
const outputsMapKey1 = outputsMapKeys.next();
expect(outputsMapKey1.done).toBe(false);
expect(outputsMapKey1.value).toBe('output_0');
const outputTensorMessage = outputsMapMessage.get(outputsMapKey1.value);
expect(outputTensorMessage.getName()).toBe('StatefulPartitionedCall:0');
expect(
getEnumKeyFromValue(messages.DataType, outputTensorMessage.getDtype()))
.toBe('DT_FLOAT');
});
it('get enum key based on value', () => {
const DataType = messages.DataType;
const enumKey0 = getEnumKeyFromValue(DataType, 0);
expect(enumKey0).toBe('DT_INVALID');
const enumKey1 = getEnumKeyFromValue(DataType, 1);
expect(enumKey1).toBe('DT_FLOAT');
const enumKey2 = getEnumKeyFromValue(DataType, 2);
expect(enumKey2).toBe('DT_DOUBLE');
});
it('read non-exist file', async done => {
try {
await readSavedModelProto('/not-exist');
done.fail();
} catch (err) {
expect(err.message)
.toBe(`There is no saved_model.pb file in the directory: /not-exist`);
done();
}
});
it('inspect SavedModel metagraphs', async () => {
const modelInfo = await tf.node.getMetaGraphsFromSavedModel(
'./test_objects/saved_model/times_three_float');
/**
* The inspection output should be
* [{
* 'tags': ['serve'],
* 'signatureDefs': {
* '__saved_model_init_op': {
* 'inputs': {},
* 'outputs': {
* '__saved_model_init_op': {
* 'dtype': 'DT_INVALID',
* 'name': 'NoOp',
* 'shape': []
* }
* }
* },
* 'serving_default': {
* 'inputs': {
* 'x': {
* 'dtype': 'DT_FLOAT',
* 'name': 'serving_default_x:0',
* 'shape':[]
* }
* },
* 'outputs': {
* 'output_0': {
* 'dtype': 'DT_FLOAT',
* 'name': 'StatefulPartitionedCall:0',
* 'shape': []
* }
* }
* }
* }
* }]
*/
expect(modelInfo.length).toBe(1);
expect(modelInfo[0].tags.length).toBe(1);
expect(modelInfo[0].tags[0]).toBe('serve');
expect(Object.keys(modelInfo[0].signatureDefs).length).toBe(2);
expect(Object.keys(modelInfo[0].signatureDefs)[0])
.toBe('__saved_model_init_op');
expect(Object.keys(modelInfo[0].signatureDefs)[1]).toBe('serving_default');
expect(Object.keys(modelInfo[0].signatureDefs['serving_default'].inputs)
.length)
.toBe(1);
expect(modelInfo[0].signatureDefs['serving_default'].inputs['x'].name)
.toBe('serving_default_x:0');
expect(modelInfo[0].signatureDefs['serving_default'].inputs['x'].dtype)
.toBe('DT_FLOAT');
expect(Object.keys(modelInfo[0].signatureDefs['serving_default'].outputs)
.length)
.toBe(1);
expect(
modelInfo[0].signatureDefs['serving_default'].outputs['output_0'].name)
.toBe('StatefulPartitionedCall:0');
expect(
modelInfo[0].signatureDefs['serving_default'].outputs['output_0'].dtype)
.toBe('DT_FLOAT');
});
it('get input and output node names from SavedModel metagraphs', async () => {
const modelInfo = await tf.node.getMetaGraphsFromSavedModel(
'./test_objects/saved_model/times_three_float');
const inputAndOutputNodeNames = getInputAndOutputNodeNameFromMetaGraphInfo(
modelInfo, ['serve'], 'serving_default');
expect(inputAndOutputNodeNames.length).toBe(2);
expect(inputAndOutputNodeNames[0]['x']).toBe('serving_default_x:0');
expect(inputAndOutputNodeNames[1]['output_0'])
.toBe('StatefulPartitionedCall:0');
});
it('load TFSavedModel', async () => {
const loadSavedModelMetaGraphSpy =
spyOn(nodeBackend(), 'loadSavedModelMetaGraph').and.callThrough();
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(0);
const model = await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve'],
'serving_default');
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
model.dispose();
});
it('load TFSavedModel with wrong tags throw exception', async done => {
try {
await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve', 'gpu'],
'serving_default');
done.fail();
} catch (error) {
expect(error.message)
.toBe('The SavedModel does not have tags: serve,gpu');
done();
}
});
it('load TFSavedModel with wrong signature throw exception', async done => {
try {
await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve'],
'wrong_signature');
done.fail();
} catch (error) {
expect(error.message)
.toBe('The SavedModel does not have signature: wrong_signature');
done();
}
});
it('load TFSavedModel and delete', async () => {
const loadSavedModelMetaGraphSpy =
spyOn(nodeBackend(), 'loadSavedModelMetaGraph').and.callThrough();
const deleteSavedModelSpy =
spyOn(nodeBackend(), 'deleteSavedModel').and.callThrough();
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(0);
expect(deleteSavedModelSpy).toHaveBeenCalledTimes(0);
const model = await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve'],
'serving_default');
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
expect(deleteSavedModelSpy).toHaveBeenCalledTimes(0);
model.dispose();
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
expect(deleteSavedModelSpy).toHaveBeenCalledTimes(1);
});
it('delete TFSavedModel multiple times throw exception', async done => {
const model = await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve'],
'serving_default');
model.dispose();
try {
model.dispose();
done.fail();
} catch (error) {
expect(error.message).toBe('This SavedModel has already been deleted.');
done();
}
});
it('load multiple signatures from the same metagraph only call binding once',
async () => {
const backend = nodeBackend();
const loadSavedModelMetaGraphSpy =
spyOn(backend, 'loadSavedModelMetaGraph').and.callThrough();
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(0);
const model1 = await tf.node.loadSavedModel(
'./test_objects/saved_model/module_with_multiple_signatures',
['serve'], 'serving_default');
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
const model2 = await tf.node.loadSavedModel(
'./test_objects/saved_model/module_with_multiple_signatures',
['serve'], 'timestwo');
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
model1.dispose();
model2.dispose();
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
});
it('load signature after delete call binding', async () => {
const backend = nodeBackend();
const spyOnCallBindingLoad =
spyOn(backend, 'loadSavedModelMetaGraph').and.callThrough();
const spyOnNodeBackendDelete =
spyOn(backend, 'deleteSavedModel').and.callThrough();
expect(spyOnCallBindingLoad).toHaveBeenCalledTimes(0);
expect(spyOnNodeBackendDelete).toHaveBeenCalledTimes(0);
const model1 = await tf.node.loadSavedModel(
'./test_objects/saved_model/module_with_multiple_signatures', ['serve'],
'serving_default');
expect(spyOnCallBindingLoad).toHaveBeenCalledTimes(1);
expect(spyOnNodeBackendDelete).toHaveBeenCalledTimes(0);
model1.dispose();
expect(spyOnNodeBackendDelete).toHaveBeenCalledTimes(1);
expect(spyOnCallBindingLoad).toHaveBeenCalledTimes(1);
const model2 = await tf.node.loadSavedModel(
'./test_objects/saved_model/module_with_multiple_signatures', ['serve'],
'timestwo');
expect(spyOnCallBindingLoad).toHaveBeenCalledTimes(2);
expect(spyOnNodeBackendDelete).toHaveBeenCalledTimes(1);
model2.dispose();
expect(spyOnCallBindingLoad).toHaveBeenCalledTimes(2);
expect(spyOnNodeBackendDelete).toHaveBeenCalledTimes(2);
});
it('throw error when input tensors do not match input ops', async done => {
try {
const model = await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve'],
'serving_default');
const input1 = tf.tensor1d([1.0, 2, 3]);
const input2 = tf.tensor1d([1.0, 2, 3]);
model.predict([input1, input2]);
done.fail();
} catch (error) {
expect(error.message)
.toBe('Input op names and input tensors length does not match.');
done();
}
});
it('execute model float times three', async () => {
const model = await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve'],
'serving_default');
const input = tf.tensor1d([1.0, 2, 3]);
const output = model.predict(input) as tf.Tensor;
expect(output.shape).toEqual(input.shape);
expect(output.dtype).toBe(input.dtype);
expect(output.dtype).toBe('float32');
test_util.expectArraysClose(await output.data(), await input.mul(3).data());
model.dispose();
});
it('execute model with tensor array as input', async () => {
const model = await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve'],
'serving_default');
const input = tf.tensor1d([1.0, 2, 3]);
const outputArray = model.predict([input]) as tf.Tensor[];
expect(outputArray.length).toBe(1);
const output = outputArray[0];
expect(output.shape).toEqual(input.shape);
expect(output.dtype).toBe(input.dtype);
expect(output.dtype).toBe('float32');
test_util.expectArraysClose(await output.data(), [3.0, 6.0, 9.0]);
model.dispose();
});
it('execute model with tensor map as input', async () => {
const model = await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve'],
'serving_default');
const input = tf.tensor1d([1.0, 2, 3]);
const outputMap = model.predict({'x': input}) as NamedTensorMap;
const output = outputMap['output_0'];
expect(output.shape).toEqual(input.shape);
expect(output.dtype).toBe(input.dtype);
expect(output.dtype).toBe('float32');
test_util.expectArraysClose(await output.data(), [3.0, 6.0, 9.0]);
model.dispose();
});
it('execute model with wrong tensor name', async done => {
try {
const model = await tf.node.loadSavedModel(
'./test_objects/saved_model/times_three_float', ['serve'],
'serving_default');
const input = tf.tensor1d([1.0, 2, 3]);
model.predict({'xyz': input});
done.fail();
} catch (error) {
expect(error.message)
.toBe(
'The model signatureDef input names are x, however ' +
'the provided input names are xyz.');
done();
}
});
it('execute model int times two', async () => {
const model = await tf.node.loadSavedModel(
'./test_objects/saved_model/times_two_int', ['serve'],
'serving_default');
const input = tf.tensor1d([1, 2, 3], 'int32');
const output = model.predict(input) as tf.Tensor;
expect(output.shape).toEqual(input.shape);
expect(output.dtype).toBe(input.dtype);
test_util.expectArraysClose(await output.data(), [2, 4, 6]);
model.dispose();
});
it('execute multiple signatures from the same model', async () => {
const backend = nodeBackend();
const loadSavedModelMetaGraphSpy =
spyOn(backend, 'loadSavedModelMetaGraph').and.callThrough();
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(0);
const model1 = await tf.node.loadSavedModel(
'./test_objects/saved_model/module_with_multiple_signatures', ['serve'],
'serving_default');
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
const input1 = tf.tensor1d([1, 2, 3]);
const output1 = model1.predict(input1) as tf.Tensor;
expect(output1.shape).toEqual(input1.shape);
expect(output1.dtype).toBe(input1.dtype);
test_util.expectArraysClose(await output1.data(), [3.0, 6.0, 9.0]);
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
const model2 = await tf.node.loadSavedModel(
'./test_objects/saved_model/module_with_multiple_signatures', ['serve'],
'timestwo');
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
const input2 = tf.tensor1d([1, 2, 3]);
const output2 = model2.predict(input2) as tf.Tensor;
expect(output2.shape).toEqual(input2.shape);
expect(output2.dtype).toBe(input2.dtype);
test_util.expectArraysClose(await output2.data(), [2.0, 4.0, 6.0]);
expect(loadSavedModelMetaGraphSpy).toHaveBeenCalledTimes(1);
model1.dispose();
model2.dispose();
});
});