Table of content
%load_ext autoreload
%autoreload 2
from __future__ import division, print_function
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
np.set_printoptions(precision=4)#, suppress=True)
%cd -q ../probe
from NeuroTools.parameters import ParameterSet
from SLIP import Image
from LogGabor import LogGabor
from SparseEdges import SparseEdges
#! defining framework
#!-------------------
mp = SparseEdges('https://raw.githubusercontent.com/bicv/SparseEdges/master/default_param.py')
mp.pe.N = 4
mp.pe.MP_alpha=1.
mp.pe.do_whitening = False
!ls -l ../probe/cache_dir/experiment_test_*
-rw-r--r-- 1 laurentperrinet staff 6272 Jun 15 16:41 ../probe/cache_dir/experiment_test_MP.npy -rw-r--r-- 1 laurentperrinet staff 12416 Jun 15 18:44 ../probe/cache_dir/experiment_test_nowhitening.npy -rw-r--r-- 1 laurentperrinet staff 24704 Jun 15 18:04 ../probe/cache_dir/experiment_test_whitening.npy -rw-r--r-- 1 laurentperrinet staff 4224 Jun 15 18:04 ../probe/cache_dir/experiment_test_whitening_MSE.npy
cardinal points in X, Y (clockwise): E (=right=3 o'clock=horizontal), ESE, S, WSW, W, WNW, N, ENE :
X = np.array([0, 1./4, 1./2, 1./4, 0, -1./4, -1./2])#, -1./4])\n",
Y = np.array([1./2, np.sqrt(3)/4, 0, -np.sqrt(3)/4, -1./2, -np.sqrt(3)/4, 0.])#, np.sqrt(3)/
Theta = np.arctan2(-X, Y) # perpendicular to circle
Theta = ((Theta + np.pi/2 - np.pi/mp.pe.N_phi/2 ) % (np.pi)) - np.pi/2 + np.pi/mp.pe.N_phi/2
print(['%0.1f' % (angle*180/np.pi) for angle in Theta])
['-0.0', '-30.0', '90.0', '30.0', '-0.0', '-30.0', '90.0']
We set the [0,0] index of the array in the upper left left corner of the axes.
The X, Y image axis are defined in the matrix style (origin on the upper left; X going vertically down, Y going right horizontally) - the angle is defined as the angle wrt to the horizontal axis :
fig, a = mp.show_edges(np.vstack(((X/2. + .5)*mp.pe.N_X, (Y/2. + .5)*mp.pe.N_X, Theta, .1*np.ones(X.shape), np.ones(X.shape), np.zeros(X.shape) )))
t = [a.text(Y_, X_, r'(%0.1f,%0.1f), $\theta=$%0.1f' % (X_, Y_, angle*180/np.pi)) for (X_, Y_, angle) in zip((X/2. + .5)*mp.pe.N_X, (Y/2. + .5)*mp.pe.N_X, Theta)]
a.set_xlabel('Y (columns in the image matrix - from west to east)')
_ = a.set_ylabel('X (rows in the image matrix - from north to south)')
Overlaying an image :
mp = SparseEdges('https://raw.githubusercontent.com/bicv/SparseEdges/master/default_param.py')
mp.pe.N = 128 # number of edges
mp.pe.figsize_edges = 9
#! defining a reference test image (see test_Image)
image = np.zeros((mp.pe.N_X, mp.pe.N_Y))
image[mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4, mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4] = 1
image[mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4, mp.pe.N_X//4:mp.pe.N_X//2] = -1
edges = np.vstack(((X/2. + .5)*mp.pe.N_X, (Y/2. + .5)*mp.pe.N_X, Theta, .1*np.ones(X.shape), np.ones(X.shape), np.zeros(X.shape) ))
fig, a = mp.show_edges(edges, image=image)
#fig, a = mp.show_edges(edges, image=im.whitening(image))
t = [a.text(Y_, X_, r'(%0.1f,%0.1f), $\theta=$%0.1f' % (X_, Y_, angle*180/np.pi)) for (X_, Y_, angle) in zip((X/2. + .5)*mp.pe.N_X, (Y/2. + .5)*mp.pe.N_X, Theta)]
a.set_xlabel('Y (columns in the image matrix - from west to east)')
_ = a.set_ylabel('X (lines in the image matrix - from north to south)')
mp = SparseEdges('https://raw.githubusercontent.com/bicv/SparseEdges/master/default_param.py')
mp.set_size((32, 64))
mp.pe.N = 128 # number of edges
mp.pe.figsize_edges = 4.5
#! defining a reference test image (see test_Image)
image = np.zeros((mp.pe.N_X, mp.pe.N_Y))
image[mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4, mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4] = 1
image[mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4, mp.pe.N_X//4:mp.pe.N_X//2] = -1
edges = np.vstack(((X/2. + .5)*mp.pe.N_X-1, (Y/2. + .5)*mp.pe.N_X-1, Theta, .5*np.ones(X.shape), np.ones(X.shape), np.zeros(X.shape) ))
fig, a = mp.show_edges(edges, image=mp.whitening(image))
mp.set_size((256, 256))
#image = mp.imread('../../SLIP/database/lena256.png')
image = mp.imread('https://raw.githubusercontent.com/bicv/SLIP/master/database/lena256.png')
mp.pe.N = 128 # number of edges
mp.pe.figsize_edges = 9
edges = np.vstack(((X/2. + .5)*mp.pe.N_X-1, (Y/2. + .5)*mp.pe.N_X-1, Theta, .1*np.ones(X.shape), np.ones(X.shape), np.zeros(X.shape) ))
fig, a = mp.show_edges(edges, image=mp.whitening(image))
mp.set_size((128, 128))
#! defining a reference test image (see test_Image)
image = np.zeros((mp.pe.N_X, mp.pe.N_Y))
image[mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4, mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4] = 1
image[mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4, mp.pe.N_X//4:mp.pe.N_X//2] = -1
mp.imshow(image);
%%writefile experiment_test_MP.py
import numpy as np
from SparseEdges import SparseEdges
mp = SparseEdges('https://raw.githubusercontent.com/bicv/SparseEdges/master/default_param.py')
mp.pe.N = 128 # number of edges
mp.pe.figsize_edges = 9
#! defining a reference test image (see test_Image)
image = np.zeros((mp.pe.N_X, mp.pe.N_Y))
image[mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4, mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4] = 1
image[mp.pe.N_X//2:mp.pe.N_X//2+mp.pe.N_X//4, mp.pe.N_X//4:mp.pe.N_X//2] = -1
white = mp.pipeline(image, center=True, do_whitening=True)
import os
matname = os.path.join(mp.pe.matpath, 'experiment_test_MP.npy')
try:
edges = np.load(matname)
except Exception:
edges, C_res = mp.run_mp(white, verbose=True)
try:
os.mkdir(mp.pe.matpath)
except Exception:
pass
np.save(matname, edges)
Overwriting experiment_test_MP.py
%run experiment_test_MP.py
<Figure size 432x288 with 0 Axes>
#fig, a = mp.show_edges(edges, image=mp.whitening(image))
fig, a = mp.show_edges(edges, image=mp.dewhitening(mp.reconstruct(edges)))
%%writefile experiment_test_whitening.py
import numpy as np
from SparseEdges import SparseEdges
mp = SparseEdges('https://raw.githubusercontent.com/bicv/SparseEdges/master/default_param.py')
mp.pe.N = 2**9
mp.pe.MP_alpha = 1
mp.init()
image = mp.imread('https://raw.githubusercontent.com/bicv/SLIP/master/database/serre07_targets/B_N107001.jpg')
white = mp.pipeline(image, do_whitening=True)
import os
matname = os.path.join(mp.pe.matpath, 'experiment_test_whitening.npy')
try:
edges = np.load(matname)
except Exception:
edges, C_res = mp.run_mp(white, verbose=True)
np.save(matname, edges)
matname_MSE = os.path.join(mp.pe.matpath, 'experiment_test_whitening_MSE.npy')
try:
MSE = np.load(matname_MSE)
except Exception:
MSE = np.ones(mp.pe.N)
image_rec = np.zeros_like(image)
for i_N in range(mp.pe.N):
MSE[i_N] = ((white-image_rec)**2).sum()
image_rec += mp.reconstruct(edges[:, i_N][:, np.newaxis])
np.save(matname_MSE, MSE)
Overwriting experiment_test_whitening.py
%run experiment_test_whitening.py
<Figure size 432x288 with 0 Axes>
#edges[0, :] = pe.N_X - edges[0,:] # HACK
#edges[2, :] = - edges[2,:] # HACK
#edges[:, 119] = edges[:, 118] # HACK
fig_width_pt = 318.670 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27 # Convert pt to inches
fig_width = fig_width_pt*inches_per_pt # width in inches
mp.pe.figsize_edges = .382 * fig_width
fig, a = mp.show_edges(edges, image=image, color='brown', show_phase=False) #, pedestal=.0, gamma=.3
#import os
#fig.savefig(os.path.join(mp.pe.figpath, 'SparseLets_A.pdf'))
#! recontructing the image from the edges
image_rec = mp.reconstruct(edges)
print('MSE-W = ', ((white-image_rec)**2).sum()/((white)**2).sum())
MSE-W = 0.3351719916880489
fig, a = mp.show_edges(edges, image=image_rec*1., color='brown')
Recontructing the original image by dewhitening the reconstructed image:
fig, a = mp.show_edges(edges, image=mp.dewhitening(image_rec)*1., color='brown')
MSE = np.load(os.path.join(mp.pe.matpath, 'experiment_test_whitening_MSE.npy'))
print(MSE.shape, mp.pe.N)
(512,) 512
# checking the quick computation of the MSE in MP
MSE_0 = (white**2).sum()
print('stats on white:', white.mean(), white.std(), np.sqrt(MSE_0), np.sqrt(MSE[0]))
print ('mp.pe.MP_alpha=', mp.pe.MP_alpha)
MSE_MP = np.ones(mp.pe.N)
MSE_MP[1:] = 1. - np.cumsum(edges[4, :-1]**2) * (2 - mp.pe.MP_alpha)/mp.pe.MP_alpha / MSE_0
plt.figure(figsize=(12,6))
plt.subplot(111)
plt.plot(np.sqrt(MSE/MSE[0]), 'ro', label='true', alpha=.2)
plt.plot(np.sqrt(MSE_MP), 'g--', label='MP-vec', lw=2)
plt.xlim([0, mp.pe.N])
plt.ylim([0, 1])
plt.xlabel('# atoms')
plt.ylabel('MSE')
_ = plt.legend()
stats on white: -0.00020648729955733365 0.0902248428093429 23.0976202472936 23.0976202472936 mp.pe.MP_alpha= 1
%%writefile experiment_test_nowhitening.py
import numpy as np
from SparseEdges import SparseEdges
mp = SparseEdges('https://raw.githubusercontent.com/bicv/SparseEdges/master/default_param.py')
mp.pe.N = 256
mp.pe.do_whitening = False
#image = mp.imread('https://raw.githubusercontent.com/bicv/SLIP/master/database/serre07_targets/B_N107001.jpg')
image = mp.imread('../../SLIP/database/serre07_targets/B_N107001.jpg')
mp.pe.figsize_edges = 9
image = mp.pipeline(image, do_whitening=False)
import os
matname = os.path.join(mp.pe.matpath, 'experiment_test_nowhitening.npy')
try:
edges = np.load(matname)
except Exception:
edges, C_res = mp.run_mp(image, verbose=True)
np.save(matname, edges)
Overwriting experiment_test_nowhitening.py
%run experiment_test_nowhitening.py
<Figure size 432x288 with 0 Axes>
#! recontructing the image from the edges
image_ = mp.reconstruct(edges)
print('MSE = ', ((image-image_)**2).sum()/((image)**2).sum())
MSE = 0.08428340351792607
#! recontructing the image from the edges
image_ = mp.reconstruct(edges)
print('WMSE = ', ((mp.whitening(image-image_))**2).sum()/((mp.whitening(image))**2).sum())
WMSE = 0.6103895965836417
fig, a = mp.show_edges(edges, image=image_*1., show_mask=True, color='brown')
%load_ext watermark
%watermark
2018-07-13T17:10:45+02:00 CPython 3.7.0 IPython 6.4.0 compiler : Clang 9.1.0 (clang-902.0.39.2) system : Darwin release : 17.6.0 machine : x86_64 processor : i386 CPU cores : 4 interpreter: 64bit
%load_ext version_information
%version_information numpy, scipy, matplotlib, sympy, pillow, imageio
Software | Version |
---|---|
Python | 3.7.0 64bit [Clang 9.1.0 (clang-902.0.39.2)] |
IPython | 6.4.0 |
OS | Darwin 17.6.0 x86_64 i386 64bit |
numpy | 1.14.5 |
scipy | 1.1.0 |
matplotlib | 2.2.2 |
sympy | 1.2 |
pillow | 5.2.0 |
imageio | 2.3.0 |
Fri Jul 13 17:10:46 2018 CEST |
%cd -q ../notebooks