Guest User

Untitled

a guest
Jun 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. // Given an image file name, read in the data, try to decode it as an image,
  2. // resize it to the requested size, and then scale the values as desired.
  3. Status ReadTensorFromImageFile(string file_name, const int input_height,
  4. const int input_width, const float input_mean,
  5. const float input_std,
  6. std::vector<Tensor>* out_tensors) {
  7. auto root = tensorflow::Scope::NewRootScope();
  8. using namespace ::tensorflow::ops; // NOLINT(build/namespaces)
  9.  
  10. string input_name = "file_reader";
  11. string output_name = "normalized";
  12. auto file_reader =
  13. tensorflow::ops::ReadFile(root.WithOpName(input_name), file_name);
  14. // Now try to figure out what kind of file it is and decode it.
  15. const int wanted_channels = 3;
  16. tensorflow::Output image_reader;
  17. if (tensorflow::StringPiece(file_name).ends_with(".png")) {
  18. image_reader = DecodePng(root.WithOpName("png_reader"), file_reader,
  19. DecodePng::Channels(wanted_channels));
  20. } else if (tensorflow::StringPiece(file_name).ends_with(".gif")) {
  21. image_reader = DecodeGif(root.WithOpName("gif_reader"), file_reader);
  22. } else {
  23. // Assume if it's neither a PNG nor a GIF then it must be a JPEG.
  24. image_reader = DecodeJpeg(root.WithOpName("jpeg_reader"), file_reader,
  25. DecodeJpeg::Channels(wanted_channels));
  26. }
  27. // Now cast the image data to float so we can do normal math on it.
  28. auto float_caster =
  29. Cast(root.WithOpName("float_caster"), image_reader, tensorflow::DT_FLOAT);
  30. // The convention for image ops in TensorFlow is that all images are expected
  31. // to be in batches, so that they're four-dimensional arrays with indices of
  32. // [batch, height, width, channel]. Because we only have a single image, we
  33. // have to add a batch dimension of 1 to the start with ExpandDims().
  34. auto dims_expander = ExpandDims(root, float_caster, 0);
  35. // Bilinearly resize the image to fit the required dimensions.
  36. auto resized = ResizeBilinear(
  37. root, dims_expander,
  38. Const(root.WithOpName("size"), {input_height, input_width}));
  39. // Subtract the mean and divide by the scale.
  40. Div(root.WithOpName(output_name), Sub(root, resized, {input_mean}),
  41. {input_std});
  42.  
  43. // This runs the GraphDef network definition that we've just constructed, and
  44. // returns the results in the output tensor.
  45. tensorflow::GraphDef graph;
  46. TF_RETURN_IF_ERROR(root.ToGraphDef(&graph));
  47.  
  48. std::unique_ptr<tensorflow::Session> session(
  49. tensorflow::NewSession(tensorflow::SessionOptions()));
  50. TF_RETURN_IF_ERROR(session->Create(graph));
  51. TF_RETURN_IF_ERROR(session->Run({}, {output_name}, {}, out_tensors));
  52. return Status::OK();
  53. }
  54.  
  55. Status read_tensor_status =
  56. ReadTensorFromImageFile(image_path, input_height, input_width, input_mean,
  57. input_std, &resized_tensors);
  58. if (!read_tensor_status.ok()) {
  59. LOG(ERROR) << read_tensor_status;
  60. return -1;
  61. }
  62. // @resized_tensor: the tensor storing the image
  63. const Tensor &resized_tensor = resized_tensors[0];
  64. auto resized_tensor_height = resized_tensor.shape().dim_sizes()[1];
  65. auto resized_tensor_width = resized_tensor.shape().dim_sizes()[2];
  66. std::cout << "resized_tensor_height:t" << resized_tensor_height
  67. << "nresized_tensor_width:t" << resized_tensor_width << std::endl;
  68.  
  69. resized_tensor_height: 636
  70. resized_tensor_width: 1024
Add Comment
Please, Sign In to add comment