Beispiel #1
0
 def forward(self, img):
     factors = []
     # noise & blur
     blur_factor = 0
     if self.blur is not None:
         blur_factor = random.uniform(*self.blur)
         img = imfilter(
             img,
             torch.tensor(gaussian_kernel(15, blur_factor),
                          device=img.device), self.blur_padding)
     awgn_factor = (0, 0, 0)
     if self.awgn is not None:
         _r = random.uniform(*self.awgn)
         _g = random.uniform(*self.awgn)
         _b = random.uniform(*self.awgn)
         img += gaussian_noise(img, stddev=(_r, _g, _b))
         awgn_factor = (_r, _g, _b)
     poisson_factor = (_r, _g, _b)
     if self.poisson is not None:
         _r = random.uniform(*self.poisson)
         _g = random.uniform(*self.poisson)
         _b = random.uniform(*self.poisson)
         img += poisson_noise(img, stddev=(_r, _g, _b))
         poisson_factor = (_r, _g, _b)
     fac = [blur_factor, *awgn_factor, *poisson_factor]
     factors.append(torch.tensor(fac))
     img = img.clamp(0, 1)
     return img, torch.stack(factors).to(img.device)
Beispiel #2
0
 def gen_kernel(self, ktype, ksize, l1, l2=None, theta=0):
   if ktype == 'isotropic':
     kernel = gaussian_kernel(ksize, l1)
   elif ktype == 'anisotropic':
     kernel = anisotropic_gaussian_kernel(ksize, theta, l1, l2 or l1)
   else:
     # TODO(wenyi) this is "directKernel"
     raise NotImplementedError("DirectKernel not implemented.")
   return kernel
#  Copyright (c) 2017-2020 Wenyi Tang.
#  Author: Wenyi Tang
#  Email: [email protected]
#  Update: 2020 - 2 - 13

import unittest

import numpy as np

from VSR.Util.Math import (anisotropic_gaussian_kernel, gaussian_kernel)
from VSR.Backend import BACKEND

_K1 = gaussian_kernel(15, 2)
_K2 = anisotropic_gaussian_kernel(15, 1, 5, 3)


class ImFilter(unittest.TestCase):
    # For ones([4, 4])
    y_gold = np.array([[0.3151, 0.3776, 0.3776, 0.3151],
                       [0.3776, 0.4524, 0.4524, 0.3776],
                       [0.3776, 0.4524, 0.4524, 0.3776],
                       [0.3151, 0.3776, 0.3776, 0.3151]])
    z_gold = np.array([[0.3391, 0.3950, 0.3774, 0.2950],
                       [0.3850, 0.4627, 0.4557, 0.3677],
                       [0.3677, 0.4557, 0.4627, 0.3850],
                       [0.2950, 0.3774, 0.3950, 0.3391]])

    def test_torch(self):
        if BACKEND != 'pytorch':
            return
        import torch