gpt4 book ai didi

c - 运行 Tensorflow session 会产生一个空张量

转载 作者:行者123 更新时间:2023-11-30 16:14:21 24 4
gpt4 key购买 nike

我正在尝试使用 Tensorflow C API 与 Deeplab 图运行 session 。 Deeplab 的卡住图在 Cityscapes 上进行了预训练,从这里下载: http://download.tensorflow.org/models/deeplabv3_mnv2_cityscapes_train_2018_02_05.tar.gz

当我使用 python 运行时,我得到以下分段输出: Segmentation output using Python

通过 python 行打印出图形的所有张量:tensors = [n.values() for n in tf.get_default_graph().get_operations()]
,发现输入张量的维度为{1,?,?,3},输出张量为{1,?,?},输入输出张量的数据类型为分别为 uint8 和 int64。我使用此信息编写了一个 C++ 方法来运行图形 session :

int Deeplab::run_segmentation(image_t* img, segmap_t* seg) {
using namespace std;

// Allocate the input tensor
TF_Tensor* const input = TF_NewTensor(TF_UINT8, img->dims, 4, img->data_ptr, img->bytes, &free_tensor, NULL);
TF_Operation* oper_in = TF_GraphOperationByName(graph, "ImageTensor");
const TF_Output oper_in_ = {oper_in, 0};

// Allocate the output tensor
TF_Tensor* output = TF_NewTensor(TF_INT64, seg->dims, 3, seg->data_ptr, seg->bytes, &free_tensor, NULL);
TF_Operation* oper_out = TF_GraphOperationByName(graph, "SemanticPredictions");
const TF_Output oper_out_ = {oper_out, 0};

// Run the session on the input tensor
TF_SessionRun(session, nullptr, &oper_in_, &input, 1, &oper_out_, &output, 1, nullptr, 0, nullptr, status);

return TF_GetCode(status); // https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/tf_status.h#L42
}

其中参数类型image_tsegmap_t包含调用TF_NewTensor所需的参数。它们只是保存指向输入/输出张量分配的缓冲区的指针、张量的维度以及字节大小:

typedef struct segmap {
const int64_t* dims;
size_t bytes;
int64_t* data_ptr;
} segmap_t;

typedef struct image {
const int64_t* dims;
size_t bytes;
uint8_t* data_ptr;
} image_t;

然后,我使用 OpenCV 用街道场景图像填充一个数组(与上面相同),并将 image_tsegmap_t 结构体传递到 session 运行方法中:

    // Allocate input image object
const int64_t dims_in[4] = {1, new_size.width, new_size.height, 3};
image_t* img_in = (image_t*)malloc(sizeof(image_t));
img_in->dims = &dims_in[0];
//img_in->data_ptr = (uint8_t*)malloc(new_size.width*new_size.height*3);
img_in->data_ptr = resized_image.data;
img_in->bytes = new_size.width*new_size.height*3*sizeof(uint8_t);

// Allocate output segmentation map object
const int64_t dims_out[3] = {1, new_size.width, new_size.height};
segmap_t* seg_out = (segmap_t*)malloc(sizeof(segmap_t));
seg_out->dims = &dims_out[0];
seg_out->data_ptr = (int64_t*)calloc(new_size.width*new_size.height, sizeof(int64_t));
seg_out->bytes = new_size.width*new_size.height*sizeof(int64_t);

但是生成的张量 (set_out->data_ptr) 全部由 0 组成。该图似乎执行了大约 5 秒,与工作 python 实现的时间相同。不知何故,该图无法将输出张量数据转储到我分配的缓冲区中。我做错了什么?

最佳答案

有两个错误:

首先,Deeplab的输入张量维度为{1, height, width, 3},输出张量维度为{1, height, width}。所以我不得不交换高度和宽度。

此外,由于某种原因,您必须使用 TF_TensorData 方法从张量中获取数据。通过执行 TF_NewTensor(..., data_ptr, ...) 创建输出张量,然后运行 ​​TF_SessionRun,最后访问 data_ptr 不会工作。您必须通过调用 TF_AllocateTensor(...) 创建输出张量,运行 TF_SessionRun,并使用 TF_TensorData(&tensor) 访问张量数据>。

关于c - 运行 Tensorflow session 会产生一个空张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57739331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com