styler00dollar / VSGAN-tensorrt-docker

Using VapourSynth with super resolution and interpolation models and speeding them up with TensorRT.
BSD 3-Clause "New" or "Revised" License
286 stars 30 forks source link

cant make it work rife+upscale #60

Closed ghost closed 9 months ago

ghost commented 9 months ago

hi. this is my inference config

import sys
import os

sys.path.append("/workspace/tensorrt/")
import vapoursynth as vs
import functools
from src.scene_detect import scene_detect
from src.vfi_inference import vfi_frame_merger
from src.rife_trt import rife_trt

core = vs.core
vs_api_below4 = vs.__api_version__.api_major < 4
core.num_threads = 8

core.std.LoadPlugin(path="/usr/local/lib/libvstrt.so")

def metrics_func(clip):
    offs1 = core.std.BlankClip(clip, length=1) + clip[:-1]
    offs1 = core.std.CopyFrameProps(offs1, clip)
    return core.vmaf.Metric(clip, offs1, 2)

def inference_clip(video_path="", clip=None):
    interp_scale = 2

    clip = core.bs.VideoSource(source=video_path)

    # ssim
    clip_metric = vs.core.resize.Bicubic(
        clip, width=224, height=224, format=vs.YUV420P8, matrix_s="709"
    )
    clip_metric = metrics_func(clip_metric)

    # scene detect
    clip_sc = core.misc.SCDetect(
        clip,
        threshold=0.100,
    )

    # adjusting clip length
    clip = vs.core.resize.Bicubic(clip, format=vs.RGBH, matrix_in_s="709")
    clip_orig = vs.core.std.Interleave([clip] * interp_scale)
    clip_metric = vs.core.std.Interleave([clip_metric] * interp_scale)

    # interp
    clip = rife_trt(
        clip,
        multi=interp_scale,
        scale=1.0,
        device_id=0,
        num_streams=2,
        engine_path="/workspace/tensorrt/rife.engine",
    )

    # replacing frames
    clip = core.akarin.Select([clip, clip_orig], clip_metric, "x.float_ssim 0.999 >")
    clip = core.akarin.Select([clip, clip_orig], clip_sc, "x._SceneChangeNext 1 0 ?")

    # upscale
    upscaled = core.trt.Model(
        clip,
        engine_path="/workspace/tensorrt/compact.engine",
        num_streams=2,
    )

    # ssim
    upscaled_metrics = vs.core.resize.Bicubic(
        clip, width=224, height=224, format=vs.YUV420P8, matrix_s="709"
    )
    upscaled_metrics = metrics_func(upscaled_metrics)

    # replacing frames
    clip = core.akarin.Select(
        [upscaled, upscaled[1:] + upscaled[-1]],
        upscaled_metrics,
        "x.float_ssim 0.999 >",
    )

    clip = vs.core.resize.Bicubic(clip, format=vs.YUV420P10, matrix_s="709")

    return clip
    [!] Invalid Engine or Context. Please ensure the engine was built correctly. See error log for details.
Script evaluation failed:
Python exception: Invalid Engine or Context. Please ensure the engine was built correctly. See error log for details.

Traceback (most recent call last):
  File "src/cython/vapoursynth.pyx", line 3225, in vapoursynth._vpy_evaluate
  File "src/cython/vapoursynth.pyx", line 3226, in vapoursynth._vpy_evaluate
  File "inference_batch.py", line 7, in <module>
    clip = inference_clip(
           ^^^^^^^^^^^^^^^
  File "/workspace/tensorrt/inference_config.py", line 47, in inference_clip
    clip = rife_trt(
           ^^^^^^^^^
  File "/workspace/tensorrt/src/rife_trt.py", line 32, in rife_trt
    if check_model_precision_trt(engine_path) == "float32":
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/tensorrt/src/rife_trt.py", line 18, in check_model_precision_trt
    with runner:
  File "/usr/local/lib/python3.11/site-packages/polygraphy/backend/base/runner.py", line 62, in __enter__
    self.activate()
  File "/usr/local/lib/python3.11/site-packages/polygraphy/backend/base/runner.py", line 97, in activate
    self.activate_impl()
  File "/usr/local/lib/python3.11/site-packages/polygraphy/util/util.py", line 710, in wrapped
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/polygraphy/backend/trt/runner.py", line 145, in activate_impl
    G_LOGGER.critical(
  File "/usr/local/lib/python3.11/site-packages/polygraphy/logger/logger.py", line 605, in critical
    raise ExceptionType(message) from None
polygraphy.exception.exception.PolygraphyException: Invalid Engine or Context. Please ensure the engine was built correctly. See error log for details.

[lrc @ 0x5d31f1671500] Format lrc detected only with low score of 5, misdetection possible!

I followed the read me to create engines but i got error after running python main.py what can I do?

styler00dollar commented 9 months ago

This issue and a workaround was mentioned in https://github.com/styler00dollar/VSGAN-tensorrt-docker/issues/47#issuecomment-1886004315. Still need to figure out a proper way to fix this or force the user to say if rife is fp16.

ghost commented 9 months ago

thanks

ghost commented 9 months ago

@styler00dollar is fast mode on 4.14? if not how can I achieve that

styler00dollar commented 9 months ago

I would prefer people to not mix unrelated things into issues.

Rife 4.14 technically has fast mode set to true, because that part of the network was removed. Only older rife have it. If you want to know more, read the rife source code. You won't be able to turn off parts of the network to make it faster though.