-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathtfgo_test.go
431 lines (358 loc) · 13.7 KB
/
tfgo_test.go
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
/*
Copyright 2017-2022 Paolo Galeone. All right 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.
*/
package tfgo_test
import (
"math"
"reflect"
"testing"
tf "github.com/galeone/tensorflow/tensorflow/go"
tg "github.com/galeone/tfgo"
)
func TestNewScope(t *testing.T) {
root := tg.NewRoot()
scope := tg.NewScope(root)
if scope == nil {
t.Error("NewScope shouldn't return nil")
}
}
func TestTensor(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("The code panic, but it shouldn't: %v", r)
}
}()
root := tg.NewRoot()
tensorA := tg.NewTensor(root, tg.Const(root, [3]int32{1, 2, 3}))
if tensorA == nil {
t.Fatal("NewTensor shouldn't return nil")
}
// shouldn't panic
tensorA.Check()
tensorB := tg.NewTensor(root, tg.Const(root, [3]int32{1, 2, 3}))
// For not changing the content of A
// Create a new tensor with the same content of A
// on every invocation.
// Change the content on the fly is useful when used chaning the operations
add := tensorA.Clone().Add(tensorB.Output).Output
mul := tensorA.Clone().Mul(tensorB.Output).Output
// types must be always well defined
// never use a number, e.i. 2, but force a type e.i. int32(2)
pow := tensorA.Clone().Pow(tg.Const(root, int32(2))).Output
sqrt := tensorA.Clone().Sqrt().Output
square := tensorA.Clone().Square().Output
shape32 := tensorA.Clone().Shape32(true)
shape64 := tensorA.Clone().Shape64(true)
if len(shape32) != len(shape64) {
t.Errorf("Expected len(shape32) = len(shape64), but got: %v != %v", len(shape32), len(shape64))
}
// remove first dim
shape32 = tensorA.Clone().Shape32(false)
shape64 = tensorA.Clone().Shape64(false)
if len(shape32) != len(shape64) {
t.Errorf("Expected len(shape32) = len(shape64), but got: %v != %v", len(shape32), len(shape64))
}
matA := tg.NewTensor(root, tg.Const(root, [2][2]int32{{1, 2}, {-1, -2}}))
matB := tg.NewTensor(root, tg.Const(root, [2][1]int32{{10}, {100}}))
// chain op without clone, matA now is matmul result
matA = matA.MatMul(matB.Output)
result := tg.Exec(root, []tf.Output{add, mul, pow, sqrt, square, matA.Output}, nil, nil)
if result[0].Value().([]int32)[0] != 2 {
t.Errorf("Expected 2 as first value in sum, but got: %v", result[0].Value().([]int32)[0])
}
if result[1].Value().([]int32)[0] != 1 {
t.Errorf("Expected 1 as first value in mul, but got: %v", result[1].Value().([]int32))
}
if result[2].Value().([]int32)[0] != 1 {
t.Errorf("Expected 1 as first value in pow, but got: %v", result[2].Value().([]int32)[0])
}
if result[3].Value().([]int32)[0] != 1 {
t.Errorf("Expected 1 as first value in sqrt, but got: %v", result[3].Value().([]int32)[0])
}
if result[4].Value().([]int32)[0] != result[2].Value().([]int32)[0] {
t.Errorf("Expected output of square being equal to tensor² but got: %v vs %v", result[4].Value().([]int32), result[2].Value().([]int32))
}
if result[5].Value().([][]int32)[0][0] != 210 {
t.Errorf("Expected output of matmul in pos 0,0 should be 210, but got: %v", result[5].Value().([][]int32))
}
}
func TestTensorPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
root := tg.NewRoot()
tensorA := tg.NewTensor(root, tg.Const(root, [3]int32{1, 2, 3}))
if tensorA == nil {
t.Fatal("NewTensor shouldn't return nil")
}
// shouldn't panic
tensorA.Check()
tensorB := tg.NewTensor(root, tg.Const(root, [3]int32{1, 2, 3}))
add := tensorA.Add(tensorB.Output)
result := tg.Exec(root, []tf.Output{add.Output}, nil, nil)
if result[0].Value().([]int32)[0] != 2 {
t.Errorf("Expected 2 as first value in sum, but got: %v", result[0].Value().([]int32)[0])
}
// After the Exec operation, everything should panic because the graph has been finalized
// and the graph, thus, has been built and it's unmodifiable
tensorA = tensorA.Cast(tf.Float)
if tensorA == nil {
t.Error("Cast operation shouldn't return nil")
}
tensorA.Check()
}
func TestBatchify(t *testing.T) {
root := tg.NewRoot()
var tensors []tf.Output
for i := 0; i < 10; i++ {
tensors = append(tensors, tg.Const(root, [3]int32{1, 2, 3}))
}
batch := tg.Batchify(root, tensors)
if batch.Shape().NumDimensions() != 2 {
t.Errorf("Expected 2D tensor, but got: %dD tensor", batch.Shape().NumDimensions())
}
shape, _ := batch.Shape().ToSlice()
if shape[0] != 10 || shape[1] != 3 {
t.Errorf("Expected shape (10,3), got (%d,%d)", shape[0], shape[1])
}
result := tg.Exec(root, []tf.Output{batch}, nil, nil)
// Note the cast to [][] and not to [10][3]
matrixResult := result[0].Value().([][]int32)
var expectedMatrix [][]int32
row := []int32{1, 2, 3}
for i := 0; i < 10; i++ {
expectedMatrix = append(expectedMatrix, row)
}
if !reflect.DeepEqual(matrixResult, expectedMatrix) {
t.Errorf("Expected matrix %v\n Got matrix %v", expectedMatrix, matrixResult)
}
}
func TestIsClose(t *testing.T) {
root := tg.NewRoot()
A := tg.Const(root, []float32{0.1, 0.2, 0.3, 1e-1, 1e-2, 1e-3, 1e-4, 1e-6, 5e-5})
B := tg.Const(root, []float32{0.11, 0.2, 0.299, 0, 2e-2, 2e-3, 2e-4, 0, 10})
relTol := tg.Const(root, float32(1e-3))
absTol := tg.Const(root, float32(1e-6))
isClose := tg.IsClose(root, A, B, relTol, absTol)
expected := []bool{false, true, false, false, false, false, false, true, false}
results := tg.Exec(root, []tf.Output{isClose}, nil, nil)
result := results[0].Value().([]bool)
if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected %v\n Got %v", expected, result)
}
}
func TestPanicModelRestore(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
// Panics because the tag does not exist
tg.LoadModel("test_models/keras", []string{"tagwat"}, nil)
}
func TestPanicModelWhenOpNotExists(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
model := tg.LoadModel("test_models/keras", []string{"tag"}, nil)
model.Op("does not exists", 0)
}
func TestPanicModelWhenOpOutputNotExists(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
model := tg.LoadModel("test_models/keras", []string{"tag"}, nil)
// Esists but wroing output number (1 instead of 0)
model.Op("LeNetDropout/softmax_linear/Identity", 1)
}
func TestLoadModel(t *testing.T) {
// A model exported with tf.saved_model.save()
// automatically comes with the "serve" tag because the SavedModel
// file format is designed for serving.
// This tag contains the various functions exported. Among these, there is
// always present the "serving_default" signature_def. This signature def
// works exactly like the TF 1.x graph. Get the input tensor and the output tensor,
// and use them as placeholder to feed and output to get, respectively.
// To get info inside a SavedModel the best tool is saved_model_cli
// that comes with the TensorFlow Python package.
// e.g. saved_model_cli show --all --dir output/keras
// gives, among the others, this info:
// signature_def['serving_default']:
// The given SavedModel SignatureDef contains the following input(s):
// inputs['inputs_input'] tensor_info:
// dtype: DT_FLOAT
// shape: (-1, 28, 28, 1)
// name: serving_default_inputs_input:0
// The given SavedModel SignatureDef contains the following output(s):
// outputs['logits'] tensor_info:
// dtype: DT_FLOAT
// shape: (-1, 10)
// name: StatefulPartitionedCall:0
// Method name is: tensorflow/serving/predict
model := tg.LoadModel("test_models/output/keras", []string{"serve"}, nil)
fakeInput, _ := tf.NewTensor([1][28][28][1]float32{})
results := model.Exec([]tf.Output{
model.Op("StatefulPartitionedCall", 0),
}, map[tf.Output]*tf.Tensor{
model.Op("serving_default_inputs_input", 0): fakeInput,
})
if results[0].Shape()[0] != 1 || results[0].Shape()[1] != 10 {
t.Errorf("Expected output shape of [1,10], got %v", results[0].Shape())
}
}
func TestPanicImportModelReadFile(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
// Panics because the model file does not exists
tg.ImportModel("test_models/export/fake", "", nil)
}
func TestPanicModelExec(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
model := tg.LoadModel("test_models/export", []string{"tag"}, nil)
// fake input with meaningless type should make the model crash
fakeInput, _ := tf.NewTensor([1][28][28][1]string{})
model.Exec([]tf.Output{
model.Op("LeNetDropout/softmax_linear/Identity", 0),
}, map[tf.Output]*tf.Tensor{
model.Op("input_", 0): fakeInput,
})
}
func TestIsIntegerFloat(t *testing.T) {
root := tg.NewRoot()
A := tg.Const(root, int64(0))
B := tg.Const(root, []float32{0.11})
if tg.IsInteger(B.DataType()) {
t.Error("Expected a float type, but integer found")
}
if !tg.IsInteger(A.DataType()) {
t.Error("A supposed to be integer, but IsInteger said no")
}
if tg.IsFloat(A.DataType()) {
t.Error("A is integer, but IsFloat returned true")
}
if !tg.IsFloat(B.DataType()) {
t.Error("Expected a float type, but float32 has been considered not float")
}
}
func TestMinValue(t *testing.T) {
root := tg.NewRoot()
A := tg.Const(root, int64(0))
B := tg.Const(root, float64(0))
if tg.MinValue(A.DataType()) != math.MinInt64 {
t.Errorf("expected MinValue of dype int64 to be equal to math.MinInt64, but got %v", tg.MinValue(A.DataType()))
}
if tg.MinValue(B.DataType()) != math.SmallestNonzeroFloat64 {
t.Errorf("expected MinValue of dype float64 to be equal to math.SmallestNonzeroFloat64 but got %v", tg.MinValue(B.DataType()))
}
A = tg.Cast(root, A, tf.Int32)
if tg.MinValue(A.DataType()) != math.MinInt32 {
t.Errorf("expected MinValue of dype int32 to be equal to math.MinInt32, but got %v", tg.MinValue(A.DataType()))
}
A = tg.Cast(root, A, tf.Int16)
if tg.MinValue(A.DataType()) != math.MinInt16 {
t.Errorf("expected MinValue of dype int16 to be equal to math.MinInt16, but got %v", tg.MinValue(A.DataType()))
}
A = tg.Cast(root, A, tf.Int8)
if tg.MinValue(A.DataType()) != math.MinInt8 {
t.Errorf("expected MinValue of dype int8 to be equal to math.MinInt8, but got %v", tg.MinValue(A.DataType()))
}
A = tg.Cast(root, A, tf.Uint8)
if tg.MinValue(A.DataType()) != 0 {
t.Errorf("expected MinValue of dype uint8 to be equal to 0, but got %v", tg.MinValue(A.DataType()))
}
A = tg.Cast(root, A, tf.Uint16)
if tg.MinValue(A.DataType()) != 0 {
t.Errorf("expected MinValue of dype uint16 to be equal to 0, but got %v", tg.MinValue(A.DataType()))
}
B = tg.Cast(root, B, tf.Float)
if tg.MinValue(B.DataType()) != math.SmallestNonzeroFloat32 {
t.Errorf("expected MinValue of dype float32 to be equal to math.SmallestNonzeroFloat32 but got %v", tg.MinValue(B.DataType()))
}
B = tg.Cast(root, B, tf.Half)
if tg.MinValue(B.DataType()) != 6.10*math.Pow10(-5) {
t.Errorf("expected MinValue of dype float32 to be equal to 6.1*10^{-5} but got %v", tg.MinValue(B.DataType()))
}
}
func TestMaxValuePanic(t *testing.T) {
defer func() {
// Panic on max on unsupported dtype
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
root := tg.NewRoot()
s := tg.Const(root, "test")
tg.MaxValue(s.DataType())
}
func TestMinValuePanic(t *testing.T) {
defer func() {
// Panic on max on unsupported dtype
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
root := tg.NewRoot()
s := tg.Const(root, "test")
tg.MinValue(s.DataType())
}
func TestMaxValue(t *testing.T) {
root := tg.NewRoot()
A := tg.Const(root, int64(0))
B := tg.Const(root, float64(0))
if tg.MaxValue(A.DataType()) != math.MaxInt64 {
t.Errorf("expected MaxValue of dype int64 to be equal to math.MaxInt64, but got %v", tg.MaxValue(A.DataType()))
}
if tg.MaxValue(B.DataType()) != math.MaxFloat64 {
t.Errorf("expected MaxValue of dype float64 to be equal to math.MaxFloat64 but got %v", tg.MaxValue(B.DataType()))
}
A = tg.Cast(root, A, tf.Int32)
if tg.MaxValue(A.DataType()) != math.MaxInt32 {
t.Errorf("expected MaxValue of dype int32 to be equal to math.MaxInt32, but got %v", tg.MaxValue(A.DataType()))
}
A = tg.Cast(root, A, tf.Int16)
if tg.MaxValue(A.DataType()) != math.MaxInt16 {
t.Errorf("expected MaxValue of dype int16 to be equal to math.MaxInt16, but got %v", tg.MaxValue(A.DataType()))
}
A = tg.Cast(root, A, tf.Int8)
if tg.MaxValue(A.DataType()) != math.MaxInt8 {
t.Errorf("expected MaxValue of dype int8 to be equal to math.MaxInt8, but got %v", tg.MaxValue(A.DataType()))
}
A = tg.Cast(root, A, tf.Uint8)
if tg.MaxValue(A.DataType()) != math.MaxUint8 {
t.Errorf("expected MaxValue of dype uint8 to be equal to math.MaxUint8, but got %v", tg.MaxValue(A.DataType()))
}
A = tg.Cast(root, A, tf.Uint16)
if tg.MaxValue(A.DataType()) != math.MaxUint16 {
t.Errorf("expected MaxValue of dype uint16 to be equal to math.MaxUint16, but got %v", tg.MaxValue(A.DataType()))
}
B = tg.Cast(root, B, tf.Float)
if tg.MaxValue(B.DataType()) != math.MaxFloat32 {
t.Errorf("expected MaxValue of dype float32 to be equal to math.MaxFloat32 but got %v", tg.MaxValue(B.DataType()))
}
B = tg.Cast(root, B, tf.Half)
if tg.MaxValue(B.DataType()) != math.MaxFloat32/math.Pow(2, 16) {
t.Errorf("expected MaxValue of dype float32 to be equal to math.MaxFloat32 / math.Pow(2, 16) but got %v", tg.MaxValue(B.DataType()))
}
}