forked from YerevaNN/Spoken-language-identification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_augm_network.py
89 lines (75 loc) · 3.2 KB
/
test_augm_network.py
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
import sys
import caffe
import numpy as np
caffe.set_mode_gpu()
# info about classes
file = open('trainingData.csv')
data = file.readlines()[1:]
langs = set()
for line in data:
filepath, language = line.split(',')
language = language.strip()
langs.add(language)
langs = sorted(langs)
# network parameters:
deploy_name = 'augm_32r-2-64r-2-64r-2-128r-2-128r-2-256r-2-1024rd0.3-1024rd0.3'
network_name = 'augm_dropout0.3_on_augm84K-lr0.01_30K'
iterations = '90000'
aveSamples = 20 # average over this many samples
net = caffe.Classifier(model_file='prototxt/deploy.' + deploy_name + '.prototxt',
pretrained_file='models/' + network_name + '_iter_' + iterations + '.caffemodel')
net.blobs['data'].reshape(1, 1, 256, 768)
predict_set = sys.argv[1]
if (predict_set == "test"):
folder = 'test/png/'
f = open('testingData.csv')
cnt = 12320
print_file = open('predictions/test_' + network_name + '_iter_' + iterations + '_' + str(aveSamples) + '.csv', 'w')
elif (predict_set == "val"):
folder = '/home/brainstorm/caffe/Data/mnt/3/language/train/pngaugm/'
f = open('valEqual.csv')
cnt = 12320
print_file = open('predictions/validation_' + network_name + '_iter_' + iterations + '_' + str(aveSamples) + '.csv', 'w')
else: # train
folder = '/home/brainstorm/caffe/Data/mnt/3/language/train/pngaugm/'
f = open('trainEqual.csv')
cnt = 10000
print_file = open('predictions/train_' + network_name + '_iter_' + iterations + '_' + str(aveSamples) + '.csv', 'w')
preds = []
labels = []
topcoder_score = 0.0
processed = 0
for iter in range(cnt):
st = f.readline()
if (predict_set == "val" or predict_set == "train"):
(name, label) = st.split(',')
label = int(label)
else:
name = st.strip()[:-4]
processed += 1
out = np.zeros((176, ))
for randomIndex in range(aveSamples):
image = caffe.io.load_image(folder + name + '.' + str(randomIndex) + '.png', color=False)
image = np.transpose(image, (2, 0, 1))
#image = np.concatenate([image, np.zeros((1, 256, 858 - 768), dtype=np.float32)], axis=2)
net.blobs['data'].data[...] = image
out += net.forward()['loss'][0]
pred = sorted([(x, it) for it, x in enumerate(out)], reverse=True)
if (predict_set == "val" or predict_set == "train"):
if (pred[0][1] == label):
topcoder_score = topcoder_score + 1000
elif (pred[1][1] == label):
topcoder_score = topcoder_score + 400
elif (pred[2][1] == label):
topcoder_score = topcoder_score + 160
for i in range(3):
lang_id = pred[i][1]
lang = langs[lang_id]
print_file.write(name + '.mp3,' + lang + ',' + str(i + 1) + '\n')
if (iter % 100 == 0):
print >> sys.stderr, network_name + '_iter_' + iterations + '_' + str(aveSamples)
print >> sys.stderr, "processed %d / %d images (%d samples/mp3)" % (iter, cnt, aveSamples)
print >> sys.stderr, "score: ", topcoder_score
print >> sys.stderr, "expected score:", topcoder_score / processed * 35200
print >> sys.stderr, "Final score: ", topcoder_score, " / ", cnt, "000"
print >> sys.stderr, "expected score:", topcoder_score / processed * 35200