4

对于视频中的对象检测,我们使用大小大于 1 的批次。但是在视频结束时,当剩余的帧少于批次大小时,我们会收到以下错误:

2018-06-07 15:31:26.456907: W tensorflow/core/framework/op_kernel.cc:1290] CtxFailure at image_resizer_state.h:84: Invalid argument: output dimensions must be positive

为了阅读我们使用 Opencv 3.3.0 的视频,帧存储在向量中batch

for (size_t i = 0; i < batch_size; i++) {
  ret = cap.read(readImage);
  if (!ret) {
    break;
  }
  batch.push_back(readImage.clone());
}

该向量被转换为 tf Tensor 为

tensorflow::Tensor inputImg(
    tensorflow::DT_UINT8,
    tensorflow::TensorShape(
        {static_cast<long long int>(batch.size()), height, width, depth}));
for (size_t i = 0; i < batch.size(); i++) {
  auto tmp = inputImg.Slice(i, i + 1);
  uint8_t *p = tmp.flat<uint8_t>().data();
  cv::Mat cameraImg(height, width, CV_8UC3, p);
  batch[i].convertTo(cameraImg, CV_8UC3);
}

您会注意到 Tensor 使用实际批次的大小进行初始化,因此它在开始时包含定义数量的图像,而在视频结束时包含较少的图像。

之后我们运行“检测器”

  std::vector<tensorflow::Tensor> output_tensors;
  tensorflow::Status status =
      session->Run({{"image_tensor:0", inputImg}}, {"detection_boxes:0", "detection_scores:0",
                    "detection_classes:0", "num_detections:0"}, {}, &output_tensors);

最终出现错误(status.ok() == False并出现提到的消息)。

所以在整个视频过程中我们没有错误,但是当只剩下 2 张图像时(所以向量batch的大小为 2),但之前的批量大小为 5,我们有错误并且output_tensors大小为 0。我们尝试运行我们的检测器每次迭代都会随机更改批次大小,并且当当前批次的大小与前一个批次不同时没有错误,它只发生在视频的末尾。

如果我们选择批量大小这样

video_size % batch_size == 0

该错误也不会发生。

我们使用从源代码master分支)构建的 TF。

有谁知道这个问题的解决方案?

4

0 回答 0