I am working on Dictionary Learning
by using SPAMS
in Python
.
Here is my code =>
import spams
import numpy as np
from PIL import Image
import time
import matplotlib.pyplot as plt
img_file = 'gray-car.jpg'
try:
img = Image.open(img_file)
except:
print("Cannot load image %s : skipping test" %img_file)
I = np.array(img) / 255.
print('Shape : ',I.shape)
if I.ndim == 3:
A = np.asfortranarray(I.reshape((I.shape[0],I.shape[1] * I.shape[2])))
rgb = True
else:
A = np.asfortranarray(I)
rgb = False
m = 8;n = 8;
X = spams.im2col_sliding(A,m,n,rgb)
X = X - np.tile(np.mean(X,0),(X.shape[0],1))
X = np.asfortranarray(X / np.tile(np.sqrt((X * X).sum(axis=0)),(X.shape[0],1)),dtype=np.float64)
param = { 'K' : 100, # learns a dictionary with 100 elements
'lambda1' : 0.15, 'numThreads' : 4, 'batchsize' : 400,
'iter' : 1000}
tic = time.time()
D = spams.trainDL(X,**param)
tac = time.time()
t = tac - tic
print('time of computation for Dictionary Learning: %f' %t)
##param['approx'] = 0
# save dictionnary as dict.png
plt.imshow(D)
plt.show()
_objective(X,D,param,'dict')
Here the error that I am getting during learning =>
noise.py:27: RuntimeWarning: invalid value encountered in true_divide
X = np.asfortranarray(X / np.tile(np.sqrt((X *
X).sum(axis=0)),(X.shape[0],1)),dtype=np.float64)
/usr/local/lib/python3.4/dist-packages/numpy/core/fromnumeric.py:2699:
VisibleDeprecationWarning: `rank` is deprecated; use the `ndim`
attribute or function instead. To find the rank of a matrix see
`numpy.linalg.matrix_rank`. VisibleDeprecationWarning)
Input Image that I have used here =>