speech_dataset.lua 6.85 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

local SpeechDataset = torch.class('SpeechDataset')


function SpeechDataset:__init(config)
    print("loading " .. config.datafile)
    collectgarbage()
    self.data=torch.load(config.datafile);
	print(type(self.data));

    self.countGarbageCollector=0;
	
    self.label=torch.load(config.labelfile);
    print("data loaded")
    self.path = nil
Hannah MUCKENHIRN committed
35
    if config.pathfile ~= "" then
Hannah MUCKENHIRN committed
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        self.path = torch.load(config.pathfile)
    end

    self.vad = nil
    if config.vadfile ~= "" then
        self.vad = torch.load(config.vadfile)
    end

    self.nData = #self.data
    
    self.samp = config.nSamplePerFrame -- number of raw samples per considered labeled frame (e.g. 10ms -> 160 samples @ 16kHz)
		
    self.feat=config.feat;
	if self.feat:match("MFCC") then
		self.isMFCC=true
    else
		self.isMFCC=false;
    end

    
    self.norm=config.norm
    
    self.cont=config.contextframe

    self.nInput=self.samp*(2*self.cont+1)

    self.countTooShort=0
    self.isTooShort = torch.Tensor(self.nData):zero()
    for i=1,self.nData do
    	if (self.data[i]:size(1) < self.nInput) then
    		self.isTooShort[i]=1
			if (self.data[i]:size(1) > self.nInput/2+1) then
				local origSize=self.data[i]:size(1)
			 	self.data[i]:resize(self.nInput)
				
			 	for j=1,self.nInput-origSize do
			 		self.data[i][j+origSize]=self.data[i][origSize-j]
			 	end
			else
				self.data[i]:resize(self.nInput)
				self.countTooShort=self.countTooShort+1
			end
		end
    end


    self.nFrame=torch.Tensor(self.nData);
    for i=1,self.nData do
		self.nFrame[i]=math.floor(self.data[i]:size(1)/self.samp);
	end

	self.nExample=self.nFrame:sum()
    

    print("There are "..self.countTooShort.." utterances too short over "..self.nData)

	self.map=torch.Tensor(self.nExample,2);
	local k=1;
	for i=1,self.nData do
		for j=1,self.nFrame[i] do
			self.map[k][1]=i;
			self.map[k][2]=j;
			k=k+1;
		end
	end
	if config.normMean~=nil then
		self.normMean=config.normMean
		self.normStd=config.normStd
	end
end


function SpeechDataset:normalize()
	if self.isMFCC then

		if self.norm=="seq" then
	
			for i=1,self.nData do
				local nframe=self.data[i]:size(1)/self.samp
				self.data[i]:resize(nframe,self.samp)	
				for j=1,self.samp do
					self.data[i]:select(2,j):add(-self.data[i]:select(2,j):mean())
					self.data[i]:select(2,j):div(self.data[i]:select(2,j):std())
				end
				self.data[i]:resize(nframe*self.samp)	
			end
	
		elseif self.norm=="dset" then
			
			for i=1,self.nData do
				local nframe=self.data[i]:size(1)/self.samp
				self.data[i]:resize(nframe,self.samp)	
			end
			if self.normMean==nil then 
				self.normMean=torch.Tensor(self.samp)
				self.normStd=torch.Tensor(self.samp)
				for j=1,self.samp do
					local mean=0
					local norm=0
					local var=0
					
					for i=1,#self.data do
						local nframe=self.data[i]:size(1)
						local s=self.data[i]:select(2,j):sum()
						local s2=torch.pow(self.data[i]:select(2,j),2):sum()
						mean=mean+s
						var=var+s2
						norm=norm+nframe
					end
					self.normStd[j]=math.sqrt((var-(mean*mean)/norm)/(norm-1))
					self.normMean[j]=mean/norm
				end
			end
				
			for i=1,#self.data do
				local nframe=self.data[i]:size(1)
				for j=1,self.samp do
					self.data[i]:select(2,j):add(-self.normMean[j])
					self.data[i]:select(2,j):div(self.normStd[j])
				end
				self.data[i]:resize(nframe*self.samp)	
			end	
	
		end		
	else
		if self.norm=="dset" then
			if self.normMean==nil then 
				local mean=0
				local norm=0
				local var=0
				for i=1,#self.data do
					if i % 2000 == 0 then
                        			print (i .. " 1st loop, memory: " .. collectgarbage("count"))
                        			collectgarbage()
                    			end
					local normMean=self.data[i]:float():mean()
					local seq=self.data[i]:float():add(-normMean)
					local s=seq:sum()
					local s2=torch.pow(seq,2):sum()
					mean=mean+s
					var=var+s2
					norm=norm+seq:size(1)
				end
				self.normStd=math.sqrt((var-(mean*mean)/norm)/(norm-1))
				self.normMean=mean/norm
			end

		end
	end	

end


function SpeechDataset:get_data(k,temp)

	local i=self.map[k][1]
	local j=self.map[k][2]
	local nsam_adj=self.cont*self.samp

	if j-1-self.cont<=0 then
		local supfr=self.cont-j+1
		temp:narrow(1,supfr*self.samp+1,self.nInput-supfr*self.samp):copy(self.data[i]:narrow(1,1,self.nInput-supfr*self.samp));

		for k=1,supfr do
			temp:narrow(1,(k-1)*self.samp+1,self.samp):copy(self.data[i]:narrow(1,1,self.samp));
		end

	elseif (j+self.cont)> self.nFrame[i] then
		
		local supsam= j+self.cont - self.nFrame[i]
		local Lunp=(2*self.cont+1-supsam)*self.samp --number of sample no out of bound
		temp:narrow(1,1,Lunp):copy(self.data[i]:narrow(1,self.data[i]:size(1)-Lunp,Lunp));

		for k=1,supsam do
			temp:narrow(1,(k-1)*self.samp+1+Lunp,self.samp):copy(self.data[i]:narrow(1,self.data[i]:size(1)-self.samp+1,self.samp));
		end

	else
		temp:copy(self.data[i]:narrow(1,(j-1-self.cont)*self.samp+1,self.nInput))

	end

	if self.countGarbageCollector%1000==0 then
			collectgarbage()
	end
	self.countGarbageCollector=self.countGarbageCollector+1
	local normMean=self.data[i]:float():mean()
	temp=temp:float():add(-normMean);

	if self.norm=="win" then
		temp=temp:float():add(-temp:float():mean());
		temp=temp:float():div(temp:float():std());
	elseif self.isMFCC==false and self.norm=="seq" then
		normStd=self.data[i]:float():std()
		temp=temp:float():div(normStd);
	elseif self.isMFCC==false and self.norm=="dset" then	
		temp=temp:float():add(-self.normMean);
		temp=temp:float():div(self.normStd);
	end
		
end

function SpeechDataset:get_label(k)

	return self.label[self.map[k][1]]

end

function SpeechDataset:get_path(k)
    if self.path == nil then
Hannah MUCKENHIRN committed
246
        return ""
Hannah MUCKENHIRN committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    end
	return self.path[self.map[k][1]]
end

function SpeechDataset:get_vad(k)
    if self.vad == nil then
        return 1
    end
    local vad=0
	i=self.map[k][1]
	j=self.map[k][2]
	if((self.vad[i]:size(1))+1==j) then
		vad=self.vad[i][j-1]
	elseif((self.vad[i]:size(1))+2==j) then
		vad=self.vad[i][j-2]
	else
		vad=self.vad[i][j]
	end
		
	return vad
end

function SpeechDataset:is_too_short(k)
	return self.isTooShort[self.map[k][1]]
end