A series of tests of the SLIP package.
In [1]:
%load_ext autoreload
%autoreload 2
import os
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
fopts = {'fontsize':18}
In [2]:
from SLIP import Image, imread
In [3]:
help(Image)
In [4]:
im = Image() # default is 'https://raw.githubusercontent.com/bicv/SLIP/master/default_param.py'
print(im.pe)
N_X and N_Y and mask_exponent are mandatory)
In [5]:
im = Image({'N_X':32, 'N_Y':64})
print(im.pe)
ndarray (dimensions N_X and N_Y are guessed from this array)
In [6]:
im = Image(np.empty((32, 64)))
print(im.pe)
In [7]:
im = Image('database/lena64.png')
print(im.pe)
In [8]:
im = Image('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png')
print(im.pe)
In [9]:
im = Image('file://default_param.py')
im = Image('default_param.py')
print(im.pe.keys())
In [10]:
im = Image('https://raw.githubusercontent.com/bicv/SLIP/master/default_param.py')
print(im.pe.keys())
In [11]:
from NeuroTools.parameters import ParameterSet
from SLIP import Image
im = Image(ParameterSet({'N_X':128, 'N_Y':256}))
The parameters may be handled using the properties of the ParameterSet object.
In [12]:
help(im.set_size)
In [13]:
im = Image() # default is {'N_X':128, 'N_Y':128}
print(im.pe)
In [14]:
im.set_size((512, 234))
print(im.pe)
In [15]:
im.set_size(np.ones((512, 234)))
print(im.pe)
In [16]:
im.set_size('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png')
print(im.pe)
we use https://docs.python.org/3.4/library/logging.html to handle logging of events
In [17]:
im = Image()
print('Verbosity level=', im.pe.verbose)
In [18]:
im.log.debug(' > this should not appear')
im.log.info(' > this should not appear')
im.log.error(' > this *should* appear')
In [19]:
im.pe.verbose = 15
im.init_logging()
im.log.debug(' > this should not appear')
im.log.info(' > this *should* appear')
im.log.error(' > this *should* appear')
In [20]:
try:
Image({'N_X':128, 'N_Y':-12, 'mask_exponent':3})
except Exception as e:
print(e)
In [21]:
im = Image()
print(im.pe)
image = im.imread('database/lena64.png')
print(im.pe)
In [22]:
image = im.imread('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png')
print(im.pe)
In [23]:
'N_X' in im.pe.keys()
Out[23]:
In [24]:
im = Image()
print(im.pe)
image = im.imread('database/lena64.png')
_ = im.imshow(image)
In [25]:
image = im.imread('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png')
_ = im.imshow(image)
In [26]:
_ = im.show_spectrum(image)
In [27]:
axs = []
im = Image({'N_X':32, 'N_Y':128, 'seed':None, 'do_mask':False})
im.pe.datapath = 'database/'
for name_database in ['serre07_targets', 'serre07_distractors']:
fig = plt.figure(figsize=(14, 3.5))
for _ in range(4):
image, filename, croparea = im.patch(name_database, )
ax = fig.add_subplot(1, 4, _+1)
im.imshow(image, fig=fig, ax=ax)
fig.show()
In [28]:
im = Image({'N_X':64, 'N_Y':128, 'seed':None, 'do_mask':False, 'N_image':10})
im.pe.datapath = 'database/'
help(im.make_imagelist)
# extract one image list from a database
imagelist = im.make_imagelist('serre07_targets')
print('Number of images:', len(imagelist))
print('First image:', imagelist[0])
In [29]:
help(im.get_imagelist)
im.pe.figpath, im.pe.matpath = '/tmp/fig', '/tmp/mat'
# store one image list from a database
imagelist = im.get_imagelist('classifier', 'serre07_targets')
!ls -l /tmp/mat
print('Number of images:', len(imagelist))
print('First image:', imagelist[0])
In [30]:
im = Image({'N_X':128, 'N_Y':128, 'seed':None, 'do_mask':False, 'N_image':10})
im.pe.figpath, im.pe.matpath = '/tmp/fig', '/tmp/mat'
# now we can access again this stored list
!ls -l /tmp/mat
imagelist = im.get_imagelist('classifier', 'serre07_targets')
print('Number of images:', len(imagelist))
print('First image:', imagelist[0])
defining a reference test image; check the axis labels for a (x,y) translation
In [31]:
im = Image({'N_X':128, 'N_Y':256})
image = np.zeros((im.pe.N_X, im.pe.N_Y))
image[im.pe.N_X//2:im.pe.N_X//2+im.pe.N_X//4, im.pe.N_X//2:im.pe.N_X//2+im.pe.N_X//4] = 1 # white square on the right
image[im.pe.N_X//2:im.pe.N_X//2+im.pe.N_X//4, im.pe.N_X//4:im.pe.N_X//2] = -1 # black square on its left
_ = im.imshow(image, axis=True)
translating the image by an integer by rolling indices
In [32]:
print(im.pe.N_X//8., im.pe.N_Y//4.)
_ = im.imshow(np.roll(np.roll(image, np.int(im.pe.N_X//8.), axis=0), np.int(im.pe.N_X//4.), axis=1))
remember axis conventions are: x going down, y going right with the origin on top
translating the image down
In [33]:
_ = im.imshow(im.translate(image, [im.pe.N_X//8., 0.]))
translating the image right
In [34]:
_ = im.imshow(im.translate(image, [0., im.pe.N_Y//4.]))
translating the image on both axis
In [35]:
_ = im.imshow(im.translate(image, [im.pe.N_X//8., im.pe.N_Y//4.]))
translating the image over the torus
In [36]:
_ = im.imshow(im.translate(image, [im.pe.N_X//2., im.pe.N_Y//4.]))
null translation gives the same image
In [37]:
_ = im.imshow(image - im.translate(image, [0., 0.]), norm=False)
the lg.translate function is invertible
In [38]:
_ = im.imshow(image - im.translate(im.translate(image, [1.64, -2.233]), [-1.64, 2.233]), norm=False)
also true for bigger translations (we separate integer part from fractional part in the translation)
In [39]:
_ = im.imshow(image - im.translate(im.translate(image, [182.64, -286.233]), [-182.64, 286.233], preshift=True), norm=False)
but not always true when we don't separate
In [40]:
_ = im.imshow(image - im.translate(im.translate(image, [182.64, -286.233]), [-182.64, 286.233], preshift=False), norm=False)
the lg.translate function is periodic
In [41]:
_ = im.imshow(image - im.translate(image, [-45*im.pe.N_X, 5*im.pe.N_Y]), norm=False)
lg.translate function is associative
In [42]:
_ = im.imshow(im.translate(image, [1., .0]) - im.translate(im.translate(image, [.5, .0]), [.5, .0]), norm=False)
In [43]:
_ = im.imshow(im.translate(image, [2., .0]) - im.translate(im.translate(image, [1.5, -1.0]), [.5, 1.0]), norm=False)
Check out the discussion @ https://laurentperrinet.github.io/sciblog/posts/2017-09-20-the-fastest-2d-convolution-in-the-world.html
In [44]:
axs = []
im = Image('default_param.py')
im.pe.datapath = 'database/'
for name_database in ['serre07_targets', 'serre07_distractors']:
for _ in range(4):
fig = plt.figure(figsize=(14, 7))
image, filename, croparea = im.patch(name_database, do_whitening=False)
ax = fig.add_subplot(1, 2, 1)
fig , ax = im.imshow(image, fig=fig, ax=ax)
ax = fig.add_subplot(1, 2, 2)
image, filename, croparea = im.patch(name_database, filename=filename, croparea=croparea, do_whitening=True)
fig , ax = im.imshow(image, fig=fig, ax=ax)
plt.show()
In [45]:
%load_ext watermark
%watermark