How to use the kymatio.Scattering1D function in kymatio

To help you get started, we’ve selected a few kymatio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kymatio / kymatio / examples / 1d / plot_classif.py View on Github external
# 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
github kymatio / kymatio / examples / 1d / plot_real_signal.py View on Github external
# We are now ready to set up the parameters for the scattering transform.
# First, the number of samples, `T`, is given by the size of our input `x`.
# The averaging scale is specified as a power of two, `2**J`. Here, we set
# `J = 6` to get an averaging, or maximum, scattering scale of `2**6 = 64`
# samples. Finally, we set the number of wavelets per octave, `Q`, to `16`.
# This lets us resolve frequencies at a resolution of `1/16` octaves.

T = x.shape[-1]
J = 6
Q = 16

###############################################################################
# Finally, we are able to create the object which computes our scattering
# transform, `scattering`.

scattering = Scattering1D(J, T, Q)

###############################################################################
# Compute and display the scattering coefficients
# -----------------------------------------------
# Computing the scattering transform of a PyTorch Tensor is achieved using the
# `forward()` method of the `Scattering1D` class. The output is another Tensor
# of shape `(B, C, T)`. Here, `B` is the same as for the input `x`, but `C` is
# the number of scattering coefficient outputs, and `T` is the number of
# samples along the time axis. This is typically much smaller than the number
# of input samples since the scattering transform performs an average in time
# and subsamples the result to save memory.

Sx = scattering.forward(x)

###############################################################################
# To display the scattering coefficients, we must first identify which belong
github kymatio / kymatio / examples / 1d / plot_synthetic.py View on Github external
###############################################################################
# Spectrogram
# -----------
# Let's take a look at the signal spectrogram
plt.figure(figsize=(10, 10))
plt.specgram(x.numpy().ravel(), Fs=1024)
plt.title("Time-Frequency spectrogram of signal")

###############################################################################
# Doing the scattering transform
# ------------------------------
J = 6
Q = 16

scattering = Scattering1D(J, T, Q)

# get the metadata on the coordinates of the scattering
meta = Scattering1D.compute_meta_scattering(J, Q)
order0 = (meta['order'] == 0)
order1 = (meta['order'] == 1)
order2 = (meta['order'] == 2)

s = scattering.forward(x)[0]
plt.figure(figsize=(10, 10), dpi=300)
plt.subplot(3, 1, 1)
plt.plot(s[order0].numpy())
plt.title("Scattering order 0")
plt.subplot(3, 1, 2)
plt.imshow(s[order1].numpy(), aspect='auto')
plt.title("Scattering order 1")
plt.subplot(3, 1, 3)
github kymatio / kymatio / examples / 1d / plot_synthetic.py View on Github external
# -----------
# Let's take a look at the signal spectrogram
plt.figure(figsize=(10, 10))
plt.specgram(x.numpy().ravel(), Fs=1024)
plt.title("Time-Frequency spectrogram of signal")

###############################################################################
# Doing the scattering transform
# ------------------------------
J = 6
Q = 16

scattering = Scattering1D(J, T, Q)

# get the metadata on the coordinates of the scattering
meta = Scattering1D.compute_meta_scattering(J, Q)
order0 = (meta['order'] == 0)
order1 = (meta['order'] == 1)
order2 = (meta['order'] == 2)

s = scattering.forward(x)[0]
plt.figure(figsize=(10, 10), dpi=300)
plt.subplot(3, 1, 1)
plt.plot(s[order0].numpy())
plt.title("Scattering order 0")
plt.subplot(3, 1, 2)
plt.imshow(s[order1].numpy(), aspect='auto')
plt.title("Scattering order 1")
plt.subplot(3, 1, 3)
plt.imshow(s[order2].numpy(), aspect='auto')
plt.title("Scattering order 2")
github kymatio / kymatio / examples / 1d / plot_real_signal.py View on Github external
# Computing the scattering transform of a PyTorch Tensor is achieved using the
# `forward()` method of the `Scattering1D` class. The output is another Tensor
# of shape `(B, C, T)`. Here, `B` is the same as for the input `x`, but `C` is
# the number of scattering coefficient outputs, and `T` is the number of
# samples along the time axis. This is typically much smaller than the number
# of input samples since the scattering transform performs an average in time
# and subsamples the result to save memory.

Sx = scattering.forward(x)

###############################################################################
# To display the scattering coefficients, we must first identify which belong
# to each order (zeroth, first, or second). We do this by extracting the `meta`
# information from the scattering object and constructing masks for each order.

meta = Scattering1D.compute_meta_scattering(J, Q)
order0 = (meta['order'] == 0)
order1 = (meta['order'] == 1)
order2 = (meta['order'] == 2)


###############################################################################
# First, we plot the original signal `x`. Note that we have to index it as
# `x[0,0,:]` to convert it to a one-dimensional array and convert it to a
# numpy array using the `numpy()` method.

plt.figure(figsize=(8, 2))
plt.plot(x[0,:].numpy())
plt.title('Original signal')

###############################################################################
# We now plot the zeroth-order scattering coefficient, which is simply an
github kymatio / kymatio / examples / 1d / compute_speed.py View on Github external
# Determine which devices (CPU or GPU) that are supported by the current
# backend.

devices = []
if backend.NAME == 'torch':
    devices.append('cpu')
if backend.NAME == 'torch' and torch.cuda.is_available():
    devices.append('gpu')
if backend.NAME == 'skcuda' and torch.cuda.is_available():
    devices.append('gpu')

###############################################################################
# Create the `Scattering1D` object using the given parameters and generate
# some compatible test data with the specified batch size.

scattering = Scattering1D(J, T, Q)

x = torch.randn(batch_size, T, dtype=torch.float32)

###############################################################################
# Run the benchmark
# -----------------
# For each device, we need to convert the `scattering` object and the Tensor
# `x` to the appropriate type, invoke `times` calls to the `scattering.forward`
# and print the running times. Before the timer starts, we add an extra
# `scattering.forward` call to ensure any first-time overhead, such as memory
# allocation and CUDA kernel compilation, is not counted. If the benchmark is
# running on the GPU, we also need to call `torch.cuda.synchronize()` before
# and after the benchmark to make sure that all CUDA kernels have finished
# executing.

for device in devices: