我正在使用Dictionary Learning
中的SPAMS
处理Python
。
这是我的代码=>
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')
这是我在学习期间得到的错误=>
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)
我在这里使用的输入图像=>
答案 0 :(得分:2)
让我们猜测X
不知何故代表一个图像,并且有一个白色的'边界:
In [127]: X=np.zeros((10,10))
In [128]: X[3:8,3:8]=1
....
In [130]: np.sqrt(X*X).sum(axis=0)
Out[130]: array([ 0., 0., 0., 5., 5., 5., 5., 5., 0., 0.])
平铺分区可以很容易地包含0,并产生运行时警告:
In [134]: X/np.tile(np.sqrt((X * X).sum(axis=0)),(X.shape[0],1))
/usr/local/bin/ipython3:1: RuntimeWarning: invalid value encountered in true_divide
#!/usr/bin/python3
Out[134]:
array([[ nan, nan, nan, 0. , 0. ,
0. , 0. , 0. , nan, nan],
[ nan, nan, nan, 0. , 0. ,
0. , 0. , 0. , nan, nan],
... ,
0. , 0. , 0. , nan, nan]])
我不知道它为什么使用asfortranarray()
,但我不会从中获得任何进一步的错误。
VisibleDeprecationWarning in python
是询问此VisibleDeprecationWarning
的帖子示例。 Theres是np.rank
函数:
In [138]: np.rank(X)
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: `rank` is deprecated; use the `ndim` attribute or function instead. To find the rank of a matrix see `numpy.linalg.matrix_rank`.
#!/usr/bin/python3
一些旧的或模糊的代码使用的是rank
而不是ndim
。
我很想责怪asfortranarray
,我以前没见过它。但它目前的代码只有:
return array(a, dtype, copy=False, order='F', ndmin=1)
不应该隐藏rank
电话的隐藏角落。
我认为我们需要更多地了解X
,尤其是它的形状和类型。也许它的步伐也很大。 im2col_sliding
表示它是使用as_strided
生成的滑动窗口数组。