BandwiseBinaryClassifier is no longer work in progress

This commit is contained in:
Si11ium 2020-05-05 10:58:34 +02:00
parent f285200917
commit 3c776f13c5
3 changed files with 16 additions and 15 deletions

View File

@ -1,15 +1,13 @@
import librosa
from librosa import display
import torch
from scipy.signal import butter, lfilter
from ml_lib.modules.utils import AutoPad
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_scaled = x_std * (max - min) + min
x_scaled = x_std * (max_val - min_val) + min_val
return x_scaled

View File

@ -1,14 +1,17 @@
from typing import Union
import torch
import warnings
from torch import nn
from ml_lib.modules.utils import AutoPad, Interpolate, ShapeMixin
DEVICE = torch.cuda.is_available()
#
# Sub - Modules
###################
class ConvModule(ShapeMixin, nn.Module):
def __init__(self, in_shape, conv_filters, conv_kernel, activation: nn.Module = nn.ELU, pooling_size=None,

View File

@ -27,7 +27,7 @@ class ShapeMixin:
@property
def shape(self):
x = torch.randn(self.in_shape).unsqueeze(0)
output = self(x)
output: torch.Tensor = self(x)
return output.shape[1:]
@ -234,10 +234,10 @@ class AutoPadToShape(object):
def __call__(self, x):
if not torch.is_tensor(x):
x = torch.as_tensor(x)
if x.shape == self.shape:
if x.shape[1:] == self.shape:
return x
embedding = torch.zeros(self.shape)
embedding[: x.shape] = x
embedding = torch.zeros((x.shape[0], *self.shape), device='cuda' if x.is_cuda else'cpu')
embedding[:, :x.shape[1], :x.shape[2], :x.shape[3]] = x
return embedding
def __repr__(self):
@ -253,20 +253,20 @@ class HorizontalSplitter(nn.Module):
self.in_shape = 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.autopad = AutoPadToShape(self.shape)
def foward(self, x):
def forward(self, x):
n_blocks = list()
for block_idx in range(self.n):
start = (self.channel, block_idx * self.height, self.width)
end = (self.channel, (block_idx + 1) * self.height, self.width)
block = self.autopad(x[start:end])
start = block_idx * self.new_height
end = (block_idx + 1) * self.new_height
block = self.autopad(x[:, :, start:end, :])
n_blocks.append(block)
return tuple(n_blocks)
return n_blocks
class HorizontalMerger(nn.Module):