Skip to content

Commit

Permalink
Add ResizeRight for cleaner gradients when resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Sepiol committed Oct 1, 2021
1 parent bb96df3 commit ef51825
Show file tree
Hide file tree
Showing 4 changed files with 517 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cgd/ResizeRight/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Assaf Shocher

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 cgd/ResizeRight/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from cgd.ResizeRight.interp_methods import *
70 changes: 70 additions & 0 deletions cgd/ResizeRight/interp_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from math import pi

try:
import torch
except ImportError:
torch = None

try:
import numpy
except ImportError:
numpy = None

if numpy is None and torch is None:
raise ImportError("Must have either Numpy or PyTorch but both not found")


def set_framework_dependencies(x):
if type(x) is numpy.ndarray:
to_dtype = lambda a: a
fw = numpy
else:
to_dtype = lambda a: a.to(x.dtype)
fw = torch
eps = fw.finfo(fw.float32).eps
return fw, to_dtype, eps


def support_sz(sz):
def wrapper(f):
f.support_sz = sz
return f
return wrapper


@support_sz(4)
def cubic(x):
fw, to_dtype, eps = set_framework_dependencies(x)
absx = fw.abs(x)
absx2 = absx ** 2
absx3 = absx ** 3
return ((1.5 * absx3 - 2.5 * absx2 + 1.) * to_dtype(absx <= 1.) +
(-0.5 * absx3 + 2.5 * absx2 - 4. * absx + 2.) *
to_dtype((1. < absx) & (absx <= 2.)))


@support_sz(4)
def lanczos2(x):
fw, to_dtype, eps = set_framework_dependencies(x)
return (((fw.sin(pi * x) * fw.sin(pi * x / 2) + eps) /
((pi**2 * x**2 / 2) + eps)) * to_dtype(abs(x) < 2))


@support_sz(6)
def lanczos3(x):
fw, to_dtype, eps = set_framework_dependencies(x)
return (((fw.sin(pi * x) * fw.sin(pi * x / 3) + eps) /
((pi**2 * x**2 / 3) + eps)) * to_dtype(abs(x) < 3))


@support_sz(2)
def linear(x):
fw, to_dtype, eps = set_framework_dependencies(x)
return ((x + 1) * to_dtype((-1 <= x) & (x < 0)) + (1 - x) *
to_dtype((0 <= x) & (x <= 1)))


@support_sz(1)
def box(x):
fw, to_dtype, eps = set_framework_dependencies(x)
return to_dtype((-1 <= x) & (x < 0)) + to_dtype((0 <= x) & (x <= 1))
Loading

0 comments on commit ef51825

Please sign in to comment.