Spaces:
Sleeping
Sleeping
File size: 6,251 Bytes
907b7f3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import cv2
import random
import numpy as np
__all__ = ['Compose', 'Normalize', 'CenterCrop', 'RgbToGray', 'RandomCrop',
'HorizontalFlip', 'AddNoise', 'NormalizeUtterance']
class Compose(object):
"""Compose several preprocess together.
Args:
preprocess (list of ``Preprocess`` objects): list of preprocess to compose.
"""
# preprecess ([preprocess]) : dataloaders.py์์ ์ฌ์ฉ๋จ
# preprocessing['train'] = Compose([
# Normalize( 0.0,255.0 ),
# RandomCrop(crop_size),
# HorizontalFlip(0.5),
# Normalize(mean, std) ])
def __init__(self, preprocess):
self.preprocess = preprocess
def __call__(self, sample):
for t in self.preprocess:
sample = t(sample)
return sample # preprocess์ ๋ด๊ธด ๊ฐ augmentation ์ ์ฒ๋ฆฌ๊ฐ sample์ ๋ด๊ฒจ ๋ฐํ๋๋ค.
def __repr__(self): # __repr__() : ๊ดํธ ์์ ์๋ ๊ฒ์ ๋ฌธ์์ด๋ก ๋ฐํ
format_string = self.__class__.__name__ + '('
for t in self.preprocess:
format_string += '\n'
format_string += ' {0}'.format(t)
format_string += '\n)'
return format_string # ํด๋์ค๋ช
, ์ ์ฒ๋ฆฌ๋ช
๋ฑ์ ๊ดํธ ์์ ์ถ๋ ฅ
class RgbToGray(object):
"""Convert image to grayscale.
Converts a numpy.ndarray (H x W x C) in the range
[0, 255] to a numpy.ndarray of shape (H x W x C) in the range [0.0, 1.0].
"""
def __call__(self, frames):
"""
Args:
img (numpy.ndarray): Image to be converted to gray.
Returns:
numpy.ndarray: grey image
"""
frames = np.stack([cv2.cvtColor(_, cv2.COLOR_RGB2GRAY) for _ in frames], axis=0)
return frames
def __repr__(self):
return self.__class__.__name__ + '()'
class Normalize(object):
"""Normalize a ndarray image with mean and standard deviation.
"""
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, frames):
"""
Args:
tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
Returns:
Tensor: Normalized Tensor image.
"""
frames = (frames - self.mean) / self.std # ํธ์ฐจ๋ฅผ ํ์ค ํธ์ฐจ๋ก ๋๋ ๊ฐ : z-score normalization
return frames
def __repr__(self):
return self.__class__.__name__+'(mean={0}, std={1})'.format(self.mean, self.std)
class CenterCrop(object):
"""Crop the given image at the center
"""
def __init__(self, size):
self.size = size
def __call__(self, frames):
"""
Args:
img (numpy.ndarray): Images to be cropped.
Returns:
numpy.ndarray: Cropped image.
"""
t, h, w = frames.shape
th, tw = self.size # ์๋ฅด๋ ค๊ณ ์ง์ ํ ๋์ด์ ๋์ด ์ฌ์ด์ฆ
delta_w = int(round((w - tw))/2.)
delta_h = int(round((h - th))/2.)
frames = frames[:, delta_h:delta_h+th, delta_w:delta_w+tw]
return frames # center crop๋ ์ด๋ฏธ์ง ๋ฐํ (np.array)
class RandomCrop(object):
"""Crop the given image at the center
"""
def __init__(self, size):
self.size = size
def __call__(self, frames):
"""
Args:
img (numpy.ndarray): Images to be cropped.
Returns:
numpy.ndarray: Cropped image.
"""
t, h, w = frames.shape # size: 96,96
th, tw = self.size
delta_w = random.randint(0, w-tw)
delta_h = random.randint(0, h-th)
frames = frames[:, delta_h:delta_h+th, delta_w:delta_w+tw]
return frames # random crop๋ ์ด๋ฏธ์ง ๋ฐํ (np.array)
def __repr__(self):
return self.__class__.__name__ + '(size={0})'.format(self.size) # random crop๋ ์ฌ์ด์ฆ๋ฅผ ๋ฐํ
class HorizontalFlip(object): # HorizontalFlip(๋น์จ๊ฐ ์
)
"""Flip image horizontally.
"""
def __init__(self, flip_ratio):
self.flip_ratio = flip_ratio
def __call__(self, frames):
"""
Args:
img (numpy.ndarray): Images to be flipped with a probability flip_ratio
Returns:
numpy.ndarray: Cropped image.
"""
t, h, w = frames.shape
if random.random() < self.flip_ratio:
for index in range(t):
frames[index] = cv2.flip(frames[index], 1)
return frames
class NormalizeUtterance():
"""Normalize per raw audio by removing the mean and divided by the standard deviation
"""
# z-score ์ ๊ทํ๋ฅผ ์คํ
def __call__(self, signal):
signal_std = 0. if np.std(signal)==0. else np.std(signal)
signal_mean = np.mean(signal)
return (signal - signal_mean) / signal_std
class AddNoise(object):
"""Add SNR noise [-1, 1]
"""
# snr(signal-to-noise ratio) : ์ ํธ ๋ ์ก์ ๋น, ์ด ๊ฐ์ด ํด์๋ก
def __init__(self, noise, snr_levels=[-5, 0, 5, 10, 15, 20, 9999]):
assert noise.dtype in [np.float32, np.float64], "noise only supports float data type" # noise๋ dtype๋ง ์ง์ํ๋ค.
self.noise = noise
self.snr_levels = snr_levels
def get_power(self, clip):
clip2 = clip.copy()
clip2 = clip2 **2
return np.sum(clip2) / (len(clip2) * 1.0)
def __call__(self, signal):
assert signal.dtype in [np.float32, np.float64], "signal only supports float32 data type" # signal์ dtype๋ง ์ง์ํ๋ค.
snr_target = random.choice(self.snr_levels)
if snr_target == 9999:
return signal
else:
# -- get noise
start_idx = random.randint(0, len(self.noise)-len(signal))
noise_clip = self.noise[start_idx:start_idx+len(signal)]
sig_power = self.get_power(signal)
noise_clip_power = self.get_power(noise_clip)
factor = (sig_power / noise_clip_power ) / (10**(snr_target / 10.0))
desired_signal = (signal + noise_clip*np.sqrt(factor)).astype(np.float32)
return desired_signal
|