In [15]:
cd ..


/Users/ejhumphrey/src/ejhumphrey-public/harmonic-cnn

In [17]:
import glob
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pescador
import seaborn
import scipy.signal as signal

import wcqtlib.train.streams as S

%matplotlib inline

seaborn.set_style('dark')
np.set_printoptions(precision=4, suppress=True)

pltargs = dict(
    interpolation='nearest', aspect='auto', 
    origin='lower')

In [2]:
cifar_fmt = "/Volumes/camel/cifar/cifar-10-batches-py/data_batch_{}.npz"

In [31]:
# load all images
x_obs = []
for fname in glob.glob(cifar_fmt.format("*")):
    x_obs += [np.load(fname)['data']]

x_obs = np.concatenate(x_obs, axis=0)
x_gray = x_obs.mean(axis=-1)

In [32]:
ncols = 5
fig, axes = plt.subplots(nrows=1, ncols=ncols, figsize=(10, 2))
for ax, n in zip(axes, np.random.permutation(len(x_gray))[:ncols]):
    ax.imshow(x_gray[n], cmap=plt.cm.gray)
    ax.set_xticklabels([])
    ax.set_yticklabels([])
    ax.set_axis_off()
plt.tight_layout()



In [33]:
x_mean = x_gray.mean(axis=0)
x_var = x_gray.var(axis=0)
x_centered = x_gray - x_mean

In [34]:
x_dim = 12
y_dim = 12
img_corr = np.array([signal.correlate2d(x, x[x_dim:-x_dim, y_dim:-y_dim], mode='valid') 
                     for x in x_centered])

In [37]:
img_corr_file = 'data/cifar_ave_pixel_corr.npy'
np.save(img_corr_file, img_corr.mean(axis=0))

In [38]:
img_corr = np.load(img_corr_file)

In [39]:
fig, ax = plt.subplots(figsize=(4, 4))
ax.imshow(img_corr, 
          interpolation='nearest', cmap=plt.cm.hot,
          extent=(-x_dim, x_dim, -y_dim, y_dim))
ax.set_xlabel(r'$\delta_x$')
ax.set_ylabel(r'$\delta_y$')
ax.set_title("Pixel Correlation Heatmap")
plt.tight_layout()


$C[n, m] = \frac{1}{IJK}\sum_{i=0}^{I-1}\sum_{j=0}^{J-1} \sum_{k=0}^{K-1} \bar{X}_i[j, k] * \bar{X}_i[j + n, k + m]$

$Y_i[n, m] = \frac{1}{JK}\sum_j \sum_k X_i[j, k] * X_i[j + n, k + m]$

i would expect to see E[(S[t, f] - mu[t, f]) * (S[t + x, f + y] - mu[t + f, mu + y])] where (x, y) range in +- MAX_FILTER_SIZE, and (t, f) range over the feasible region of the input


In [11]:
npz_files = glob.glob("/Volumes/camel/chordrec/cqts/*npz")
print(len(npz_files))
# Build a dataframe for the streamer
# and backfill an instrument so the streamer will still work...
recs = [dict(cqt=fname, instrument='flute') for fname in npz_files]
dframe = pd.DataFrame.from_records(recs)


302

In [18]:
seeds = [pescador.Streamer(S.cqt_slices, row, t_len=20) 
         for idx, row in dframe.iterrows()]
stream = pescador.mux(seeds, None, k=50, lam=50)

In [19]:
x_cqt = np.concatenate([next(stream)['x_in'][0] 
                        for n in range(25000)], axis=0)
print(x_cqt.shape)


(25000, 20, 204)

In [20]:
x_centered = np.log1p(50*x_cqt) - np.log1p(50*x_cqt).mean(axis=0)

In [21]:
# Symmetrical autocorr
bpo = 24
n_oct = 2
tdim = 6
fdim = (n_oct*bpo)
z_corr = np.array([signal.correlate2d(x, x[tdim:-tdim, fdim:-fdim], 
                                      mode='valid') 
                   for x in x_centered])

In [42]:
cqt_corr_file = 'data/cqt_ave_tf_corr.npy'
np.save(cqt_corr_file, z_corr.mean(axis=0))

In [43]:
cqt_corr = np.load(cqt_corr_file)

In [46]:
fig, ax = plt.subplots(1, 1, figsize=(12, 3))
ax.imshow(cqt_corr[:, ::-1], interpolation='nearest', aspect='auto',
          cmap=plt.cm.hot,
          extent=(-fdim, fdim, -tdim, tdim))
ax.set_xlabel("$i + \delta_f$")
ax.set_ylabel("$j + \delta_t$")
ax.set_title("Time-Frequency Correlation Heatmap")


Out[46]:
<matplotlib.text.Text at 0x142d11810>

In [27]:
fig, ax = plt.subplots(1, 1, figsize=(1, 4))
ax.imshow(z_corr.mean(axis=0).T, interpolation='nearest', aspect='auto',
          cmap=plt.cm.hot,
          extent=(-tdim, tdim, -fdim, fdim))
ax.set_xlabel("$i + \delta_t$")
ax.set_ylabel("$j + \delta_f$")
ax.set_title("Time-Frequency Correlation Heatmap")


Out[27]:
<matplotlib.text.Text at 0x15bc03e50>