'How to solve "RuntimeError: CUDA error: invalid device ordinal"?
I'm trying to run this code. I don't know what is wrong with it, but this code is not running. and I don't know how to solve this problem.
import cv2
from facial_emotion_recognition import EmotionRecognition
emotion_detector = EmotionRecognition(device='gpu', gpu_id=1)
camera = cv2.VideoCapture(0)
while True:
image = camera.read()[1]
image = emotion_detector.recognise_emotion(image, return_type='BGR')
cv2.imshow('Camera', image)
key = cv2.waitKey(1)
if key == 27:
break
camera.release()
cv2.destroyAllWindows()
but I'm getting this error:
Traceback (most recent call last):
File "/home/fahim/Documents/Python_projects/Python tutorials/pantech AI Master/Computer_Vision/Day 8 Face emotion recognition/emotion.py", line 4, in <module>
emotion_detector = EmotionRecognition(device='gpu', gpu_id=1)
File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/facial_emotion_recognition/facial_emotion_recognition.py", line 25, in __init__
self.network = NetworkV2(in_c=1, nl=32, out_f=7).to(self.device)
File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 607, in to
return self._apply(convert)
File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 354, in _apply
module._apply(fn)
File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 354, in _apply
module._apply(fn)
File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 376, in _apply
param_applied = fn(param)
File "/home/fahim/anaconda3/envs/Computer_Vision/lib/python3.7/site-packages/torch/nn/modules/module.py", line 605, in convert
return t.to(device, dtype if t.is_floating_point() else None, non_blocking)
RuntimeError: CUDA error: invalid device ordinal
Process finished with exit code 1
This is my the configuration of my computer: GPU: NVIDIA GeForce MX130 CPU: Intel i5-10210U (8) @ 4.200GHz Help me to solve this please.
Solution 1:[1]
Try changing:
emotion_detector = EmotionRecognition(device='gpu', gpu_id=1)
To:
emotion_detector = EmotionRecognition(device='gpu', gpu_id=0)
gpu_id
is only effective when more than one GPU is detected, you only seem to have one GPU, so it throws an error since you tell the function to get GPU 2 (since we count from 0).
Solution 2:[2]
If it's not from usingemotion_detector = EmotionRecognition(device='gpu', gpu_id=0)
or similiar function...Try below work-around
As in my case ,it was due to presence of this line in my code
torch.set_default_tensor_type('torch.cuda.FloatTensor')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jss367 |
Solution 2 | Prajot Kuvalekar |