Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- Status ReadTensorFromImageFile(string file_name, const int input_height,
- const int input_width, const float input_mean,
- const float input_std,
- std::vector<Tensor>* out_tensors) {
- auto root = tensorflow::Scope::NewRootScope();
- using namespace ::tensorflow::ops; // NOLINT(build/namespaces)
- string input_name = "file_reader";
- string output_name = "normalized";
- auto file_reader =
- tensorflow::ops::ReadFile(root.WithOpName(input_name), file_name);
- // Now try to figure out what kind of file it is and decode it.
- const int wanted_channels = 3;
- tensorflow::Output image_reader;
- if (tensorflow::StringPiece(file_name).ends_with(".png")) {
- image_reader = DecodePng(root.WithOpName("png_reader"), file_reader,
- DecodePng::Channels(wanted_channels));
- } else if (tensorflow::StringPiece(file_name).ends_with(".gif")) {
- image_reader = DecodeGif(root.WithOpName("gif_reader"), file_reader);
- } else {
- // Assume if it's neither a PNG nor a GIF then it must be a JPEG.
- image_reader = DecodeJpeg(root.WithOpName("jpeg_reader"), file_reader,
- DecodeJpeg::Channels(wanted_channels));
- }
- // Now cast the image data to float so we can do normal math on it.
- auto float_caster =
- Cast(root.WithOpName("float_caster"), image_reader, tensorflow::DT_FLOAT);
- // The convention for image ops in TensorFlow is that all images are expected
- // to be in batches, so that they're four-dimensional arrays with indices of
- // [batch, height, width, channel]. Because we only have a single image, we
- // have to add a batch dimension of 1 to the start with ExpandDims().
- auto dims_expander = ExpandDims(root, float_caster, 0);
- // Bilinearly resize the image to fit the required dimensions.
- auto resized = ResizeBilinear(
- root, dims_expander,
- Const(root.WithOpName("size"), {input_height, input_width}));
- // Subtract the mean and divide by the scale.
- Div(root.WithOpName(output_name), Sub(root, resized, {input_mean}),
- {input_std});
- // This runs the GraphDef network definition that we've just constructed, and
- // returns the results in the output tensor.
- tensorflow::GraphDef graph;
- TF_RETURN_IF_ERROR(root.ToGraphDef(&graph));
- std::unique_ptr<tensorflow::Session> session(
- tensorflow::NewSession(tensorflow::SessionOptions()));
- TF_RETURN_IF_ERROR(session->Create(graph));
- TF_RETURN_IF_ERROR(session->Run({}, {output_name}, {}, out_tensors));
- return Status::OK();
- }
- Status read_tensor_status =
- ReadTensorFromImageFile(image_path, input_height, input_width, input_mean,
- input_std, &resized_tensors);
- if (!read_tensor_status.ok()) {
- LOG(ERROR) << read_tensor_status;
- return -1;
- }
- // @resized_tensor: the tensor storing the image
- const Tensor &resized_tensor = resized_tensors[0];
- auto resized_tensor_height = resized_tensor.shape().dim_sizes()[1];
- auto resized_tensor_width = resized_tensor.shape().dim_sizes()[2];
- std::cout << "resized_tensor_height:t" << resized_tensor_height
- << "nresized_tensor_width:t" << resized_tensor_width << std::endl;
- resized_tensor_height: 636
- resized_tensor_width: 1024
Add Comment
Please, Sign In to add comment