annotate deep/amt/amt.py @ 399:99905d9bc9dd

Initial commit for calculating the test error of the AMT classifier
author humel
date Wed, 28 Apr 2010 00:38:31 -0400
parents
children 86d5e583e278
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
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
13 def test_error():
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
14 turks = []
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
15 reader = csv.DictReader(open(CVSFILE), delimiter=',')
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
16 entries = [ turk for turk in reader ]
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
17
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
18 errors = numpy.zeros((len(entries),))
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
19 if len(entries) % turks_per_batch != 0 :
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
20 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
21
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
22 total_uniq_entries = len(entries) / turks_per_batch
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
23
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
24 errors = numpy.zeros((len(entries),))
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
25 error_means = numpy.zeros((total_uniq_entries,))
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
26 error_variances = numpy.zeros((total_uniq_entries,))
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
27
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
28
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
29 for i in range(total_uniq_entries):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
30 for t in range(turks_per_batch):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
31 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
32 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
33 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
34
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
35 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
36 print 'Error variance : ' + str(error_variances.mean()*10) +'%'
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
37
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
38
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
39 def get_error(entry):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
40 file = parse_filename(entry[img_url])
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
41 f = open(PATH+file,'r')
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
42 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
43 f.close()
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
44 test_error = 0
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
45 for i in range(len(answer_labels)):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
46 try:
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
47 answer = int(entry[answer_labels[i]])
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
48 except:
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
49 try :
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
50 answer = ord(entry[answer_labels[i]])
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
51 except:
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
52 test_error+=1
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
53 continue
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
54 if answer != int(labels[i]):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
55 test_error+=1
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
56 return test_error
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
57
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
58
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
59 def parse_filename(string):
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
60 filename = string.split('/')[-1]
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
61 return filename.split('.')[0]+'.txt'
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
62
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
63 if __name__ =='__main__':
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
64 import sys
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
65 CVSFILE = sys.argv[1]
99905d9bc9dd Initial commit for calculating the test error of the AMT classifier
humel
parents:
diff changeset
66 test_error()