-
Notifications
You must be signed in to change notification settings - Fork 93
/
eval_dcase.lua
152 lines (122 loc) · 3.82 KB
/
eval_dcase.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
140
141
142
143
144
145
146
147
148
149
150
151
152
-- run with it:
-- $ CUDA_VISIBLE_DEVICES=0 th eval_dcase.lua
-- The results will be written into "results.txt" file
require 'svm'
require 'audio'
require 'xlua'
require 'nn'
require 'cunn'
require 'cudnn'
require 'math'
-- Commandline Arguments
cmd = torch.CmdLine()
cmd:text()
cmd:text()
cmd:text('Evaluate Soundnet on DCASE2014')
cmd:text()
cmd:text('Options')
cmd:option('-net','models/soundnet8_final.t7','net name')
cmd:option('-layer',18,'layer')
cmd:option('-timewindow',5,'initial time window')
cmd:option('-outfile','results.txt','output filename')
cmd:text()
-- parse input params
params = cmd:parse(arg)
opt = {
data_root = 'dcase/',
train_set = 'dcase/train.txt',
test_set = 'dcase/test.txt',
sound_length = 30, -- secs
net = params['net'],
layer = params['layer'],
time_segment = params['timewindow'],
outfile = params['outfile']
}
net = torch.load(opt.net)
net:evaluate()
net:cuda()
print(net)
print('extracting layer ' .. opt.layer)
function read_dataset(file)
print('reading ' .. file)
local total = 0
sample_count = 0
local twindow_size = 0
for line in io.lines(file) do total = total + 1 end
local dataset,labels
local counter = 1
for line in io.lines(file) do
local split = {}
for k in string.gmatch(line, "%S+") do table.insert(split, k) end
local snd,sr = audio.load(opt.data_root .. '/' .. split[1])
snd:mul(2^-22)
net:forward(snd:view(1,1,-1,1):cuda())
if opt.layer == 25 then
feat = torch.cat(net.modules[opt.layer].output[1]:float():squeeze(),net.modules[opt.layer].output[2]:float():squeeze(),1)
else
feat = net.modules[opt.layer].output:float():squeeze()
end
if counter == 1 then
twindow_size = torch.ceil((opt.time_segment/opt.sound_length)*feat:size(2));
sample_count = feat:size(2) - twindow_size + 1
dataset = torch.Tensor(total*sample_count, feat:size(1), twindow_size)
print ('Time window size: ' .. twindow_size)
labels = torch.Tensor(total*sample_count)
end
for i=1,sample_count do
dataset[{ counter, {}, {} }]:copy(feat[{ {}, {i,i+twindow_size-1} }])
labels[counter] = tonumber(split[2])
counter = counter + 1
end
xlua.progress(counter, total*sample_count)
end
return {dataset,labels}
end
function convert_to_liblinear(dataset)
local ds = {}
for i=1,dataset[1]:size(1) do
local label = dataset[2][i]
local feat = dataset[1][i]:view(-1)
local nz = feat:gt(0):nonzero()
local val
if nz:dim() > 0 then
nz = nz:view(-1)
val = feat:index(1,nz)
else
nz = torch.Tensor{1}
val = torch.Tensor{0}
end
table.insert(ds, {label, {nz:int(), val:float()}})
end
return ds
end
-- Feature Extranction & Data Preparation
train_data = read_dataset(opt.train_set)
test_data = read_dataset(opt.test_set)
train_data_ll = convert_to_liblinear(train_data)
test_data_ll = convert_to_liblinear(test_data)
-- SVM training
C = 0.01
print('train')
model = liblinear.train(train_data_ll, '-c ' .. C .. ' -s 1 -B 1 ');
print('training accuracy:')
liblinear.predict(train_data_ll, model);
print('testing accuracy:')
dec,conf,vals = liblinear.predict(test_data_ll, model);
test_count = 0
for line in io.lines(opt.test_set) do test_count = test_count + 1 end
pred_labels = torch.Tensor(test_count);
gt = torch.Tensor(test_count)
for i=1,test_count do
scores = torch.mean(vals[ {{(i-1)*sample_count+1,i*sample_count}}],1)
max, ind = torch.max(scores,2)
pred_labels[i] = ind
gt[i] = test_data[2][i*sample_count]+1
end
accuracy = torch.mean(torch.eq(gt,pred_labels):type('torch.DoubleTensor'))
print ('Accuracy:' .. accuracy)
-- Append results to file
file = io.open(opt.outfile, "a")
io.output(file)
io.write("Net: " .. opt.net .. "\t Layer: " .. opt.layer .. "\t TimeWindow: " .. opt.time_segment .. "\t Accuracy: " .. accuracy .. "\n")
io.close(file)