filterVisual.lua 3.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
--[[This software purpose is to train convolutional neural networks for voice presentation attack detection.

Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/
Written by Hannah Muckenhirn <hannah.muckenhirn@idiap.ch>,

This file is part of CNN-voice-PAD.

CNN-voice-PAD is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as
published by the Free Software Foundation.

CNN-voice-PAD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CNN-voice-PAD. If not, see <http://www.gnu.org/licenses/>.--]]

Hannah MUCKENHIRN committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
cmd=torch.CmdLine();
cmd:option('-model','','model to analyse');									
cmd:option('-comp',"",'2nd model to compare');
cmd:option('-l',0,'Layer to use');
cmd:option('-nfft',512,'FFT points');
cmd:option('-cum',"true",'plot cumulative response, true by default');
cmd:option('-save',"",'save figure to this filename');
par=cmd:parse(arg);

require "nn"
signal = require "signal"
gnuplot = require "gnuplot"

-- load model
local modelfile=torch.load(par.model);

net=modelfile[1]
params=modelfile[2]

nlay=par.l
nfft=par.nfft 
save=par.save

nfilt=net:get(nlay).weight:size(1);
kw=net:get(nlay).kW;
dim=net:get(nlay).weight:size(2)/kw;


local freq=torch.range(1,nfft/2+1)
freq:mul(8000)
freq:div(nfft/2+1);

function filterFFT(net,nlay,nfft)
	
	local fftlen=nfft/2+1
	
	if nlay==1 then
	
		Fcomp=torch.Tensor(nfilt,nfft,2)
		F=torch.Tensor(nfilt,fftlen)
		
		for i=1,nfilt do
			-- complex Fourier trans
			wts= net:get(nlay).weight[i]
			padding = torch.FloatTensor(nfft-wts:size(1)):fill(0)
			sig = torch.cat(wts, padding, 1)
			Fcomp[i]=signal.fft(sig);
			local Ftemp=Fcomp[i]
			Fabs = torch.cmul(Ftemp,Ftemp)
			Fabs = torch.sum( Fabs,2)
			Fabs = torch.squeeze( Fabs:sqrt() )
			-- normalized magnitude spectrum 
			F[i]=torch.div(Fabs:narrow(1,1,fftlen),Fabs:narrow(1,1,fftlen):sum());

		end
		
	else
	
		Fcomp=torch.Tensor(nfilt,dim,nfft,2);
		F=torch.Tensor(nfilt,dim,fftlen);
		for i=1,nfilt do
			
			for j=1,dim do
		
				local ff=torch.reshape(net:get(nlay).weight[i],dim,kw)[j]

				padding = torch.FloatTensor(nfft-ff:size(1)):fill(0)
				sig = torch.cat(ff, padding, 1)

				Fcomp[i][j]=signal.fft(sig);
				local Ftemp=Fcomp[i][j]
				local Fabs = torch.cmul(Ftemp,Ftemp)
				Fabs = torch.sum( Fabs,2)
				Fabs = torch.squeeze( Fabs:sqrt() )
				F[i][j]=torch.div(Fabs:narrow(1,1,fftlen),Fabs:narrow(1,1,fftlen):sum());
		
			end		
		end
	
	end
	
	return F,Fcomp

end
	
F=filterFFT(net,nlay,nfft)
if par.cum=="true" then
	Fcum=torch.Tensor(F[1]:size()):fill(0)
	for i=1,nfilt do
		Fcum=Fcum+F[i]
	end
	if save~="" then
        gnuplot.pngfigure(save)
		gnuplot.plot({freq,Fcum})
		gnuplot.title("Cumulative response")
        gnuplot.xlabel("Hz")
        gnuplot.plotflush()
	else
		gnuplot.figure(1)
		gnuplot.plot({freq,Fcum})
		gnuplot.title("Cumulative response")
        gnuplot.xlabel("Hz")
	end
		
else		
	-- plotting a few random filters
	local p=torch.randperm(nfilt)
	if nlay==1 then
		for i=1,20 do
			gnuplot.figure(i)
			gnuplot.plot({freq,F[p[i]]})
		end
	else
		local p2=torch.randperm(dim)
		for i=1,4 do
			gnuplot.figure(i)
			gnuplot.plot(F[p[i]][p2[i]])
		end
	end	
end