classify_image.cu 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  3. * Full license terms provided in LICENSE.md file.
  4. */
  5. #include <iostream>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <vector>
  9. #include <NvInfer.h>
  10. #include <opencv2/opencv.hpp>
  11. #include "examples/classify_image/utils.h"
  12. using namespace std;
  13. using namespace nvinfer1;
  14. class Logger : public ILogger
  15. {
  16. void log(Severity severity, const char * msg) override
  17. {
  18. if (severity != Severity::kINFO)
  19. cout << msg << endl;
  20. }
  21. } gLogger;
  22. /**
  23. * image_file: path to image
  24. * plan_file: path of the serialized engine file
  25. * label_file: file with <class_name> per line
  26. * input_name: name of the input tensor
  27. * output_name: name of the output tensor
  28. * preprocessing_fn: 'vgg' or 'inception'
  29. */
  30. int main(int argc, char *argv[])
  31. {
  32. if (argc != 7)
  33. {
  34. cout << "Usage: classify_image <image_file> <plan_file> <label_file> <input_name> <output_name> <preprocessing_fn>\n";
  35. return 0;
  36. }
  37. string imageFilename = argv[1];
  38. string planFilename = argv[2];
  39. string labelFilename = argv[3];
  40. string inputName = argv[4];
  41. string outputName = argv[5];
  42. string preprocessingFn = argv[6];
  43. /* load the engine */
  44. cout << "Loading TensorRT engine from plan file..." << endl;
  45. ifstream planFile(planFilename);
  46. stringstream planBuffer;
  47. planBuffer << planFile.rdbuf();
  48. string plan = planBuffer.str();
  49. IRuntime *runtime = createInferRuntime(gLogger);
  50. ICudaEngine *engine = runtime->deserializeCudaEngine((void*)plan.data(), plan.size(), nullptr);
  51. IExecutionContext *context = engine->createExecutionContext();
  52. /* get the input / output dimensions */
  53. int inputBindingIndex, outputBindingIndex;
  54. inputBindingIndex = engine->getBindingIndex(inputName.c_str());
  55. outputBindingIndex = engine->getBindingIndex(outputName.c_str());
  56. Dims inputDims, outputDims;
  57. inputDims = engine->getBindingDimensions(inputBindingIndex);
  58. outputDims = engine->getBindingDimensions(outputBindingIndex);
  59. int inputWidth, inputHeight;
  60. inputHeight = inputDims.d[1];
  61. inputWidth = inputDims.d[2];
  62. /* read image, convert color, and resize */
  63. cout << "Preprocessing input..." << endl;
  64. cv::Mat image = cv::imread(imageFilename, CV_LOAD_IMAGE_COLOR);
  65. cv::cvtColor(image, image, cv::COLOR_BGR2RGB, 3);
  66. cv::resize(image, image, cv::Size(inputWidth, inputHeight));
  67. /* convert from uint8+NHWC to float+NCHW */
  68. float *inputDataHost, *outputDataHost;
  69. size_t numInput, numOutput;
  70. numInput = numTensorElements(inputDims);
  71. numOutput = numTensorElements(outputDims);
  72. inputDataHost = (float*) malloc(numInput * sizeof(float));
  73. outputDataHost = (float*) malloc(numOutput * sizeof(float));
  74. cvImageToTensor(image, inputDataHost, inputDims);
  75. if (preprocessingFn == "vgg")
  76. preprocessVgg(inputDataHost, inputDims);
  77. else if (preprocessingFn == "inception")
  78. preprocessInception(inputDataHost, inputDims);
  79. else
  80. cout << "Unsupported preprocessing function. Results may not be correct.\n" << endl;
  81. /* transfer to device */
  82. float *inputDataDevice, *outputDataDevice;
  83. cudaMalloc(&inputDataDevice, numInput * sizeof(float));
  84. cudaMalloc(&outputDataDevice, numOutput * sizeof(float));
  85. cudaMemcpy(inputDataDevice, inputDataHost, numInput * sizeof(float), cudaMemcpyHostToDevice);
  86. void *bindings[2];
  87. bindings[inputBindingIndex] = (void*) inputDataDevice;
  88. bindings[outputBindingIndex] = (void*) outputDataDevice;
  89. /* execute engine */
  90. cout << "Executing inference engine..." << endl;
  91. const int kBatchSize = 1;
  92. context->execute(kBatchSize, bindings);
  93. /* transfer output back to host */
  94. cudaMemcpy(outputDataHost, outputDataDevice, numOutput * sizeof(float), cudaMemcpyDeviceToHost);
  95. /* parse output */
  96. vector<size_t> sortedIndices = argsort(outputDataHost, outputDims);
  97. cout << "\nThe top-5 indices are: ";
  98. for (int i = 0; i < 5; i++)
  99. cout << sortedIndices[i] << " ";
  100. ifstream labelsFile(labelFilename);
  101. vector<string> labelMap;
  102. string label;
  103. while(getline(labelsFile, label))
  104. {
  105. labelMap.push_back(label);
  106. }
  107. cout << "\nWhich corresponds to class labels: ";
  108. for (int i = 0; i < 5; i++)
  109. cout << endl << i << ". " << labelMap[sortedIndices[i]];
  110. cout << endl;
  111. /* clean up */
  112. runtime->destroy();
  113. engine->destroy();
  114. context->destroy();
  115. free(inputDataHost);
  116. free(outputDataHost);
  117. cudaFree(inputDataDevice);
  118. cudaFree(outputDataDevice);
  119. return 0;
  120. }