Skip to content

Commit

Permalink
feat: Support CPU mode (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky committed Dec 29, 2023
1 parent 614a04b commit 28e29bd
Show file tree
Hide file tree
Showing 20 changed files with 1,506 additions and 32 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/CI-Linux-x64-Clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ on:
- LICENSE
workflow_dispatch:

env:
GITHUB_ACTIONS: true

jobs:
Linux-x64-Clang:
strategy:
Expand Down Expand Up @@ -66,9 +69,14 @@ jobs:
cmake -DOpenMP_CXX_FLAGS="-fexceptions -frtti" ..
cmake --build . -j 4
- name: dist
- name: Test
run: |
cp src/build/realesrgan_ncnn_vulkan_wrapper.*.so src/realesrgan_ncnn_py
pdm install
pdm run test
- name: dist
run: |
mkdir dist
cp -r src/realesrgan_ncnn_py dist
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/CI-Linux-x64-GCC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ on:
- LICENSE
workflow_dispatch:

env:
GITHUB_ACTIONS: true

jobs:
Linux-x64-GCC:
strategy:
Expand Down Expand Up @@ -63,9 +66,14 @@ jobs:
cmake -DOpenMP_CXX_FLAGS="-fexceptions -frtti" ..
cmake --build . -j 4
- name: dist
- name: Test
run: |
cp src/build/realesrgan_ncnn_vulkan_wrapper.*.so src/realesrgan_ncnn_py
pdm install
pdm run test
- name: dist
run: |
mkdir dist
cp -r src/realesrgan_ncnn_py dist
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/CI-MacOS-Universal-Clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:
workflow_dispatch:

env:
GITHUB_ACTIONS: true
DEVELOPER_DIR: /Applications/Xcode_14.2.app/Contents/Developer

jobs:
Expand Down Expand Up @@ -102,6 +103,11 @@ jobs:
if: matrix.python-version == '3.11'
run: lipo -create src/build-arm64/realesrgan_ncnn_vulkan_wrapper.cpython-311-darwin.so src/build-x86_64/realesrgan_ncnn_vulkan_wrapper.cpython-311-darwin.so -o src/realesrgan_ncnn_py/realesrgan_ncnn_vulkan_wrapper.cpython-311-darwin.so

- name: Test
run: |
pdm install
pdm run test
- name: dist
run: |
mkdir dist
Expand Down
12 changes: 10 additions & 2 deletions .github/workflows/CI-Windows-x64-MSVC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ on:
- LICENSE
workflow_dispatch:

env:
GITHUB_ACTIONS: true

jobs:
windows:
strategy:
Expand Down Expand Up @@ -63,11 +66,16 @@ jobs:
cmake -A x64 -DCMAKE_CXX_FLAGS="-frtti -fexceptions" ..
cmake --build . --config Release -j 4
- name: dist
- name: Test
run: |
mkdir dist
echo F | xcopy .\src\build\Release\realesrgan_ncnn_vulkan_wrapper.*.pyd .\src\realesrgan_ncnn_py
echo F | xcopy .\tests\vulkan-1.dll .\src\realesrgan_ncnn_py
pdm install
pdm run test
- name: dist
run: |
mkdir dist
echo D | xcopy .\src\realesrgan_ncnn_py dist
- name: upload
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/Release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
workflow_dispatch:

env:
GITHUB_ACTIONS: true
DEVELOPER_DIR: /Applications/Xcode_14.2.app/Contents/Developer

jobs:
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/test_pip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: test_pip

on:
schedule:
- cron: "0 19 * 1 *"
workflow_dispatch:

env:
GITHUB_ACTIONS: true

jobs:
test:
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
os-version: ["macos-latest", "windows-latest", "ubuntu-20.04"]

runs-on: ${{ matrix.os-version }}
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
architecture: x64

- name: vulkan dll
if: matrix.os-version == 'windows-latest'
run: echo F | xcopy .\tests\vulkan-1.dll C:\Windows\System32

- name: Check Python version
run: |
python --version
- name: Test
run: |
pip install --upgrade pip chardet
pip install pathlib opencv-python scikit-image Pillow pytest pytest-cov realesrgan-ncnn-py
python -m pytest tests
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "src/Real-ESRGAN-ncnn-vulkan"]
path = src/Real-ESRGAN-ncnn-vulkan
url = https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan
[submodule "src/pybind11"]
path = src/pybind11
url = https://github.com/pybind/pybind11
[submodule "src/Real-ESRGAN-ncnn-vulkan/src/ncnn"]
path = src/Real-ESRGAN-ncnn-vulkan/src/ncnn
url = https://github.com/Tencent/ncnn
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ repos:
rev: v1.7.1
hooks:
- id: mypy
args: [src, tests]
args: [src/realesrgan_ncnn_py, tests]
pass_filenames: false
additional_dependencies:
- types-requests
Expand Down
46 changes: 29 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ Python Binding for realesrgan-ncnn-py with PyBind11
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/realesrgan-ncnn-py)

