BandwiseBinaryClassifier is no longer work in progress
This commit is contained in:
@ -1,15 +1,13 @@
|
|||||||
import librosa
|
import librosa
|
||||||
from librosa import display
|
|
||||||
import torch
|
import torch
|
||||||
from scipy.signal import butter, lfilter
|
from scipy.signal import butter, lfilter
|
||||||
|
|
||||||
from ml_lib.modules.utils import AutoPad
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
def scale_minmax(x, min=0.0, max=1.0):
|
def scale_minmax(x, min_val=0.0, max_val=1.0):
|
||||||
x_std = (x - x.min()) / (x.max() - x.min())
|
x_std = (x - x.min()) / (x.max() - x.min())
|
||||||
x_scaled = x_std * (max - min) + min
|
x_scaled = x_std * (max_val - min_val) + min_val
|
||||||
return x_scaled
|
return x_scaled
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
|
import torch
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from torch import nn
|
from torch import nn
|
||||||
from ml_lib.modules.utils import AutoPad, Interpolate, ShapeMixin
|
from ml_lib.modules.utils import AutoPad, Interpolate, ShapeMixin
|
||||||
|
|
||||||
|
DEVICE = torch.cuda.is_available()
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Sub - Modules
|
# Sub - Modules
|
||||||
###################
|
###################
|
||||||
|
|
||||||
class ConvModule(ShapeMixin, nn.Module):
|
class ConvModule(ShapeMixin, nn.Module):
|
||||||
|
|
||||||
def __init__(self, in_shape, conv_filters, conv_kernel, activation: nn.Module = nn.ELU, pooling_size=None,
|
def __init__(self, in_shape, conv_filters, conv_kernel, activation: nn.Module = nn.ELU, pooling_size=None,
|
||||||
|
@ -27,7 +27,7 @@ class ShapeMixin:
|
|||||||
@property
|
@property
|
||||||
def shape(self):
|
def shape(self):
|
||||||
x = torch.randn(self.in_shape).unsqueeze(0)
|
x = torch.randn(self.in_shape).unsqueeze(0)
|
||||||
output = self(x)
|
output: torch.Tensor = self(x)
|
||||||
return output.shape[1:]
|
return output.shape[1:]
|
||||||
|
|
||||||
|
|
||||||
@ -234,10 +234,10 @@ class AutoPadToShape(object):
|
|||||||
def __call__(self, x):
|
def __call__(self, x):
|
||||||
if not torch.is_tensor(x):
|
if not torch.is_tensor(x):
|
||||||
x = torch.as_tensor(x)
|
x = torch.as_tensor(x)
|
||||||
if x.shape == self.shape:
|
if x.shape[1:] == self.shape:
|
||||||
return x
|
return x
|
||||||
embedding = torch.zeros(self.shape)
|
embedding = torch.zeros((x.shape[0], *self.shape), device='cuda' if x.is_cuda else'cpu')
|
||||||
embedding[: x.shape] = x
|
embedding[:, :x.shape[1], :x.shape[2], :x.shape[3]] = x
|
||||||
return embedding
|
return embedding
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@ -253,20 +253,20 @@ class HorizontalSplitter(nn.Module):
|
|||||||
self.in_shape = in_shape
|
self.in_shape = in_shape
|
||||||
|
|
||||||
self.channel, self.height, self.width = self.in_shape
|
self.channel, self.height, self.width = self.in_shape
|
||||||
self.new_height = (self.height // self.n_horizontal_splits) + 1 if self.height % self.n != 0 else 0
|
self.new_height = (self.height // self.n) + 1 if self.height % self.n != 0 else 0
|
||||||
|
|
||||||
self.shape = (self.channel, self.new_height, self.width)
|
self.shape = (self.channel, self.new_height, self.width)
|
||||||
self.autopad = AutoPadToShape(self.shape)
|
self.autopad = AutoPadToShape(self.shape)
|
||||||
|
|
||||||
def foward(self, x):
|
def forward(self, x):
|
||||||
n_blocks = list()
|
n_blocks = list()
|
||||||
for block_idx in range(self.n):
|
for block_idx in range(self.n):
|
||||||
start = (self.channel, block_idx * self.height, self.width)
|
start = block_idx * self.new_height
|
||||||
end = (self.channel, (block_idx + 1) * self.height, self.width)
|
end = (block_idx + 1) * self.new_height
|
||||||
block = self.autopad(x[start:end])
|
block = self.autopad(x[:, :, start:end, :])
|
||||||
n_blocks.append(block)
|
n_blocks.append(block)
|
||||||
|
|
||||||
return tuple(n_blocks)
|
return n_blocks
|
||||||
|
|
||||||
|
|
||||||
class HorizontalMerger(nn.Module):
|
class HorizontalMerger(nn.Module):
|
||||||
|
Reference in New Issue
Block a user