ZCA白化
来源:互联网 发布:排卵期计算软件下载 编辑:程序博客网 时间:2024/04/30 04:22
- patches = [];
- tx = imread('test.jpg');%load('pcaData.txt','-ascii');
- tx = double(tx);
- x = zeros(size(tx, 1) * size(tx, 2), size(tx, 3));
- tx = tx(:);
- for i = 1 : size(x,2)
- x(:, i) = tx(1 + size(x, 1) * (i - 1) : size(x,1) * i);
- end
- patches = x';
- %for i = 1 : size(x,2);
- % im = imread(strcat('train\', num2str(i), '.png'));
- % im = reshape(im, [1, 32*32*3]);
- % im = double(im);
- % % centralize
- % im(1:32*32) = im(1:32*32) - mean(im(1:32*32));
- % im(32*32+1:2*32*32) = im(32*32+1:2*32*32) - mean(im(32*32+1:2*32*32));
- % im(2*32*32+1:3*32*32) = im(2*32*32+1:3*32*32) - mean(im(2*32*32+1:3*32*32));
- %
- % patches = [patches, im'];
- %end
- y = ZCA_whitening(patches);
- y = y * y';
- y = ZCA_normalize(y);
- covx = 1/1000 * patches * patches';
- covx = ZCA_normalize(covx);
- hold on
- subplot(1,2,1)
- imshow(uint8(covx))
- subplot(1,2,2)
- imshow(uint8(y));
- function y = ZCA_normalize(x)
- [row, col] = size(x);
- tx = [];
- for i = 1 : row
- tx = [tx, x(i,:)];
- end
- tx = tx - min(tx);
- tx = tx / max(tx) * 255;
- y = zeros(row, col);
- for i = 1 : row
- y(i, :) = tx((i - 1) * col + 1 : i * col);
- end
- end
- %% 假定每一个d维数据是0均值的,那么这n个d维数据的矩阵X(d*n)的协方差矩阵为
- %% covX = 1/(n-1)*X*X'
- %% 为了消除维数之间的相关性,做变换W,得到Y,即Y = W*X
- %% 下面求‘去相关矩阵W'.由于W消除了相关性,所以Y*Y'是对角阵,故令W满足
- %% Y*Y' = (n-1)*I
- %% 由于满足条件的W很多,那么不妨设W=W'
- %% 之后的推导比较自然,比较难想到的就是将X*X'做正交分解为P*D*P'
-
- function y = ZCAwhitening(x)
- [dim, n] = size(x);
- [P, D] = schur(x * x');
- w = sqrt(n-1) * P * D^(-1/2) * P';
- y = w * x;
- end
效果图——数据集是取自Kaggle上的Tiny Image Classification的比赛 0 0