Skip to content

Commit

Permalink
submit
Browse files Browse the repository at this point in the history
  • Loading branch information
harisreedhar committed Aug 2, 2023
1 parent dcdffde commit cdc6fe2
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
38 changes: 38 additions & 0 deletions GFPGAN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import cv2
# import torch
import onnxruntime
import numpy as np

# gfpgan converted to onnx
# using https://github.com/xuanandsix/GFPGAN-onnxruntime-demo
# same inference code for GFPGANv1.2, GFPGANv1.3, GFPGANv1.4

class GFPGAN:
def __init__(self, model_path="GFPGANv1.4.onnx", device='cpu'):
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)
self.resolution = self.session.get_inputs()[0].shape[-2:]

def preprocess(self, img):
img = cv2.resize(img, self.resolution, interpolation=cv2.INTER_LINEAR)
img = img.astype(np.float32)[:,:,::-1] / 255.0
img = img.transpose((2, 0, 1))
img = (img - 0.5) / 0.5
img = np.expand_dims(img, axis=0).astype(np.float32)
return img

def postprocess(self, img):
img = (img.transpose(1,2,0).clip(-1,1) + 1) * 0.5
img = (img * 255)[:,:,::-1]
img = img.clip(0, 255).astype('uint8')
return img

def enhance(self, img):
img = self.preprocess(img)
output = self.session.run(None, {'input':img})[0][0]
output = self.postprocess(output)
return output
34 changes: 34 additions & 0 deletions GPEN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import cv2
# import torch
import onnxruntime
import numpy as np

class GPEN:
def __init__(self, model_path="GPEN-BFR-512.onnx", device='cpu'):
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)
self.resolution = self.session.get_inputs()[0].shape[-2:]

def preprocess(self, img):
img = cv2.resize(img, self.resolution, interpolation=cv2.INTER_LINEAR)
img = img.astype(np.float32)[:,:,::-1] / 255.0
img = img.transpose((2, 0, 1))
img = (img - 0.5) / 0.5
img = np.expand_dims(img, axis=0).astype(np.float32)
return img

def postprocess(self, img):
img = (img.transpose(1,2,0).clip(-1,1) + 1) * 0.5
img = (img * 255)[:,:,::-1]
img = img.clip(0, 255).astype('uint8')
return img

def enhance(self, img):
img = self.preprocess(img)
output = self.session.run(None, {'input':img})[0][0]
output = self.postprocess(output)
return output
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License
Copyright (c) [2023] [Harisreedhar]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

**The Software may include models or any other intellectual property that are owned and licensed separately by their respective authors or copyright holders. The ownership and licensing terms of these components supersede the terms of this MIT License. It is your responsibility to review and comply with the individual licensing terms of such components.**

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Face-Upscalers-ONNX
ONNX-Powered Inference for State-of-the-Art Face Upscalers

## MIT License for Repository Codes

The codebase in this repository, **excluding the converted models**, is licensed under the permissive MIT License. This means you can freely use, modify, and distribute the code in this repository without any restrictions, provided that you include the original copyright notice and disclaimer.
38 changes: 38 additions & 0 deletions codeformer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import cv2
# import torch
import onnxruntime
import numpy as np

# codeformer converted to onnx
# using https://github.com/redthing1/CodeFormer

class CodeFormer:
def __init__(self, model_path="codeformer.onnx", device='cpu'):
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)
self.resolution = self.session.get_inputs()[0].shape[-2:]

def preprocess(self, img, w):
img = cv2.resize(img, self.resolution, interpolation=cv2.INTER_LINEAR)
img = img.astype(np.float32)[:,:,::-1] / 255.0
img = img.transpose((2, 0, 1))
img = (img - 0.5) / 0.5
img = np.expand_dims(img, axis=0).astype(np.float32)
w = np.array([w], dtype=np.double)
return img, w

def postprocess(self, img):
img = (img.transpose(1,2,0).clip(-1,1) + 1) * 0.5
img = (img * 255)[:,:,::-1]
img = img.clip(0, 255).astype('uint8')
return img

def enhance(self, img, w=0.9):
img, w = self.preprocess(img, w)
output = self.session.run(None, {'x':img, 'w':w})[0][0]
output = self.postprocess(output)
return output

0 comments on commit cdc6fe2

Please sign in to comment.