Giter VIP home page Giter VIP logo

vapoursynth-super-resolution-helper's Introduction

Scripts that automate the installation of VapourSynth and a bunch of ESRGAN/MXnet super resolution stuff. Because it's a pain to install by hand, and you can upscale with a GUI!

Check out the wiki!

WzLvUe.gif

demo2.gif

High quality video upscaling is the main use case, but images/textures are supported as well.

ONLY supports Windows. Works best with Nvidia GPUs.

Scripts under construction! Some of the project works, but the CUDA installer needs testing, and FFMPEG, batch scripts with useful presets, and some other things are still missing!

vapoursynth-super-resolution-helper's People

Contributors

alphaatlas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vapoursynth-super-resolution-helper's Issues

More Encoding Presets

More variety needs to be added VSEdit's pre-made video encoding presets if possible, or as .bat files otherwise:

  • Nvenc x264

  • Nvenc x265

  • VCE x264

  • VCE x265

  • "Quick" CPU x265 for test encodes

  • Slow/high quality CPU x264 and x265 (as NN upscaling is the bottleneck anyway)

  • MPEG2 for older video game video formats (?)

  • 4K VP9 for Max Quality YouTube Uploads (?)

Most of these will probably require a FFMPEG executable download, as well as options to remux the audio. Pre-made encoding options beyond this are better suited to a dedicated encoding program like StaxRip or Hybrid.

AMD/Intel GPU Support

AMD GPUs (and Intel IGPs) can theoretically run a variety of pretrained models with OpenCV. WolframRhodium wrote some example VapourSynth code just for that: https://github.com/WolframRhodium/muvsfunc/blob/master/Collections/examples/super_resolution_opencv.vpy

Here's a slightly modified version that ran a Caffe model on a lowly AMD 6620G IGP (with a tiny PNG texture):

# super resolution using OpenCV
# note: the input image to the network is not cropped, which might triggers out-of-memory error.

import os

# Set OpenCL device in format `<Platform>:<CPU|GPU|ACCELERATOR|nothing=GPU/CPU>:<DeviceName or ID>`
# examples: 'AMD:GPU:', ':GPU:1', 'Intel:CPU:', 
# https://github.com/opencv/opencv/wiki/OpenCL-optimizations#opencv-opencl-configuration-options
os.environ['OPENCV_OPENCL_DEVICE'] = 'AMD:GPU:' # use GPU to accelerate processing


import vapoursynth as vs
import cv2
import mvsfunc as mvf
import muvsfunc_numpy as mufnp

core = vs.get_core()


# global params
src = core.std.BlankClip(width=640, height=360, length=1000, format=vs.RGBS) # can be RGB/YUV/GRAY

if 'GPU' in os.environ['OPENCV_OPENCL_DEVICE']:
    if cv2.ocl.haveOpenCL() and cv2.ocl.useOpenCL():
        backend = cv2.dnn.DNN_BACKEND_OPENCV
        target = cv2.dnn.DNN_TARGET_OPENCL # available on NVIDIA GPU since OpenCV 4.0.1, but only works on Intel GPU before OpenCV 3.4.2
    else:
        backend = cv.dnn.DNN_BACKEND_DEFAULT
        target = cv2.dnn.DNN_TARGET_CPU

###Change SR parameters here:
sr_args = dict(up_scale=2, is_rgb_model=True, pad=(7,7,7,7))

#Example caffe model.
# https://github.com/alterzero/DBPN-caffe
# https://drive.google.com/drive/folders/1ahbeoEHkjxoo4NV1wReOmpoRWbl448z-?usp=sharing
#sr_args = dict(prototxt=r'DBPN_mat_2x.prototxt',
        caffe_model=r'DBPN_2x.caffemodel', up_scale=2, is_rgb_model=True)


# internel functions
def channel_last(arr):
    """Convert a CHW array to HWC."""
    ndim = arr.ndim
    return arr.swapaxes(ndim - 3, ndim - 2).swapaxes(ndim - 2, ndim - 1)


def super_resolution_core(img, net, pad=None, crop=None):
    if pad is not None:
        img = cv2.copyMakeBorder(img, *pad, 1)

    blob = cv2.dnn.blobFromImage(img)

    net.setInput(blob, '')

    super_res = net.forward()

    if img.ndim == 2:
        if crop is not None:
            return super_res[0, 0, crop[0]:-crop[1], crop[2]:-crop[3]]
        else:
            return super_res[0, 0, :, :]
    else:
        # the output is BGR rather than RGB so channel reversal is needed
        if crop is not None:
            return channel_last(super_res[0, ::-1, crop[0]:-crop[1], crop[2]:-crop[3]])
        else:
            return channel_last(super_res[0, ::-1, :, :])


