Make predictions on CPU (CPU Usage too high)

Hello Everyone,

I trained the model on GPU and saved the model. But, I want to load this model and make the predictions on CPU. Unfortunately, the CPU usage is too high as you can see below.

img = Image.open(filePath)
        tfms = transforms.Compose([
            transforms.Resize((config["Img_Height_Pred"], config["Img_Width_Pred"])),
            transforms.ToTensor(),
            transforms.Normalize([0.0], [1.0])])
        model.eval()
        img_tensor = tfms(img).unsqueeze(0)
        with torch.no_grad():
            preds = torch.sigmoid(model(img_tensor))
            preds = (preds > 0.5).float()
            tensor_to_pil = transforms.ToPILImage()(preds.squeeze_(0))

Is there anything I can do to lower the CPU usage?

Best,

Thalia

Try limiting the number of threads that PyTorch uses, using torch.set_num_threads().

Thank you! now the CPU usage is lower!
MicrosoftTeams-image (12)

1 Like