va1 commited on
Commit
db16bea
·
verified ·
1 Parent(s): 4afb079

Update inference_realesrgan.py

Browse files
Files changed (1) hide show
  1. inference_realesrgan.py +11 -18
inference_realesrgan.py CHANGED
@@ -4,8 +4,9 @@ import torch
4
  import cv2
5
  import numpy as np
6
  from PIL import Image
 
7
 
8
- # 🚀 Load Real-ESRGAN model manually from weight file
9
  MODEL_PATH = "weight/RealESRGAN_x4plus.pth"
10
 
11
  # Ensure the model exists
@@ -13,21 +14,13 @@ if not os.path.exists(MODEL_PATH):
13
  print(f"[ERROR] Model file {MODEL_PATH} not found. Please download it using download_model.py.")
14
  exit(1)
15
 
16
- # 🚀 Fix: Manually load the ESRGAN model without requiring 'realesrgan' package
17
- from torch.hub import load_state_dict_from_url
18
- model_url = "https://huggingface.co/va1/CViT-Deepfake-Model/resolve/main/RealESRGAN_x4plus.pth"
19
-
20
- # Load weights if missing
21
- if not os.path.exists(MODEL_PATH):
22
- torch.hub.download_url_to_file(model_url, MODEL_PATH)
23
-
24
- # Initialize ESRGAN model
25
- device = "cuda" if torch.cuda.is_available() else "cpu"
26
-
27
- # 🚀 Fix: Directly use ESRGAN weight without requiring 'realesrgan'
28
- from model.esrgan import RealESRGANer
29
- upsampler = RealESRGANer(scale=4, model_path=MODEL_PATH, model=None, tile=0, tile_pad=10, pre_pad=0,
30
- half=True if torch.cuda.is_available() else False)
31
 
32
  def upscale_image(input_path, output_path):
33
  """Upscales an image using Real-ESRGAN."""
@@ -43,7 +36,7 @@ def upscale_image(input_path, output_path):
43
 
44
  # Apply super-resolution
45
  print(f"[INFO] Upscaling image: {input_path}")
46
- output, _ = upsampler.enhance(np.array(img_pil), outscale=2)
47
  output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)
48
 
49
  # Save upscaled image
@@ -59,4 +52,4 @@ if __name__ == "__main__":
59
  parser.add_argument("-o", "--output", type=str, required=True, help="Path to save the upscaled image")
60
  args = parser.parse_args()
61
 
62
- upscale_image(args.input, args.output)
 
4
  import cv2
5
  import numpy as np
6
  from PIL import Image
7
+ from realesrgan import RealESRGANer
8
 
9
+ # Load Real-ESRGAN model manually from weight file
10
  MODEL_PATH = "weight/RealESRGAN_x4plus.pth"
11
 
12
  # Ensure the model exists
 
14
  print(f"[ERROR] Model file {MODEL_PATH} not found. Please download it using download_model.py.")
15
  exit(1)
16
 
17
+ # Initialize the RealESRGANer instance
18
+ realesrgan = RealESRGANer(
19
+ scale=4, # Assuming a scale of 4x
20
+ model_path=MODEL_PATH,
21
+ device='cuda' if torch.cuda.is_available() else 'cpu', # Use GPU if available
22
+ half=False # Set to True if you want to use half precision
23
+ )
 
 
 
 
 
 
 
 
24
 
25
  def upscale_image(input_path, output_path):
26
  """Upscales an image using Real-ESRGAN."""
 
36
 
37
  # Apply super-resolution
38
  print(f"[INFO] Upscaling image: {input_path}")
39
+ output, _ = realesrgan.enhance(np.array(img_pil), outscale=2)
40
  output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)
41
 
42
  # Save upscaled image
 
52
  parser.add_argument("-o", "--output", type=str, required=True, help="Path to save the upscaled image")
53
  args = parser.parse_args()
54
 
55
+ upscale_image(args.input, args.output)