MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ReadTensorFromImageFile

Function ReadTensorFromImageFile

tensorflow/examples/multibox_detector/main.cc:78–136  ·  view source on GitHub ↗

Given an image file name, read in the data, try to decode it as an image, resize it to the requested size, and then scale the values as desired.

Source from the content-addressed store, hash-verified

76// Given an image file name, read in the data, try to decode it as an image,
77// resize it to the requested size, and then scale the values as desired.
78Status ReadTensorFromImageFile(const string& file_name, const int input_height,
79 const int input_width, const float input_mean,
80 const float input_std,
81 std::vector<Tensor>* out_tensors) {
82 auto root = tensorflow::Scope::NewRootScope();
83 using namespace ::tensorflow::ops; // NOLINT(build/namespaces)
84
85 string input_name = "file_reader";
86 string original_name = "identity";
87 string output_name = "normalized";
88 auto file_reader =
89 tensorflow::ops::ReadFile(root.WithOpName(input_name), file_name);
90 // Now try to figure out what kind of file it is and decode it.
91 const int wanted_channels = 3;
92 tensorflow::Output image_reader;
93 if (tensorflow::str_util::EndsWith(file_name, ".png")) {
94 image_reader = DecodePng(root.WithOpName("png_reader"), file_reader,
95 DecodePng::Channels(wanted_channels));
96 } else if (tensorflow::str_util::EndsWith(file_name, ".gif")) {
97 image_reader = DecodeGif(root.WithOpName("gif_reader"), file_reader);
98 } else {
99 // Assume if it's neither a PNG nor a GIF then it must be a JPEG.
100 image_reader = DecodeJpeg(root.WithOpName("jpeg_reader"), file_reader,
101 DecodeJpeg::Channels(wanted_channels));
102 }
103
104 // Also return identity so that we can know the original dimensions and
105 // optionally save the image out with bounding boxes overlaid.
106 auto original_image = Identity(root.WithOpName(original_name), image_reader);
107
108 // Now cast the image data to float so we can do normal math on it.
109 auto float_caster = Cast(root.WithOpName("float_caster"), original_image,
110 tensorflow::DT_FLOAT);
111 // The convention for image ops in TensorFlow is that all images are expected
112 // to be in batches, so that they're four-dimensional arrays with indices of
113 // [batch, height, width, channel]. Because we only have a single image, we
114 // have to add a batch dimension of 1 to the start with ExpandDims().
115 auto dims_expander = ExpandDims(root, float_caster, 0);
116
117 // Bilinearly resize the image to fit the required dimensions.
118 auto resized = ResizeBilinear(
119 root, dims_expander,
120 Const(root.WithOpName("size"), {input_height, input_width}));
121 // Subtract the mean and divide by the scale.
122 Div(root.WithOpName(output_name), Sub(root, resized, {input_mean}),
123 {input_std});
124
125 // This runs the GraphDef network definition that we've just constructed, and
126 // returns the results in the output tensor.
127 tensorflow::GraphDef graph;
128 TF_RETURN_IF_ERROR(root.ToGraphDef(&graph));
129
130 std::unique_ptr<tensorflow::Session> session(
131 tensorflow::NewSession(tensorflow::SessionOptions()));
132 TF_RETURN_IF_ERROR(session->Create(graph));
133 TF_RETURN_IF_ERROR(
134 session->Run({}, {output_name, original_name}, {}, out_tensors));
135 return Status::OK();

Callers 1

mainFunction · 0.70

Calls 15

EndsWithFunction · 0.85
DecodePngFunction · 0.85
DecodeGifFunction · 0.85
DecodeJpegFunction · 0.85
WithOpNameMethod · 0.80
ReadFileFunction · 0.50
IdentityFunction · 0.50
CastFunction · 0.50
ExpandDimsFunction · 0.50
ResizeBilinearFunction · 0.50
ConstFunction · 0.50
DivFunction · 0.50

Tested by

no test coverage detected