我有一个加载到 GPU 上的 cnn 模型,对于每个图像,必须创建并分离一个新线程才能在此图像上运行模型。这可能吗?如果可以,安全吗?
我在 pytorch 和 libtorch 中使用相同的跟踪模型,但得到不同的输出。
\nimport cv2\nimport numpy as np \nimport torch\nimport torchvision\nfrom torchvision import transforms as trans\n\n\n# device for pytorch\ndevice = torch.device(\'cuda:0\')\n\ntorch.set_default_tensor_type(\'torch.cuda.FloatTensor\')\n\nmodel = torch.jit.load("traced_facelearner_model_new.pt")\nmodel.eval()\n\n# read the example image used for tracing\nimage=cv2.imread("videos/example.jpg")\n\ntest_transform = trans.Compose([\n trans.ToTensor(),\n trans.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])\n ]) \n\nresized_image = cv2.resize(image, (112, 112))\n\ntens = test_transform(resized_image).to(device).unsqueeze(0)\noutput = model(tens)\nprint(output)\nRun Code Online (Sandbox Code Playgroud)\n#include <iostream>\n#include <algorithm> \n#include <opencv2/opencv.hpp>\n#include <torch/script.h>\n\n\nint main()\n{\n try\n {\n torch::jit::script::Module model = torch::jit::load("traced_facelearner_model_new.pt");\n model.to(torch::kCUDA);\n model.eval();\n\n cv::Mat visibleFrame = cv::imread("example.jpg");\n\n cv::resize(visibleFrame, visibleFrame, cv::Size(112, 112));\n …Run Code Online (Sandbox Code Playgroud)