main.cc
// 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.
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.
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(const 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; 
  string input_name = "file_reader";
  string output_name = "normalized";
  Tensor input(tensorflow::DT_STRING, tensorflow::TensorShape());
  TF_RETURN_IF_ERROR(
      ReadEntireFile(tensorflow::Env::Default(), file_name, &input));
  auto file_reader =
      Placeholder(root.WithOpName("input"), tensorflow::DataType::DT_STRING);
  std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
      {"input", input},
  };
  const int wanted_channels = 3;
  tensorflow::Output image_reader;
  if (tensorflow::str_util::EndsWith(file_name, ".png")) {
    image_reader = DecodePng(root.WithOpName("png_reader"), file_reader,
                             DecodePng::Channels(wanted_channels));
  } else if (tensorflow::str_util::EndsWith(file_name, ".gif")) {
    image_reader =
        Squeeze(root.WithOpName("squeeze_first_dim"),
                DecodeGif(root.WithOpName("gif_reader"), file_reader));
  } else if (tensorflow::str_util::EndsWith(file_name, ".bmp")) {
    image_reader = DecodeBmp(root.WithOpName("bmp_reader"), file_reader);
  } else {
    image_reader = DecodeJpeg(root.WithOpName("jpeg_reader"), file_reader,
                              DecodeJpeg::Channels(wanted_channels));
  }
  auto uint8_caster =
      Cast(root.WithOpName("uint8_caster"), image_reader, tensorflow::DT_UINT8);
  auto dims_expander = ExpandDims(root.WithOpName("dim"), uint8_caster, 0);
  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({inputs}, {"dim"}, {}, out_tensors));
  return Status::OK();
}
Status ReadTensorFromImageFile ( const 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 ; string input_name = " " ; string output_name = " " ; Tensor input ( tensorflow :: DT_STRING , tensorflow :: TensorShape ( ) ) ; TF_RETURN_IF_ERROR ( ReadEntireFile ( tensorflow :: Env :: Default ( ) , file_name , & input ) ) ; auto file_reader = Placeholder ( root . WithOpName ( " " ) , tensorflow :: DataType :: DT_STRING ) ; std :: vector < std :: pair < string , tensorflow :: Tensor > > inputs = { { " " , input } , } ; const int wanted_channels = 3 ; tensorflow :: Output image_reader ; if ( tensorflow :: str_util :: EndsWith ( file_name , " " ) ) { image_reader = DecodePng ( root . WithOpName ( " " ) , file_reader , DecodePng :: Channels ( wanted_channels ) ) ; } else if ( tensorflow :: str_util :: EndsWith ( file_name , " " ) ) { image_reader = Squeeze ( root . WithOpName ( " " ) , DecodeGif ( root . WithOpName ( " " ) , file_reader ) ) ; } else if ( tensorflow :: str_util :: EndsWith ( file_name , " " ) ) { image_reader = DecodeBmp ( root . WithOpName ( " " ) , file_reader ) ; } else { image_reader = DecodeJpeg ( root . WithOpName ( " " ) , file_reader , DecodeJpeg :: Channels ( wanted_channels ) ) ; } auto uint8_caster = Cast ( root . WithOpName ( " " ) , image_reader , tensorflow :: DT_UINT8 ) ; auto dims_expander = ExpandDims ( root . WithOpName ( " " ) , uint8_caster , 0 ) ; 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 ( { inputs } , { " " } , { } , out_tensors ) ) ; return Status :: OK ( ) ; }
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.
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 .
// NOLINT(build/namespaces) // read file_name into a tensor named input // use a placeholder to read input data // Now try to figure out what kind of file it is and decode it. // gif decoder returns 4-D tensor, remove the first dim // Assume if it's neither a PNG nor a GIF then it must be a JPEG. // 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(). // 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. // auto div =  Div(root.WithOpName(output_name), Sub(root, dims_expander, // {input_mean}), //     {input_std}); // cast to int // auto uint8_caster =  Cast(root.WithOpName("uint8_caster"), div, // tensorflow::DT_UINT8); // This runs the GraphDef network definition that we've just constructed, and // returns the results in the output tensor.