Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.M, self.N = self.shape
if not self.backend:
from ..backend.tensorflow_backend import backend # is imported like a module and not a class?
self.backend = backend
elif self.backend.name[0:10] != 'tensorflow':
raise RuntimeError('This backend is not supported.')
if 2 ** self.J > self.shape[0] or 2 ** self.J > self.shape[1]:
raise RuntimeError('The smallest dimension should be larger than 2^J')
self.M_padded, self.N_padded = compute_padding(self.M, self.N, self.J)
# pads equally on a given side if the amount of padding to add is an even number of pixels, otherwise it adds an extra pixel
self.pad = self.backend.Pad(
[(self.M_padded - self.M) // 2, (self.M_padded - self.M + 1) // 2, (self.N_padded - self.N) // 2,
(self.N_padded - self.N + 1) // 2], [self.M, self.N], pre_pad=self.pre_pad)
self.unpad = self.backend.unpad
filters = filter_bank(self.M_padded, self.N_padded, self.J, self.L)
self.phi, self.psi = filters['phi'], filters['psi']
self.M, self.N = self.shape
if not self.backend:
from ..backend.numpy_backend import backend # is imported like a module and not a class?
self.backend = backend
elif self.backend.name[0:5] != 'numpy':
raise RuntimeError('This backend is not supported.')
if 2 ** self.J > self.shape[0] or 2 ** self.J > self.shape[1]:
raise RuntimeError('The smallest dimension should be larger than 2^J')
self.M_padded, self.N_padded = compute_padding(self.M, self.N, self.J)
# pads equally on a given side if the amount of padding to add is an even number of pixels, otherwise it adds an extra pixel
self.pad = self.backend.Pad(
[(self.M_padded - self.M) // 2, (self.M_padded - self.M + 1) // 2, (self.N_padded - self.N) // 2,
(self.N_padded - self.N + 1) // 2], [self.M, self.N], pre_pad=self.pre_pad)
self.unpad = self.backend.unpad
filters = filter_bank(self.M_padded, self.N_padded, self.J, self.L)
self.phi, self.psi = filters['phi'], filters['psi']
def build(self):
self.M, self.N = self.shape
self.modulus = Modulus()
self.M_padded, self.N_padded = compute_padding(self.M, self.N, self.J)
# pads equally on a given side if the amount of padding to add is an even number of pixels, otherwise it adds an extra pixel
self.pad = Pad([(self.M_padded - self.M) // 2, (self.M_padded - self.M+1) // 2, (self.N_padded - self.N) // 2, (self.N_padded - self.N + 1) // 2], [self.M, self.N], pre_pad=self.pre_pad)
self.subsample_fourier = SubsampleFourier()
# Create the filters
filters = filter_bank(self.M_padded, self.N_padded, self.J, self.L)
self.Psi = convert_filters(filters['psi'])
self.Phi = convert_filters([filters['phi'][j] for j in range(self.J)])
def build(self):
self.M, self.N = self.shape
if 2 ** self.J > self.M or 2 ** self.J > self.N:
raise RuntimeError('The smallest dimension should be larger than 2^J.')
self.M_padded, self.N_padded = compute_padding(self.M, self.N, self.J)
# pads equally on a given side if the amount of padding to add is an even number of pixels, otherwise it adds an extra pixel
self.pad = self.backend.Pad([(self.M_padded - self.M) // 2, (self.M_padded - self.M+1) // 2, (self.N_padded - self.N) // 2,
(self.N_padded - self.N + 1) // 2], [self.M, self.N], pre_pad=self.pre_pad)
self.unpad = self.backend.unpad
def build(self):
self.M, self.N = self.shape
# use the default backend if no backend is provided
if not self.backend:
from ..backend.torch_backend import backend
self.backend = backend
elif self.backend.name[0:5] != 'torch':
raise RuntimeError('This backend is not supported.')
if 2 ** self.J > self.shape[0] or 2 ** self.J > self.shape[1]:
raise RuntimeError('The smallest dimension should be larger than 2^J.')
self.M_padded, self.N_padded = compute_padding(self.M, self.N, self.J)
# pads equally on a given side if the amount of padding to add is an even number of pixels, otherwise it adds an extra pixel
self.pad = self.backend.Pad([(self.M_padded - self.M) // 2, (self.M_padded - self.M+1) // 2, (self.N_padded - self.N) // 2,
(self.N_padded - self.N + 1) // 2], [self.M, self.N], pre_pad=self.pre_pad)
self.unpad = self.backend.unpad
self.create_and_register_filters()
def build(self):
self.M, self.N = self.shape
self.modulus = Modulus()
self.M_padded, self.N_padded = compute_padding(self.M, self.N, self.J)
# pads equally on a given side if the amount of padding to add is an even number of pixels, otherwise it adds an extra pixel
self.pad = Pad([(self.M_padded - self.M) // 2, (self.M_padded - self.M+1) // 2, (self.N_padded - self.N) // 2, (self.N_padded - self.N + 1) // 2], [self.M, self.N], pre_pad=self.pre_pad)
self.subsample_fourier = SubsampleFourier()
# Create the filters
filters = filter_bank(self.M_padded, self.N_padded, self.J, self.L)
self.Psi = convert_filters(filters['psi'])
self.Phi = convert_filters([filters['phi'][j] for j in range(self.J)])
def build(self):
self.M, self.N = self.shape
if not self.backend:
from ..backend.tensorflow_backend import backend # is imported like a module and not a class?
self.backend = backend
elif self.backend.name[0:10] != 'tensorflow':
raise RuntimeError('This backend is not supported.')
if 2 ** self.J > self.shape[0] or 2 ** self.J > self.shape[1]:
raise RuntimeError('The smallest dimension should be larger than 2^J')
self.M_padded, self.N_padded = compute_padding(self.M, self.N, self.J)
# pads equally on a given side if the amount of padding to add is an even number of pixels, otherwise it adds an extra pixel
self.pad = self.backend.Pad(
[(self.M_padded - self.M) // 2, (self.M_padded - self.M + 1) // 2, (self.N_padded - self.N) // 2,
(self.N_padded - self.N + 1) // 2], [self.M, self.N], pre_pad=self.pre_pad)
self.unpad = self.backend.unpad
filters = filter_bank(self.M_padded, self.N_padded, self.J, self.L)
self.phi, self.psi = filters['phi'], filters['psi']
def build(self):
self.M, self.N = self.shape
if not self.backend:
from ..backend.numpy_backend import backend # is imported like a module and not a class?
self.backend = backend
elif self.backend.name[0:5] != 'numpy':
raise RuntimeError('This backend is not supported.')
if 2 ** self.J > self.shape[0] or 2 ** self.J > self.shape[1]:
raise RuntimeError('The smallest dimension should be larger than 2^J')
self.M_padded, self.N_padded = compute_padding(self.M, self.N, self.J)
# pads equally on a given side if the amount of padding to add is an even number of pixels, otherwise it adds an extra pixel
self.pad = self.backend.Pad(
[(self.M_padded - self.M) // 2, (self.M_padded - self.M + 1) // 2, (self.N_padded - self.N) // 2,
(self.N_padded - self.N + 1) // 2], [self.M, self.N], pre_pad=self.pre_pad)
self.unpad = self.backend.unpad
filters = filter_bank(self.M_padded, self.N_padded, self.J, self.L)
self.phi, self.psi = filters['phi'], filters['psi']
# If it's too long, truncate it.
if x.numel() > T:
x = x[:T]
# If it's too short, zero-pad it.
start = (T - x.numel()) // 2
x_all[k,start:start + x.numel()] = x
y_all[k] = y
###############################################################################
# Log-scattering transform
# ------------------------
# We now create the `Scattering1D` object that will be used to calculate the
# scattering coefficients.
scattering = Scattering1D(J, T, Q)
###############################################################################
# If we are using CUDA, the scattering transform object must be transferred to
# the GPU by calling its `cuda()` method. The data is similarly transferred.
if use_cuda:
scattering.cuda()
x_all = x_all.cuda()
y_all = y_all.cuda()
###############################################################################
# Compute the scattering transform for all signals in the dataset.
Sx_all = scattering.forward(x_all)
###############################################################################
# Since it does not carry useful information, we remove the zeroth-order
# scattering coefficients, which are always placed in the first channel of
order2_size = self.L ** 2 * J * (J - 1) // 2
output_size = order0_size + order1_size
if self.max_order == 2:
output_size += order2_size
S = input.new(input.size(0),
input.size(1),
output_size,
self.M_padded//(2**J)-2,
self.N_padded//(2**J)-2)
U_r = pad(input)
U_0_c = fft(U_r, 'C2C') # We trick here with U_r and U_2_c
# First low pass filter
U_1_c = subsample_fourier(cdgmm(U_0_c, phi[0]), k=2**J)
U_J_r = fft(U_1_c, 'C2R')
S[..., 0, :, :] = unpad(U_J_r)
n_order1 = 1
n_order2 = 1 + order1_size
for n1 in range(len(psi)):
j1 = psi[n1]['j']
U_1_c = cdgmm(U_0_c, psi[n1][0])
if(j1 > 0):
U_1_c = subsample_fourier(U_1_c, k=2 ** j1)
U_1_c = fft(U_1_c, 'C2C', inverse=True)
U_1_c = fft(modulus(U_1_c), 'C2C')
# Second low pass filter