如何在python中的函数内部调用函数?

时间:2019-07-09 12:35:59

标签: python python-3.x

我想在函数内部调用函数以打印c。有两个功能。

std::shared_ptr<torch::jit::script::Module> module = torch::jit::load("model.pt");
module->to(torch::kCUDA);
assert(module != nullptr);
std::cout << "ok\n";


std::vector<torch::jit::IValue> inputs;
cv::Mat image;
image = cv::imread("pic.jpeg", 1);
cv::Mat image_resized;
cv::resize(image, image_resized, cv::Size(224, 224));
cv::cvtColor(image_resized, image_resized, cv::COLOR_BGR2RGB);
cv::Mat image_resized_float;
image_resized.convertTo(image_resized_float, CV_32F, 1.0 / 255);

auto img_tensor = torch::from_blob(image_resized_float.data, { 1, 224, 224, 3 }, torch::kFloat32);
cout << "img tensor loaded..\n";
img_tensor = img_tensor.permute({ 0, 3, 1, 2 });
img_tensor[0][0] = img_tensor[0][0].sub(0.485).div(0.229);
img_tensor[0][1] = img_tensor[0][1].sub(0.456).div(0.224);
img_tensor[0][2] = img_tensor[0][2].sub(0.406).div(0.225);

// to GPU
img_tensor = img_tensor.to(at::kCUDA);

torch::Tensor out_tensor2 = module->forward({ img_tensor }).toTensor(); //SEGFAULT

1 个答案:

答案 0 :(得分:2)

您以相同的方式调用它。使用调用语法:...(...)

您的难题是对该对象的引用,因此您可以对其进行调用。最明显的解决方案是返回函数。

def ash():
    def ush():
        a = 5
        b = 6
        c = a + b
        return c
    return ush

print(ash()())
# or
ush = ash()
print(ush())