我正在将Tensorflow与已编译的库一起使用。
我从改编label image example开始。我正在读取改编后的Point2D pointInParent = rotation.transform(polyline.getPoints().get(4),
polyline.getPoints().get(5));
中的原始文件。在这里,我正在读取大小为ReadTensorFromImageFile
的文件,并调整其大小并将其重塑为最终的张量形状为256 x 256
。参见下面的代码。
[1, 256, 256, 1]
调用此函数的代码如下
Status ReadTensorFromImageFile(const string& file_name, const int input_height,
const int input_width,
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 = "resized";
// read file_name into a tensor named input
Tensor input(tensorflow::DT_STRING, tensorflow::TensorShape());
TF_RETURN_IF_ERROR(
ReadEntireFile(tensorflow::Env::Default(), file_name, &input));
// use a placeholder to read input data
auto file_reader =
Placeholder(root.WithOpName("input"), tensorflow::DataType::DT_STRING);
std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
{"input", input},
};
// Now try to figure out what kind of file it is and decode it.
tensorflow::Output image_reader;
if (tensorflow::str_util::EndsWith(file_name, ".raw")) {
image_reader = DecodeRaw(root.WithOpName("raw_reader"), file_reader,
tensorflow::DT_FLOAT);
} else {
// Assume if it's neither a PNG nor a GIF then it must be a JPEG.
return tensorflow::errors::NotFound(
"No decoder found for file ", file_name);
}
// 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);
auto resha = Reshape(root.WithOpName("foo"), float_caster, Const(root, {1, 512, 512, 1}));
auto resized = ResizeBilinear(
root, resha,
Const(root.WithOpName(output_name), {input_height, input_width}));
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}, {output_name}, {}, out_tensors));
return Status::OK();
}
这里的问题是,将 std::vector<Tensor> resized_tensors;
Status read_tensor_status =
ReadTensorFromImageFile(image, 256, 256, &resized_tensors);
if (!read_tensor_status.ok()) {
LOG(ERROR) << read_tensor_status;
return -1;
}
const Tensor& resized_tensor = resized_tensors[0];
LOG(ERROR) << resized_tensor.DebugString();
整形为正常工作,并且调试输出([1, 512, 512, 1]
)符合预期:DebugString()
。
但是,如果要调整该图像的大小,就像在该函数中所做的那样,我的输出将变得很奇怪:Tensor<type: float shape: [1,512,512,1] values: [[[1][1][1]]]...>
。数据类型,大小和内容与预期不符。
我已经尝试过使用reshape和resize参数的几种组合,但是似乎没有任何效果。您是否知道如何解决此问题或我做错了什么? 谢谢。
答案 0 :(得分:0)
好吧,我对Tensorflow的了解不多,但是在这里,您使用{input_height, input_width}
给出了大小为2的向量,所以也许尝试使用{1, input_height, input_width, 1}