Trying to get ESRGAN BG Upscaling to work with CPU #158

Open
opened 2026-01-29 21:44:21 +00:00 by claunia · 4 comments
Owner

Originally created by @zarc70 on GitHub (Feb 15, 2022).

TL;DR How do I make ESRGAN Background Upscaling work using CPU

Hi, love the new model and it works great (As did the old ones)! I know that the warning on the ESRGAN Background Upscaling with CPU gives the warning that it is slow and it is set not to occur when the model is running on CPU. On the old utils, When I edit the code to ignore the warning (i.e.

if args.bg_upsampler == 'realesrgan':
if not torch.cuda.is_available(): # CPU
import warnings
warnings.warn('The unoptimized RealESRGAN is very slow on CPU. We do not use it. '
'If you really want to use it, please modify the corresponding codes.')
bg_upsampler = 'none' CHANGE NONE TO realesrgan

I get this error message:
***\GFPGAN\gfpgan\utils.py", line 106, in enhance
bg_img = self.bg_upsampler.enhance(img, outscale=self.upscale)[0]
AttributeError: 'str' object has no attribute 'enhance'

Is this due to not having torch no grad on CPU, or something of that nature, (as the enhance is defined above in utils as:
def enhance(self, img, has_aligned=False, only_center_face=False, paste_back=True):
self.face_helper.clean_all()

    if has_aligned:
        img = cv2.resize(img, (512, 512))
        self.face_helper.cropped_faces = [img]
    else:
        self.face_helper.read_image(img)
        # get face landmarks for each face
        self.face_helper.get_face_landmarks_5(only_center_face=only_center_face, eye_dist_threshold=5)
        # eye_dist_threshold=5: skip faces whose eye distance is smaller than 5 pixels
        # align and warp each face
        self.face_helper.align_warp_face()

Perhaps this was corrected in the updated utils and inference.py, but I'm still having significant trouble with it.

Originally created by @zarc70 on GitHub (Feb 15, 2022). TL;DR How do I make ESRGAN Background Upscaling work using CPU Hi, love the new model and it works great (As did the old ones)! I know that the warning on the ESRGAN Background Upscaling with CPU gives the warning that it is slow and it is set not to occur when the model is running on CPU. On the old utils, When I edit the code to ignore the warning (i.e. if args.bg_upsampler == 'realesrgan': if not torch.cuda.is_available(): # CPU import warnings warnings.warn('The unoptimized RealESRGAN is very slow on CPU. We do not use it. ' 'If you really want to use it, please modify the corresponding codes.') bg_upsampler = 'none' **CHANGE NONE TO realesrgan** I get this error message: ***\GFPGAN\gfpgan\utils.py", line 106, in enhance bg_img = self.bg_upsampler.enhance(img, outscale=self.upscale)[0] AttributeError: 'str' object has no attribute 'enhance' Is this due to not having torch no grad on CPU, or something of that nature, (as the enhance is defined above in utils as: def enhance(self, img, has_aligned=False, only_center_face=False, paste_back=True): self.face_helper.clean_all() if has_aligned: img = cv2.resize(img, (512, 512)) self.face_helper.cropped_faces = [img] else: self.face_helper.read_image(img) # get face landmarks for each face self.face_helper.get_face_landmarks_5(only_center_face=only_center_face, eye_dist_threshold=5) # eye_dist_threshold=5: skip faces whose eye distance is smaller than 5 pixels # align and warp each face self.face_helper.align_warp_face() Perhaps this was corrected in the updated utils and inference.py, but I'm still having significant trouble with it.
Author
Owner

@xinntao commented on GitHub (Feb 15, 2022):

Thanks for reporting the issue, I will fix it later~

@xinntao commented on GitHub (Feb 15, 2022): Thanks for reporting the issue, I will fix it later~
Author
Owner

@zarc70 commented on GitHub (Feb 18, 2022):

Thank you!!!

@zarc70 commented on GitHub (Feb 18, 2022): Thank you!!!
Author
Owner

@toto4c19 commented on GitHub (Mar 25, 2022):

Hi. I have tried with CPU BG Upscaling and also have the same problem. May I know when will it be fixed? I teasted with using CPU for running real-ESRGAN with no issue.

@toto4c19 commented on GitHub (Mar 25, 2022): Hi. I have tried with CPU BG Upscaling and also have the same problem. May I know when will it be fixed? I teasted with using CPU for running real-ESRGAN with no issue.
Author
Owner

@pipeob0 commented on GitHub (Apr 10, 2022):

Change
# ------------------------ set up background upsampler ------------------------ if args.bg_upsampler == 'realesrgan': if not torch.cuda.is_available(): # CPU import warnings warnings.warn('The unoptimized RealESRGAN is slow on CPU. We do not use it. ' 'If you really want to use it, please modify the corresponding codes.') bg_upsampler = None else: from basicsr.archs.rrdbnet_arch import RRDBNet from realesrgan import RealESRGANer model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2) bg_upsampler = RealESRGANer( scale=2, model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth', model=model, tile=args.bg_tile, tile_pad=10, pre_pad=0, half=True) # need to set False in CPU mode else: bg_upsampler = None
with
# ------------------------ set up background upsampler ------------------------ if args.bg_upsampler == 'realesrgan': from basicsr.archs.rrdbnet_arch import RRDBNet from realesrgan import RealESRGANer model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2) bg_upsampler = RealESRGANer( scale=2, model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth', model=model, tile=args.bg_tile, tile_pad=10, pre_pad=0, half=False) # need to set False in CPU mode else: bg_upsampler = None
worked for me

@pipeob0 commented on GitHub (Apr 10, 2022): Change `# ------------------------ set up background upsampler ------------------------ if args.bg_upsampler == 'realesrgan': if not torch.cuda.is_available(): # CPU import warnings warnings.warn('The unoptimized RealESRGAN is slow on CPU. We do not use it. ' 'If you really want to use it, please modify the corresponding codes.') bg_upsampler = None else: from basicsr.archs.rrdbnet_arch import RRDBNet from realesrgan import RealESRGANer model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2) bg_upsampler = RealESRGANer( scale=2, model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth', model=model, tile=args.bg_tile, tile_pad=10, pre_pad=0, half=True) # need to set False in CPU mode else: bg_upsampler = None ` with `# ------------------------ set up background upsampler ------------------------ if args.bg_upsampler == 'realesrgan': from basicsr.archs.rrdbnet_arch import RRDBNet from realesrgan import RealESRGANer model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2) bg_upsampler = RealESRGANer( scale=2, model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth', model=model, tile=args.bg_tile, tile_pad=10, pre_pad=0, half=False) # need to set False in CPU mode else: bg_upsampler = None ` worked for me
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: TencentARC/GFPGAN#158