def run_super_resolution(clip, prototxt, caffe_model, up_scale=2, is_rgb_model=True, pad=None, crop=None, backend=None, target=None):
    """ Super-Resolution without color family hadling
    """
    ###Put the path(s) to your model here!
    ###See: https://docs.opencv.org/3.4/d6/d0f/group__dnn.html#ga3b34fe7a29494a6a4295c169a7d32422
    net = cv2.dnn.readNet("Path to model")

    if backend is not None:
        net.setPreferableBackend(backend)

    if target is not None:
        net.setPreferableTarget(target)

    if up_scale != 1:
        blank = core.std.BlankClip(clip, width=clip.width*up_scale, height=clip.height*up_scale)
        super_res = mufnp.numpy_process([blank, clip], super_resolution_core, net=net, 
            input_per_plane=(not is_rgb_model), output_per_plane=(not is_rgb_model), pad=pad, crop=crop, 
            omit_first_clip=True)
    else:
        super_res = mufnp.numpy_process(clip, super_resolution_core, net=net, 
            input_per_plane=(not is_rgb_model), output_per_plane=(not is_rgb_model), pad=pad, crop=crop)

    return super_res


def super_resolution(clip, up_scale=2, is_rgb_model=True, pad=None, crop=None, backend=None, target=None, pre_upscale=False, upscale_uv=False, merge_residual=False):
    """ Super-Resolution with color family hadling

    The color space of the output depends on the algorithm
    """

    isGray = clip.format.color_family == vs.GRAY
    isRGB = clip.format.color_family == vs.RGB

    if is_rgb_model and not isRGB:
        clip = mvf.ToRGB(clip, depth=32)

    elif not is_rgb_model:
        if isRGB:
            clip = mvf.ToYUV(clip, depth=32)

        if not isGray and not upscale_uv: # isYUV/RGB and only upscale Y
            clip = mvf.GetPlane(clip)

    clip = mvf.Depth(clip, depth=32)

    if pre_upscale:
        clip = core.resize.Bicubic(clip, clip.width*up_scale, clip.height*up_scale, filter_param_a=0, filter_param_b=0.5)
        up_scale = 1

    super_res = run_super_resolution(clip, prototxt=prototxt, caffe_model=caffe_model, 
        up_scale=up_scale, is_rgb_model=is_rgb_model, pad=pad, crop=crop, backend=backend, target=target)

    if merge_residual:
        low_res = core.resize.Bicubic(clip, super_res.width, super_res.height, filter_param_a=0, filter_param_b=0.5)
        super_res = core.std.Expr([super_res, low_res], ['x y +'])

    return super_res

sr = super_resolution(src, **sr_args, backend=backend, target=target)

sr.set_output()

However, I can't seem to load any pretrained PyTorch models, getting the same error as this user when I try: https://answers.opencv.org/question/220422/cv2dnnreadnetfromtorchpretrained_model_path-throws-error/

Does OpenCV even support PyTorch models? It seemingly recognizes and tries to load the .pth file, but I haven't found any specific documentation on PyTorch support. I probably need to open an issue in the OpenCV GitHub repo or on their own support site if I can't resolve this problem.

Not installed successfully, cannot proceed

Hello :)
I wanted to give this project a try and tried to install the Downloader 0.3 release by downloading the exe to
C:\VapourSynth-Super-Resolution-Helper\

A lot of stuff was downloaded and extracted just fine, but at some point i got this message:

grafik

So what is weird:

  1. "D:\obj\windows-release\37amd64-Release"
    -> This path does not exist. I have a harddisk volume "D:" but "D:\obj..." doesn't exist.
    Also i'm not sure why D matters anyway, since i'm downloading to C.

  2. While i have installed a software called "shotgun" which also uses ffmpeg, i can't see why your installer would try to access it. Especially because ffmpeg was already downloaded by your installer before.

Anyway, I hit Enter, and the console closed.
So i tried to start the downloader again, but it says:

Existing VapourSynth Fatpack installation found in this directory. Please delete
 it or move this installer somewhere else!
If you just want to update everything, please use the updater batch file

Press ENTER to continue...

BUT: there is no updater batch file in the extracted folder though.
So it seems i'm stuck...

The extracted "VapourSynth64Portable" folder has a size of ~800mb, so i guess there is still a lot missing.

Do you have any idea what i should do?

Best regards

Fix graphics detection

On systems with a Nvidia/Intel GPU, both GPUs are detected. Only the Nvidia GPU warning should be displayed.

Remove Unnecessary CUDA Modules

Determine what CUDA modules MXNet and PyTorch need to run. Currently, the online installer grabs quite a few unnecessary modules, and I suspect its leading to failed CUDA installations like this: #9

Initial Release To Do List

  • Fix batch image processing.

  • Fix ProSR parsing.

  • Add some premade VapourSynth scripts

  • Get FFmpeg working.

  • Write up a ReadMe

  • Start a Wiki with some basic info/links on VS scripting and notes on neural networks

  • Talk with ChaosKing and WolframRhodium? If I ever "release" this outside of GitHub, I'd be redistributing their own compilations to an audience they might not want to support.

  • Add some more alpha channel support.

  • Find a stable download link for a pretrained ESRGAN model.

  • Thread the installer, without compromising the ability to debug the script.

  • Clean up the example .vpy scripts, so they're more readable.

  • Test the CUDA installer

  • Add info on BasicSR, .vpy scripts for getting training data from videos, artifact generation for training, etc. See: https://gist.github.com/AlphaAtlas/45ac013da4eabd3316435f72eef64494

