How to solve "RuntimeError: CUDA error: invalid device ordinal"?
Question:
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.
Answers:
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).
If it’s not from using
emotion_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')
In my case it was accidentally setting CUDA_VISIBLE_DEVICES=0
and attempting to set a process to a device other than 0. To fix that, you can either run
unset CUDA_VISIBLE_DEVICES
or
export CUDA_VISIBLE_DEVICES=1,2,3,4,...
depending on the number of GPUs you have/want to use.
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.
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).
If it’s not from using
emotion_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')
In my case it was accidentally setting CUDA_VISIBLE_DEVICES=0
and attempting to set a process to a device other than 0. To fix that, you can either run
unset CUDA_VISIBLE_DEVICES
or
export CUDA_VISIBLE_DEVICES=1,2,3,4,...
depending on the number of GPUs you have/want to use.