sczhou commited on
Commit
396b5e1
·
1 Parent(s): f75e5d7

enable realesrgan in CPU (#40)

Browse files
Files changed (3) hide show
  1. README.md +2 -2
  2. app.py +3 -2
  3. inference_codeformer.py +33 -21
README.md CHANGED
@@ -7,9 +7,9 @@
7
  [Paper](https://arxiv.org/abs/2206.11253) | [Project Page](https://shangchenzhou.com/projects/CodeFormer/) | [Video](https://youtu.be/d3VDpkXlueI)
8
 
9
 
10
- <a href="https://colab.research.google.com/drive/1m52PNveE4PBhYrecj34cnpEeiHcC5LTb?usp=sharing"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="google colab logo"></a> [![Hugging Face](https://img.shields.io/badge/Demo-%F0%9F%A4%97%20Hugging%20Face-blue)](https://huggingface.co/spaces/sczhou/CodeFormer) [![Replicate](https://img.shields.io/badge/Demo-%F0%9F%9A%80%20Replicate-blue)](https://replicate.com/sczhou/codeformer) ![visitors](https://visitor-badge.glitch.me/badge?page_id=sczhou/CodeFormer)
11
 
12
- <!-- ![visitors](https://visitor-badge.laobi.icu/badge?page_id=sczhou/CodeFormer) -->
13
 
14
 
15
  [Shangchen Zhou](https://shangchenzhou.com/), [Kelvin C.K. Chan](https://ckkelvinchan.github.io/), [Chongyi Li](https://li-chongyi.github.io/), [Chen Change Loy](https://www.mmlab-ntu.com/person/ccloy/)
 
7
  [Paper](https://arxiv.org/abs/2206.11253) | [Project Page](https://shangchenzhou.com/projects/CodeFormer/) | [Video](https://youtu.be/d3VDpkXlueI)
8
 
9
 
10
+ <a href="https://colab.research.google.com/drive/1m52PNveE4PBhYrecj34cnpEeiHcC5LTb?usp=sharing"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="google colab logo"></a> [![Hugging Face](https://img.shields.io/badge/Demo-%F0%9F%A4%97%20Hugging%20Face-blue)](https://huggingface.co/spaces/sczhou/CodeFormer) [![Replicate](https://img.shields.io/badge/Demo-%F0%9F%9A%80%20Replicate-blue)](https://replicate.com/sczhou/codeformer) ![visitors](https://visitor-badge.laobi.icu/badge?page_id=sczhou/CodeFormer)
11
 
12
+ <!-- ![visitors](https://visitor-badge.glitch.me/badge?page_id=sczhou/CodeFormer) -->
13
 
14
 
15
  [Shangchen Zhou](https://shangchenzhou.com/), [Kelvin C.K. Chan](https://ckkelvinchan.github.io/), [Chongyi Li](https://li-chongyi.github.io/), [Chen Change Loy](https://www.mmlab-ntu.com/person/ccloy/)
app.py CHANGED
@@ -193,7 +193,7 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
193
  imwrite(restored_img, str(save_path))
194
 
195
  restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
196
- return restored_img
197
 
198
 
199
 
@@ -230,7 +230,7 @@ Redistribution and use for non-commercial purposes should follow this license.
230
 
231
  If you have any questions, please feel free to reach me out at <b>shangchenzhou@gmail.com</b>.
232
 
233
- ![visitors](https://visitor-badge.glitch.me/badge?page_id=sczhou/CodeFormer)
234
  """
235
 
236
  demo = gr.Interface(
@@ -242,6 +242,7 @@ demo = gr.Interface(
242
  gr.Slider(0, 1, value=0.5, step=0.01, label='Codeformer_Fidelity: 0 for better quality, 1 for better identity')
243
  ], [
244
  gr.outputs.Image(type="numpy", label="Output"),
 
245
  ],
246
  title=title,
247
  description=description,
 
193
  imwrite(restored_img, str(save_path))
194
 
195
  restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
196
+ return restored_img, save_path
197
 
198
 
199
 
 
230
 
231
  If you have any questions, please feel free to reach me out at <b>shangchenzhou@gmail.com</b>.
232
 
233
+ ![visitors](https://visitor-badge.laobi.icu/badge?page_id=sczhou/CodeFormer)
234
  """
235
 
236
  demo = gr.Interface(
 
242
  gr.Slider(0, 1, value=0.5, step=0.01, label='Codeformer_Fidelity: 0 for better quality, 1 for better identity')
243
  ], [
244
  gr.outputs.Image(type="numpy", label="Output"),
245
+ gr.outputs.File(label="Download the output")
246
  ],
247
  title=title,
248
  description=description,
inference_codeformer.py CHANGED
@@ -17,25 +17,36 @@ pretrain_model_url = {
17
  }
18
 
19
  def set_realesrgan():
20
- if not torch.cuda.is_available(): # CPU
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  import warnings
22
- warnings.warn('The unoptimized RealESRGAN is slow on CPU. We do not use it. '
23
- 'If you really want to use it, please modify the corresponding codes.',
 
24
  category=RuntimeWarning)
25
- bg_upsampler = None
26
- else:
27
- from basicsr.archs.rrdbnet_arch import RRDBNet
28
- from basicsr.utils.realesrgan_utils import RealESRGANer
29
- model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
30
- bg_upsampler = RealESRGANer(
31
- scale=2,
32
- model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
33
- model=model,
34
- tile=args.bg_tile,
35
- tile_pad=40,
36
- pre_pad=0,
37
- half=True) # need to set False in CPU mode
38
- return bg_upsampler
39
 
40
  if __name__ == '__main__':
41
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
@@ -71,10 +82,11 @@ if __name__ == '__main__':
71
 
72
  # ------------------ set up face upsampler ------------------
73
  if args.face_upsample:
74
- if bg_upsampler is not None:
75
- face_upsampler = bg_upsampler
76
- else:
77
- face_upsampler = set_realesrgan()
 
78
  else:
79
  face_upsampler = None
80
 
 
17
  }
18
 
19
  def set_realesrgan():
20
+ from basicsr.archs.rrdbnet_arch import RRDBNet
21
+ from basicsr.utils.realesrgan_utils import RealESRGANer
22
+
23
+ cuda_is_available = torch.cuda.is_available()
24
+ half = True if cuda_is_available else False
25
+ model = RRDBNet(
26
+ num_in_ch=3,
27
+ num_out_ch=3,
28
+ num_feat=64,
29
+ num_block=23,
30
+ num_grow_ch=32,
31
+ scale=2,
32
+ )
33
+ upsampler = RealESRGANer(
34
+ scale=2,
35
+ model_path="https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth",
36
+ model=model,
37
+ tile=args.bg_tile,
38
+ tile_pad=40,
39
+ pre_pad=0,
40
+ half=half, # need to set False in CPU mode
41
+ )
42
+
43
+ if not cuda_is_available: # CPU
44
  import warnings
45
+ warnings.warn('Runing on CPU now... '
46
+ 'The unoptimized RealESRGAN is slow on CPU. '
47
+ 'If you want to disable it, please remove `--bg_upsampler` and `--face_upsample` in command.',
48
  category=RuntimeWarning)
49
+ return upsampler
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  if __name__ == '__main__':
52
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
82
 
83
  # ------------------ set up face upsampler ------------------
84
  if args.face_upsample:
85
+ face_upsampler = None
86
+ # if bg_upsampler is not None:
87
+ # face_upsampler = bg_upsampler
88
+ # else:
89
+ # face_upsampler = set_realesrgan()
90
  else:
91
  face_upsampler = None
92