diff --git "a/upscaler/codeformer.py" "b/upscaler/codeformer.py" --- "a/upscaler/codeformer.py" +++ "b/upscaler/codeformer.py" @@ -1,4044 +1,46 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Swapm/upscaler/codeformer.py at main · G-force78/Swapm - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
- Skip to content - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - -
- - -
-
- - - - - - -
-
-
-

- Global navigation -

-
-
-
-
- -
-
-
- -
- - -
- - - - -
-
-
- - -

© 2024 GitHub, Inc.

- -
-
-
- -
- -
-
- - - -
-
- - - - -
-
-
-

- Navigate back to -

-
-
- -
-
-
- - - - -
-
- -
- -
-
- -
-
- - -
- - - Create new... - - - - - - - -Issues - - -Pull requests - -
- - - - - - - Notifications - - - - -
- - - - - - - -
-
-
-

- Account menu -

-
-
- - - G-force78 - -
- - - Create new... - - - - - -
-
-
-
- -
-
-
- -
- - - -
-
-
- -
-
- -
-
- -
- -
- - - - - -
-
-
- - - -
- - - - -
- -
- - - - - - - - -
- - - - - - -
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - -
- -
- - - - G-force78  /   - Swapm  /   - -
-
- - - -
- - -
-
- Clear Command Palette -
-
- - - -
-
- Tip: - Type # to search pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search discussions -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type ! to search projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search teams -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search people and organizations -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type > to activate command mode -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Go to your accessibility settings to change your keyboard shortcuts -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type author:@me to search your content -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:pr to filter to pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:issue to filter to issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:project to filter to projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:open to filter to open content -
-
- Type ? for help and tips -
-
-
- -
- -
-
- We’ve encountered an error and some results aren't available at this time. Type a new search or try again later. -
-
- - No results matched your search - - - - - - - - - - -
- - - - - Search for issues and pull requests - - # - - - - Search for issues, pull requests, discussions, and projects - - # - - - - Search for organizations, repositories, and users - - @ - - - - Search for projects - - ! - - - - Search for files - - / - - - - Activate command mode - - > - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Filter to pull requests - - # is:pr - - - - Filter to issues - - # is:issue - - - - Filter to discussions - - # is:discussion - - - - Filter to projects - - # is:project - - - - Filter to open issues, pull requests, and discussions - - # is:open - - - - - - - - - - - - - - - - -
-
-
- -
- - - - - - - - - - -
- - -
-
-
- - - -
- This repository has been archived by the owner on Feb 17, 2024. It is now read-only. -
- - - - - - - - - - - - - -
- Open in github.dev - Open in a new github.dev tab - Open in codespace - - - - - - - - - - - - - - - -

Files

t

Latest commit

 

History

History
executable file
·
37 lines (29 loc) · 1.38 KB

codeformer.py

File metadata and controls

executable file
·
37 lines (29 loc) · 1.38 KB

Symbols

Find definitions and references for functions and other symbols in this file by clicking a symbol below or in the code.
r
  • class
    CodeFormerEnhancer
    • func
      __init__
    • func
      enhance
-
- - - - -
- -
- -
-
- -
- - - - - - - - - - - - - - - - - - - - - - -
- -
-
- - - +import cv2 +import torch +import onnx +import onnxruntime +import numpy as np + +import time + +# codeformer converted to onnx +# using https://github.com/redthing1/CodeFormer + + +class CodeFormerEnhancer: + def __init__(self, model_path="codeformer.onnx", device="cpu"): + model = onnx.load(model_path) + session_options = onnxruntime.SessionOptions() + session_options.graph_optimization_level = ( + onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL + ) + providers = ["CPUExecutionProvider"] + if device == "cuda": + providers = [ + ("CUDAExecutionProvider", {"cudnn_conv_algo_search": "DEFAULT"}), + "CPUExecutionProvider", + ] + self.session = onnxruntime.InferenceSession( + model_path, sess_options=session_options, providers=providers + ) + + def enhance(self, img, w=0.9): + img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR) + img = img.astype(np.float32)[:, :, ::-1] / 255.0 + img = img.transpose((2, 0, 1)) + nrm_mean = np.array([0.5, 0.5, 0.5]).reshape((-1, 1, 1)) + nrm_std = np.array([0.5, 0.5, 0.5]).reshape((-1, 1, 1)) + img = (img - nrm_mean) / nrm_std + + img = np.expand_dims(img, axis=0) + + out = self.session.run( + None, {"x": img.astype(np.float32), "w": np.array([w], dtype=np.double)} + )[0] + out = (out[0].transpose(1, 2, 0).clip(-1, 1) + 1) * 0.5 + out = (out * 255)[:, :, ::-1] + + return out.astype("uint8")