Skip to content

RuntimeError: a Tensor with 3 elements cannot be converted to Scalar #4359

@xiaochunrilihe

Description

@xiaochunrilihe

🐛 Describe the bug

Hi, I had met a runtime error problem when running my MTCNN C++ script on Ubuntu18.04 CPU. The python script is called by C++ script. The model has two inputs: img.data and scales. img.data is the rgb pic file which read and saved by tofile() python function. (I had built all lib from source.)

# running cmd
./testcase ./data/mtcnn.script 1 3 184 320 1

Here is my cpp script:

#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>

#include "torch/script.h"
#include "torch/torch.h"

int main(int argc, const char *argv[])
{
    if (argc != 7)
    {
        std::cerr << "testcase torch.model N C H W num_loop" << std::endl;
        return -1;
    }
    const std::string module_path = argv[1];
    int N = std::atoi(argv[2]);
    int C = std::atoi(argv[3]);
    int H = std::atoi(argv[4]);
    int W = std::atoi(argv[5]);
    int num_loop = std::atoi(argv[6]);

    if (N < 1 && H < 1)
    {
        std::cerr << "error, input num is invalid, N = " << N << ", C = " << C
                  << ", H = " << H << ", W = " << W << std::endl;
        return -1;
    }
    int ele_num = N * C * H * W;
    std::vector<float> data(ele_num);
    std::vector<int64_t> input_dims{N, C, H, W};
    // random_data(data);

    // load data
    std::ifstream fin("/data/img.data", std::ios::in | std::ios::binary);
    float tmp[ele_num] = {0};
    fin.read((char *)&tmp, sizeof(tmp));
    // std::cout << tmp[0] << std::endl;
    for (int i = 0; i < ele_num; i++)
    {
        data[i] = tmp[i];
    }
    // std::cout << data[0] << " " << data[ele_num - 1] << " ";
    // std::cout << std::endl;
    torch::jit::script::Module module;
    // torch::Device device(torch::kCUDA, 0);
    torch::Device device(torch::kCPU, 0);
    try
    {
        module = torch::jit::load(module_path, device);
    }
    catch (const c10::Error &e)
    {
        std::cerr << "Fail to load model to device: " << 0
                  << ", reason: " << e.what();
    }
      torch::NoGradGuard no_grad;
      auto opts = torch::TensorOptions().dtype(torch::kFloat32);
      std::vector<torch::jit::IValue> inputs;
      inputs.emplace_back(
      torch::from_blob((void *)data.data(), input_dims, opts).to(device));
      std::vector<float> scales = {0.6, 0.4254, 0.2};
      std::vector<int64_t> scales_dims = {1, 3};
      inputs.emplace_back(torch::from_blob((void *)scales.data(), scales_dims, opts).to(device));
      at::Tensor torch_out;
      int count_i = num_loop;
      while (count_i--)
      {
          torch_out = module.forward(inputs).toTensor().to(torch::kCPU);
      }
    return 0;
}

Here is a part of mtcnn_scripts.py

class MTCNN(nn.Module):
    def __init__(
        self, image_size=160, margin=0, min_face_size=20,
        thresholds=[0.7, 0.8, 0.8], factor=0.709, post_process=True,
        select_largest=True, selection_method=None, keep_all=False, device=None
    ):
        super().__init__()

        self.image_size = image_size
        self.margin = margin
        self.min_face_size = min_face_size
        self.thresholds = torch.tensor(thresholds)
        self.factor = factor
        self.post_process = post_process
        self.select_largest = select_largest
        self.keep_all = keep_all
        self.selection_method = selection_method
        #self.imresample = nn.functional.interpolate(img, size, mode="area")    
     
        self.pnet = PNet()
        self.rnet = RNet()
        self.onet = ONet()

        self.device = torch.device('cpu')
        if device is not None:
            self.device = device
            self.to(device)

        if not self.selection_method:
            self.selection_method = 'largest' if self.select_largest else 'probability'

    def forward(self, img, scales:torch.Tensor):
        boxes = []
        image_inds = []
        scale_picks = []
        offset = 0
        h, w = img.shape[2:4]
	print(scales)
        for scale in scales:
            print(scale)
            print(type(scale))
            im_data = torch.nn.functional.interpolate(img, (int(h * scale + 1), int(w * scale + 1)), mode="area")
            im_data = (im_data - 127.5) * 0.0078125
            reg, probs = self.pnet(im_data)
            boxes_scale, image_inds_scale = generateBoundingBox(reg, probs[:, 1], scale, self.thresholds[0])
            boxes.append(boxes_scale)
            image_inds.append(image_inds_scale)

            pick = batched_nms(boxes_scale[:, :4], boxes_scale[:, 4], image_inds_scale, 0.5)
            scale_picks.append(pick + offset)
            offset += boxes_scale.shape[0]

Here is the error log info:

terminate called after throwing an instance of 'std::runtime_error'
  what():  The following operation failed in the TorchScript interpreter.
Traceback of TorchScript, serialized code (most recent call last):
  File "code/__torch__/facenet_pytorch/models/mtcnn_script.py", line 36, in forward
      scale = torch.select(scales, 0, _7)
      _8 = torch.add(torch.mul(scale, h), 1, 1)
      _9 = int(_8)
           ~~~ <--- HERE
      _10 = torch.add(torch.mul(scale, w), 1, 1)
      im_data = _0(img, [_9, int(_10)], None, "area", None, None, )

Traceback of TorchScript, original code (most recent call last):
  File "/anaconda3/envs/torch16/lib/python3.7/site-packages/facenet_pytorch/models/mtcnn_script.py", line 265, in forward
       # reg, probs = self.pnet(img)
        for scale in scales:
            im_data = torch.nn.functional.interpolate(img, (int(h * scale + 1), int(w * scale + 1)), mode="area")
                                                            ~~~ <--- HERE
            im_data = (im_data - 127.5) * 0.0078125
            reg, probs = self.pnet(im_data)
RuntimeError: a Tensor with 3 elements cannot be converted to Scalar

Aborted (core dumped)

Versions

libtorch version = 1.7.1
torchvision version = 0.8.2
libpng version = 1.6
libjpeg version = 0.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions