<jupyter_start><jupyter_text>using torch to build up a pyramidhttps://pytorch.org/docs/master/nn.functional.htmltorch.nn.functional.interpolate<jupyter_code>import torch
img_tens = torch.Tensor(img_orig[None, None, ...])
print('Tensor shape=', img_tens.shape)
from torch.nn.functional import interpolate
mode='bilinear'
width = 32
base_levels = 1.61803
base_levels = 2

img_down = img_tens
n_levels = 1
while max(img_down.shape) > width :
    img_down = interpolate(img_down, scale_factor=1/base_levels, mode=mode)
    n_levels += 1
    print('Tensor shape=', img_down.shape, ', n_levels=', n_levels)<jupyter_output>Tensor shape= torch.Size([1, 1, 384, 384]) , n_levels= 2
Tensor shape= torch.Size([1, 1, 192, 192]) , n_levels= 3
Tensor shape= torch.Size([1, 1, 96, 96]) , n_levels= 4
Tensor shape= torch.Size([1, 1, 48, 48]) , n_levels= 5
Tensor shape= torch.Size([1, 1, 24, 24]) , n_levels= 6<jupyter_text>Applying on the central crop of $64\times64$<jupyter_code>1024**2 / (40 * 32**2)
img_tens = torch.Tensor(img_orig[None, None, ...])
img_down = img_tens
img_crop = torch.zeros((1, n_levels, width, width))
fig, axs = plt.subplots(1, n_levels-1, figsize=(20,20))
for i_level, ax in enumerate(axs):
    img_residual = img_down
    img_down = interpolate(img_down, scale_factor=1/base_levels, mode=mode)
    img_residual -= interpolate(img_down, size=img_residual.shape[-2:], mode=mode)
    
    h_res, w_res = img_residual.shape[-2:]
    img_crop[0, i_level, :, :] = img_residual[:, :, 
                        (h_res//2-width//2):(h_res//2+width//2), 
                        (w_res//2-width//2):(w_res//2+width//2)]
    
    ax.imshow(img_crop.numpy()[0, i_level, ...], cmap='gray')
print('Tensor shape=', img_crop.shape)<jupyter_output>Tensor shape= torch.Size([1, 6, 32, 32])<jupyter_text>creating a set of filters<jupyter_code>from LogGabor import LogGabor
pe = {'N_X': width, 'N_Y': width, 'do_mask': False, 'do_whitening': True,
      'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N':
          0.07, 'white_N_0': 0.0, 'white_f_0': 0.4, 'white_alpha': 1.4,
      'white_steepness': 4.0, 'white_recompute': False, 'base_levels':
          base_levels, 'n_theta': 24, 'B_sf': 0.4, 'B_theta': 0.17453277777777776,
      'use_cache': True, 'figpath': 'results', 'edgefigpath':
          'results/edges', 'matpath': 'cache_dir', 'edgematpath':
          'cache_dir/edges', 'datapath': 'database/', 'ext': '.pdf', 'figsize':
          14.0, 'formats': ['pdf', 'png', 'jpg'], 'dpi': 450, 'verbose': 0}
lg = LogGabor(pe)
print('lg shape=', lg.pe.N_X, lg.pe.N_Y)
def local_filter(azimuth, theta, phase, sf_0=.3, radius=width/4):

    x, y = lg.pe.N_X//2, lg.pe.N_Y//2 # center
    x += radius * np.cos(azimuth)
    y += radius * np.sin(azimuth)
    
    return lg.normalize(lg.invert(
        lg.loggabor(x, y, sf_0=sf_0, B_sf=lg.pe.B_sf, theta=theta, B_theta=lg.pe.B_theta) * np.exp(-1j * phase)))

K = local_filter(azimuth=0, theta=0, phase=0, radius=width/4)
print('K shape=', K.shape)
print('K min max=', K.min(), K.max())
n_test = 8
fig, axs = plt.subplots(1, n_test, figsize=(20,20))
for i_azimuth, ax in enumerate(axs):
    ax.imshow(local_filter(azimuth=i_azimuth*2*np.pi/n_test, 
                           theta=0, phase=0), vmin=-1, vmax=1, cmap='plasma')

fig, axs = plt.subplots(1, n_test, figsize=(20,20))
for i_azimuth, ax in enumerate(axs):
    ax.imshow(local_filter(azimuth=i_azimuth*2*np.pi/n_test, 
                           theta=i_azimuth*np.pi/n_test, phase=0), vmin=-1, vmax=1, cmap='plasma')

fig, axs = plt.subplots(1, n_test, figsize=(20,20))
for i_phase, ax in enumerate(axs):
    ax.imshow(local_filter(azimuth=0, theta=0, phase=i_phase*2*np.pi/n_test), vmin=-1, vmax=1, cmap='plasma')


n_azimuth = 8 
n_theta = 12
n_phase = 2

#K = np.zeros((width*width, n_azimuth*n_theta*n_phase))
K = np.zeros((width, width, n_azimuth*n_theta*n_phase))
i_ = 0
for i_azimuth in range(n_azimuth):
    for i_theta in range(n_theta):
        for i_phase in range(n_phase):
            #i_ = i_azimuth * n_theta*n_phase + i_theta * n_phase + i_phase
            #print(i_)
            K[..., i_] = local_filter(azimuth=i_azimuth*2*np.pi/n_azimuth, 
                                       theta=i_theta*np.pi/n_theta, 
                                       phase=i_phase*np.pi/n_phase)#.ravel()
            i_ += 1
print('K shape=', K.shape)
print('K min max=', K.min(), K.max())<jupyter_output>K shape= (32, 32, 192)
K min max= -1.0 1.0<jupyter_text>applying the filter<jupyter_code>K_ = torch.Tensor(K)
print('Tensor shape=', K_.shape)
print('Tensor shape=', img_crop.shape)
# torch.einsum('i, j, k, l->ij', img_crop, K_)<jupyter_output><empty_output><jupyter_text>https://pytorch.org/docs/1.2.0/nn.functional.htmllinearr"""Applies a linear transformation to the incoming data: :math:`y = xA^T + b`.Shape:    - Input: :math:`(N, *, in\_features)` where `*` means any number of      additional dimensions    - Weight: :math:`(out\_features, in\_features)`    - Bias: :math:`(out\_features)`    - Output: :math:`(N, *, out\_features)`"""<jupyter_code>img_crop_ = img_crop.reshape((1, 6, 1024))
print('Tensor shape=', img_crop_.shape)
#from torch.nn.functional import linear
#out = linear(img_crop_[0, :, :], K_, bias=None)<jupyter_output><empty_output><jupyter_text>https://pytorch.org/docs/master/torch.htmltorch.pinverse<jupyter_code>#torch.dot?
#torch.dot(torch.rand(5, 4), torch.rand(5, 4))
torch.tensordot?
%%timeit
out = torch.tensordot(img_crop, K_,  dims=2)
out = torch.tensordot(img_crop, K_,  dims=2)
print('Tensor shape=', out.shape)<jupyter_output>Tensor shape= torch.Size([1, 6, 192])<jupyter_text>reconstruction of cropped central images layer by layer<jupyter_code>fig, axs = plt.subplots(1, n_levels-1, figsize=(20,20))
for i_level, ax in enumerate(axs):
    ax.imshow(img_crop.numpy()[0, i_level, ...], cmap='gray')
print('Tensor shape=', img_crop.shape)
print('Tensor shape=', K_.shape)
K__ = K_.reshape((1024, n_azimuth*n_theta*n_phase))
print('Tensor shape=', K__.shape)
K_inv = torch.pinverse(K__)
print('Tensor shape=', K_inv.shape)
img_rec =  torch.tensordot(out, K_inv,  dims=1).reshape((1, 6, 32, 32))
print('Tensor shape=', img_rec.shape)
fig, axs = plt.subplots(1, n_levels-1, figsize=(20,20))
for i_level, ax in enumerate(axs):
    ax.imshow(img_rec.numpy()[0, i_level, ...], cmap='gray')
print('Tensor shape=', img_crop.shape)
img_rec =  torch.tensordot(out, K__.T,  dims=1).reshape((1, 6, 32, 32))
print('Tensor shape=', img_rec.shape)
fig, axs = plt.subplots(1, n_levels-1, figsize=(20,20))
for i_level, ax in enumerate(axs):
    ax.imshow(img_rec.numpy()[0, i_level, ...], cmap='gray')
print('Tensor shape=', img_crop.shape)<jupyter_output>Tensor shape= torch.Size([1, 6, 32, 32])
{'fraction_non_alphanumeric': 0.17850259451445516, 'fraction_numerical': 0.0385470719051149, 'mean_word_length': 4.241647241647241, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '544308', 'license': 'MIT', 'n_tokens_mistral': 2958, 'n_tokens_neox': 2741, 'n_words': 528, 'path': 'dev/2020-02-07_retina_warp.ipynb', 'repo_name': 'bicv/POLO', 'size': '6745'}