-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
CNTKLibraryCPPEvalExamples.cpp
737 lines (623 loc) · 32.2 KB
/
CNTKLibraryCPPEvalExamples.cpp
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
// CNTKLibraryCPPEvalExamples.cpp : Sample application shows how to evaluate a model using CNTK V2 API.
//
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS // "secure" CRT not available on all platforms.
#endif
#include <thread>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "CNTKLibrary.h"
using namespace CNTK;
template <typename ElementType>
void PrintOutput(size_t, std::vector<std::vector<ElementType>>);
/// <summary>
/// The example shows
/// - how to load model.
/// - how to prepare input data for a single sample.
/// - how to prepare input and output data map.
/// - how to evaluate a model.
/// - how to retrieve evaluation result and retrieve output data in dense format.
/// Note: The example uses the model trained by <CNTK>/Examples/Image/Classification/ResNet/Python/TrainResNet_CIFAR10.py
/// Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
/// The parameter 'modelFile' specifies the path to the model.
/// </summary>
void EvaluationSingleSampleUsingDense(const wchar_t* modelFile, const DeviceDescriptor& device)
{
printf("\n===== Evaluate single sample using dense format.\n");
// Load the model.
// The model is trained by <CNTK>/Examples/Image/Classification/ResNet/Python/TrainResNet_CIFAR10.py
// Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
FunctionPtr modelFunc = Function::Load(modelFile, device);
// Get input variable. The model has only one single input.
Variable inputVar = modelFunc->Arguments()[0];
// The model has only one output.
// If the model has more than one output, use modelFunc->Outputs to get the list of output variables.
Variable outputVar = modelFunc->Output();
// Prepare input data.
// For evaluating an image, you first need to perform some image preprocessing to make sure that the input image has the correct size and layout
// that match the model inputs.
// Please note that the model used by this example expects the CHW image layout.
// inputVar.Shape[0] is image width, inputVar.Shape[1] is image height, and inputVar.Shape[2] is channels.
// For simplicity and avoiding external dependencies, we skip the preprocessing step here, and just use some artificially created data as input.
std::vector<float> inputData(inputVar.Shape().TotalSize());
for (size_t i = 0; i < inputData.size(); ++i)
{
inputData[i] = static_cast<float>(i % 255);
}
// Create input value and input data map
ValuePtr inputVal = Value::CreateBatch(inputVar.Shape(), inputData, device);
std::unordered_map<Variable, ValuePtr> inputDataMap = { { inputVar, inputVal } };
// Create output data map. Using null as Value to indicate using system allocated memory.
// Alternatively, create a Value object and add it to the data map.
std::unordered_map<Variable, ValuePtr> outputDataMap = { { outputVar, nullptr } };
// Start evaluation on the device
modelFunc->Evaluate(inputDataMap, outputDataMap, device);
// Get evaluate result as dense output
ValuePtr outputVal = outputDataMap[outputVar];
std::vector<std::vector<float>> outputData;
outputVal->CopyVariableValueTo(outputVar, outputData);
PrintOutput<float>(outputVar.Shape().TotalSize(), outputData);
}
/// <summary>
/// The example shows
/// - how to load model.
/// - how to prepare input data for a batch of samples.
/// - how to prepare input and output data map.
/// - how to evaluate a model.
/// - how to retrieve evaluation result and retrieve output data in dense format.
/// Note: The example uses the model trained by <CNTK>/Examples/Image/Classification/ResNet/Python/TrainResNet_CIFAR10.py
/// Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
/// The parameter 'modelFile' specifies the path to the model.
/// </summary>
void EvaluationBatchUsingDense(const wchar_t* modelFile, const DeviceDescriptor& device)
{
printf("\n===== Evaluate batch of samples using dense format.\n");
// The number of samples in the batch.
size_t sampleCount = 3;
// Load the model.
// The model is trained by <CNTK>/Examples/Image/Classification/ResNet/Python/TrainResNet_CIFAR10.py
// Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
FunctionPtr modelFunc = Function::Load(modelFile, device);
// Get input variable. The model has only one single input.
Variable inputVar = modelFunc->Arguments()[0];
// The model has only one output.
// If the model has more than one output, use modelFunc->Outputs to get the list of output variables.
Variable outputVar = modelFunc->Output();
// Prepare input data.
// For evaluating an image, you first need to perform some image preprocessing to make sure that the input image has the correct size and layout
// that match the model inputs.
// Please note that the model used by this example expects the CHW image layout.
// inputVar.Shape[0] is image width, inputVar.Shape[1] is image height, and inputVar.Shape[2] is channels.
// For simplicity and avoiding external dependencies, we skip the preprocessing step here, and just use some artificially created data as input.
std::vector<float> inputData(inputVar.Shape().TotalSize() * sampleCount);
for (size_t i = 0; i < inputData.size(); ++i)
{
inputData[i] = static_cast<float>(i % 255);
}
// Create input value and input data map.
ValuePtr inputVal = Value::CreateBatch(inputVar.Shape(), inputData, device);
std::unordered_map<Variable, ValuePtr> inputDataMap = { { inputVar, inputVal } };
// Create output data map. Using null as Value to indicate using system allocated memory.
// Alternatively, create a Value object and add it to the data map.
std::unordered_map<Variable, ValuePtr> outputDataMap = { { outputVar, nullptr } };
// Start evaluation on the device
modelFunc->Evaluate(inputDataMap, outputDataMap, device);
// Get evaluate result as dense output
ValuePtr outputVal = outputDataMap[outputVar];
std::vector<std::vector<float>> outputData;
outputVal->CopyVariableValueTo(outputVar, outputData);
PrintOutput<float>(outputVar.Shape().TotalSize(), outputData);
}
void RunEvaluationOnSingleSample(FunctionPtr, const DeviceDescriptor&);
/// <summary>
/// The example shows
/// - how to evaluate multiple sample requests in parallel.
/// Note: The example uses the model trained by <CNTK>/Examples/Image/Classification/ResNet/Python/TrainResNet_CIFAR10.py
/// Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
/// The parameter 'modelFile' specifies the path to the model.
/// </summary>
void ParallelEvaluationExample(const wchar_t* modelFile, const DeviceDescriptor& device)
{
printf("\n===== Evaluate multiple requests in parallel.\n");
size_t threadCount = 3;
// Load the model.
// The model is trained by <CNTK>/Examples/Image/Classification/ResNet/Python/TrainResNet_CIFAR10.py
// Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
FunctionPtr modelFunc = Function::Load(modelFile, device);
// Run evaluation in parallel.
std::vector<std::thread> threadList(threadCount);
for (int th = 0; th < threadCount; ++th)
{
threadList[th] = std::thread(RunEvaluationOnSingleSample, modelFunc->Clone(ParameterCloningMethod::Share), device);
}
for (int th = 0; th < threadCount; ++th)
{
threadList[th].join();
printf("thread %d joined.\n", th);
}
}
void RunEvaluationOnSingleSample(FunctionPtr evalInstance, const DeviceDescriptor& device)
{
// Get input variable. The model has only one single input.
Variable inputVar = evalInstance->Arguments()[0];
// The model has only one output.
// If the model has more than one output, use modelFunc->Outputs to get the list of output variables.
Variable outputVar = evalInstance->Output();
// Prepare input data.
// For evaluating an image, you first need to perform some image preprocessing to make sure that the input image has the correct size and layout
// that match the model inputs.
// Please note that the model used by this example expects the CHW image layout.
// inputVar.Shape[0] is image width, inputVar.Shape[1] is image height, and inputVar.Shape[2] is channels.
// For simplicity and avoiding external dependencies, we skip the preprocessing step here, and just use some artificially created data as input.
std::vector<float> inputData(inputVar.Shape().TotalSize());
for (size_t i = 0; i < inputData.size(); ++i)
{
inputData[i] = static_cast<float>(i % 255);
}
// Create input value and input data map
ValuePtr inputVal = Value::CreateBatch(inputVar.Shape(), inputData, device);
std::unordered_map<Variable, ValuePtr> inputDataMap = { { inputVar, inputVal } };
// Create output data map. Using null as Value to indicate using system allocated memory.
// Alternatively, create a Value object and add it to the data map.
std::unordered_map<Variable, ValuePtr> outputDataMap = { { outputVar, nullptr } };
// Start evaluation on the device
evalInstance->Evaluate(inputDataMap, outputDataMap, device);
// Get evaluate result as dense output
ValuePtr outputVal = outputDataMap[outputVar];
std::vector<std::vector<float>> outputData;
outputVal->CopyVariableValueTo(outputVar, outputData);
}
std::unordered_map<std::string, size_t> BuildVocabIndex(const wchar_t*);
std::vector<std::string> BuildSlotIndex(const wchar_t*);
/// <summary>
/// The example shows
/// - how to load model.
/// - how to prepare input data as sequence using one-hot vector.
/// - how to prepare input and output data map.
/// - how to evaluate a model.
/// - how to retrieve evaluation result.
/// The examples uses the model trained by <CNTK>/Examples/LanguageUnderstanding/ATIS/Python/LanguageUnderstanding.py
/// Please see README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS about how to train the model.
/// The parameter 'modelFile' specifies the path to the model.
/// The vocabularyFile specifies the vacabulary file used by the ATIS model, e.g. <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/query.wl
/// The labelFile specifies the label file used by the ATIS model, e.g. <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/slots.wl
/// </summary>
/// <param name="device">Specify on which device to run the evaluation</param>
void EvaluationSingleSequenceUsingOneHot(const wchar_t* modelFile, const wchar_t* vocabularyFile, const wchar_t* labelFile, const DeviceDescriptor& device)
{
printf("\n===== Evaluate single sequence using one-hot vector.\n");
// Load the model.
// The model is trained by <CNTK>/Examples/LanguageUnderstanding/ATIS/Python/LanguageUnderstanding.py
// Please see README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS about how to train the model.
FunctionPtr modelFunc = Function::Load(modelFile, device);
// Read word and slot index files.
std::unordered_map<std::string, size_t> vocabToIndex = BuildVocabIndex(vocabularyFile);
std::vector<std::string> indexToSlots = BuildSlotIndex(labelFile);
// Get input variable. The model has only one single input.
Variable inputVar = modelFunc->Arguments()[0];
size_t vocabSize = inputVar.Shape().TotalSize();
const char *inputSentence = "BOS i would like to find a flight from charlotte to las vegas that makes a stop in st. louis EOS";
std::vector<size_t> seqData;
std::vector<std::string> inputWords;
std::stringstream inputStream;
std::string word;
size_t index;
// build one-hot index for the input sequence.
inputStream.str(inputSentence);
while (inputStream >> word)
{
inputWords.push_back(word);
index = vocabToIndex.at(word);
seqData.push_back(index);
}
// SeqStartFlag is used to indicate whether this sequence is a new sequence (true) or concatenating the previous sequence (false).
bool seqStartFlag = true;
// Create input value using one-hot vector and input data map
ValuePtr inputVal = Value::CreateSequence<float>(vocabSize, seqData, seqStartFlag, device);
std::unordered_map<Variable, ValuePtr> inputDataMap = { { inputVar, inputVal } };
// The model has only one output.
// If the model has more than one output, use modelFunc->Outputs to get the list of output variables.
Variable outputVar = modelFunc->Output();
// Create output data map. Using null as Value to indicate using system allocated memory.
// Alternatively, create a Value object and add it to the data map.
std::unordered_map<Variable, ValuePtr> outputDataMap = { { outputVar, nullptr } };
// Start evaluation on the device
modelFunc->Evaluate(inputDataMap, outputDataMap, device);
// Get evaluate result as dense output
ValuePtr outputVal = outputDataMap[outputVar];
std::vector<std::vector<float>> outputData;
outputVal->CopyVariableValueTo(outputVar, outputData);
// output the result
size_t outputSampleSize = outputVar.Shape().TotalSize();
if (outputData.size() != 1)
{
throw("Only one sequence of slots is expected as output.");
}
std::vector<float> slotSeq = outputData[0];
if (slotSeq.size() % outputSampleSize != 0)
{
throw("The number of elements in the slot sequence is not a multiple of sample size");
}
size_t numOfSlotsInOutput = slotSeq.size() / outputSampleSize;
if (inputWords.size() != numOfSlotsInOutput)
{
throw("The number of input words and the number of output slots do not match");
}
for (size_t i = 0; i < numOfSlotsInOutput; i++)
{
float max = slotSeq[i * outputSampleSize];
size_t maxIndex = 0;
for (size_t j = 1; j < outputSampleSize; j++)
{
if (slotSeq[i * outputSampleSize + j] > max)
{
max = slotSeq[i * outputSampleSize + j];
maxIndex = j;
}
}
printf(" %10s ---- %s\n", inputWords[i].c_str(), indexToSlots[maxIndex].c_str());
}
printf("\n");
}
/// <summary>
/// The example shows
/// - how to load model.
/// - how to prepare input data as batch of sequences with variable length.
/// how to prepare data using one-hot vector format.
/// - how to prepare input and output data map.
/// - how to evaluate a model.
/// The example uses the model trained by <CNTK>/Examples/LanguageUnderstanding/ATIS/Python/LanguageUnderstanding.py
/// Please see README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS about how to train the model.
/// The parameter 'modelFile' specifies the path to the model.
/// The vocabularyFile specifies the vacabulary file used by the ATIS model, e.g. <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/query.wl
/// The labelFile specifies the label file used by the ATIS model, e.g. <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/slots.wl
/// </summary>
void EvaluationBatchOfSequencesUsingOneHot(const wchar_t* modelFile, const wchar_t* vocabularyFile, const wchar_t* labelFile, const DeviceDescriptor& device)
{
printf("\n===== Evaluate batch of sequences with variable length using one-hot vector.\n");
// Load the model.
// The model is trained by <CNTK>/Examples/LanguageUnderstanding/ATIS/Python/LanguageUnderstanding.py
// Please see README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS about how to train the model.
FunctionPtr modelFunc = Function::Load(modelFile, device);
// Read word and slot index files.
std::unordered_map<std::string, size_t> vocabToIndex = BuildVocabIndex(vocabularyFile);
std::vector<std::string> indexToSlots = BuildSlotIndex(labelFile);
// Get input variable. The model has only one single input.
Variable inputVar = modelFunc->Arguments()[0];
size_t vocabSize = inputVar.Shape().TotalSize();
std::vector<const char *> inputSentences = {
"BOS i would like to find a flight from charlotte to las vegas that makes a stop in st. louis EOS",
"BOS flights from new york to seattle EOS"
};
// Prepare input data.
std::vector<std::vector<std::string>> inputWordsList(inputSentences.size());
// Each sample is represented by an index to the one-hot vector, so the index of the non-zero value of each sample is saved in the inner list.
// The outer list represents sequences contained in the batch.
std::vector<std::vector<size_t>> inputBatch;
// SeqStartFlagBatch is used to indicate whether this sequence is a new sequence (true) or concatenating the previous sequence (false).
std::vector<bool> seqStartFlagBatch;
std::string word;
size_t index;
for (size_t seqIndex = 0; seqIndex < inputSentences.size(); seqIndex++)
{
std::stringstream inputStream;
std::vector<size_t> seqData;
// build one-hot index for the input sequences.
inputStream.str(inputSentences[seqIndex]);
while (inputStream >> word)
{
inputWordsList[seqIndex].push_back(word);
index = vocabToIndex.at(word);
seqData.push_back(index);
}
inputBatch.push_back(seqData);
seqStartFlagBatch.push_back(true);
}
// Create input value representing the batch data and input data map
ValuePtr inputVal = Value::CreateBatchOfSequences<float>(vocabSize, inputBatch, seqStartFlagBatch, device);
std::unordered_map<Variable, ValuePtr> inputDataMap = { { inputVar, inputVal } };
// The model has only one output.
// If the model has more than one output, use modelFunc->Outputs to get the list of output variables.
Variable outputVar = modelFunc->Output();
// Create output data map. Using null as Value to indicate using system allocated memory.
// Alternatively, create a Value object and add it to the data map.
std::unordered_map<Variable, ValuePtr> outputDataMap = { { outputVar, nullptr } };
// Start evaluation on the device
modelFunc->Evaluate(inputDataMap, outputDataMap, device);
// Get evaluate result as dense output
ValuePtr outputVal = outputDataMap[outputVar];
std::vector<std::vector<float>> outputData;
outputVal->CopyVariableValueTo(outputVar, outputData);
// output the result
size_t outputSampleSize = outputVar.Shape().TotalSize();
if (outputData.size() != inputBatch.size())
{
throw("The number of sequence in output does not match that in input.");
}
printf("The number of sequences in the batch: %d\n", (int)outputData.size());
for (size_t seqno = 0; seqno < outputData.size(); seqno++)
{
std::vector<float> slotSeq = outputData[seqno];
printf("Sequence %d:\n", (int)seqno);
if (slotSeq.size() % outputSampleSize != 0)
{
throw("The number of elements in the slot sequence is not a multiple of sample size");
}
size_t numOfSlotsInOutput = slotSeq.size() / outputSampleSize;
if (inputWordsList[seqno].size() != numOfSlotsInOutput)
{
throw("The number of input words and the number of output slots do not match");
}
for (size_t i = 0; i < numOfSlotsInOutput; i++)
{
float max = slotSeq[i * outputSampleSize];
size_t maxIndex = 0;
for (size_t j = 1; j < outputSampleSize; j++)
{
if (slotSeq[i * outputSampleSize + j] > max)
{
max = slotSeq[i * outputSampleSize + j];
maxIndex = j;
}
}
printf(" %10s ---- %s\n", inputWordsList[seqno][i].c_str(), indexToSlots[maxIndex].c_str());
}
printf("\n");
}
}
/// <summary>
/// The example shows
/// - how to prepare input data as sequence using sparse input.
/// The example uses the model trained by <CNTK>/Examples/LanguageUnderstanding/ATIS/Python/LanguageUnderstanding.py
/// Please see README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS about how to train the model.
/// The parameter 'modelFile' specifies the path to the model.
/// The vocabularyFile specifies the vacabulary file used by the ATIS model, e.g. <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/query.wl
/// The labelFile specifies the label file used by the ATIS model, e.g. <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/slots.wl
/// </summary>
void EvaluationSingleSequenceUsingSparse(const wchar_t* modelFile, const wchar_t* vocabularyFile, const wchar_t* labelFile, const DeviceDescriptor& device)
{
printf("\n===== Evaluate single sequence using sparse input.\n");
// Load the model.
// The model is trained by <CNTK>/Examples/LanguageUnderstanding/ATIS/Python/LanguageUnderstanding.py
// Please see README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS about how to train the model.
FunctionPtr modelFunc = Function::Load(modelFile, device);
// Read word and slot index files.
std::unordered_map<std::string, size_t> vocabToIndex = BuildVocabIndex(vocabularyFile);
std::vector<std::string> indexToSlots = BuildSlotIndex(labelFile);
// Get input variable. The model has only one single input.
Variable inputVar = modelFunc->Arguments()[0];
size_t vocabSize = inputVar.Shape().TotalSize();
const char *inputSentence = "BOS i would like to find a flight from charlotte to las vegas that makes a stop in st. louis EOS";
std::vector<size_t> seqData;
std::vector<std::string> inputWords;
std::stringstream inputStream;
std::string word;
// build one-hot index for the input sequence.
inputStream.str(inputSentence);
while (inputStream >> word)
{
inputWords.push_back(word);
}
size_t seqLen = inputWords.size();
// For this example, only 1 non-zero value for each sample.
size_t numNonZeroValues = seqLen * 1;
std::vector<SparseIndexType> colStarts;
std::vector<SparseIndexType> rowIndices;
std::vector<float> nonZeroValues;
size_t count = 0;
for (; count < seqLen; count++)
{
// Get the index of the word
auto nonZeroValueIndex = static_cast<SparseIndexType>(vocabToIndex[inputWords[count]]);
// Add the sample to the sequence
nonZeroValues.push_back(1.0);
rowIndices.push_back(nonZeroValueIndex);
colStarts.push_back(static_cast<SparseIndexType>(count));
}
colStarts.push_back(static_cast<SparseIndexType>(numNonZeroValues));
// Create input value using one-hot vector and input data map
ValuePtr inputVal = Value::CreateSequence<float>(vocabSize, seqLen, colStarts.data(), rowIndices.data(), nonZeroValues.data(), numNonZeroValues, device);
std::unordered_map<Variable, ValuePtr> inputDataMap = { { inputVar, inputVal } };
// The model has only one output.
// If the model has more than one output, use modelFunc->Outputs to get the list of output variables.
Variable outputVar = modelFunc->Output();
// Create output data map. Using null as Value to indicate using system allocated memory.
// Alternatively, create a Value object and add it to the data map.
std::unordered_map<Variable, ValuePtr> outputDataMap = { { outputVar, nullptr } };
// Start evaluation on the device
modelFunc->Evaluate(inputDataMap, outputDataMap, device);
// Get evaluate result as dense output
ValuePtr outputVal = outputDataMap[outputVar];
std::vector<std::vector<float>> outputData;
outputVal->CopyVariableValueTo(outputVar, outputData);
// output the result
size_t outputSampleSize = outputVar.Shape().TotalSize();
if (outputData.size() != 1)
{
throw("Only one sequence of slots is expected as output.");
}
std::vector<float> slotSeq = outputData[0];
if (slotSeq.size() % outputSampleSize != 0)
{
throw("The number of elements in the slot sequence is not a multiple of sample size");
}
size_t numOfSlotsInOutput = slotSeq.size() / outputSampleSize;
if (inputWords.size() != numOfSlotsInOutput)
{
throw("The number of input words and the number of output slots do not match");
}
for (size_t i = 0; i < numOfSlotsInOutput; i++)
{
float max = slotSeq[i * outputSampleSize];
size_t maxIndex = 0;
for (size_t j = 1; j < outputSampleSize; j++)
{
if (slotSeq[i * outputSampleSize + j] > max)
{
max = slotSeq[i * outputSampleSize + j];
maxIndex = j;
}
}
printf(" %10s ---- %s\n", inputWords[i].c_str(), indexToSlots[maxIndex].c_str());
}
printf("\n");
}
/// <summary>
/// The example shows
/// - how to load a pretrained model and evaluate an intermediate layer of its network.
/// Note: The example uses the model trained by <CNTK>/Examples/Image/Classification/ResNet/Python/TrainResNet_CIFAR10.py
/// Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
/// The parameter 'modelFilePath' specifies the path to the model.
/// </summary>
void EvaluateIntermediateLayer(const wchar_t* modelFilePath, const DeviceDescriptor& device)
{
printf("\n===== Evaluate intermediate layer =====\n");
// Load the model.
FunctionPtr rootFunc = Function::Load(modelFilePath, device);
std::wstring intermediateLayerName = L"final_avg_pooling";
FunctionPtr interLayerPrimitiveFunc = rootFunc->FindByName(intermediateLayerName);
// The Function returned by FindByName is a primitive function.
// For evaluation, it is required to create a composite function from the primitive function.
FunctionPtr modelFunc = AsComposite(interLayerPrimitiveFunc);
Variable outputVar = modelFunc->Output();
Variable inputVar = modelFunc->Arguments()[0];
// Prepare input data.
// For evaluating an image, you first need to perform some image preprocessing to make sure that the input image has the correct size and layout
// that match the model inputs.
// Please note that the model used by this example expects the CHW image layout.
// inputVar.Shape[0] is image width, inputVar.Shape[1] is image height, and inputVar.Shape[2] is channels.
// For simplicity and avoiding external dependencies, we skip the preprocessing step here, and just use some artificially created data as input.
std::vector<float> inputData(inputVar.Shape().TotalSize());
for (size_t i = 0; i < inputData.size(); ++i)
{
inputData[i] = static_cast<float>(i % 255);
}
// Create input value and input data map
ValuePtr inputVal = Value::CreateBatch(inputVar.Shape(), inputData, device);
std::unordered_map<Variable, ValuePtr> inputDataMap = { { inputVar, inputVal } };
// Create output data map. Using null as Value to indicate using system allocated memory.
// Alternatively, create a Value object and add it to the data map.
std::unordered_map<Variable, ValuePtr> outputDataMap = { { outputVar, nullptr } };
// Start evaluation on the device
modelFunc->Evaluate(inputDataMap, outputDataMap, device);
// Get evaluate result as dense output
ValuePtr outputVal = outputDataMap[outputVar];
std::vector<std::vector<float>> outputData;
outputVal->CopyVariableValueTo(outputVar, outputData);
PrintOutput<float>(outputVar.Shape().TotalSize(), outputData);
}
/// <summary>
/// The example shows
/// - how to load a pretrained model and evaluate several nodes by combining their outputs
/// Note: The example uses the model trained by <CNTK>/Examples/Image/Classification/ResNet/Python/TrainResNet_CIFAR10.py
/// Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
/// The parameter 'modelFilePath' specifies the path to the model.
/// </summary>
void EvaluateCombinedOutputs(const wchar_t* modelFilePath, const DeviceDescriptor& device)
{
printf("\n===== Evaluate combined outputs =====\n");
// Load the model.
FunctionPtr modelFunc = Function::Load(modelFilePath, device);
// Get node of interest
std::wstring intermediateLayerName = L"final_avg_pooling";
FunctionPtr interLayerPrimitiveFunc = modelFunc->FindByName(intermediateLayerName);
Variable poolingOutput = interLayerPrimitiveFunc->Output();
// Create a function which combine outputs from the node "final_avg_polling" and the final layer of the model.
FunctionPtr evalFunc = Combine( { modelFunc->Output(), poolingOutput });
Variable inputVar = evalFunc->Arguments()[0];
// Prepare input data.
// For evaluating an image, you first need to perform some image preprocessing to make sure that the input image has the correct size and layout
// that match the model inputs.
// Please note that the model used by this example expects the CHW image layout.
// inputVar.Shape[0] is image width, inputVar.Shape[1] is image height, and inputVar.Shape[2] is channels.
// For simplicity and avoiding external dependencies, we skip the preprocessing step here, and just use some artificially created data as input.
std::vector<float> inputData(inputVar.Shape().TotalSize());
for (size_t i = 0; i < inputData.size(); ++i)
{
inputData[i] = static_cast<float>(i % 255);
}
// Create input value and input data map
ValuePtr inputVal = Value::CreateBatch(inputVar.Shape(), inputData, device);
std::unordered_map<Variable, ValuePtr> inputDataMap = { { inputVar, inputVal } };
// Create output data map. Using null as Value to indicate using system allocated memory.
// Alternatively, create a Value object and add it to the data map.
Variable modelOutput = evalFunc->Outputs()[0];
Variable interLayerOutput = evalFunc->Outputs()[1];
std::unordered_map<Variable, ValuePtr> outputDataMap = { { modelOutput, nullptr }, { interLayerOutput, nullptr } };
// Start evaluation on the device
evalFunc->Evaluate(inputDataMap, outputDataMap, device);
// Get evaluate result as dense outputs
for(auto & outputVariableValuePair : outputDataMap)
{
auto variable = outputVariableValuePair.first;
auto value = outputVariableValuePair.second;
std::vector<std::vector<float>> outputData;
value->CopyVariableValueTo(variable, outputData);
PrintOutput<float>(variable.Shape().TotalSize(), outputData);
}
}
std::shared_ptr<std::fstream> GetIfstream(const wchar_t *filePath)
{
const size_t pathBufferLen = 1024;
char pathBuffer[pathBufferLen];
size_t writtenBytes = ::wcstombs(pathBuffer, filePath, pathBufferLen);
if (writtenBytes == (size_t)-1)
throw ("Unknown characters in the file path.");
else if (writtenBytes == pathBufferLen)
throw("The file path is too long");
return std::make_shared<std::fstream>(pathBuffer);
}
std::unordered_map<std::string, size_t> BuildVocabIndex(const wchar_t *filePath)
{
std::unordered_map<std::string, size_t> vocab;
std::string str;
size_t idx = 0;
std::shared_ptr<std::fstream> input = GetIfstream(filePath);
while (*input >> str)
vocab[str] = idx++;
return vocab;
}
std::vector<std::string> BuildSlotIndex(const wchar_t *filePath)
{
std::shared_ptr<std::fstream> input = GetIfstream(filePath);
std::vector<std::string> slots;
std::string str;
while (*input >> str)
slots.push_back(str);
return slots;
}
/// <summary>
/// Print out the evalaution results.
/// </summary>
template <typename ElementType>
void PrintOutput(size_t sampleSize, std::vector<std::vector<ElementType>> outputBuffer)
{
printf("The batch contains %d sequences.\n", (int)outputBuffer.size());
for(size_t seqNo = 0; seqNo < outputBuffer.size(); seqNo++)
{
auto seq = outputBuffer[seqNo];
if (seq.size() % sampleSize != 0)
throw("The number of elements in the sequence is not a multiple of sample size");
printf("Sequence %d contains %d samples.\n", (int)seqNo, (int)(seq.size() / sampleSize));
size_t sampleNo = 0;
for(size_t i = 0; i < seq.size(); )
{
if (i % sampleSize == 0)
printf(" sample %d: ", (int)sampleNo);
printf("%f", seq[i++]);
if (i % sampleSize == 0)
{
printf(".\n");
sampleNo++;
}
else
printf(", ");
}
}
}