-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathDnnRetrainTransform.cs
More file actions
1367 lines (1177 loc) · 65.6 KB
/
DnnRetrainTransform.cs
File metadata and controls
1367 lines (1177 loc) · 65.6 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.ML;
using Microsoft.ML.CommandLine;
using Microsoft.ML.Data;
using Microsoft.ML.Internal.Utilities;
using Microsoft.ML.Runtime;
using Microsoft.ML.TensorFlow;
using Microsoft.ML.Transforms;
using NumSharp;
using Tensorflow;
using static Microsoft.ML.TensorFlow.TensorFlowUtils;
using static Tensorflow.Binding;
[assembly: LoadableClass(DnnRetrainTransformer.Summary, typeof(IDataTransform), typeof(DnnRetrainTransformer),
typeof(DnnRetrainEstimator.Options), typeof(SignatureDataTransform), DnnRetrainTransformer.UserName, DnnRetrainTransformer.ShortName)]
[assembly: LoadableClass(DnnRetrainTransformer.Summary, typeof(IDataTransform), typeof(DnnRetrainTransformer), null, typeof(SignatureLoadDataTransform),
DnnRetrainTransformer.UserName, DnnRetrainTransformer.LoaderSignature)]
[assembly: LoadableClass(typeof(DnnRetrainTransformer), null, typeof(SignatureLoadModel),
DnnRetrainTransformer.UserName, DnnRetrainTransformer.LoaderSignature)]
[assembly: LoadableClass(typeof(IRowMapper), typeof(DnnRetrainTransformer), null, typeof(SignatureLoadRowMapper),
DnnRetrainTransformer.UserName, DnnRetrainTransformer.LoaderSignature)]
namespace Microsoft.ML.Transforms
{
/// <summary>
/// <see cref="ITransformer" /> for the <see cref="DnnRetrainEstimator"/>.
/// </summary>
internal sealed class DnnRetrainTransformer : RowToRowTransformerBase, IDisposable
{
private bool _isDisposed;
private readonly IHostEnvironment _env;
private readonly string _modelLocation;
private readonly bool _isTemporarySavedModel;
private readonly bool _addBatchDimensionInput;
private Session _session;
private readonly DataViewType[] _outputTypes;
private readonly TF_DataType[] _tfOutputTypes;
private readonly TF_DataType[] _tfInputTypes;
private readonly TensorShape[] _tfInputShapes;
private readonly (Operation, int)[] _tfInputOperations;
private readonly (Operation, int)[] _tfOutputOperations;
private TF_Output[] _tfInputNodes;
private readonly TF_Output[] _tfOutputNodes;
private Graph Graph => _session.graph;
private readonly Dictionary<string, string> _idvToTfMapping;
private readonly string[] _inputs;
private readonly string[] _outputs;
internal const string Summary = "Re-Trains Dnn models.";
internal const string UserName = "DnnRtTransform";
internal const string ShortName = "DnnRtTransform";
internal const string LoaderSignature = "DnnRtTransform";
internal static class DefaultModelFileNames
{
public const string VariablesFolder = "variables";
public const string Index = "variables.index";
public const string Data = "variables.data-00000-of-00001";
public const string Graph = "saved_model.pb";
public const string TmpMlnetModel = "mlnet_model";
}
private static VersionInfo GetVersionInfo()
{
return new VersionInfo(
modelSignature: "DNNTRANS",
//verWrittenCur: 0x00010001, // Initial
verWrittenCur: 0x00000001,
verReadableCur: 0x00000001,
verWeCanReadBack: 0x00000001,
loaderSignature: LoaderSignature,
loaderAssemblyName: typeof(DnnRetrainTransformer).Assembly.FullName);
}
// Factory method for SignatureLoadModel.
private static DnnRetrainTransformer Create(IHostEnvironment env, ModelLoadContext ctx)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(ctx, nameof(ctx));
ctx.CheckAtModel(GetVersionInfo());
// *** Binary format ***
// byte: indicator for frozen models
// byte: indicator for adding batch dimension in input
// int: number of input columns
// for each input column
// int: id of int column name
// int: number of output columns
// for each output column
// int: id of output column name
// stream: tensorFlow model.
GetModelInfo(env, ctx, out string[] inputs, out string[] outputs, out bool isFrozen, out bool addBatchDimensionInput);
if (isFrozen)
{
byte[] modelBytes = null;
if (!ctx.TryLoadBinaryStream("TFModel", r => modelBytes = r.ReadByteArray()))
throw env.ExceptDecode();
return new DnnRetrainTransformer(env, TensorFlowUtils.LoadTFSession(env, modelBytes), outputs, inputs,
null, false, addBatchDimensionInput, 1);
}
var tempDirPath = Path.GetFullPath(Path.Combine(Path.GetTempPath(), nameof(DnnRetrainTransformer) + "_" + Guid.NewGuid()));
CreateFolderWithAclIfNotExists(env, tempDirPath);
try
{
var load = ctx.TryLoadBinaryStream("TFSavedModel", br =>
{
int count = br.ReadInt32();
for (int n = 0; n < count; n++)
{
string relativeFile = br.ReadString();
long fileLength = br.ReadInt64();
string fullFilePath = Path.Combine(tempDirPath, relativeFile);
string fullFileDir = Path.GetDirectoryName(fullFilePath);
if (fullFileDir != tempDirPath)
{
CreateFolderWithAclIfNotExists(env, fullFileDir);
}
using (var fs = new FileStream(fullFilePath, FileMode.Create, FileAccess.Write))
{
long actualRead = br.BaseStream.CopyRange(fs, fileLength);
env.Assert(actualRead == fileLength);
}
}
});
return new DnnRetrainTransformer(env, GetSession(env, tempDirPath), outputs, inputs, tempDirPath, true,
addBatchDimensionInput, 1);
}
catch (Exception)
{
DeleteFolderWithRetries(env, tempDirPath);
throw;
}
}
// Factory method for SignatureDataTransform.
internal static IDataTransform Create(IHostEnvironment env, DnnRetrainEstimator.Options options, IDataView input)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(options, nameof(options));
env.CheckValue(input, nameof(input));
env.CheckValue(options.InputColumns, nameof(options.InputColumns));
env.CheckValue(options.OutputColumns, nameof(options.OutputColumns));
return new DnnRetrainTransformer(env, options, input).MakeDataTransform(input);
}
internal DnnRetrainTransformer(IHostEnvironment env, DnnRetrainEstimator.Options options, IDataView input)
: this(env, options, LoadDnnModel(env, options.ModelLocation), input)
{
}
internal DnnRetrainTransformer(IHostEnvironment env, DnnRetrainEstimator.Options options, ML.TensorFlow.TensorFlowSessionWrapper tensorFlowModel, IDataView input, IDataView validationSet = null)
: this(env, tensorFlowModel.Session, options.OutputColumns, options.InputColumns,
options.ModelLocation, false, options.AddBatchDimensionInputs, options.BatchSize)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(options, nameof(options));
env.CheckValue(input, nameof(input));
CheckTrainingParameters(options);
if (!IsSavedModel(env, options.ModelLocation))
throw env.ExceptNotSupp("TensorFlowTransform: Re-Training of TensorFlow model is only supported for un-frozen model.");
TrainCore(options, input, validationSet);
}
private void CheckTrainingParameters(DnnRetrainEstimator.Options options)
{
Host.CheckNonWhiteSpace(options.LabelColumn, nameof(options.LabelColumn));
Host.CheckNonWhiteSpace(options.OptimizationOperation, nameof(options.OptimizationOperation));
if (_session.graph.OperationByName(options.OptimizationOperation) == null)
throw Host.ExceptParam(nameof(options.OptimizationOperation), $"Optimization operation '{options.OptimizationOperation}' does not exist in the model");
Host.CheckNonWhiteSpace(options.TensorFlowLabel, nameof(options.TensorFlowLabel));
if (_session.graph.OperationByName(options.TensorFlowLabel) == null)
throw Host.ExceptParam(nameof(options.TensorFlowLabel), $"'{options.TensorFlowLabel}' does not exist in the model");
Host.CheckNonWhiteSpace(options.SaveLocationOperation, nameof(options.SaveLocationOperation));
if (_session.graph.OperationByName(options.SaveLocationOperation) == null)
throw Host.ExceptParam(nameof(options.SaveLocationOperation), $"'{options.SaveLocationOperation}' does not exist in the model");
Host.CheckNonWhiteSpace(options.SaveOperation, nameof(options.SaveOperation));
if (_session.graph.OperationByName(options.SaveOperation) == null)
throw Host.ExceptParam(nameof(options.SaveOperation), $"'{options.SaveOperation}' does not exist in the model");
if (options.LossOperation != null)
{
Host.CheckNonWhiteSpace(options.LossOperation, nameof(options.LossOperation));
if (_session.graph.OperationByName(options.LossOperation) == null)
throw Host.ExceptParam(nameof(options.LossOperation), $"'{options.LossOperation}' does not exist in the model");
}
if (options.MetricOperation != null)
{
Host.CheckNonWhiteSpace(options.MetricOperation, nameof(options.MetricOperation));
if (_session.graph.OperationByName(options.MetricOperation) == null)
throw Host.ExceptParam(nameof(options.MetricOperation), $"'{options.MetricOperation}' does not exist in the model");
}
if (options.LearningRateOperation != null)
{
Host.CheckNonWhiteSpace(options.LearningRateOperation, nameof(options.LearningRateOperation));
if (_session.graph.OperationByName(options.LearningRateOperation) == null)
throw Host.ExceptParam(nameof(options.LearningRateOperation), $"'{options.LearningRateOperation}' does not exist in the model");
}
}
private (int, bool, TF_DataType, TensorShape) GetTrainingInputInfo(DataViewSchema inputSchema, string columnName, string tfNodeName, int batchSize)
{
if (!inputSchema.TryGetColumnIndex(columnName, out int inputColIndex))
throw Host.Except($"Column {columnName} doesn't exist");
var type = inputSchema[inputColIndex].Type;
var isInputVector = type is VectorDataViewType;
(Operation inputTensor, int index) = GetOperationFromName(tfNodeName, _session);
var tfInput = new TF_Input(inputTensor, index);
var tfInputType = inputTensor.OpType == "Placeholder" ? inputTensor.OutputType(index) :
inputTensor.InputType(index);
var tfInputShape = ((Tensor)inputTensor).TensorShape;
var numInputDims = tfInputShape != null ? tfInputShape.ndim : -1;
if (isInputVector && (tfInputShape == null || (numInputDims == 0)))
{
var vecType = (VectorDataViewType)type;
var colTypeDims = new int[vecType.Dimensions.Length + 1];
colTypeDims[0] = -1;
for (int indexLocal = 0; indexLocal < vecType.Dimensions.Length; indexLocal += 1)
colTypeDims[indexLocal + 1] = vecType.Dimensions[indexLocal];
tfInputShape = new TensorShape(colTypeDims);
}
if (numInputDims != -1)
{
var newShape = new int[numInputDims];
var dims = tfInputShape.dims;
newShape[0] = dims[0] == 0 || dims[0] == -1 ? batchSize : dims[0];
for (int j = 1; j < numInputDims; j++)
newShape[j] = dims[j];
tfInputShape = new TensorShape(newShape);
}
var expectedType = Tf2MlNetType(tfInputType);
var actualType = type.GetItemType().RawType;
if (type is KeyDataViewType && actualType == typeof(UInt32))
actualType = typeof(Int64);
if (actualType != expectedType.RawType)
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "input", columnName, expectedType.ToString(), type.ToString());
return (inputColIndex, isInputVector, tfInputType, tfInputShape);
}
private void TrainCore(DnnRetrainEstimator.Options options, IDataView input, IDataView validationSet)
{
var inputsForTraining = new string[_inputs.Length + 1];
var inputColIndices = new int[inputsForTraining.Length];
var isInputVector = new bool[inputsForTraining.Length];
var tfInputTypes = new TF_DataType[inputsForTraining.Length];
var tfInputShapes = new TensorShape[inputsForTraining.Length];
for (int i = 0; i < _inputs.Length; i++)
inputsForTraining[i] = _idvToTfMapping[_inputs[i]];
var inputSchema = input.Schema;
for (int i = 0; i < inputsForTraining.Length - 1; i++)
(inputColIndices[i], isInputVector[i], tfInputTypes[i], tfInputShapes[i]) =
GetTrainingInputInfo(inputSchema, _inputs[i], inputsForTraining[i], options.BatchSize);
var index = inputsForTraining.Length - 1;
inputsForTraining[index] = options.TensorFlowLabel;
(inputColIndices[index], isInputVector[index], tfInputTypes[index], tfInputShapes[index]) =
GetTrainingInputInfo(inputSchema, options.LabelColumn, inputsForTraining[index], options.BatchSize);
// Create graph inputs.
Operation labelOp;
int labelOpIdx;
(labelOp, labelOpIdx) = GetOperationFromName(options.TensorFlowLabel, _session);
TF_Output[] tfInputs;
if (!string.IsNullOrEmpty(options.LearningRateOperation))
tfInputs = new TF_Output[_tfInputNodes.Length + 2]; //Inputs + Label + Learning Rate.
else
tfInputs = new TF_Output[_tfInputNodes.Length + 1]; //Inputs + Label.
Array.Copy(_tfInputNodes, tfInputs, _tfInputNodes.Length);
tfInputs[_tfInputNodes.Length] = new TF_Output(labelOp, labelOpIdx);
var lr = GetOperationFromName(options.LearningRateOperation, _session);
tfInputs[_tfInputNodes.Length + 1] = new TF_Output(lr.Item1, lr.Item2);
// Create graph operations.
IntPtr[] ops = null;
if (options.OptimizationOperation != null)
ops = new[] { c_api.TF_GraphOperationByName(Graph, options.OptimizationOperation) };
// Instantiate the graph.
string[] outputs = null;
if (options.LossOperation != null && options.MetricOperation != null)
outputs = new[] { options.LossOperation, options.MetricOperation };
else if (options.LossOperation != null)
outputs = new[] { options.LossOperation };
else if (options.MetricOperation != null)
outputs = new[] { options.MetricOperation };
Runner runner = new Runner(_session, new[] { options.LearningRateOperation }.Concat(inputsForTraining).ToArray(),
outputs, new[] { options.OptimizationOperation }).AddInput(new Tensor(options.LearningRate), 0);
var cols = input.Schema.Where(c => inputColIndices.Contains(c.Index));
for (int epoch = 0; epoch < options.Epoch; epoch++)
{
using (var cursor = input.GetRowCursor(cols))
{
var srcTensorGetters = GetTensorValueGetters(cursor, inputColIndices, isInputVector, tfInputTypes, tfInputShapes);
bool isDataLeft = false;
using (var ch = Host.Start("Training TensorFlow model..."))
using (var pch = Host.StartProgressChannel("TensorFlow training progress..."))
{
float loss = 0;
float metric = 0;
pch.SetHeader(new ProgressHeader(new[] { "Loss", "Metric" }, new[] { "Epoch" }), (e) => e.SetProgress(0, epoch, options.Epoch));
while (cursor.MoveNext())
{
for (int i = 0; i < inputsForTraining.Length; i++)
{
isDataLeft = true;
srcTensorGetters[i].BufferTrainingData();
}
if (((cursor.Position + 1) % options.BatchSize) == 0)
{
isDataLeft = false;
var (l, m) = ExecuteGraphAndRetrieveMetrics(inputsForTraining, srcTensorGetters, runner);
loss += l;
metric += m;
}
}
if (isDataLeft)
{
isDataLeft = false;
ch.Warning("Not training on the last batch. The batch size is less than {0}.", options.BatchSize);
}
pch.Checkpoint(new double?[] { loss, metric });
}
}
}
UpdateModelOnDisk(options.ModelLocation, options);
}
private (float loss, float metric) ExecuteGraphAndRetrieveMetrics(
string[] inputs,
ITensorValueGetter[] srcTensorGetters,
Runner runner)
{
float loss = 0.0f;
float metric = 0.0f;
for (int i = 0; i < inputs.Length; i++)
runner.AddInput(srcTensorGetters[i].GetBufferedBatchTensor(), i + 1);
Tensor[] tensor = runner.Run();
if (tensor.Length > 0 && tensor[0] != IntPtr.Zero)
{
tensor[0].ToScalar<float>(ref loss);
tensor[0].Dispose();
}
if (tensor.Length > 1 && tensor[1] != IntPtr.Zero)
{
tensor[1].ToScalar<float>(ref metric);
tensor[1].Dispose();
}
return (loss, metric);
}
/// <summary>
/// Updates the model on the disk.
/// After retraining Session and Graphs are both up-to-date
/// However model on disk is not which is used to serialzed to ML.Net stream
/// </summary>
private void UpdateModelOnDisk(string modelDir, DnnRetrainEstimator.Options options)
{
try
{
// Save the model on disk
var path = Path.Combine(modelDir, DefaultModelFileNames.TmpMlnetModel);
//var input = GetOperationFromName(options.SaveLocationOperation, Session);
var runner = new Runner(_session, new[] { options.SaveLocationOperation },
null, new[] { options.SaveOperation }).AddInput(new Tensor(path), 0);
runner.Run();
// Preserve original files
var variablesPath = Path.Combine(modelDir, DefaultModelFileNames.VariablesFolder);
var archivePath = Path.Combine(variablesPath + "-" + Guid.NewGuid().ToString());
Directory.CreateDirectory(archivePath);
foreach (var f in Directory.GetFiles(variablesPath))
File.Copy(f, Path.Combine(archivePath, Path.GetFileName(f)));
string[] modelFilePaths = null;
// There are two ways parameters are saved depending on
// either `saver_def = tf.train.Saver().as_saver_def()` was called in Python before `tf.saved_model.simple_save` or not.
// If `saver_def = tf.train.Saver().as_saver_def()` was called files are saved in top directory.
// If not then temporary directory is created in current directory which starts with `mlnet_model`
// and files are saved there.
var tmpParamDir = Directory.GetDirectories(modelDir, DefaultModelFileNames.TmpMlnetModel + "*");
if (tmpParamDir != null && tmpParamDir.Length > 0)
modelFilePaths = Directory.GetFiles(tmpParamDir[0]);
else
modelFilePaths = Directory.GetFiles(modelDir, DefaultModelFileNames.TmpMlnetModel + "*");
foreach (var file in modelFilePaths)
{
if (file.EndsWith(".data-00000-of-00001"))
{
var destination = Path.Combine(variablesPath, DefaultModelFileNames.Data);
if (File.Exists(destination))
File.Delete(destination);
Directory.Move(file, destination);
}
if (file.EndsWith(".index"))
{
var destination = Path.Combine(variablesPath, DefaultModelFileNames.Index);
if (File.Exists(destination))
File.Delete(destination);
Directory.Move(file, destination);
}
}
if (tmpParamDir != null && tmpParamDir.Length > 0)
DeleteFolderWithRetries(Host, tmpParamDir[0]);
}
catch (Exception e)
{
throw Host.ExceptIO(e, "Error serializing TensorFlow retrained model to disk.");
}
}
private static ITensorValueGetter CreateTensorValueGetter<T>(DataViewRow input, bool isVector, int colIndex, TensorShape tfShape, bool keyType = false)
{
if (isVector)
return new TensorValueGetterVec<T>(input, colIndex, tfShape);
return new TensorValueGetter<T>(input, colIndex, tfShape, keyType);
}
private static ITensorValueGetter CreateTensorValueGetter(DataViewRow input, TF_DataType tfType, bool isVector, int colIndex, TensorShape tfShape)
{
var type = Tf2MlNetType(tfType);
if (input.Schema[colIndex].Type is KeyDataViewType && type.RawType == typeof(Int64))
return Utils.MarshalInvoke(CreateTensorValueGetter<int>, typeof(UInt32), input, isVector, colIndex, tfShape, true);
return Utils.MarshalInvoke(CreateTensorValueGetter<int>, type.RawType, input, isVector, colIndex, tfShape, false);
}
private static ITensorValueGetter[] GetTensorValueGetters(
DataViewRow input,
int[] inputColIndices,
bool[] isInputVector,
TF_DataType[] tfInputTypes,
TensorShape[] tfInputShapes)
{
var srcTensorGetters = new ITensorValueGetter[inputColIndices.Length];
for (int i = 0; i < inputColIndices.Length; i++)
{
int colIndex = inputColIndices[i];
srcTensorGetters[i] = CreateTensorValueGetter(input, tfInputTypes[i], isInputVector[i], colIndex, tfInputShapes[i]);
}
return srcTensorGetters;
}
// Factory method for SignatureLoadDataTransform.
private static IDataTransform Create(IHostEnvironment env, ModelLoadContext ctx, IDataView input)
=> Create(env, ctx).MakeDataTransform(input);
// Factory method for SignatureLoadRowMapper.
private static IRowMapper Create(IHostEnvironment env, ModelLoadContext ctx, DataViewSchema inputSchema)
=> Create(env, ctx).MakeRowMapper(inputSchema);
private static void GetModelInfo(IHostEnvironment env, ModelLoadContext ctx, out string[] inputs,
out string[] outputs, out bool isFrozen, out bool addBatchDimensionInput)
{
isFrozen = ctx.Reader.ReadBoolByte();
addBatchDimensionInput = ctx.Reader.ReadBoolByte();
var numInputs = ctx.Reader.ReadInt32();
env.CheckDecode(numInputs > 0);
inputs = new string[numInputs];
for (int j = 0; j < inputs.Length; j++)
inputs[j] = ctx.LoadNonEmptyString();
var numOutputs = ctx.Reader.ReadInt32();
env.CheckDecode(numOutputs > 0);
outputs = new string[numOutputs];
for (int j = 0; j < outputs.Length; j++)
outputs[j] = ctx.LoadNonEmptyString();
}
internal DnnRetrainTransformer(IHostEnvironment env, Session session, string[] outputColumnNames,
string[] inputColumnNames, string modelLocation, bool isTemporarySavedModel,
bool addBatchDimensionInput, int batchSize)
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(DnnRetrainTransformer)))
{
Host.CheckValue(session, nameof(session));
Host.CheckNonEmpty(inputColumnNames, nameof(inputColumnNames));
Host.CheckNonEmpty(outputColumnNames, nameof(outputColumnNames));
_env = env;
_session = session;
_modelLocation = Path.IsPathRooted(modelLocation) ? modelLocation : Path.Combine(Directory.GetCurrentDirectory(), modelLocation);
_isTemporarySavedModel = isTemporarySavedModel;
_addBatchDimensionInput = addBatchDimensionInput;
_inputs = inputColumnNames;
_outputs = outputColumnNames;
_idvToTfMapping = new Dictionary<string, string>();
foreach (var x in _inputs)
_idvToTfMapping[x] = x;
foreach (var x in _outputs)
_idvToTfMapping[x] = x;
(_tfOutputTypes, _outputTypes, _tfOutputOperations) = GetOutputInfo(Host, _session, _outputs);
(_tfInputTypes, _tfInputShapes, _tfInputOperations) = GetInputInfo(Host, _session, _inputs.Select(x => _idvToTfMapping[x]).ToArray(), batchSize);
_tfInputNodes = new TF_Output[_inputs.Length];
_tfOutputNodes = new TF_Output[_outputs.Length];
for (int index = 0; index < _tfInputOperations.Length; index += 1)
_tfInputNodes[index] = new TF_Output(_tfInputOperations[index].Item1, _tfInputOperations[index].Item2);
for (int index = 0; index < _tfOutputOperations.Length; index += 1)
_tfOutputNodes[index] = new TF_Output(_tfOutputOperations[index].Item1, _tfOutputOperations[index].Item2);
}
private static (Operation, int) GetOperationFromName(string operation, Session session)
{
var p = operation.IndexOf(':');
if (p != -1 && p != operation.Length - 1)
{
var op = operation.Substring(0, p);
if (int.TryParse(operation.Substring(p + 1), out var idx))
{
return (session.graph.OperationByName(op), idx);
}
}
return (session.graph.OperationByName(operation), 0);
}
internal static (TF_DataType[] tfInputTypes, TensorShape[] tfInputShapes, (Operation, int)[]) GetInputInfo(IHost host, Session session, string[] inputs, int batchSize = 1)
{
var tfInputTypes = new TF_DataType[inputs.Length];
var tfInputShapes = new TensorShape[inputs.Length];
var tfInputOperations = new (Operation, int)[inputs.Length];
int index = 0;
foreach (var input in inputs)
{
host.CheckNonWhiteSpace(input, nameof(inputs));
(Operation inputTensor, int inputTensorIndex) = GetOperationFromName(input, session);
if (inputTensor == null)
throw host.ExceptParam(nameof(inputs), $"Input column '{input}' does not exist in the model");
TF_DataType tfInputType = string.Compare(inputTensor.OpType, "PlaceHolder", true) == 0 ? inputTensor.OutputType(inputTensorIndex) : inputTensor.InputType(index);
if (!IsTypeSupported(tfInputType))
throw host.ExceptParam(nameof(session), $"Input type '{tfInputType}' of input column '{input}' is not supported in TensorFlow");
tfInputTypes[index] = tfInputType;
tfInputShapes[index] = ((Tensor)inputTensor).TensorShape;
tfInputOperations[index] = (inputTensor, inputTensorIndex);
index++;
}
return (tfInputTypes, tfInputShapes, tfInputOperations);
}
internal static TensorShape GetTensorShape(TF_Output output, Graph graph, Status status = null)
{
if (graph == IntPtr.Zero)
new ObjectDisposedException(nameof(graph));
var cstatus = status == null ? new Status() : status;
var n = c_api.TF_GraphGetTensorNumDims(graph, output, cstatus);
cstatus.Check();
if (n == -1)
return new TensorShape(new int[0]);
var dims = new long[n];
c_api.TF_GraphGetTensorShape(graph, output, dims, dims.Length, cstatus);
cstatus.Check();
return new TensorShape(dims.Select(x => (int)x).ToArray());
}
internal static (TF_DataType[] tfOutputTypes, DataViewType[] outputTypes, (Operation, int)[]) GetOutputInfo(IHost host, Session session, string[] outputs)
{
var tfOutputTypes = new TF_DataType[outputs.Length];
var outputTypes = new DataViewType[outputs.Length];
var newNames = new HashSet<string>();
var tfOutputOperations = new (Operation, int)[outputs.Length];
for (int i = 0; i < outputs.Length; i++)
{
host.CheckNonWhiteSpace(outputs[i], nameof(outputs));
if (!newNames.Add(outputs[i]))
throw host.ExceptParam(nameof(outputs), $"Output column '{outputs[i]}' specified multiple times");
(Tensor outputTensor, int outputIndex) = GetOperationFromName(outputs[i], session);
if (outputTensor == null)
throw host.ExceptParam(nameof(outputs), $"Output column '{outputs[i]}' does not exist in the model");
var tfOutputType = ((Operation)outputTensor).OutputType(outputIndex);
var shape = GetTensorShape(new TF_Output((Operation)outputTensor, outputIndex), session.graph);
// The transformer can only retrieve the output as fixed length vector with shape of kind [-1, d1, d2, d3, ...]
// i.e. the first dimension (if unknown) is assumed to be batch dimension.
// If there are other dimension that are unknown the transformer will return a variable length vector.
// This is the work around in absence of reshape transformer.
int[] dims = shape.ndim > 0 ? shape.dims.Skip(shape.dims[0] == -1 ? 1 : 0).ToArray() : new[] { 0 };
for (int j = 0; j < dims.Length; j++)
dims[j] = dims[j] == -1 ? 0 : dims[j];
if (dims == null || dims.Length == 0)
{
dims = new[] { 1 };
outputTypes[i] = Tf2MlNetType(tfOutputType);
}
else
{
var type = Tf2MlNetType(tfOutputType);
outputTypes[i] = new VectorDataViewType(type, dims);
}
tfOutputTypes[i] = tfOutputType;
tfOutputOperations[i] = (outputTensor, outputIndex);
}
return (tfOutputTypes, outputTypes, tfOutputOperations);
}
private protected override IRowMapper MakeRowMapper(DataViewSchema inputSchema) => new Mapper(this, inputSchema);
private protected override void SaveModel(ModelSaveContext ctx)
{
Host.AssertValue(ctx);
ctx.CheckAtModel();
ctx.SetVersionInfo(GetVersionInfo());
// *** Binary format ***
// byte: indicator for frozen models
// byte: indicator for adding batch dimension in input
// int: number of input columns
// for each input column
// int: id of int column name
// int: number of output columns
// for each output column
// int: id of output column name
// stream: tensorFlow model.
var isFrozen = !IsSavedModel(_env, _modelLocation);
ctx.Writer.WriteBoolByte(isFrozen);
ctx.Writer.WriteBoolByte(_addBatchDimensionInput);
Host.AssertNonEmpty(_inputs);
ctx.Writer.Write(_inputs.Length);
foreach (var colName in _inputs)
ctx.SaveNonEmptyString(colName);
Host.AssertNonEmpty(_outputs);
ctx.Writer.Write(_outputs.Length);
foreach (var colName in _outputs)
ctx.SaveNonEmptyString(colName);
ctx.SaveBinaryStream("TFSavedModel", w =>
{
// only these files need to be saved.
string[] modelFilePaths =
{
Path.Combine(_modelLocation, DefaultModelFileNames.Graph),
Path.Combine(_modelLocation, DefaultModelFileNames.VariablesFolder, DefaultModelFileNames.Data),
Path.Combine(_modelLocation, DefaultModelFileNames.VariablesFolder, DefaultModelFileNames.Index),
};
w.Write(modelFilePaths.Length);
foreach (var fullPath in modelFilePaths)
{
var relativePath = fullPath.Substring(_modelLocation.Length + 1);
w.Write(relativePath);
using (var fs = new FileStream(fullPath, FileMode.Open))
{
long fileLength = fs.Length;
w.Write(fileLength);
long actualWritten = fs.CopyRange(w.BaseStream, fileLength);
Host.Assert(actualWritten == fileLength);
}
}
});
}
public void Dispose()
{
if (_isDisposed)
return;
// Ensure that the Session is not null and it's handle is not Zero, as it may have already been disposed/finalized.
// Technically we shouldn't be calling this if disposing == false, since we're running in finalizer
// and the GC doesn't guarantee ordering of finalization of managed objects, but we have to make sure
// that the Session is closed before deleting our temporary directory.
try
{
if (_session != null && _session != IntPtr.Zero)
{
if (_session.graph != null)
_session.graph.Dispose();
_session.close();
}
}
finally
{
if (IsSavedModel(_env, _modelLocation) && _isTemporarySavedModel)
{
DeleteFolderWithRetries(Host, _modelLocation);
}
_isDisposed = true;
}
}
private sealed class Mapper : MapperBase
{
private readonly DnnRetrainTransformer _parent;
private readonly int[] _inputColIndices;
private readonly bool[] _isInputVector;
private readonly TensorShape[] _fullySpecifiedShapes;
private readonly ConcurrentBag<Runner> _runners;
public Mapper(DnnRetrainTransformer parent, DataViewSchema inputSchema) :
base(Contracts.CheckRef(parent, nameof(parent)).Host.Register(nameof(Mapper)), inputSchema, parent)
{
Host.CheckValue(parent, nameof(parent));
_parent = parent;
_inputColIndices = new int[_parent._inputs.Length];
_isInputVector = new bool[_parent._inputs.Length];
_fullySpecifiedShapes = new TensorShape[_parent._inputs.Length];
for (int i = 0; i < _parent._inputs.Length; i++)
{
if (!inputSchema.TryGetColumnIndex(_parent._inputs[i], out _inputColIndices[i]))
throw Host.ExceptSchemaMismatch(nameof(InputSchema), "source", _parent._inputs[i]);
var type = inputSchema[_inputColIndices[i]].Type;
if (type is VectorDataViewType vecType && vecType.Size == 0)
throw Host.Except("Variable length input columns not supported");
_isInputVector[i] = type is VectorDataViewType;
if (!_isInputVector[i])
throw Host.Except("Non-vector columns are not supported and should be loaded as vector columns of size 1");
vecType = (VectorDataViewType)type;
var expectedType = Tf2MlNetType(_parent._tfInputTypes[i]);
if (type.GetItemType() != expectedType)
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "input", _parent._inputs[i], expectedType.ToString(), type.ToString());
var originalShape = _parent._tfInputShapes[i];
var shape = originalShape.dims;
var colTypeDims = vecType.Dimensions.Select(dim => (int)dim).ToArray();
if (shape == null || (shape.Length == 0))
_fullySpecifiedShapes[i] = new TensorShape(colTypeDims);
else
{
// If the column is one dimension we make sure that the total size of the TF shape matches.
// Compute the total size of the known dimensions of the shape.
int valCount = 1;
int numOfUnkDim = 0;
foreach (var s in shape)
{
if (s > 0)
valCount *= s;
else
numOfUnkDim++;
}
// The column length should be divisible by this, so that the other dimensions can be integral.
int typeValueCount = type.GetValueCount();
if (typeValueCount % valCount != 0)
throw Contracts.Except($"Input shape mismatch: Input '{_parent._inputs[i]}' has shape {originalShape.ToString()}, but input data is of length {typeValueCount}.");
// If the shape is multi-dimensional, we should be able to create the length of the vector by plugging
// in a single value for the unknown shapes. For example, if the shape is [?,?,3], then there should exist a value
// d such that d*d*3 is equal to the length of the input column.
var d = numOfUnkDim > 0 ? Math.Pow(typeValueCount / valCount, 1.0 / numOfUnkDim) : 0;
if (d - (int)d != 0)
throw Contracts.Except($"Input shape mismatch: Input '{_parent._inputs[i]}' has shape {originalShape.ToString()}, but input data is of length {typeValueCount}.");
// Fill in the unknown dimensions.
var originalShapeDims = originalShape.dims;
var originalShapeNdim = originalShape.ndim;
var l = new int[originalShapeNdim];
for (int ishape = 0; ishape < originalShapeNdim; ishape++)
l[ishape] = originalShapeDims[ishape] == -1 ? (int)d : originalShapeDims[ishape];
_fullySpecifiedShapes[i] = new TensorShape(l);
}
if (_parent._addBatchDimensionInput)
{
var l = new int[_fullySpecifiedShapes[i].ndim + 1];
l[0] = 1;
for (int ishape = 1; ishape < l.Length; ishape++)
l[ishape] = _fullySpecifiedShapes[i].dims[ishape - 1];
_fullySpecifiedShapes[i] = new TensorShape(l);
}
}
_runners = new ConcurrentBag<Runner>();
}
private protected override void SaveModel(ModelSaveContext ctx) => _parent.SaveModel(ctx);
private class OutputCache
{
public long Position;
public Dictionary<string, Tensor> Outputs;
public OutputCache()
{
Position = -1;
Outputs = new Dictionary<string, Tensor>();
}
}
protected override Delegate MakeGetter(DataViewRow input, int iinfo, Func<int, bool> activeOutput, out Action disposer)
{
disposer = null;
Host.AssertValue(input);
var outputCache = new OutputCache();
var activeOutputColNames = _parent._outputs.Where((x, i) => activeOutput(i)).ToArray();
var type = Tf2MlNetType(_parent._tfOutputTypes[iinfo]).RawType;
Host.Assert(type == _parent._outputTypes[iinfo].GetItemType().RawType);
var srcTensorGetters = GetTensorValueGetters(input, _inputColIndices, _isInputVector, _parent._tfInputTypes, _fullySpecifiedShapes);
return Utils.MarshalInvoke(MakeGetter<int>, type, input, iinfo, srcTensorGetters, activeOutputColNames, outputCache);
}
private Delegate MakeGetter<T>(DataViewRow input, int iinfo, ITensorValueGetter[] srcTensorGetters, string[] activeOutputColNames, OutputCache outputCache) where T : unmanaged
{
Host.AssertValue(input);
if (_parent._outputTypes[iinfo].IsStandardScalar())
{
ValueGetter<T> valuegetter = (ref T dst) =>
{
UpdateCacheIfNeeded(input.Position, srcTensorGetters, activeOutputColNames, outputCache);
var tensor = outputCache.Outputs[_parent._outputs[iinfo]];
tensor.ToScalar<T>(ref dst);
};
return valuegetter;
}
else
{
if (_parent._tfOutputTypes[iinfo] == TF_DataType.TF_STRING)
{
ValueGetter<VBuffer<T>> valuegetter = (ref VBuffer<T> dst) =>
{
UpdateCacheIfNeeded(input.Position, srcTensorGetters, activeOutputColNames, outputCache);
var tensor = outputCache.Outputs[_parent._outputs[iinfo]];
var tensorSize = tensor.TensorShape.dims.Where(x => x > 0).Aggregate((x, y) => x * y);
var editor = VBufferEditor.Create(ref dst, (int)tensorSize);
FetchStringData(tensor, editor.Values);
dst = editor.Commit();
};
return valuegetter;
}
else
{
ValueGetter<VBuffer<T>> valuegetter = (ref VBuffer<T> dst) =>
{
UpdateCacheIfNeeded(input.Position, srcTensorGetters, activeOutputColNames, outputCache);
var tensor = outputCache.Outputs[_parent._outputs[iinfo]];
var tensorSize = tensor.TensorShape.dims.Where(x => x > 0).Aggregate((x, y) => x * y);
var editor = VBufferEditor.Create(ref dst, (int)tensorSize);
tensor.CopyTo<T>(editor.Values);
dst = editor.Commit();
};
return valuegetter;
}
}
}
private void UpdateCacheIfNeeded(long position, ITensorValueGetter[] srcTensorGetters, string[] activeOutputColNames, OutputCache outputCache)
{
if (outputCache.Position != position)
{
if (_parent.Graph.graph_key != tf.get_default_graph().graph_key)
_parent._session.graph.as_default();
Runner runner = new Runner(_parent._session,
_parent._inputs.Select(x => _parent._idvToTfMapping[x]).ToArray(),
_parent._outputs.Select(x => _parent._idvToTfMapping[x]).ToArray());
// Feed the inputs.
for (int i = 0; i < _parent._inputs.Length; i++)
runner.AddInput(srcTensorGetters[i].GetTensor(), 0);
// Execute the graph.
var tensors = runner.Run();
Contracts.Assert(tensors.Length > 0);
for (int j = 0; j < activeOutputColNames.Length; j++)
outputCache.Outputs[activeOutputColNames[j]] = tensors[j];
outputCache.Position = position;
}
}
private protected override Func<int, bool> GetDependenciesCore(Func<int, bool> activeOutput)
{
return col => Enumerable.Range(0, _parent._outputs.Length).Any(i => activeOutput(i)) && _inputColIndices.Any(i => i == col);
}
protected override DataViewSchema.DetachedColumn[] GetOutputColumnsCore()
{
var info = new DataViewSchema.DetachedColumn[_parent._outputs.Length];
for (int i = 0; i < _parent._outputs.Length; i++)
info[i] = new DataViewSchema.DetachedColumn(_parent._outputs[i], _parent._outputTypes[i], null);
return info;
}
}
private interface ITensorValueGetter
{
Tensor GetTensor();
void BufferTrainingData();
Tensor GetBufferedBatchTensor();
}
private class TensorValueGetter<T> : ITensorValueGetter
{
private readonly ValueGetter<T> _srcgetter;
private readonly T[] _bufferedData;
private readonly Int64[] _bufferedDataLong;
private readonly TensorShape _tfShape;
private int _position;
private readonly bool _keyType;
private long[] _dims;
public TensorValueGetter(DataViewRow input, int colIndex, TensorShape tfShape, bool keyType = false)
{
_srcgetter = input.GetGetter<T>(input.Schema[colIndex]);
_tfShape = tfShape;
long size = 0;
_position = 0;
if (tfShape.dims.Length != 0)
{
size = 1;
foreach (var dim in tfShape.dims)
size *= dim;
_dims = _tfShape.dims.Select(x => (long)x).ToArray();
}
if (keyType)
_bufferedDataLong = new long[size];
else
_bufferedData = new T[size];
_keyType = keyType;
}
public Tensor GetTensor()
{