├── .gitignore ├── tests ├── 0_default.png └── test_upscale.py ├── .gitmodules ├── pyproject.toml ├── MANIFEST.in ├── realsr_ncnn_vulkan_python ├── realsr.i ├── realsr_wrapped.h ├── realsr_wrapped.cpp ├── realsr_ncnn_vulkan.py └── CMakeLists.txt ├── setup.cfg ├── setup.py ├── LICENSE ├── README.md └── .github └── workflows ├── ci.yml └── release.yml /.gitignore: -------------------------------------------------------------------------------- 1 | /src/build/ -------------------------------------------------------------------------------- /tests/0_default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/media2x/realsr-ncnn-vulkan-python/HEAD/tests/0_default.png -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "realsr_ncnn_vulkan_python/realsr-ncnn-vulkan"] 2 | path = realsr_ncnn_vulkan_python/realsr-ncnn-vulkan 3 | url = https://github.com/nihui/realsr-ncnn-vulkan.git 4 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = [ 3 | "setuptools>=40.8.0", 4 | "wheel", 5 | "setuptools_scm", 6 | "cmake>=3.9", 7 | "cmake-build-extension", 8 | ] 9 | build-backend = "setuptools.build_meta" 10 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include realsr_ncnn_vulkan_python/realsr-ncnn-vulkan/models * 2 | recursive-include realsr_ncnn_vulkan_python/realsr-ncnn-vulkan/src * 3 | include realsr_ncnn_vulkan_python/realsr-ncnn-vulkan/LICENSE 4 | include realsr_ncnn_vulkan_python/realsr-ncnn-vulkan/README.md 5 | -------------------------------------------------------------------------------- /realsr_ncnn_vulkan_python/realsr.i: -------------------------------------------------------------------------------- 1 | %module realsr_ncnn_vulkan_wrapper 2 | 3 | %include "cpointer.i" 4 | %include "carrays.i" 5 | %include "std_string.i" 6 | %include "std_wstring.i" 7 | %include "stdint.i" 8 | %include "pybuffer.i" 9 | 10 | %pybuffer_mutable_string(unsigned char *d); 11 | %pointer_functions(std::string, str_p); 12 | %pointer_functions(std::wstring, wstr_p); 13 | 14 | %{ 15 | #include "realsr.h" 16 | #include "realsr_wrapped.h" 17 | %} 18 | 19 | class RealSR 20 | { 21 | public: 22 | RealSR(int gpuid, bool tta_mode = false, int num_threads = 1); 23 | ~RealSR(); 24 | 25 | // realsr parameters 26 | int scale; 27 | int tilesize; 28 | int prepadding; 29 | }; 30 | %include "realsr_wrapped.h" -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = realsr-ncnn-vulkan-python 3 | version = 1.0.6 4 | author = ArchieMeng 5 | author_email = archiemeng@protonmail.com 6 | maintainer = K4YT3X 7 | maintainer_email = i@k4yt3x.com 8 | license = MIT 9 | license_file = LICENSE 10 | platform = any 11 | description = A Python FFI of nihui/realsr-ncnn-vulkan achieved with SWIG 12 | url = https://github.com/media2x/realsr-ncnn-vulkan-python 13 | long_description = file: README.md 14 | long_description_content_type = text/markdown 15 | classifiers = 16 | Environment :: Console 17 | Programming Language :: Python 18 | Programming Language :: Python :: 3 19 | Operating System :: OS Independent 20 | keywords = 21 | realsr 22 | ncnn 23 | vulkan 24 | swig 25 | 26 | [options] 27 | packages = find: 28 | install_requires = pillow 29 | python_requires = >=3.6 30 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import os 4 | import pathlib 5 | import setuptools 6 | 7 | import cmake_build_extension 8 | 9 | cmake_flags = [ 10 | "-DBUILD_SHARED_LIBS:BOOL=OFF", 11 | "-DCALL_FROM_SETUP_PY:BOOL=ON", 12 | ] 13 | if "CMAKE_FLAGS" in os.environ: 14 | flags = os.environ["CMAKE_FLAGS"] 15 | cmake_flags.extend(flags.split()) 16 | 17 | setuptools.setup( 18 | ext_modules=[ 19 | cmake_build_extension.CMakeExtension( 20 | name="realsr-ncnn-vulkan-python", 21 | install_prefix="realsr_ncnn_vulkan_python", 22 | write_top_level_init="from .realsr_ncnn_vulkan import Realsr, RealSR, wrapped", 23 | source_dir=str(pathlib.Path(__file__).parent / "realsr_ncnn_vulkan_python"), 24 | cmake_configure_options=cmake_flags, 25 | ) 26 | ], 27 | cmdclass={"build_ext": cmake_build_extension.BuildExtension}, 28 | ) 29 | -------------------------------------------------------------------------------- /realsr_ncnn_vulkan_python/realsr_wrapped.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by archiemeng on 29/3/21. 3 | // 4 | 5 | #ifndef REALSR_NCNN_VULKAN_REALSR_WRAPPED_H 6 | #define REALSR_NCNN_VULKAN_REALSR_WRAPPED_H 7 | #include "realsr.h" 8 | 9 | // wrapper class of ncnn::Mat 10 | typedef struct Image{ 11 | unsigned char *data; 12 | int w; 13 | int h; 14 | int elempack; 15 | Image(unsigned char *d, int w, int h, int channels) { 16 | this->data = d; 17 | this->w = w; 18 | this->h = h; 19 | this->elempack = channels; 20 | } 21 | 22 | } Image; 23 | 24 | union StringType { 25 | std::string *str; 26 | std::wstring *wstr; 27 | }; 28 | 29 | class RealSRWrapped : public RealSR { 30 | public: 31 | RealSRWrapped(int gpuid, bool tta_mode = false, int num_threads = 1); 32 | int load(const StringType ¶mpath, const StringType &modelpath); 33 | int process(const Image &inimage, Image outimage); 34 | }; 35 | 36 | int get_gpu_count(); 37 | uint32_t get_heap_budget(int gpuid); 38 | #endif //REALSR_NCNN_VULKAN_REALSR_WRAPPED_H 39 | -------------------------------------------------------------------------------- /realsr_ncnn_vulkan_python/realsr_wrapped.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by archiemeng on 29/3/21. 3 | // 4 | #include "realsr_wrapped.h" 5 | 6 | RealSRWrapped::RealSRWrapped(int gpuid, bool tta_mode, int num_threads) : RealSR(gpuid, tta_mode, num_threads) {} 7 | 8 | int RealSRWrapped::load(const StringType ¶mpath, const StringType &modelpath) { 9 | #if _WIN32 10 | return RealSR::load(*parampath.wstr, *modelpath.wstr); 11 | #else 12 | return RealSR::load(*parampath.str, *modelpath.str); 13 | #endif 14 | } 15 | 16 | int RealSRWrapped::process(const Image &inimage, Image outimage) { 17 | int c = inimage.elempack; 18 | ncnn::Mat inimagemat = ncnn::Mat(inimage.w, inimage.h, (void*) inimage.data, (size_t) c, c); 19 | ncnn::Mat outimagemat = ncnn::Mat(outimage.w, outimage.h, (void*) outimage.data, (size_t) c, c); 20 | return RealSR::process(inimagemat, outimagemat); 21 | }; 22 | 23 | uint32_t get_heap_budget(int gpuid) { 24 | return ncnn::get_gpu_device(gpuid)->get_heap_budget(); 25 | } 26 | 27 | int get_gpu_count() { 28 | return ncnn::get_gpu_count(); 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 ArchieMeng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /tests/test_upscale.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | import logging 4 | import os 5 | from pathlib import Path 6 | 7 | from PIL import Image, ImageChops, ImageStat 8 | from realsr_ncnn_vulkan_python import RealSR, wrapped 9 | 10 | tests_path = Path(__file__).parent 11 | images_path = ( 12 | tests_path.parent / "realsr_ncnn_vulkan_python" / "realsr-ncnn-vulkan" / "images" 13 | ) 14 | 15 | # Use GPU 0 if available. 16 | if wrapped.get_gpu_count() > 0: 17 | gpu_id = 0 18 | num_threads = 1 19 | else: 20 | gpu_id = -1 21 | # use all cores with CPU mode 22 | num_threads = os.cpu_count() 23 | 24 | 25 | def _calc_image_diff(image0: Image.Image, image1: Image.Image) -> float: 26 | """ 27 | calculate the percentage of differences between two images 28 | 29 | :param image0 Image.Image: the first frame 30 | :param image1 Image.Image: the second frame 31 | :rtype float: the percent difference between the two images 32 | """ 33 | difference = ImageChops.difference(image0, image1) 34 | difference_stat = ImageStat.Stat(difference) 35 | percent_diff = sum(difference_stat.mean) / (len(difference_stat.mean) * 255) * 100 36 | return percent_diff 37 | 38 | 39 | def test_default(): 40 | input_image = Image.open(images_path / "0.png") 41 | 42 | upscaler = RealSR(gpu_id, num_threads=num_threads) 43 | output_image = upscaler.process(input_image) 44 | 45 | test_image = Image.open(tests_path / "0_default.png") 46 | percent_diff = _calc_image_diff(test_image, output_image) 47 | logging.getLogger().info(f"%diff: {percent_diff}") 48 | 49 | test_image.close() 50 | output_image.close() 51 | input_image.close() 52 | 53 | assert percent_diff < 0.5 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RealSR ncnn Vulkan Python 2 | 3 | ## Introduction 4 | [realsr-ncnn-vulkan](https://github.com/nihui/realsr-ncnn-vulkan) is nihui's ncnn implementation of Real-World Super-Resolution via Kernel Estimation and Noise Injection super resolution. 5 | 6 | realsr-ncnn-vulkan-python wraps [realsr-ncnn-vulkan project](https://github.com/nihui/realsr-ncnn-vulkan) by SWIG to make it easier to integrate realsr-ncnn-vulkan with existing python projects. 7 | 8 | ## Downloads 9 | 10 | Linux/Windos/Mac X86_64 build releases are available now. 11 | 12 | However, for Linux distro with GLIBC < 2.29 (like Ubuntu 18.04), the ubuntu-1804 pre-built should be used. 13 | 14 | ## Build 15 | 16 | First, you have to install python, python development package (Python native development libs in Visual Studio), vulkan SDK and SWIG on your platform. And then: 17 | 18 | ### Linux 19 | ```shell 20 | git clone https://github.com/ArchieMeng/realsr-ncnn-vulkan-python.git 21 | cd realsr-ncnn-vulkan-python 22 | git submodule update --init --recursive 23 | cmake -B build src 24 | cd build 25 | make 26 | ``` 27 | 28 | ### Windows 29 | I used Visual Studio 2019 and msvc v142 to build this project for Windows. 30 | 31 | Install visual studio and open the project directory, and build. Job done. 32 | 33 | The only problem on Windows is that, you cannot use [CMake for Windows](https://cmake.org/download/) to generate the Visual Studio solution file and build it. This will make the lib crash on loading. 34 | 35 | The only way is [use Visual Studio to open the project as directory](https://www.microfocus.com/documentation/visual-cobol/vc50/VS2019/GUID-BE1C48AA-DB22-4F38-9644-E9B48658EF36.html), and build it from Visual Studio. 36 | 37 | ## About RealSR 38 | 39 | Real-World Super-Resolution via Kernel Estimation and Noise Injection (CVPRW 2020) 40 | 41 | https://github.com/jixiaozhong/RealSR 42 | 43 | Xiaozhong Ji, Yun Cao, Ying Tai, Chengjie Wang, Jilin Li, and Feiyue Huang 44 | 45 | *Tencent YouTu Lab* 46 | 47 | Our solution is the **winner of CVPR NTIRE 2020 Challenge on Real-World Super-Resolution** in both tracks. 48 | 49 | https://arxiv.org/abs/2005.01996 50 | 51 | ## Usages 52 | 53 | ### Example Program 54 | 55 | ```Python 56 | from PIL import Image 57 | from realsr_ncnn_vulkan import RealSR 58 | # if installed from pypi or binary wheels, 59 | # from realsr_ncnn_vulkan_python import RealSR 60 | 61 | im = Image.open("0.png") 62 | upscaler = RealSR(0, scale=4) 63 | out_im = upscaler.process(im) 64 | out_im.save("temp.png") 65 | ``` 66 | 67 | If you encounter crash or error, try to upgrade your GPU driver 68 | 69 | - Intel: https://downloadcenter.intel.com/product/80939/Graphics-Drivers 70 | - AMD: https://www.amd.com/en/support 71 | - NVIDIA: https://www.nvidia.com/Download/index.aspx 72 | 73 | ## Original RealSR NCNN Vulkan Project 74 | 75 | - https://github.com/nihui/realsr-ncnn-vulkan 76 | 77 | ## Original RealSR Project 78 | 79 | - https://github.com/jixiaozhong/RealSR 80 | 81 | ## Other Open-Source Code Used 82 | 83 | - https://github.com/Tencent/ncnn for fast neural network inference on ALL PLATFORMS 84 | - https://github.com/webmproject/libwebp for encoding and decoding Webp images on ALL PLATFORMS 85 | - https://github.com/nothings/stb for decoding and encoding image on Linux / MacOS 86 | - https://github.com/tronkko/dirent for listing files in directory on Windows 87 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | push: 4 | branches: 5 | - master 6 | pull_request: {} 7 | workflow_dispatch: {} 8 | jobs: 9 | windows: 10 | strategy: 11 | matrix: 12 | python-version: [ '3.6', '3.7', '3.8', '3.9', '3.10'] 13 | 14 | runs-on: windows-latest 15 | steps: 16 | - uses: actions/checkout@v2 17 | with: 18 | submodules: recursive 19 | - uses: ilammy/msvc-dev-cmd@v1 20 | - name: cache-vulkansdk 21 | id: cache-vulkansdk 22 | uses: actions/cache@v1 23 | with: 24 | path: "VulkanSDK" 25 | key: VulkanSDK-1.2.162.0-Installer 26 | - name: vulkansdk 27 | if: steps.cache-vulkansdk.outputs.cache-hit != 'true' 28 | run: | 29 | Invoke-WebRequest -Uri https://sdk.lunarg.com/sdk/download/1.2.162.0/windows/VulkanSDK-1.2.162.0-Installer.exe?Human=true -OutFile VulkanSDK-1.2.162.0-Installer.exe 30 | 7z x -aoa ./VulkanSDK-1.2.162.0-Installer.exe -oVulkanSDK 31 | Remove-Item .\VulkanSDK\Demos, .\VulkanSDK\Samples, .\VulkanSDK\Third-Party, .\VulkanSDK\Tools, .\VulkanSDK\Tools32, .\VulkanSDK\Bin32, .\VulkanSDK\Lib32 -Recurse 32 | - uses: actions/setup-python@v2 33 | with: 34 | python-version: ${{ matrix.python-version }} 35 | architecture: x64 36 | - name: Check Python version 37 | run: | 38 | python --version 39 | - name: Install dependencies 40 | run: | 41 | pip install build 42 | - name: Build 43 | run: | 44 | $env:VULKAN_SDK="$(pwd)\VulkanSDK" 45 | $env:CMAKE_FLAGS="-DPY_VERSION=${{ matrix.python-version }}" 46 | python -m build -w . 47 | 48 | ubuntu: 49 | runs-on: ubuntu-latest 50 | steps: 51 | - uses: actions/checkout@v2 52 | with: 53 | submodules: recursive 54 | 55 | - name: Install dependencies 56 | run: | 57 | sudo apt-get update 58 | sudo apt-get install -y libvulkan-dev glslang-dev glslang-tools 59 | pip install build 60 | - name: Build 61 | run: | 62 | python -m build -w . 63 | 64 | ubuntu-1804: 65 | strategy: 66 | matrix: 67 | python-version: [ '3.6', '3.7', '3.8', '3.9', '3.10' ] 68 | 69 | runs-on: ubuntu-18.04 70 | steps: 71 | - uses: actions/checkout@v2 72 | with: 73 | submodules: recursive 74 | 75 | - uses: actions/setup-python@v2 76 | with: 77 | python-version: ${{ matrix.python-version }} 78 | architecture: x64 79 | 80 | - name: Check Python version 81 | run: | 82 | python --version 83 | 84 | - name: cache-vulkansdk 85 | id: cache-vulkansdk 86 | uses: actions/cache@v1 87 | with: 88 | path: "1.2.162.0" 89 | key: vulkansdk-linux-x86_64-1.2.162.0 90 | - name: vulkansdk 91 | if: steps.cache-vulkansdk.outputs.cache-hit != 'true' 92 | run: | 93 | wget https://sdk.lunarg.com/sdk/download/1.2.162.0/linux/vulkansdk-linux-x86_64-1.2.162.0.tar.gz?Human=true -O vulkansdk-linux-x86_64-1.2.162.0.tar.gz 94 | tar -xf vulkansdk-linux-x86_64-1.2.162.0.tar.gz 95 | rm -rf 1.2.162.0/source 1.2.162.0/samples 96 | find 1.2.162.0 -type f | grep -v -E 'vulkan|glslang' | xargs rm 97 | 98 | - name: Install dependencies 99 | run: | 100 | pip install build 101 | - name: Build 102 | run: | 103 | export VULKAN_SDK=`pwd`/1.2.162.0/x86_64 104 | export CMAKE_FLAGS="-DPY_VERSION=${{ matrix.python-version }}" 105 | python -m build -w . 106 | 107 | macos: 108 | strategy: 109 | matrix: 110 | python-version: [ '3.6', '3.7', '3.8', '3.9', '3.10'] 111 | 112 | runs-on: macos-latest 113 | steps: 114 | - uses: actions/checkout@v2 115 | with: 116 | submodules: recursive 117 | - name: cache-vulkansdk 118 | id: cache-vulkansdk 119 | uses: actions/cache@v1 120 | with: 121 | path: "vulkansdk-macos-1.2.162.0" 122 | key: vulkansdk-macos-1.2.162.0 123 | - name: vulkansdk 124 | if: steps.cache-vulkansdk.outputs.cache-hit != 'true' 125 | run: | 126 | wget https://sdk.lunarg.com/sdk/download/1.2.162.0/mac/vulkansdk-macos-1.2.162.0.dmg?Human=true -O vulkansdk-macos-1.2.162.0.dmg 127 | hdiutil attach vulkansdk-macos-1.2.162.0.dmg 128 | cp -r /Volumes/vulkansdk-macos-1.2.162.0 . 129 | rm -rf vulkansdk-macos-1.2.162.0/Applications 130 | find vulkansdk-macos-1.2.162.0 -type f | grep -v -E 'vulkan|glslang|MoltenVK' | xargs rm 131 | hdiutil detach /Volumes/vulkansdk-macos-1.2.162.0 132 | - uses: actions/setup-python@v2 133 | with: 134 | python-version: ${{ matrix.python-version }} 135 | architecture: x64 136 | - name: Check Python version 137 | run: | 138 | python --version 139 | - name: Install dependencies 140 | run: | 141 | pip install build 142 | - name: Build 143 | run: | 144 | export VULKAN_SDK=`pwd`/vulkansdk-macos-1.2.162.0/macOS 145 | export CMAKE_FLAGS="-DUSE_STATIC_MOLTENVK=ON -DCMAKE_OSX_ARCHITECTURES="x86_64" \ 146 | -DVulkan_INCLUDE_DIR=`pwd`/vulkansdk-macos-1.2.162.0/MoltenVK/include \ 147 | -DVulkan_LIBRARY=`pwd`/vulkansdk-macos-1.2.162.0/MoltenVK/MoltenVK.xcframework/macos-arm64_x86_64/libMoltenVK.a \ 148 | -DPY_VERSION=${{ matrix.python-version }}" 149 | python -m build -w . 150 | -------------------------------------------------------------------------------- /realsr_ncnn_vulkan_python/realsr_ncnn_vulkan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Name: RealSR ncnn Vulkan Python wrapper 5 | Author: ArchieMeng 6 | Date Created: February 4, 2021 7 | Last Modified: February 13, 2022 8 | 9 | Dev: K4YT3X 10 | Last Modified: February 13, 2022 11 | """ 12 | 13 | # built-in imports 14 | import importlib 15 | import math 16 | import pathlib 17 | import sys 18 | 19 | # third-party imports 20 | from PIL import Image 21 | 22 | # local imports 23 | if __package__ is None: 24 | import realsr_ncnn_vulkan_wrapper as wrapped 25 | else: 26 | wrapped = importlib.import_module(f"{__package__}.realsr_ncnn_vulkan_wrapper") 27 | 28 | 29 | class Realsr: 30 | def __init__( 31 | self, 32 | gpuid=0, 33 | model="models-DF2K", 34 | tta_mode=False, 35 | scale: float = 4, 36 | tilesize=0, 37 | num_threads=1, 38 | **_kwargs, 39 | ): 40 | """ 41 | RealSR class which can do image super resolution. 42 | 43 | :param gpuid: the id of the gpu device to use. 44 | :param model: the name or the path to the model 45 | :param tta_mode: whether to enable tta mode or not 46 | :param scale: scale ratio. value: float. default: 2 47 | :param tilesize: tile size. 0 for automatically setting the size. default: 0 48 | :param num_threads: number of threads. default: 1 49 | """ 50 | self._raw_realsr = wrapped.RealSRWrapped(gpuid, tta_mode, num_threads) 51 | self.model = model 52 | self.gpuid = gpuid 53 | self.scale = scale # the real scale ratio 54 | self.set_params(scale, tilesize) 55 | self.load() 56 | 57 | def set_params(self, scale=4.0, tilesize=0): 58 | """ 59 | set parameters for realsr object 60 | 61 | :param scale: 1/2. default: 2 62 | :param tilesize: default: 0 63 | :return: None 64 | """ 65 | self._raw_realsr.scale = ( 66 | 4 # control the real scale ratio at each raw process function call 67 | ) 68 | self._raw_realsr.tilesize = self.get_tilesize() if tilesize <= 0 else tilesize 69 | self._raw_realsr.prepadding = self.get_prepadding() 70 | 71 | def load( 72 | self, param_path: pathlib.Path = None, model_path: pathlib.Path = None 73 | ) -> None: 74 | """ 75 | Load models from given paths. Use self.model if one or all of the parameters are not given. 76 | 77 | :param param_path: the path to model params. usually ended with ".param" 78 | :param model_path: the path to model bin. usually ended with ".bin" 79 | :return: None 80 | """ 81 | if param_path is None or model_path is None: 82 | model_dir = pathlib.Path(self.model) 83 | 84 | # try to load it from module path if not exists as directory 85 | if not model_dir.is_dir(): 86 | model_dir = pathlib.Path(__file__).parent / "models" / self.model 87 | 88 | param_path = model_dir / f"x{self._raw_realsr.scale}.param" 89 | model_path = model_dir / f"x{self._raw_realsr.scale}.bin" 90 | 91 | if param_path.exists() and model_path.exists(): 92 | param_path_str, model_path_str = wrapped.StringType(), wrapped.StringType() 93 | if sys.platform in ("win32", "cygwin"): 94 | param_path_str.wstr = wrapped.new_wstr_p() 95 | wrapped.wstr_p_assign(param_path_str.wstr, str(param_path)) 96 | model_path_str.wstr = wrapped.new_wstr_p() 97 | wrapped.wstr_p_assign(model_path_str.wstr, str(model_path)) 98 | else: 99 | param_path_str.str = wrapped.new_str_p() 100 | wrapped.str_p_assign(param_path_str.str, str(param_path)) 101 | model_path_str.str = wrapped.new_str_p() 102 | wrapped.str_p_assign(model_path_str.str, str(model_path)) 103 | 104 | self._raw_realsr.load(param_path_str, model_path_str) 105 | else: 106 | raise FileNotFoundError(f"{param_path} or {model_path} not found") 107 | 108 | def process(self, im: Image) -> Image: 109 | """ 110 | Upscale the given PIL.Image, and will call RealSR.process() more than once for scale ratios greater than 4 111 | 112 | :param im: PIL.Image 113 | :return: PIL.Image 114 | """ 115 | if self.scale > 1: 116 | cur_scale = 1 117 | w, h = im.size 118 | while cur_scale < self.scale: 119 | im = self._process(im) 120 | cur_scale *= 4 121 | w, h = math.floor(w * self.scale), math.floor(h * self.scale) 122 | im = im.resize((w, h)) 123 | 124 | return im 125 | 126 | def _process(self, im: Image) -> Image: 127 | """ 128 | Call RealSR.process() once for the given PIL.Image 129 | 130 | :param im: PIL.Image 131 | :return: PIL.Image 132 | """ 133 | in_bytes = bytearray(im.tobytes()) 134 | channels = int(len(in_bytes) / (im.width * im.height)) 135 | out_bytes = bytearray((self._raw_realsr.scale ** 2) * len(in_bytes)) 136 | 137 | raw_in_image = wrapped.Image(in_bytes, im.width, im.height, channels) 138 | raw_out_image = wrapped.Image( 139 | out_bytes, 140 | self._raw_realsr.scale * im.width, 141 | self._raw_realsr.scale * im.height, 142 | channels, 143 | ) 144 | 145 | self._raw_realsr.process(raw_in_image, raw_out_image) 146 | 147 | return Image.frombytes( 148 | im.mode, 149 | (self._raw_realsr.scale * im.width, self._raw_realsr.scale * im.height), 150 | bytes(out_bytes), 151 | ) 152 | 153 | def get_prepadding(self) -> int: 154 | if self.model.find("models-DF2K") or self.model.find("models-DF2K_JPEG"): 155 | return 10 156 | else: 157 | raise NotImplementedError(f'model "{self.model}" is not supported') 158 | 159 | def get_tilesize(self): 160 | if self.model.find("models-DF2K") or self.model.find("models-DF2K_JPEG"): 161 | if self.gpuid >= 0: 162 | heap_budget = wrapped.get_heap_budget(self.gpuid) 163 | if heap_budget > 1900: 164 | return 200 165 | elif heap_budget > 550: 166 | return 100 167 | elif heap_budget > 190: 168 | return 64 169 | else: 170 | return 32 171 | else: 172 | return 200 173 | else: 174 | raise NotImplementedError(f'model "{self.model}" is not supported') 175 | 176 | 177 | class RealSR(Realsr): 178 | ... 179 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | push: 4 | tags: 5 | - '*' 6 | 7 | jobs: 8 | setup: 9 | runs-on: ubuntu-latest 10 | outputs: 11 | APPNAME: ${{ steps.get_appname.outputs.APPNAME }} 12 | VERSION: ${{ steps.get_version.outputs.VERSION }} 13 | steps: 14 | - name: Get app name 15 | id: get_appname 16 | run: echo ::set-output name=APPNAME::${{ github.event.repository.name }} 17 | - name: Get version 18 | id: get_version 19 | run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} 20 | 21 | create-release: 22 | needs: 23 | - setup 24 | name: Create Relase 25 | runs-on: ubuntu-latest 26 | outputs: 27 | upload_url: ${{ steps.create_release.outputs.upload_url }} 28 | steps: 29 | - name: Create release 30 | id: create_release 31 | uses: actions/create-release@v1 32 | env: 33 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 34 | with: 35 | tag_name: ${{ needs.setup.outputs.VERSION }} 36 | release_name: Release ${{ needs.setup.outputs.VERSION }} 37 | draft: true 38 | prerelease: false 39 | 40 | windows: 41 | strategy: 42 | matrix: 43 | python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] 44 | 45 | needs: [setup, create-release] 46 | 47 | runs-on: windows-latest 48 | env: 49 | PACKAGENAME: ${{ needs.setup.outputs.APPNAME }}-${{ needs.setup.outputs.VERSION }}-windows 50 | steps: 51 | - uses: actions/checkout@v2 52 | with: 53 | submodules: recursive 54 | - uses: ilammy/msvc-dev-cmd@v1 55 | - name: cache-vulkansdk 56 | id: cache-vulkansdk 57 | uses: actions/cache@v1 58 | with: 59 | path: "VulkanSDK" 60 | key: VulkanSDK-1.2.162.0-Installer 61 | - name: vulkansdk 62 | if: steps.cache-vulkansdk.outputs.cache-hit != 'true' 63 | run: | 64 | Invoke-WebRequest -Uri https://sdk.lunarg.com/sdk/download/1.2.162.0/windows/VulkanSDK-1.2.162.0-Installer.exe?Human=true -OutFile VulkanSDK-1.2.162.0-Installer.exe 65 | 7z x -aoa ./VulkanSDK-1.2.162.0-Installer.exe -oVulkanSDK 66 | Remove-Item .\VulkanSDK\Demos, .\VulkanSDK\Samples, .\VulkanSDK\Third-Party, .\VulkanSDK\Tools, .\VulkanSDK\Tools32, .\VulkanSDK\Bin32, .\VulkanSDK\Lib32 -Recurse 67 | - uses: actions/setup-python@v2 68 | with: 69 | python-version: ${{ matrix.python-version }} 70 | architecture: x64 71 | - name: Check Python version 72 | run: | 73 | python --version 74 | - name: Install dependencies 75 | run: | 76 | pip install build 77 | - name: Build 78 | run: | 79 | $env:VULKAN_SDK="$(pwd)\VulkanSDK" 80 | $env:CMAKE_FLAGS="-DPY_VERSION=${{ matrix.python-version }}" 81 | python -m build -w . 82 | - name: Package 83 | run: | 84 | mkdir ${{ env.PACKAGENAME }} 85 | Copy-Item -Verbose -Path "README.md" -Destination "${{ env.PACKAGENAME }}" 86 | Copy-Item -Verbose -Path "LICENSE" -Destination "${{ env.PACKAGENAME }}" 87 | Copy-Item -Verbose -Recurse -Path dist\* -Destination "${{ env.PACKAGENAME }}" 88 | 7z a -r ${{ env.PACKAGENAME }}_${{ matrix.python-version }}.zip ${{ env.PACKAGENAME }} 89 | - name: Upload 90 | uses: actions/upload-release-asset@v1 91 | env: 92 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 93 | with: 94 | upload_url: ${{ needs.create-release.outputs.upload_url }} 95 | asset_path: ${{ env.PACKAGENAME }}_${{ matrix.python-version }}.zip 96 | asset_name: ${{ env.PACKAGENAME }}_${{ matrix.python-version }}.zip 97 | asset_content_type: application/zip 98 | 99 | ubuntu: 100 | needs: [setup, create-release] 101 | runs-on: ubuntu-latest 102 | env: 103 | PACKAGENAME: ${{ needs.setup.outputs.APPNAME }}-${{ needs.setup.outputs.VERSION }}-ubuntu 104 | DEBIAN_FRONTEND: noninteractive 105 | steps: 106 | - uses: actions/checkout@v2 107 | with: 108 | submodules: recursive 109 | - name: Install dependencies 110 | run: | 111 | sudo apt-get update 112 | sudo apt-get install -y libvulkan-dev glslang-dev glslang-tools 113 | pip install build 114 | - name: Build 115 | run: | 116 | python -m build -w . 117 | - name: Package 118 | run: | 119 | mkdir -p ${{ env.PACKAGENAME }} 120 | cp README.md LICENSE ${{ env.PACKAGENAME }} 121 | cp -r dist/* ${{ env.PACKAGENAME }} 122 | zip -9 -r ${{ env.PACKAGENAME }}.zip ${{ env.PACKAGENAME }} 123 | - name: Upload 124 | uses: actions/upload-release-asset@v1 125 | env: 126 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 127 | with: 128 | upload_url: ${{ needs.create-release.outputs.upload_url }} 129 | asset_path: ${{ env.PACKAGENAME }}.zip 130 | asset_name: ${{ env.PACKAGENAME }}.zip 131 | asset_content_type: application/zip 132 | 133 | ubuntu-1804: 134 | strategy: 135 | matrix: 136 | python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] 137 | needs: [setup, create-release] 138 | runs-on: ubuntu-18.04 139 | env: 140 | PACKAGENAME: ${{ needs.setup.outputs.APPNAME }}-${{ needs.setup.outputs.VERSION }}-ubuntu1804 141 | DEBIAN_FRONTEND: noninteractive 142 | steps: 143 | - uses: actions/checkout@v2 144 | with: 145 | submodules: recursive 146 | 147 | - uses: actions/setup-python@v2 148 | with: 149 | python-version: ${{ matrix.python-version }} 150 | architecture: x64 151 | 152 | - name: Check Python version 153 | run: | 154 | python --version 155 | 156 | - name: cache-vulkansdk 157 | id: cache-vulkansdk 158 | uses: actions/cache@v1 159 | with: 160 | path: "1.2.162.0" 161 | key: vulkansdk-linux-x86_64-1.2.162.0 162 | - name: vulkansdk 163 | if: steps.cache-vulkansdk.outputs.cache-hit != 'true' 164 | run: | 165 | wget https://sdk.lunarg.com/sdk/download/1.2.162.0/linux/vulkansdk-linux-x86_64-1.2.162.0.tar.gz?Human=true -O vulkansdk-linux-x86_64-1.2.162.0.tar.gz 166 | tar -xf vulkansdk-linux-x86_64-1.2.162.0.tar.gz 167 | rm -rf 1.2.162.0/source 1.2.162.0/samples 168 | find 1.2.162.0 -type f | grep -v -E 'vulkan|glslang' | xargs rm 169 | 170 | - name: Install dependencies 171 | run: | 172 | pip install build 173 | - name: Build 174 | run: | 175 | export VULKAN_SDK=`pwd`/1.2.162.0/x86_64 176 | export CMAKE_FLAGS="-DPY_VERSION=${{ matrix.python-version }}" 177 | python -m build -w . 178 | - name: Package 179 | run: | 180 | mkdir -p ${{ env.PACKAGENAME }} 181 | cp README.md LICENSE ${{ env.PACKAGENAME }} 182 | cp -r dist/* ${{ env.PACKAGENAME }} 183 | zip -9 -r ${{ env.PACKAGENAME }}_${{ matrix.python-version }}.zip ${{ env.PACKAGENAME }} 184 | - name: Upload 185 | uses: actions/upload-release-asset@v1 186 | env: 187 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 188 | with: 189 | upload_url: ${{ needs.create-release.outputs.upload_url }} 190 | asset_path: ${{ env.PACKAGENAME }}_${{ matrix.python-version }}.zip 191 | asset_name: ${{ env.PACKAGENAME }}_${{ matrix.python-version }}.zip 192 | asset_content_type: application/zip 193 | 194 | macos: 195 | needs: [setup, create-release] 196 | strategy: 197 | matrix: 198 | python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] 199 | env: 200 | PACKAGENAME: ${{ needs.setup.outputs.APPNAME }}-${{ needs.setup.outputs.VERSION }}-macos 201 | runs-on: macos-latest 202 | steps: 203 | - uses: actions/checkout@v2 204 | with: 205 | submodules: recursive 206 | - name: cache-vulkansdk 207 | id: cache-vulkansdk 208 | uses: actions/cache@v1 209 | with: 210 | path: "vulkansdk-macos-1.2.162.0" 211 | key: vulkansdk-macos-1.2.162.0 212 | - name: vulkansdk 213 | if: steps.cache-vulkansdk.outputs.cache-hit != 'true' 214 | run: | 215 | wget https://sdk.lunarg.com/sdk/download/1.2.162.0/mac/vulkansdk-macos-1.2.162.0.dmg?Human=true -O vulkansdk-macos-1.2.162.0.dmg 216 | hdiutil attach vulkansdk-macos-1.2.162.0.dmg 217 | cp -r /Volumes/vulkansdk-macos-1.2.162.0 . 218 | rm -rf vulkansdk-macos-1.2.162.0/Applications 219 | find vulkansdk-macos-1.2.162.0 -type f | grep -v -E 'vulkan|glslang|MoltenVK' | xargs rm 220 | hdiutil detach /Volumes/vulkansdk-macos-1.2.162.0 221 | - uses: actions/setup-python@v2 222 | with: 223 | python-version: ${{ matrix.python-version }} 224 | architecture: x64 225 | - name: Check Python version 226 | run: | 227 | python --version 228 | - name: Install dependencies 229 | run: | 230 | pip install build 231 | - name: Build 232 | run: | 233 | export VULKAN_SDK=`pwd`/vulkansdk-macos-1.2.162.0/macOS 234 | export CMAKE_FLAGS="-DUSE_STATIC_MOLTENVK=ON -DCMAKE_OSX_ARCHITECTURES="x86_64" \ 235 | -DVulkan_INCLUDE_DIR=`pwd`/vulkansdk-macos-1.2.162.0/MoltenVK/include \ 236 | -DVulkan_LIBRARY=`pwd`/vulkansdk-macos-1.2.162.0/MoltenVK/MoltenVK.xcframework/macos-arm64_x86_64/libMoltenVK.a \ 237 | -DPY_VERSION=${{ matrix.python-version }}" 238 | python -m build -w . 239 | - name: Package 240 | run: | 241 | mkdir -p ${{ env.PACKAGENAME }} 242 | cp README.md LICENSE ${{ env.PACKAGENAME }} 243 | cp -r dist/* ${{ env.PACKAGENAME }} 244 | zip -9 -r ${{ env.PACKAGENAME }}_${{ matrix.python-version }}.zip ${{ env.PACKAGENAME }} 245 | - name: Upload 246 | uses: actions/upload-release-asset@v1 247 | env: 248 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 249 | with: 250 | upload_url: ${{ needs.create-release.outputs.upload_url }} 251 | asset_path: ${{ env.PACKAGENAME }}_${{ matrix.python-version }}.zip 252 | asset_name: ${{ env.PACKAGENAME }}_${{ matrix.python-version }}.zip 253 | asset_content_type: application/zip 254 | -------------------------------------------------------------------------------- /realsr_ncnn_vulkan_python/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.9) 2 | cmake_policy(SET CMP0078 NEW) 3 | cmake_policy(SET CMP0086 NEW) 4 | cmake_policy(SET CMP0091 NEW) 5 | set(CMAKE_POLICY_DEFAULT_CMP0091 NEW) 6 | set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") 7 | 8 | project(realsr-ncnn-vulkan-python) 9 | 10 | if(NOT CMAKE_BUILD_TYPE) 11 | set(CMAKE_BUILD_TYPE release CACHE STRING "Choose the type of build" FORCE) 12 | endif() 13 | 14 | 15 | # Python 16 | if (DEFINED PY_VERSION) 17 | if (${CMAKE_VERSION} VERSION_LESS "3.15") 18 | find_package(Python ${PY_VERSION} EXACT REQUIRED) 19 | else() 20 | find_package(Python ${PY_VERSION} EXACT REQUIRED COMPONENTS Development) 21 | endif() 22 | else() 23 | if (${CMAKE_VERSION} VERSION_LESS "3.15") 24 | find_package(Python REQUIRED) 25 | else() 26 | find_package(Python REQUIRED COMPONENTS Development) 27 | endif() 28 | endif() 29 | 30 | # SWIG 31 | find_package(SWIG REQUIRED COMPONENTS python) 32 | if(SWIG_FOUND) 33 | message("SWIG found: ${SWIG_EXECUTABLE}") 34 | include(${SWIG_USE_FILE}) 35 | if(NOT SWIG_python_FOUND) 36 | message(WARNING "SWIG python bindings cannot be generated") 37 | endif() 38 | endif() 39 | 40 | option(USE_SYSTEM_NCNN "build with system libncnn" OFF) 41 | option(USE_SYSTEM_WEBP "build with system libwebp" OFF) 42 | option(USE_STATIC_MOLTENVK "link moltenvk static library" OFF) 43 | 44 | find_package(Threads) 45 | find_package(OpenMP) 46 | find_package(Vulkan REQUIRED) 47 | 48 | macro(realsr_add_shader SHADER_SRC) 49 | get_filename_component( 50 | SHADER_SRC_NAME_WE 51 | ${CMAKE_CURRENT_SOURCE_DIR}/realsr-ncnn-vulkan/src/${SHADER_SRC} NAME_WE) 52 | set(SHADER_COMP_HEADER ${CMAKE_CURRENT_BINARY_DIR}/${SHADER_SRC_NAME_WE}.comp.hex.h) 53 | 54 | add_custom_command( 55 | OUTPUT ${SHADER_COMP_HEADER} 56 | COMMAND ${CMAKE_COMMAND} 57 | -DSHADER_SRC=${CMAKE_CURRENT_SOURCE_DIR}/realsr-ncnn-vulkan/src/${SHADER_SRC} 58 | -DSHADER_COMP_HEADER=${SHADER_COMP_HEADER} 59 | -P "${CMAKE_CURRENT_SOURCE_DIR}/realsr-ncnn-vulkan/src/generate_shader_comp_header.cmake" 60 | DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/realsr-ncnn-vulkan/src/${SHADER_SRC} 61 | COMMENT "Preprocessing shader source ${SHADER_SRC_NAME_WE}.comp" 62 | VERBATIM 63 | ) 64 | set_source_files_properties(${SHADER_COMP_HEADER} PROPERTIES GENERATED TRUE) 65 | 66 | list(APPEND SHADER_SPV_HEX_FILES ${SHADER_COMP_HEADER}) 67 | endmacro() 68 | 69 | include_directories(${CMAKE_CURRENT_BINARY_DIR}) 70 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/realsr-ncnn-vulkan/src) 71 | include_directories(.) 72 | 73 | if(OPENMP_FOUND) 74 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") 75 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") 76 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") 77 | endif() 78 | 79 | # enable global link time optimization 80 | cmake_policy(SET CMP0069 NEW) 81 | set(CMAKE_POLICY_DEFAULT_CMP0069 NEW) 82 | include(CheckIPOSupported) 83 | check_ipo_supported(RESULT ipo_supported OUTPUT ipo_supported_output) 84 | if(ipo_supported) 85 | set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) 86 | else() 87 | message(WARNING "IPO is not supported: ${ipo_supported_output}") 88 | endif() 89 | 90 | if(USE_SYSTEM_NCNN) 91 | set(GLSLANG_TARGET_DIR "GLSLANG-NOTFOUND" CACHE PATH "Absolute path to glslangTargets.cmake directory") 92 | if(NOT GLSLANG_TARGET_DIR AND NOT DEFINED ENV{GLSLANG_TARGET_DIR}) 93 | message(WARNING "GLSLANG_TARGET_DIR must be defined! USE_SYSTEM_NCNN will be turned off.") 94 | set(USE_SYSTEM_NCNN OFF) 95 | else() 96 | message(STATUS "Using glslang install located at ${GLSLANG_TARGET_DIR}") 97 | 98 | find_package(Threads) 99 | 100 | include("${GLSLANG_TARGET_DIR}/OSDependentTargets.cmake") 101 | include("${GLSLANG_TARGET_DIR}/OGLCompilerTargets.cmake") 102 | if(EXISTS "${GLSLANG_TARGET_DIR}/HLSLTargets.cmake") 103 | # hlsl support can be optional 104 | include("${GLSLANG_TARGET_DIR}/HLSLTargets.cmake") 105 | endif() 106 | include("${GLSLANG_TARGET_DIR}/glslangTargets.cmake") 107 | include("${GLSLANG_TARGET_DIR}/SPIRVTargets.cmake") 108 | 109 | if(NOT TARGET glslang OR NOT TARGET SPIRV) 110 | message( 111 | WARNING 112 | "glslang or SPIRV target not found! USE_SYSTEM_NCNN will be turned off." 113 | ) 114 | set(USE_SYSTEM_NCNN OFF) 115 | endif() 116 | endif() 117 | endif() 118 | 119 | if(USE_SYSTEM_NCNN) 120 | find_package(ncnn) 121 | if(NOT TARGET ncnn) 122 | message( 123 | WARNING "ncnn target not found! USE_SYSTEM_NCNN will be turned off.") 124 | set(USE_SYSTEM_NCNN OFF) 125 | endif() 126 | endif() 127 | 128 | if(NOT USE_SYSTEM_NCNN) 129 | # build ncnn library 130 | if(NOT EXISTS 131 | "${CMAKE_CURRENT_SOURCE_DIR}/realsr-ncnn-vulkan/src/ncnn/CMakeLists.txt") 132 | message( 133 | FATAL_ERROR 134 | "The submodules were not downloaded! Please update submodules with \"git submodule update --init --recursive\" and try again." 135 | ) 136 | endif() 137 | 138 | option(NCNN_INSTALL_SDK "" OFF) 139 | option(NCNN_PIXEL_ROTATE "" OFF) 140 | option(NCNN_PIXEL_AFFINE "" OFF) 141 | option(NCNN_PIXEL_DRAWING "" OFF) 142 | option(NCNN_VULKAN "" ON) 143 | option(NCNN_VULKAN_ONLINE_SPIRV "" ON) 144 | option(NCNN_BUILD_BENCHMARK "" OFF) 145 | option(NCNN_BUILD_TESTS "" OFF) 146 | option(NCNN_BUILD_TOOLS "" OFF) 147 | option(NCNN_BUILD_EXAMPLES "" OFF) 148 | option(NCNN_DISABLE_RTTI "" ON) 149 | option(NCNN_DISABLE_EXCEPTION "" ON) 150 | option(NCNN_INT8 "" OFF) 151 | 152 | option(WITH_LAYER_absval "" OFF) 153 | option(WITH_LAYER_argmax "" OFF) 154 | option(WITH_LAYER_batchnorm "" OFF) 155 | option(WITH_LAYER_bias "" OFF) 156 | option(WITH_LAYER_bnll "" OFF) 157 | option(WITH_LAYER_concat "" ON) 158 | option(WITH_LAYER_convolution "" ON) 159 | option(WITH_LAYER_crop "" ON) 160 | option(WITH_LAYER_deconvolution "" OFF) 161 | option(WITH_LAYER_dropout "" OFF) 162 | option(WITH_LAYER_eltwise "" ON) 163 | option(WITH_LAYER_elu "" OFF) 164 | option(WITH_LAYER_embed "" OFF) 165 | option(WITH_LAYER_exp "" OFF) 166 | option(WITH_LAYER_flatten "" ON) 167 | option(WITH_LAYER_innerproduct "" ON) 168 | option(WITH_LAYER_input "" ON) 169 | option(WITH_LAYER_log "" OFF) 170 | option(WITH_LAYER_lrn "" OFF) 171 | option(WITH_LAYER_memorydata "" OFF) 172 | option(WITH_LAYER_mvn "" OFF) 173 | option(WITH_LAYER_pooling "" OFF) 174 | option(WITH_LAYER_power "" OFF) 175 | option(WITH_LAYER_prelu "" OFF) 176 | option(WITH_LAYER_proposal "" OFF) 177 | option(WITH_LAYER_reduction "" OFF) 178 | option(WITH_LAYER_relu "" ON) 179 | option(WITH_LAYER_reshape "" OFF) 180 | option(WITH_LAYER_roipooling "" OFF) 181 | option(WITH_LAYER_scale "" OFF) 182 | option(WITH_LAYER_sigmoid "" OFF) 183 | option(WITH_LAYER_slice "" OFF) 184 | option(WITH_LAYER_softmax "" OFF) 185 | option(WITH_LAYER_split "" ON) 186 | option(WITH_LAYER_spp "" OFF) 187 | option(WITH_LAYER_tanh "" OFF) 188 | option(WITH_LAYER_threshold "" OFF) 189 | option(WITH_LAYER_tile "" OFF) 190 | option(WITH_LAYER_rnn "" OFF) 191 | option(WITH_LAYER_lstm "" OFF) 192 | option(WITH_LAYER_binaryop "" ON) 193 | option(WITH_LAYER_unaryop "" OFF) 194 | option(WITH_LAYER_convolutiondepthwise "" OFF) 195 | option(WITH_LAYER_padding "" ON) 196 | option(WITH_LAYER_squeeze "" OFF) 197 | option(WITH_LAYER_expanddims "" OFF) 198 | option(WITH_LAYER_normalize "" OFF) 199 | option(WITH_LAYER_permute "" OFF) 200 | option(WITH_LAYER_priorbox "" OFF) 201 | option(WITH_LAYER_detectionoutput "" OFF) 202 | option(WITH_LAYER_interp "" ON) 203 | option(WITH_LAYER_deconvolutiondepthwise "" OFF) 204 | option(WITH_LAYER_shufflechannel "" OFF) 205 | option(WITH_LAYER_instancenorm "" OFF) 206 | option(WITH_LAYER_clip "" OFF) 207 | option(WITH_LAYER_reorg "" OFF) 208 | option(WITH_LAYER_yolodetectionoutput "" OFF) 209 | option(WITH_LAYER_quantize "" OFF) 210 | option(WITH_LAYER_dequantize "" OFF) 211 | option(WITH_LAYER_yolov3detectionoutput "" OFF) 212 | option(WITH_LAYER_psroipooling "" OFF) 213 | option(WITH_LAYER_roialign "" OFF) 214 | option(WITH_LAYER_packing "" ON) 215 | option(WITH_LAYER_requantize "" OFF) 216 | option(WITH_LAYER_cast "" ON) 217 | option(WITH_LAYER_hardsigmoid "" OFF) 218 | option(WITH_LAYER_selu "" OFF) 219 | option(WITH_LAYER_hardswish "" OFF) 220 | option(WITH_LAYER_noop "" OFF) 221 | option(WITH_LAYER_pixelshuffle "" ON) 222 | option(WITH_LAYER_deepcopy "" OFF) 223 | option(WITH_LAYER_mish "" OFF) 224 | option(WITH_LAYER_statisticspooling "" OFF) 225 | option(WITH_LAYER_swish "" OFF) 226 | option(WITH_LAYER_gemm "" OFF) 227 | option(WITH_LAYER_groupnorm "" OFF) 228 | option(WITH_LAYER_layernorm "" OFF) 229 | option(WITH_LAYER_softplus "" OFF) 230 | option(WITH_LAYER_gru "" OFF) 231 | option(WITH_LAYER_multiheadattention "" OFF) 232 | option(WITH_LAYER_gelu "" OFF) 233 | option(WITH_LAYER_convolution1d "" OFF) 234 | option(WITH_LAYER_pooling1d "" OFF) 235 | option(WITH_LAYER_convolutiondepthwise1d "" OFF) 236 | option(WITH_LAYER_convolution3d "" OFF) 237 | option(WITH_LAYER_convolutiondepthwise3d "" OFF) 238 | option(WITH_LAYER_pooling3d "" OFF) 239 | option(WITH_LAYER_matmul "" OFF) 240 | option(WITH_LAYER_deconvolution1d "" OFF) 241 | option(WITH_LAYER_deconvolutiondepthwise1d "" OFF) 242 | option(WITH_LAYER_deconvolution3d "" OFF) 243 | option(WITH_LAYER_deconvolutiondepthwise3d "" OFF) 244 | option(WITH_LAYER_einsum "" OFF) 245 | option(WITH_LAYER_deformableconv2d "" OFF) 246 | 247 | add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/realsr-ncnn-vulkan/src/ncnn) 248 | endif() 249 | 250 | # if(USE_SYSTEM_WEBP) set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}") 251 | # find_package(WebP) if(NOT TARGET webp) message(WARNING "webp target not found! 252 | # USE_SYSTEM_WEBP will be turned off.") set(USE_SYSTEM_WEBP OFF) endif() endif() 253 | # 254 | # if(NOT USE_SYSTEM_WEBP) # build libwebp library if(NOT EXISTS 255 | # "${CMAKE_CURRENT_SOURCE_DIR}/libwebp/CMakeLists.txt") message(FATAL_ERROR "The 256 | # submodules were not downloaded! Please update submodules with \"git submodule 257 | # update --init --recursive\" and try again.") endif() 258 | # 259 | # option(WEBP_ENABLE_SIMD "" ON) option(WEBP_BUILD_ANIM_UTILS "" OFF) 260 | # option(WEBP_BUILD_CWEBP "" OFF) option(WEBP_BUILD_DWEBP "" OFF) 261 | # option(WEBP_BUILD_GIF2WEBP "" OFF) option(WEBP_BUILD_IMG2WEBP "" OFF) 262 | # option(WEBP_BUILD_VWEBP "" OFF) option(WEBP_BUILD_WEBPINFO "" OFF) 263 | # option(WEBP_BUILD_WEBPMUX "" OFF) option(WEBP_BUILD_EXTRAS "" OFF) 264 | # option(WEBP_BUILD_WEBP_JS "" OFF) option(WEBP_NEAR_LOSSLESS "" OFF) 265 | # option(WEBP_ENABLE_SWAP_16BIT_CSP "" OFF) 266 | # 267 | # add_subdirectory(libwebp) 268 | # 269 | # include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libwebp/src) endif() 270 | 271 | # look for vulkan compute shader and compile 272 | set(SHADER_SPV_HEX_FILES) 273 | 274 | realsr_add_shader(realsr_preproc.comp) 275 | realsr_add_shader(realsr_postproc.comp) 276 | realsr_add_shader(realsr_preproc_tta.comp) 277 | realsr_add_shader(realsr_postproc_tta.comp) 278 | 279 | add_custom_target(generate-spirv DEPENDS ${SHADER_SPV_HEX_FILES}) 280 | 281 | set(REALSR_LINK_LIBRARIES ncnn ${Vulkan_LIBRARY}) 282 | 283 | if(USE_STATIC_MOLTENVK) 284 | find_library(CoreFoundation NAMES CoreFoundation) 285 | find_library(Foundation NAMES Foundation) 286 | find_library(Metal NAMES Metal) 287 | find_library(QuartzCore NAMES QuartzCore) 288 | find_library(CoreGraphics NAMES CoreGraphics) 289 | find_library(Cocoa NAMES Cocoa) 290 | find_library(IOKit NAMES IOKit) 291 | find_library(IOSurface NAMES IOSurface) 292 | 293 | list( 294 | APPEND 295 | REALSR_LINK_LIBRARIES 296 | ${Metal} 297 | ${QuartzCore} 298 | ${CoreGraphics} 299 | ${Cocoa} 300 | ${IOKit} 301 | ${IOSurface} 302 | ${Foundation} 303 | ${CoreFoundation}) 304 | endif() 305 | 306 | if(OPENMP_FOUND) 307 | list(APPEND REALSR_LINK_LIBRARIES ${OpenMP_CXX_LIBRARIES}) 308 | endif() 309 | 310 | # original realsr build targets target_link_libraries(realsr-ncnn-vulkan 311 | # ${REALSR_LINK_LIBRARIES}) 312 | 313 | # Swig build 314 | set(UseSWIG_TARGET_NAME_PREFERENCE STANDARD) 315 | set_property(SOURCE realsr.i PROPERTY CPLUSPLUS ON DEPENDS generate-spirv) 316 | 317 | # set output directory for the .py file 318 | set_property(SOURCE realsr.i PROPERTY OUTFILE_DIR ${CMAKE_CURRENT_BINARY_DIR} 319 | )# where the .cxx file goes 320 | set_property( 321 | SOURCE realsr.i 322 | PROPERTY OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/realsr_ncnn_vulkan_python 323 | )# where the result module .py file goes 324 | swig_add_library( 325 | realsr_ncnn_vulkan_wrapper 326 | TYPE MODULE 327 | LANGUAGE python 328 | SOURCES realsr.i 329 | realsr-ncnn-vulkan/src/realsr.cpp 330 | realsr_wrapped.cpp 331 | OUTPUT_DIR 332 | ${CMAKE_CURRENT_BINARY_DIR}/realsr_ncnn_vulkan_python 333 | OUTFILE_DIR 334 | ${CMAKE_CURRENT_BINARY_DIR}/realsr_ncnn_vulkan_python) 335 | 336 | add_dependencies(realsr_ncnn_vulkan_wrapper generate-spirv) 337 | target_compile_options(realsr_ncnn_vulkan_wrapper PUBLIC -fexceptions) 338 | 339 | # set output directory of the .so file 340 | if(CALL_FROM_SETUP_PY) 341 | set_target_properties( 342 | realsr_ncnn_vulkan_wrapper PROPERTIES LIBRARY_OUTPUT_DIRECTORY 343 | ${CMAKE_CURRENT_BINARY_DIR}) 344 | else() 345 | set_target_properties( 346 | realsr_ncnn_vulkan_wrapper 347 | PROPERTIES LIBRARY_OUTPUT_DIRECTORY 348 | ${CMAKE_CURRENT_BINARY_DIR}/realsr_ncnn_vulkan_python) 349 | endif() 350 | 351 | if(${CMAKE_VERSION} VERSION_LESS "3.15") 352 | include_directories(${PYTHON_INCLUDE_DIRS}) 353 | target_link_libraries(realsr_ncnn_vulkan_wrapper ${REALSR_LINK_LIBRARIES} 354 | ${PYTHON_LIBRARIES}) 355 | else() 356 | target_link_libraries(realsr_ncnn_vulkan_wrapper ${REALSR_LINK_LIBRARIES} 357 | Python::Module) 358 | endif() 359 | 360 | # Get the autogenerated Python file 361 | get_property( 362 | WRAPPER_PY_FILE 363 | TARGET realsr_ncnn_vulkan_wrapper 364 | PROPERTY SWIG_SUPPORT_FILES) 365 | 366 | if(CALL_FROM_SETUP_PY) 367 | install(TARGETS realsr_ncnn_vulkan_wrapper 368 | LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}) 369 | 370 | # Install the autogenerated Python file 371 | install(FILES ${WRAPPER_PY_FILE} DESTINATION ${CMAKE_INSTALL_PREFIX}) 372 | install(DIRECTORY realsr-ncnn-vulkan/models 373 | DESTINATION ${CMAKE_INSTALL_PREFIX}) 374 | install(FILES realsr_ncnn_vulkan.py DESTINATION ${CMAKE_INSTALL_PREFIX}) 375 | install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../README.md DESTINATION ${CMAKE_INSTALL_PREFIX}) 376 | install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../LICENSE DESTINATION ${CMAKE_INSTALL_PREFIX}) 377 | else() 378 | file(COPY realsr-ncnn-vulkan/models 379 | DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/realsr_ncnn_vulkan_python) 380 | file(COPY realsr_ncnn_vulkan.py 381 | DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/realsr_ncnn_vulkan_python) 382 | endif() 383 | --------------------------------------------------------------------------------