Real-ESRGAN aims at developing Practical Algorithms for General Image/Video Restoration.
We extend the powerful ESRGAN to a practical restoration application (namely, Real-ESRGAN), which is trained with pure synthetic data.
We extend the powerful ESRGAN to a practical restoration application (namely, Real-ESRGAN), which is trained with pure
synthetic data.
This wrapper provides an easy-to-use interface for running the pre-trained Real-ESRGAN model.

### Current building status matrix

| System | Status | CPU (32bit) | CPU (64bit) | GPU (32bit) | GPU (64bit) |
| :-----------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :---------: | :---------: | :---------: | :----------------: |
| Linux (Clang) | [![CI-Linux-x64-Clang](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Linux-x64-Clang.yml/badge.svg)](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Linux-x64-Clang.yml) || || :white_check_mark: |
| Linux (GCC) | [![CI-Linux-x64-GCC](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Linux-x64-GCC.yml/badge.svg)](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Linux-x64-GCC.yml) || || :white_check_mark: |
| Windows | [![CI-Windows-x64-MSVC](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Windows-x64-MSVC.yml/badge.svg)](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Windows-x64-MSVC.yml) || || :white_check_mark: |
| MacOS | [![CI-MacOS-Universal-Clang](https://github.com/Tohrusky/realcugan-ncnn-py/actions/workflows/CI-MacOS-Universal-Clang.yml/badge.svg)](https://github.com/Tohrusky/realcugan-ncnn-py/actions/workflows/CI-MacOS-Universal-Clang.yml) || || :white_check_mark: |
| MacOS (ARM) | [![CI-MacOS-Universal-Clang](https://github.com/Tohrusky/realcugan-ncnn-py/actions/workflows/CI-MacOS-Universal-Clang.yml/badge.svg)](https://github.com/Tohrusky/realcugan-ncnn-py/actions/workflows/CI-MacOS-Universal-Clang.yml) || || :white_check_mark: |
| System | Status | CPU (32bit) | CPU (64bit) | GPU (32bit) | GPU (64bit) |
| :-----------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :---------: | :----------------: | :---------: | :----------------: |
| Linux (Clang) | [![CI-Linux-x64-Clang](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Linux-x64-Clang.yml/badge.svg)](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Linux-x64-Clang.yml) || :white_check_mark: || :white_check_mark: |
| Linux (GCC) | [![CI-Linux-x64-GCC](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Linux-x64-GCC.yml/badge.svg)](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Linux-x64-GCC.yml) || :white_check_mark: || :white_check_mark: |
| Windows | [![CI-Windows-x64-MSVC](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Windows-x64-MSVC.yml/badge.svg)](https://github.com/Tohrusky/realesrgan-ncnn-py/actions/workflows/CI-Windows-x64-MSVC.yml) || :white_check_mark: || :white_check_mark: |
| MacOS | [![CI-MacOS-Universal-Clang](https://github.com/Tohrusky/realcugan-ncnn-py/actions/workflows/CI-MacOS-Universal-Clang.yml/badge.svg)](https://github.com/Tohrusky/realcugan-ncnn-py/actions/workflows/CI-MacOS-Universal-Clang.yml) || :white_check_mark: || :white_check_mark: |
| MacOS (ARM) | [![CI-MacOS-Universal-Clang](https://github.com/Tohrusky/realcugan-ncnn-py/actions/workflows/CI-MacOS-Universal-Clang.yml/badge.svg)](https://github.com/Tohrusky/realcugan-ncnn-py/actions/workflows/CI-MacOS-Universal-Clang.yml) || :white_check_mark: || :white_check_mark: |

# Usage

Expand Down Expand Up @@ -56,14 +57,16 @@ realesrgan = Realesrgan(gpuid: int = 0, tta_mode: bool = False, tilesize: int =

```

Here, gpuid specifies the GPU device to use, tta_mode enables test-time augmentation, tilesize specifies the tile size for processing (0 or >= 32), and model specifies the num of the pre-trained model to use.
Here, gpuid specifies the GPU device to use, tta_mode enables test-time augmentation, tilesize specifies the tile size
for processing (0 or >= 32), and model specifies the num of the pre-trained model to use.

Once the model is initialized, you can use the upscale method to super-resolve your images:

### Pillow

```python
from PIL import Image

realesrgan = Realesrgan(gpuid=0)
with Image.open("input.jpg") as image:
image = realesrgan.process_pil(image)
Expand All @@ -74,6 +77,7 @@ with Image.open("input.jpg") as image:

```python
import cv2

realesrgan = Realesrgan(gpuid=0)
image = cv2.imdecode(np.fromfile("input.jpg", dtype=np.uint8), cv2.IMREAD_COLOR)
image = realesrgan.process_cv2(image)
Expand All @@ -84,9 +88,10 @@ cv2.imencode(".jpg", image)[1].tofile("output_cv2.jpg")

```python
import subprocess as sp

# your ffmpeg parameters
command_out = [FFMPEG_BIN,........]
command_in = [FFMPEG_BIN,........]
command_out = [FFMPEG_BIN, ........]
command_in = [FFMPEG_BIN, ........]
pipe_out = sp.Popen(command_out, stdout=sp.PIPE, bufsize=10 ** 8)
pipe_in = sp.Popen(command_in, stdin=sp.PIPE)
realesrgan = Realesrgan(gpuid=0)
Expand All @@ -102,20 +107,27 @@ while True:

[here](https://github.com/Tohrusky/realesrgan-ncnn-py/blob/main/.github/workflows/Release.yml)

_The project just only been tested in Ubuntu 18+ and Debian 9+ environments on Linux, so if the project does not work on your system, please try building it._
_The project just only been tested in Ubuntu 18+ and Debian 9+ environments on Linux, so if the project does not work on
your system, please try building it._

# References

The following references were used in the development of this project:

[xinntao/Real-ESRGAN-ncnn-vulkan](https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan) - This project was the main inspiration for our work. It provided the core implementation of the Real-ESRGAN algorithm using the ncnn and Vulkan libraries.
[xinntao/Real-ESRGAN-ncnn-vulkan](https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan) - This project was the main
inspiration for our work. It provided the core implementation of the Real-ESRGAN algorithm using the ncnn and Vulkan
libraries.

[Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN) - Real-ESRGAN is an AI super resolution model, aims at developing Practical Algorithms for General Image/Video Restoration.
[Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN) - Real-ESRGAN is an AI super resolution model, aims at developing
Practical Algorithms for General Image/Video Restoration.

[media2x/realsr-ncnn-vulkan-python](https://github.com/media2x/realsr-ncnn-vulkan-python) - This project was used as a reference for implementing the wrapper. _Special thanks_ to the original author for sharing the code.
[media2x/realsr-ncnn-vulkan-python](https://github.com/media2x/realsr-ncnn-vulkan-python) - This project was used as a
reference for implementing the wrapper. _Special thanks_ to the original author for sharing the code.

[ncnn](https://github.com/Tencent/ncnn) - ncnn is a high-performance neural network inference framework developed by Tencent AI Lab.
[ncnn](https://github.com/Tencent/ncnn) - ncnn is a high-performance neural network inference framework developed by
Tencent AI Lab.

# License

This project is licensed under the BSD 3-Clause - see the [LICENSE file](https://github.com/Tohrusky/realesrgan-ncnn-py/blob/main/LICENSE) for details.
This project is licensed under the BSD 3-Clause - see
the [LICENSE file](https://github.com/Tohrusky/realesrgan-ncnn-py/blob/main/LICENSE) for details.
1 change: 0 additions & 1 deletion src/Real-ESRGAN-ncnn-vulkan
Submodule Real-ESRGAN-ncnn-vulkan deleted from 37026f
46 changes: 46 additions & 0 deletions src/Real-ESRGAN-ncnn-vulkan/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
The MIT License (MIT)

Copyright (c) 2021 Xintao Wang

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 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.

------------------------
The following is the License of realsr-ncnn-vulkan

The MIT License (MIT)

Copyright (c) 2019 nihui

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 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.
1 change: 1 addition & 0 deletions src/Real-ESRGAN-ncnn-vulkan/src/ncnn
Submodule ncnn added at 6125c9
Loading

0 comments on commit 28e29bd

Please sign in to comment.