Nvidia Testers

As of this post, I'm about 400 miles away from my desktop with a 980 TI, so I'll need some sacrifices brave souls with Nvidia video cards for testing at some point.

Unable to Install VSGAN

Hello,

I'm using your script with Windows 10 and while it installs everything OK, it doesn't install VSGAN. I attempt to install it using the Python PIP command and receive the following:

C:\VapourSynth64Portable\VapourSynth64>python.exe -m pip install vsgan
Collecting vsgan
  Using cached vsgan-1.0.8-py3-none-any.whl (9.9 kB)
Collecting vapoursynth
  Using cached VapourSynth-49.zip (388 kB)
    ERROR: Command errored out with exit status 1:
     command: 'C:\VapourSynth64Portable\VapourSynth64\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\STREAM~1\\AppData\\Local\\Temp\\pip-install-b1yauu0s\\vapoursynth\\setup.py'"'"'; __file__='"'"'C:\\Users\\STREAM~1\\AppData\\Local\\Temp\\pip-install-b1yauu0s\\vapoursynth\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\STREAM~1\AppData\Local\Temp\pip-pip-egg-info-drpuisxm'
         cwd: C:\Users\STREAM~1\AppData\Local\Temp\pip-install-b1yauu0s\vapoursynth\
    Complete output (14 lines):
    Traceback (most recent call last):
      File "C:\Users\STREAM~1\AppData\Local\Temp\pip-install-b1yauu0s\vapoursynth\setup.py", line 60, in <module>
        dll_path = query(winreg.HKEY_LOCAL_MACHINE, REGISTRY_PATH, REGISTRY_KEY)
      File "C:\Users\STREAM~1\AppData\Local\Temp\pip-install-b1yauu0s\vapoursynth\setup.py", line 36, in query
        reg_key = winreg.OpenKey(hkey, path, 0, winreg.KEY_READ)
    FileNotFoundError: [WinError 2] The system cannot find the file specified

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\STREAM~1\AppData\Local\Temp\pip-install-b1yauu0s\vapoursynth\setup.py", line 63, in <module>
        raise OSError("Couldn't detect vapoursynth installation path")
    OSError: Couldn't detect vapoursynth installation path
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

I ran the FatDownloader.exe file as an admin and tried again with no success. Any ideas?

Miscellaneous Long Term Goals

  • Investigate deeper cross platform/AMD support. It seems that AMD's deep learning framework development is mostly geared towards Linux? See: https://rocm.github.io/install.html The ROCm MXNet branch isn't really going anywhere, but maybe some models can run on OpenCV?

  • Investigate more GPU heavy VS filters, since this project installs CUDA/cuDNN anyway. See this and this

  • Pre-Train networks from other sources

  • Upload some visual comparisons between the existing upscaling filters on the wiki.

muf.super_resolution Error: Predict: Function does not take argument(s) named padding

sr_args = dict(model_filename=r'../NeuralNetworks/SRGAN\SRGAN@leftthomas\SRGAN_2x', device_id=0,up_scale=2, is_rgb_model=True, pad=None, crop=None, pre_upscale=False)

#change super resolution arguments here!
manual_sr_args = sr_args
manual_sr_args['block_w']=64
manual_sr_args['block_h']=64

clip = core.lsmas.LWLibavSource(r"D:\vts_01.mov")

source = clip

clip = mvs.Depth(clip, depth=16)

clip = muf.super_resolution(clip, **manual_sr_args)

clip = mvs.Preview(clips=[core.text.Text(clip, "Processed"), core.text.Text(source, "Source")])


clip.set_output()

But

Traceback (most recent call last):
File "src\cython\vapoursynth.pyx", line 1946, in vapoursynth.vpy_evaluateScript
File "src\cython\vapoursynth.pyx", line 1947, in vapoursynth.vpy_evaluateScript
File "D:/VapourSynth64Portable/VapourSynthScripts/ProcessVideoAuto.vpy", line 62, in 
clip = muf.super_resolution(clip, **manual_sr_args)
File "D:\VapourSynth64Portable\Scripts\muvsfunc.py", line 4561, in super_resolution
super_res = inference(clip, device_id[0])
File "D:\VapourSynth64Portable\Scripts\muvsfunc.py", line 4533, in inference
ctx=2 if dev_id >= 0 else 1, dev_id=max(dev_id, 0))
File "src\cython\vapoursynth.pyx", line 1825, in vapoursynth.Function.__call__
vapoursynth.Error: Predict: Function does not take argument(s) named padding

Can anyone tell me why?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.