forward.lua 3.7 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
require "paths"
require "nn"
require "math"
require "gnuplot"

torch.setdefaulttensortype('torch.FloatTensor');

-- functions

paths.dofile("speech_dataset.lua")

cmd=torch.CmdLine();
cmd:option('-modelID',"",'name of model to use');
cmd:option('-model',"",'model to use');
cmd:option('-type',"eval",'[dev,eval]');
35 36 37 38
cmd:option('-data',"",'File containing data');
cmd:option('-label',"",'File containing label');
cmd:option('-path',"",'File containing path');
cmd:option('-VAD',"",'File containing path');
Hannah MUCKENHIRN committed
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
params2=cmd:parse(arg);

model=torch.load(params2.model);
modelID=params2.modelID;

net=model[1]
params=model[2]

dirname="forward_" .. params.arch .. "_"
	
for k,v in pairs(params) do
	if v~=0 and v~="" and k~="save" and k~="trainData" and k~="trainLabel" and k~="devData" and k~="devLabel" and k~="trainVAD" and k~="devVAD" then
		dirname=dirname .. k .. "=" .. v .. "_" 
	end	
end
dirname=dirname .. modelID .. "_"

os.execute("mkdir -v " .. params.save .. "/" .. dirname)
	

-- DATASET

local configValid={}

configValid.datafile=params2.data
configValid.labelfile=params2.label
configValid.pathfile=params2.path
configValid.vadfile= params2.VAD
configValid.feat="wav"
configValid.nSamplePerFrame=160 -- 10ms @ 16kHz
configValid.norm=params.norm	
configValid.contextframe=params.context

	
validSet=SpeechDataset(configValid)
if params.norm=="dset" then
	validSet.normMean=params.normMean
	validSet.normStd=params.normStd
end
validSet:normalize()


print( validSet.nData .. " validation sequences")


print("Creating network ...");


nInput=validSet.nInput
nOutput=2;

print(nInput .. " samples for each example");

seq=torch.Tensor(nInput,1):fill(0);


local savepath=params.save .. "/" .. dirname 
file_real=io.open(savepath  .. "/scores-"..params2.type.."-real","w")
file_attack=io.open(savepath  .. "/scores-"..params2.type.."-attack","w")
file_scores=io.open(savepath  .. "/scores-"..params2.type,"w")
local errNumV=0
c=1	
print("number of frames: " .. validSet.nExample)
print("number of utterances: " .. validSet.nData)


for i = 1,validSet.nData do
	local nbVoicedFrames=0
	local score=0
	target=validSet:get_label(c)
	path = validSet:get_path(c)
	if path == nil then
		path = 'label'
	end
	for j=1,validSet.nFrame[i] do
		vad = validSet:get_vad(c)
		if (vad == 1) then
			validSet:get_data(c,seq)
			net:forward(seq)
			score = score + math.exp(net.output[1])
			nbVoicedFrames = nbVoicedFrames + 1
		end
		c=c+1
	end
	if (nbVoicedFrames~=0) then
		score = score/nbVoicedFrames
	else
		print("The utterance " .. i .. " does not contain speech")
	end
	if target==1 then
		file_real:write(target .. ' ' .. target .. ' ' .. path .. ' ' .. score .. '\n')
		file_scores:write(target .. ' ' .. target .. ' ' .. path .. ' ' .. score .. '\n')
	else
		file_attack:write(target .. " attack " .. path .. ' ' .. score .. '\n')
		file_scores:write(target .. " attack " .. path .. ' ' .. score .. '\n')
	end
end
file_real:close()
file_attack:close()