classify_image.cu 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. {
  81. cout << "Invalid preprocessing function argument, must be vgg or inception. \n" << endl;
  82. return 1;
  83. }
  84. /* transfer to device */
  85. float *inputDataDevice, *outputDataDevice;
  86. cudaMalloc(&inputDataDevice, numInput * sizeof(float));
  87. cudaMalloc(&outputDataDevice, numOutput * sizeof(float));
  88. cudaMemcpy(inputDataDevice, inputDataHost, numInput * sizeof(float), cudaMemcpyHostToDevice);
  89. void *bindings[2];
  90. bindings[inputBindingIndex] = (void*) inputDataDevice;
  91. bindings[outputBindingIndex] = (void*) outputDataDevice;
  92. /* execute engine */
  93. cout << "Executing inference engine..." << endl;
  94. const int kBatchSize = 1;
  95. context->execute(kBatchSize, bindings);
  96. /* transfer output back to host */
  97. cudaMemcpy(outputDataHost, outputDataDevice, numOutput * sizeof(float), cudaMemcpyDeviceToHost);
  98. /* parse output */
  99. vector<size_t> sortedIndices = argsort(outputDataHost, outputDims);
  100. cout << "\nThe top-5 indices are: ";
  101. for (int i = 0; i < 5; i++)
  102. cout << sortedIndices[i] << " ";
  103. ifstream labelsFile(labelFilename);
  104. vector<string> labelMap;
  105. string label;
  106. while(getline(labelsFile, label))
  107. {
  108. labelMap.push_back(label);
  109. }
  110. cout << "\nWhich corresponds to class labels: ";
  111. for (int i = 0; i < 5; i++)
  112. cout << endl << i << ". " << labelMap[sortedIndices[i]];
  113. cout << endl;
  114. /* clean up */
  115. runtime->destroy();
  116. engine->destroy();
  117. context->destroy();
  118. free(inputDataHost);
  119. free(outputDataHost);
  120. cudaFree(inputDataDevice);
  121. cudaFree(outputDataDevice);
  122. return 0;
  123. }