Skip to content

Commit

Permalink
add padding to ncnn
Browse files Browse the repository at this point in the history
  • Loading branch information
mafiosnik777 committed May 23, 2023
1 parent 0b20a36 commit 0e38d7c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
30 changes: 23 additions & 7 deletions src/inference/esrgan_ncnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ def execute(n, upscaled, metric_thresh, f):

clip = vs.core.resize.Bicubic(clip, format=vs.RGBS, matrix_in_s="709")

# ncnn related padding
def pad(n):
return ((n + 7) // 8) * 8 - n

paddedHeight = False
paddedWidth = False
pxw = pad(clip.width)
pxh = pad(clip.height)

if (clip.height % 8 != 0):
clip = core.std.AddBorders(clip, bottom=pad(clip.height))
paddedHeight = True

if (clip.width % 8 != 0):
clip = core.std.AddBorders(clip, right=pad(clip.width))
paddedWidth = True

if tiling == False:
upscaled = core.ncnn.Model(clip, network_path=engine, num_streams=threading(), fp16=fp16)
else:
Expand All @@ -60,14 +77,13 @@ def execute(n, upscaled, metric_thresh, f):
partial = functools.partial(execute, upscaled=upscaled, metric_thresh=metric_thresh)
clip = core.std.FrameEval(core.std.BlankClip(clip=upscaled, width=upscaled.width, height=upscaled.height), partial, prop_src=[clip])

# padding if clip dimensions aren't divisble by 2
if (upscaled.height % 2 != 0):
upscaled = core.std.AddBorders(upscaled, bottom=1)

if (clip.width % 2 != 0):
upscaled = core.std.AddBorders(upscaled, right=1)

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

if paddedHeight:
clip = core.std.Crop(clip, bottom=pxh*2)

if paddedWidth:
clip = core.std.Crop(clip, right=pxw*2)

print("Starting video output | Threads: " + str(int(cpu_count() / 2)) + " | " + "Streams: " + str(threading()), file=sys.stderr)
clip.set_output()
30 changes: 23 additions & 7 deletions src/inference/shufflecugan_ncnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ def execute(n, upscaled, metric_thresh, f):

clip = vs.core.resize.Spline64(clip, format=vs.RGBS, matrix_in_s="709", transfer_in_s="linear")

# ncnn related padding
def pad(n):
return ((n + 7) // 8) * 8 - n

paddedHeight = False
paddedWidth = False
pxw = pad(clip.width)
pxh = pad(clip.height)

if (clip.height % 8 != 0):
clip = core.std.AddBorders(clip, bottom=pad(clip.height))
paddedHeight = True

if (clip.width % 8 != 0):
clip = core.std.AddBorders(clip, right=pad(clip.width))
paddedWidth = True

in_tile_channels = 3
in_tile_height = clip.height
in_tile_width = clip.width
Expand All @@ -69,14 +86,13 @@ def execute(n, upscaled, metric_thresh, f):
partial = functools.partial(execute, upscaled=upscaled, metric_thresh=metric_thresh)
upscaled = core.std.FrameEval(core.std.BlankClip(clip=upscaled, width=upscaled.width, height=upscaled.height), partial, prop_src=[clip])

# padding if clip dimensions aren't divisble by 2
if (upscaled.height % 2 != 0):
upscaled = core.std.AddBorders(upscaled, bottom=1)

if (clip.width % 2 != 0):
clip = core.std.AddBorders(upscaled, right=1)

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

if paddedHeight:
clip = core.std.Crop(clip, bottom=pxh*2)

if paddedWidth:
clip = core.std.Crop(clip, right=pxw*2)

print("Starting video output | Threads: " + str(int(cpu_count() / 2)) + " | " + "Streams: " + str(threading()), file=sys.stderr)
clip.set_output()

0 comments on commit 0e38d7c

Please sign in to comment.