Search code examples
pytorch

how to convert ML Project from a GPU project to CPU project?


I am learning ML and i want to re train a AI model for lane detection.

I want to be familiar with the ML training process. The accuracy/result is not my primary goal and i do not need a best ML model for lane detection.

I found this AI model and want to try it out. But i have been facing a problem:

  1. I do not have a GPU, so i wish i can train this model with my CPU. But sadly some part of this code is written with CUDA. Is there a way, i can convert this GPU code to CPU code only?

Should i find another AI-model only for the CPU training?


Solution

  • you can use the tensor.to(device) command to move a tensor to a device.

    The .to() command is also used to move a whole model to a device, like in the post you linked to.

    Another possibility is to set the device of a tensor during creation using the device= keyword argument, like in t = torch.tensor(some_list, device=device)

    To set the device dynamically in your code, you can use

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    

    to set cuda as your device if possible.

    Above is the answer for how to add CUDA in the code. SO Use Cntrl + F and remove all the keywords which forces code to run on GPU. Such as "device", "to"