Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. clear;
  2.  
  3. %% File Paths
  4. fname = 'AllCIFAR.h5';
  5. addpath(genpath('/Google Drive/Research/code/matlab/toolboxes/WaveletSoftware'));
  6.  
  7. %% Read in the HDF5 information
  8. hi = hdf5info(fname);
  9. X = hdf5read(hi.GroupHierarchy(1).Datasets(1));
  10.  
  11. %% Initializations
  12. N = 50000; % Number of images to transform
  13. imsize = [32 32]; % Image dimensions
  14. mean_removal = true; % Remove mean of individual images?
  15. L = log2(32); % Maximum number of decompositions
  16. sq = @(x_) reshape(x_,imsize); % Helper function for display
  17. [af,sf] = farras; % Load in wavelet
  18. PctRetain = 0.05; % Percent of w.coeffs. to retain
  19. K = floor(PctRetain*prod(imsize)); % # of w.coeffs. to retain
  20.  
  21. W = zeros(size(X,1),N); % Wavelet-ized dataset
  22. WT = W; % Trunc. Wavelet-ized dataset
  23.  
  24. %% Loop over Dataset
  25. for i=1:N
  26. im = sq(X(:,i))'; % convert to 2d
  27. if mean_removal
  28. im = im - mean(im(:)); % subract mean
  29. end
  30.  
  31. % Wavelet transform
  32. w = dwt2D(im,5,af);
  33. w = wcell2mat(w);
  34. w = w(:);
  35.  
  36. % Truncation
  37. wt = zeros(size(w));
  38. [~,wo] = sort(abs(w(:)),'descend');
  39. wt(wo(1:K)) = w(wo(1:K));
  40.  
  41. % Assign back to dataset
  42. W(:,i) = w(:);
  43. WT(:,i) = wt(:);
  44. if mod(i,500)==0
  45. fprintf('%0.2f%% complete.\n',i/N*100);
  46. end
  47. end
  48.  
  49. % Write out on the wavelet features
  50. hdf5write('WaveletCIFAR.h5','X',W);
  51.  
  52. %% Lets compare the wavelet datasets
  53. figure(1);
  54. colormap(parula);
  55. subplot(2,3,1);
  56. imagesc(abs(W));
  57. title('Wavelet Coefficients');
  58. colorbar();
  59. subplot(2,3,2);
  60. [x,a] = hist(abs(W(:)),100);
  61. stem(a,x,'-','Marker','.');
  62. title('Wavelet Magnitude Distribution');
  63. set(gca,'YScale','log');
  64. subplot(2,3,3);
  65. imagesc(reshape(mean(abs(W),2),imsize));
  66. axis image;
  67. title('Avg. Wavelet Magnitude');
  68. colorbar();
  69. subplot(2,3,4);
  70. imagesc(abs(WT));
  71. title('Truncated Wavelet Coefficients');
  72. colorbar();
  73. subplot(2,3,5);
  74. [x,a] = hist(abs(WT(:)),100);
  75. stem(a,x,'-','Marker','.');
  76. title('Trunc. Wavelet Magnitude Distribution');
  77. set(gca,'YScale','log');
  78. subplot(2,3,6);
  79. imagesc(reshape(mean(abs(WT),2),imsize));
  80. axis image;
  81. title('Avg. Trunc. Wavelet Magnitude');
  82. colorbar();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement