CodeFormer icon indicating copy to clipboard operation
CodeFormer copied to clipboard

ImportError: cannot import name 'get_device' from 'basicsr.utils.misc'

Open aleSheng opened this issue 2 years ago • 5 comments

The problem happens when the module BasicSR is imported. And Python would try to use the module instead of the file with the same name.

To solve the problem, I suggest to change the directory basicsr to another name, like cfbasicsr.

aleSheng avatar Mar 25 '23 14:03 aleSheng

In my case: This happens if the basicsr package is already installed from the Python repo. In that case, Python tries to lookup basicsr.utils.misc from the original package (but CodeFormer has its own basicsr). The original package does not have the get_device method, but the CodeFormer version does...

SyedMuhammadAli avatar Apr 01 '23 11:04 SyedMuhammadAli

I've met the same problem. I resolved this problem by replacing all the from basicsr.utils.misc import get_device device = get_device() with device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

chengyu666 avatar Apr 06 '23 01:04 chengyu666

A rude method is just copy the following code into the directory:basicsr\utils\misc.py


import torch
def get_device():
    if torch.cuda.is_available():
        return torch.device("cuda")
    else:
        return torch.device("cpu")

def gpu_is_available():
        return torch.cuda.is_available()

Good Luck!

mmosir avatar Apr 11 '23 14:04 mmosir

A rude method is just copy the following code into the directory:basicsr\utils\misc.py

import torch
def get_device():
    if torch.cuda.is_available():
        return torch.device("cuda")
    else:
        return torch.device("cpu")

def gpu_is_available():
        return torch.cuda.is_available()

Good Luck!

I added this code to file misc.py and it worked !

aakh1361 avatar Jul 31 '23 07:07 aakh1361

I did the above method but im now getting ImportError: cannot import name 'get_device' from 'basicsr.utils.misc'

djwashout avatar Aug 21 '23 03:08 djwashout