Sparse Filtering稀疏滤波

Sparse Filtering稀疏滤波

  它根本上是一个特征提取器,一般来说,大部分的特征学习方法都是试图去建模给定训练数据的真实分布。
  SAE的稀疏是对于模型参数的稀疏,即在cost function中加入了权值惩罚项;而SF,首先对特征矩阵的每一行进行正则化,除以其二范数,同样的方式对每一列进行正则化,最后我们最小化特征矩阵各项的绝对值和。
  聚焦在非监督学习Unsupervised feature learning算法。因为一般的非监督算法需要调整很多额外的参数hyperparameter。本文提出一个简单的算法:sparse filtering。它只有一个hyperparameter(需要学习的特征数目)需要调整。但它很有效。与其他的特征学习方法不同,sparse filtering并没有明确的构建输入数据的分布的模型。它只优化一个简单的代价函数(L2范数稀疏约束的特征),优化过程可以通过几行简单的Matlab代码就可以实现。

参考资料

http://blog.csdn.net/zouxy09/article/details/9982859
http://blog.csdn.net/itplus/article/details/22071035
http://blog.csdn.net/u013884378/article/details/20610247
http://www.cnblogs.com/kemaswill/p/3341974.html"

以下是稀疏滤波的Python代码: ``` import numpy as np from scipy import sparse def sparse_filter(image, kernel): """ Applies sparse filtering to an image using a given kernel. Parameters: image (ndarray): The image to be filtered, as a 2D numpy array. kernel (ndarray or sparse matrix): The kernel to be applied to the image. Returns: filtered_image (ndarray): The filtered image, as a 2D numpy array. """ # Ensure kernel is a sparse matrix if not isinstance(kernel, sparse.spmatrix): kernel = sparse.csr_matrix(kernel) # Define sizes of image and kernel m, n = image.shape k, l = kernel.shape # Pad image with zeros to handle edges of the image padded_image = np.pad(image, (k//2, l//2), mode='constant') # Define sparse matrix to store filter coefficients A = sparse.lil_matrix((m*n, m*n)) # Loop through each pixel in the image for i in range(m): for j in range(n): # Define indices of kernel centered at current pixel k_min, k_max = i, i + k l_min, l_max = j, j + l # Extract submatrix from padded image centered at current pixel sub_image = padded_image[k_min:k_max, l_min:l_max] # Flatten submatrix and kernel into column vectors sub_image_flat = sub_image.flatten() kernel_flat = kernel.flatten() # Calculate filter coefficient for current pixel coeff = np.dot(kernel_flat, sub_image_flat) # Add coefficient to diagonal of sparse matrix index = i*n + j A[index,index] = coeff # Apply filter to image filtered_image = A.dot(image.flatten()) filtered_image = filtered_image.reshape(m, n) return filtered_image ``` 使用示例: ``` import matplotlib.pyplot as plt from skimage import data from scipy import ndimage # Load an image image = data.camera() # Define a Gaussian kernel sigma = 2 size = int(sigma*6) + 1 kernel = ndimage.gaussian_filter(np.eye(size), sigma) # Apply sparse filtering to image filtered_image = sparse_filter(image, kernel) # Display the original and filtered images side by side fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) ax1.imshow(image, cmap='gray') ax1.set_title('Original Image') ax2.imshow(filtered_image, cmap='gray') ax2.set_title('Filtered Image') plt.show() ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值