├── .gitattributes ├── .gitignore ├── Demo_compression.asv ├── Demo_compression.m ├── Lena.png ├── model ├── ComCNN_QF=50.mat └── RecCNN_QF=50.mat └── utilities ├── Cal_PSNRSSIM.m ├── Merge_Bnorm_Demo.m ├── data_augmentation.m ├── modcrop.m └── shave.m /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Demo_compression.asv: -------------------------------------------------------------------------------- 1 | 2 | %%% This is the testing demo for gray image (Gaussian) denoising. 3 | %%% Training data: 400 images of size 180X180 4 | 5 | run('D:\Davey\matconvnet-1.0-beta23\matlab\vl_setupnn.m') ; 6 | % clear; clc; 7 | addpath('utilities'); 8 | %folderTest = 'Train400'; 9 | 10 | 11 | showResult = 1; 12 | useGPU = 0; 13 | pauseTime = 1; 14 | 15 | JPEG_Quality = 30; 16 | net1.layers = {}; 17 | net2.layers = {}; 18 | %load ComCNN 19 | load(fullfile('model','ComCNN_QF=30.mat')); 20 | net1.layers = net.layers(1:end-1); 21 | %load RecCNN 22 | load(fullfile('model','RecCNN_QF=30.mat')); 23 | net2.layers = net.layers(1:end-1); 24 | 25 | %%% move to gpu 26 | if useGPU 27 | net1 = vl_simplenn_move(net1, 'gpu') ; 28 | net2 = vl_simplenn_move(net2, 'gpu') ; 29 | end 30 | 31 | %%% PSNR and SSIM 32 | PSNRs = zeros(1,length(filePaths)); 33 | SSIMs = zeros(1,length(filePaths)); 34 | %read image 35 | label = imread('butterfly.bmp'); 36 | if size(label,3)>1 37 | label = rgb2gray(label); 38 | end 39 | 40 | label = im2single(label); 41 | 42 | [hei,wid] = size(label); 43 | if useGPU 44 | label = gpuArray(label); 45 | end 46 | 47 | % tic 48 | res = vl_simplenn(net1,label,[],[],'conserveMemory',true,'mode','test'); 49 | Low_Resolution = res(end).x; 50 | % toc 51 | 52 | if useGPU 53 | Low_Resolution = gather(Low_Resolution); 54 | end 55 | 56 | imwrite(im2uint8(Low_Resolution),sprintf('%3d.jpg',i),'jpg','Quality',JPEG_Quality);%Compression 57 | 58 | 59 | im_input = im2single(imread(sprintf('%3d.jpg',i))); 60 | input = imresize(im_input,[hei,wid],'bicubic'); 61 | 62 | 63 | %%% convert to GPU 64 | if useGPU 65 | input = gpuArray(input); 66 | end 67 | tic 68 | res = vl_simplenn(net2,input,[],[],'conserveMemory',true,'mode','test'); 69 | output = input - res(end).x; 70 | toc 71 | [PSNRWithNet, SSIMWithNet] = Cal_PSNRSSIM(im2uint8(label),im2uint8(output),0,0); 72 | 73 | if useGPU 74 | output = gather(output); 75 | input = gather(input); 76 | end 77 | 78 | figure, imshow(label); title(sprintf('Raw-Input')); 79 | figure, imshow(input); title(sprintf('Before CNN Network, PSNR: %.3f dB,SSIM: %.4f', PSNRWithoutNet, SSIMWithoutNet)); 80 | figure, imshow(output); title(sprintf('After CNN Network, PSNR: %.3f dB,SSIM: %.4f', PSNRWithNet, SSIMWithNet)); 81 | 82 | if useGPU 83 | label = gather(label); 84 | end 85 | JPEG_Quality1= 10; 86 | imwrite(label,'JPEG-Directly.jpg','jpg','Quality',JPEG_Quality1);% 87 | im_direct = im2single(imread('JPEG-Directly.jpg')); 88 | [PSNR_direct, SSIM_direct] = Cal_PSNRSSIM(im2uint8(label),im2uint8(im_direct),0,0); 89 | figure, imshow(im_direct); title(sprintf('JPEG-Directly, PSNR: %.3f dB,SSIM: %.4f', PSNR_direct, SSIM_direct)); 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /Demo_compression.m: -------------------------------------------------------------------------------- 1 | 2 | %%% This is the testing demo for gray image (Gaussian) denoising. 3 | %%% Training data: 400 images of size 180X180 4 | 5 | run('D:\Davey\matconvnet-1.0-beta23\matlab\vl_setupnn.m') ; 6 | % clear; clc; 7 | addpath('utilities'); 8 | %folderTest = 'Train400'; 9 | 10 | 11 | showResult = 1; 12 | useGPU = 1; 13 | pauseTime = 1; 14 | 15 | JPEG_Quality = 50; 16 | net1.layers = {}; 17 | net2.layers = {}; 18 | %load ComCNN 19 | load(fullfile('model','ComCNN_QF=50.mat')); 20 | net1.layers = net.layers(1:end-1); 21 | %load RecCNN 22 | load(fullfile('model','RecCNN_QF=50.mat')); 23 | net2.layers = net.layers(1:end-1); 24 | 25 | %%% move to gpu 26 | if useGPU 27 | net1 = vl_simplenn_move(net1, 'gpu') ; 28 | net2 = vl_simplenn_move(net2, 'gpu') ; 29 | end 30 | 31 | %read image 32 | % label = imread('butterfly.bmp'); 33 | label = imread('Lena.png'); 34 | 35 | if size(label,3)>1 36 | label = rgb2gray(label); 37 | end 38 | 39 | label = im2single(label); 40 | 41 | [hei,wid] = size(label); 42 | if useGPU 43 | label = gpuArray(label); 44 | end 45 | 46 | % tic 47 | res = vl_simplenn(net1,label,[],[],'conserveMemory',true,'mode','test'); 48 | Low_Resolution = res(end).x; 49 | % toc 50 | 51 | if useGPU 52 | Low_Resolution = gather(Low_Resolution); 53 | end 54 | 55 | imwrite(im2uint8(Low_Resolution),'compressed_image.jpg','jpg','Quality',JPEG_Quality);%Compression 56 | 57 | im_input = im2single(imread('compressed_image.jpg')); 58 | input = imresize(im_input,[hei,wid],'bicubic'); 59 | 60 | %%% convert to GPU 61 | if useGPU 62 | input = gpuArray(input); 63 | end 64 | % tic 65 | res = vl_simplenn(net2,input,[],[],'conserveMemory',true,'mode','test'); 66 | output = input - res(end).x; 67 | % toc 68 | [PSNRWithNet, SSIMWithNet] = Cal_PSNRSSIM(im2uint8(label),im2uint8(output),0,0); 69 | 70 | if useGPU 71 | output = gather(output); 72 | input = gather(input); 73 | end 74 | imwrite(im2uint8(Low_Resolution),'output.jpg','jpg') 75 | figure, imshow(label); title(sprintf('Raw-Input')); 76 | 77 | figure, imshow(output); title(sprintf('After CNN Network, PSNR: %.2f dB,SSIM: %.4f', PSNRWithNet, SSIMWithNet)); 78 | 79 | if useGPU 80 | label = gather(label); 81 | end 82 | JPEG_Quality1= 10; 83 | imwrite(label,'JPEG-Directly.jpg','jpg','Quality',JPEG_Quality1);% 84 | im_direct = im2single(imread('JPEG-Directly.jpg')); 85 | [PSNR_direct, SSIM_direct] = Cal_PSNRSSIM(im2uint8(label),im2uint8(im_direct),0,0); 86 | figure, imshow(im_direct); title(sprintf('JPEG-Directly, PSNR: %.2f dB,SSIM: %.4f', PSNR_direct, SSIM_direct)); 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /Lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compression-framework/compression_framwork_for_tesing/1a493f04ed8f29eee7bfb7d36ec1f5fc9a2b11eb/Lena.png -------------------------------------------------------------------------------- /model/ComCNN_QF=50.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compression-framework/compression_framwork_for_tesing/1a493f04ed8f29eee7bfb7d36ec1f5fc9a2b11eb/model/ComCNN_QF=50.mat -------------------------------------------------------------------------------- /model/RecCNN_QF=50.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/compression-framework/compression_framwork_for_tesing/1a493f04ed8f29eee7bfb7d36ec1f5fc9a2b11eb/model/RecCNN_QF=50.mat -------------------------------------------------------------------------------- /utilities/Cal_PSNRSSIM.m: -------------------------------------------------------------------------------- 1 | function [psnr_cur, ssim_cur] = Cal_PSNRSSIM(A,B,row,col) 2 | 3 | 4 | [n,m,ch]=size(B); 5 | A = A(row+1:n-row,col+1:m-col,:); 6 | B = B(row+1:n-row,col+1:m-col,:); 7 | A=double(A); % Ground-truth 8 | B=double(B); % 9 | 10 | e=A(:)-B(:); 11 | mse=mean(e.^2); 12 | psnr_cur=10*log10(255^2/mse); 13 | 14 | if ch==1 15 | [ssim_cur, ~] = ssim_index(A, B); 16 | else 17 | ssim_cur = -1; 18 | end 19 | 20 | 21 | function [mssim, ssim_map] = ssim_index(img1, img2, K, window, L) 22 | 23 | %======================================================================== 24 | %SSIM Index, Version 1.0 25 | %Copyright(c) 2003 Zhou Wang 26 | %All Rights Reserved. 27 | % 28 | %The author is with Howard Hughes Medical Institute, and Laboratory 29 | %for Computational Vision at Center for Neural Science and Courant 30 | %Institute of Mathematical Sciences, New York University. 31 | % 32 | %---------------------------------------------------------------------- 33 | %Permission to use, copy, or modify this software and its documentation 34 | %for educational and research purposes only and without fee is hereby 35 | %granted, provided that this copyright notice and the original authors' 36 | %names appear on all copies and supporting documentation. This program 37 | %shall not be used, rewritten, or adapted as the basis of a commercial 38 | %software or hardware product without first obtaining permission of the 39 | %authors. The authors make no representations about the suitability of 40 | %this software for any purpose. It is provided "as is" without express 41 | %or implied warranty. 42 | %---------------------------------------------------------------------- 43 | % 44 | %This is an implementation of the algorithm for calculating the 45 | %Structural SIMilarity (SSIM) index between two images. Please refer 46 | %to the following paper: 47 | % 48 | %Z. Wang, A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli, "Image 49 | %quality assessment: From error measurement to structural similarity" 50 | %IEEE Transactios on Image Processing, vol. 13, no. 1, Jan. 2004. 51 | % 52 | %Kindly report any suggestions or corrections to zhouwang@ieee.org 53 | % 54 | %---------------------------------------------------------------------- 55 | % 56 | %Input : (1) img1: the first image being compared 57 | % (2) img2: the second image being compared 58 | % (3) K: constants in the SSIM index formula (see the above 59 | % reference). defualt value: K = [0.01 0.03] 60 | % (4) window: local window for statistics (see the above 61 | % reference). default widnow is Gaussian given by 62 | % window = fspecial('gaussian', 11, 1.5); 63 | % (5) L: dynamic range of the images. default: L = 255 64 | % 65 | %Output: (1) mssim: the mean SSIM index value between 2 images. 66 | % If one of the images being compared is regarded as 67 | % perfect quality, then mssim can be considered as the 68 | % quality measure of the other image. 69 | % If img1 = img2, then mssim = 1. 70 | % (2) ssim_map: the SSIM index map of the test image. The map 71 | % has a smaller size than the input images. The actual size: 72 | % size(img1) - size(window) + 1. 73 | % 74 | %Default Usage: 75 | % Given 2 test images img1 and img2, whose dynamic range is 0-255 76 | % 77 | % [mssim ssim_map] = ssim_index(img1, img2); 78 | % 79 | %Advanced Usage: 80 | % User defined parameters. For example 81 | % 82 | % K = [0.05 0.05]; 83 | % window = ones(8); 84 | % L = 100; 85 | % [mssim ssim_map] = ssim_index(img1, img2, K, window, L); 86 | % 87 | %See the results: 88 | % 89 | % mssim %Gives the mssim value 90 | % imshow(max(0, ssim_map).^4) %Shows the SSIM index map 91 | % 92 | %======================================================================== 93 | 94 | 95 | if (nargin < 2 || nargin > 5) 96 | ssim_index = -Inf; 97 | ssim_map = -Inf; 98 | return; 99 | end 100 | 101 | if (size(img1) ~= size(img2)) 102 | ssim_index = -Inf; 103 | ssim_map = -Inf; 104 | return; 105 | end 106 | 107 | [M N] = size(img1); 108 | 109 | if (nargin == 2) 110 | if ((M < 11) || (N < 11)) 111 | ssim_index = -Inf; 112 | ssim_map = -Inf; 113 | return 114 | end 115 | window = fspecial('gaussian', 11, 1.5); % 116 | K(1) = 0.01; % default settings 117 | K(2) = 0.03; % 118 | L = 255; % 119 | end 120 | 121 | if (nargin == 3) 122 | if ((M < 11) || (N < 11)) 123 | ssim_index = -Inf; 124 | ssim_map = -Inf; 125 | return 126 | end 127 | window = fspecial('gaussian', 11, 1.5); 128 | L = 255; 129 | if (length(K) == 2) 130 | if (K(1) < 0 || K(2) < 0) 131 | ssim_index = -Inf; 132 | ssim_map = -Inf; 133 | return; 134 | end 135 | else 136 | ssim_index = -Inf; 137 | ssim_map = -Inf; 138 | return; 139 | end 140 | end 141 | 142 | if (nargin == 4) 143 | [H W] = size(window); 144 | if ((H*W) < 4 || (H > M) || (W > N)) 145 | ssim_index = -Inf; 146 | ssim_map = -Inf; 147 | return 148 | end 149 | L = 255; 150 | if (length(K) == 2) 151 | if (K(1) < 0 || K(2) < 0) 152 | ssim_index = -Inf; 153 | ssim_map = -Inf; 154 | return; 155 | end 156 | else 157 | ssim_index = -Inf; 158 | ssim_map = -Inf; 159 | return; 160 | end 161 | end 162 | 163 | if (nargin == 5) 164 | [H W] = size(window); 165 | if ((H*W) < 4 || (H > M) || (W > N)) 166 | ssim_index = -Inf; 167 | ssim_map = -Inf; 168 | return 169 | end 170 | if (length(K) == 2) 171 | if (K(1) < 0 || K(2) < 0) 172 | ssim_index = -Inf; 173 | ssim_map = -Inf; 174 | return; 175 | end 176 | else 177 | ssim_index = -Inf; 178 | ssim_map = -Inf; 179 | return; 180 | end 181 | end 182 | 183 | C1 = (K(1)*L)^2; 184 | C2 = (K(2)*L)^2; 185 | window = window/sum(sum(window)); 186 | img1 = double(img1); 187 | img2 = double(img2); 188 | 189 | mu1 = filter2(window, img1, 'valid'); 190 | mu2 = filter2(window, img2, 'valid'); 191 | mu1_sq = mu1.*mu1; 192 | mu2_sq = mu2.*mu2; 193 | mu1_mu2 = mu1.*mu2; 194 | sigma1_sq = filter2(window, img1.*img1, 'valid') - mu1_sq; 195 | sigma2_sq = filter2(window, img2.*img2, 'valid') - mu2_sq; 196 | sigma12 = filter2(window, img1.*img2, 'valid') - mu1_mu2; 197 | 198 | if (C1 > 0 & C2 > 0) 199 | ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)); 200 | else 201 | numerator1 = 2*mu1_mu2 + C1; 202 | numerator2 = 2*sigma12 + C2; 203 | denominator1 = mu1_sq + mu2_sq + C1; 204 | denominator2 = sigma1_sq + sigma2_sq + C2; 205 | ssim_map = ones(size(mu1)); 206 | index = (denominator1.*denominator2 > 0); 207 | ssim_map(index) = (numerator1(index).*numerator2(index))./(denominator1(index).*denominator2(index)); 208 | index = (denominator1 ~= 0) & (denominator2 == 0); 209 | ssim_map(index) = numerator1(index)./denominator1(index); 210 | end 211 | 212 | mssim = mean2(ssim_map); 213 | 214 | return 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | -------------------------------------------------------------------------------- /utilities/Merge_Bnorm_Demo.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | load('sigma=25_Bnorm.mat'); 7 | 8 | [net] = vl_simplenn_mergebnorm(net); 9 | 10 | save sigma=25 net; 11 | 12 | 13 | -------------------------------------------------------------------------------- /utilities/data_augmentation.m: -------------------------------------------------------------------------------- 1 | function image = data_augmentation(image, mode) 2 | 3 | if mode == 1 4 | return; 5 | end 6 | 7 | if mode == 2 % flipped 8 | image = flipud(image); 9 | return; 10 | end 11 | 12 | if mode == 3 % rotation 90 13 | image = rot90(image,1); 14 | return; 15 | end 16 | 17 | if mode == 4 % rotation 90 & flipped 18 | image = rot90(image,1); 19 | image = flipud(image); 20 | return; 21 | end 22 | 23 | if mode == 5 % rotation 180 24 | image = rot90(image,2); 25 | return; 26 | end 27 | 28 | if mode == 6 % rotation 180 & flipped 29 | image = rot90(image,2); 30 | image = flipud(image); 31 | return; 32 | end 33 | 34 | if mode == 7 % rotation 270 35 | image = rot90(image,3); 36 | return; 37 | end 38 | 39 | if mode == 8 % rotation 270 & flipped 40 | image = rot90(image,3); 41 | image = flipud(image); 42 | return; 43 | end 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /utilities/modcrop.m: -------------------------------------------------------------------------------- 1 | function imgs = modcrop(imgs, modulo) 2 | if size(imgs,3)==1 3 | sz = size(imgs); 4 | sz = sz - mod(sz, modulo); 5 | imgs = imgs(1:sz(1), 1:sz(2)); 6 | else 7 | tmpsz = size(imgs); 8 | sz = tmpsz(1:2); 9 | sz = sz - mod(sz, modulo); 10 | imgs = imgs(1:sz(1), 1:sz(2),:); 11 | end 12 | 13 | -------------------------------------------------------------------------------- /utilities/shave.m: -------------------------------------------------------------------------------- 1 | function I = shave(I, border) 2 | I = I(1+border(1):end-border(1), ... 3 | 1+border(2):end-border(2), :, :); 4 | --------------------------------------------------------------------------------