簡體   English   中英

如何加載 Torch model 進行 CPU 推理

[英]How load torch model for cpu inference

無法弄清楚問題是什么。 即使我將下載指向 cpu,torch 也會嘗試使用 cuda 工作:

model = nn.Sequential(
    nn.Conv2d(3,16,5),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2, stride=2),
    nn.Conv2d(16,16,5),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2, stride=2),
    nn.Conv2d(16,32,5),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2, stride=2),
    nn.Conv2d(32,64,5),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2, stride=2),
    nn.Conv2d(64,128,5),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2, stride=2),
    nn.Flatten(),
    torch.nn.Linear(1152, 256),
    torch.nn.ELU(), torch.nn.Linear(256,4)
            )
model.load_state_dict(torch.load('bbox.pth',map_location=torch.device('cpu')))
ds_trans = transforms.Compose([transforms.ToTensor(),normalize,transforms.Resize((224,224))])
batch = ds_trans(img).unsqueeze(0)
ans = model(batch)

運行結果

AssertionError: Torch not compiled with CUDA enabled

Model 是在 GPU 上學習的,GPU 推理運行良好,但我需要在另一台電腦上進行 cpu 推理。

嘗試用torchvision重新安裝pytorch

pip install torch===1.5.0 torchvision===0.6.0 -f https://download.pytorch.org/whl/torch_stable.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM