annotate deep/amt/amt.py @ 401:86d5e583e278

Fixed class number bug
author humel
date Wed, 28 Apr 2010 01:25:52 -0400
parents 99905d9bc9dd
children 83413ac10913
rev   line source
399
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
1 # Script usage : python amt.py filname.cvs
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
2
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
3 import csv,numpy,re
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
4
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
5 PATH = 'nist/'
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
6 CVSFILE = None
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
7
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
8 answer_labels = [ 'Answer.C'+str(i+1) for i in range(10) ]
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
9 img_url = 'Input.image_url'
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
10 turks_per_batch = 3
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
11
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
12
401
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
13 def setup_association():
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
14 answer_assoc = {}
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
15 for i in range(0,10):
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
16 answer_assoc[str(i)]=i
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
17 for i in range(10,36):
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
18 answer_assoc[chr(i+55)]=i
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
19 for i in range(36,62):
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
20 answer_assoc[chr(i+61)]=i
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
21 return answer_assoc
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
22
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
23 answer_assoc = setup_association()
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
24
399
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
25 def test_error():
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
26 turks = []
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
27 reader = csv.DictReader(open(CVSFILE), delimiter=',')
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
28 entries = [ turk for turk in reader ]
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
29
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
30 errors = numpy.zeros((len(entries),))
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
31 if len(entries) % turks_per_batch != 0 :
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
32 raise Exception('Wrong number of entries or turks_per_batch')
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
33
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
34 total_uniq_entries = len(entries) / turks_per_batch
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
35
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
36 errors = numpy.zeros((len(entries),))
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
37 error_means = numpy.zeros((total_uniq_entries,))
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
38 error_variances = numpy.zeros((total_uniq_entries,))
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
39
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
40
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
41 for i in range(total_uniq_entries):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
42 for t in range(turks_per_batch):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
43 errors[i*turks_per_batch+t] = get_error(entries[i*turks_per_batch+t])
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
44 error_means[i] = errors[i*turks_per_batch:(i+1)*turks_per_batch].mean()
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
45 error_variances[i] = errors[i*turks_per_batch:(i+1)*turks_per_batch].var()
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
46
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
47 print 'Average test error: ' + str(error_means.mean()*10) +'%'
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
48 print 'Error variance : ' + str(error_variances.mean()*10) +'%'
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
49
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
50
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
51 def get_error(entry):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
52 file = parse_filename(entry[img_url])
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
53 f = open(PATH+file,'r')
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
54 labels = re.sub("\s+", "",f.readline()).strip()[1:-2].split('.')
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
55 f.close()
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
56 test_error = 0
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
57 for i in range(len(answer_labels)):
401
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
58 answer = entry[answer_labels[i]]
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
59 if len(answer) != 0:
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
60 try:
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
61 if answer_assoc[answer] != int(labels[i]):
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
62 test_error+=1
399
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
63 except:
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
64 test_error+=1
401
86d5e583e278 Fixed class number bug
humel
parents: 399
diff changeset
65 else:
399
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
66 test_error+=1
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
67 return test_error
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
68
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
69
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
70 def parse_filename(string):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
71 filename = string.split('/')[-1]
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
72 return filename.split('.')[0]+'.txt'
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
73
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
74 if __name__ =='__main__':
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
75 import sys
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
76 CVSFILE = sys.argv[1]
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
77 test_error()