-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
68 lines (58 loc) · 1.78 KB
/
test.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
import math
import sys
sys.path.append('data')
sys.path.append('code')
import process
import convolutional_mlp as cnn
from os import listdir
from os.path import isfile, join
def printHeader(cnns):
sys.stdout.write("GalaxyId")
for i, net in enumerate(cnns):
for j in range(net.meta['nLabels']):
sys.stdout.write(",Class%d.%d" % (i + 1, j + 1))
print ""
def printPred(imgName, pred):
sys.stdout.write(imgName)
for i, pp in enumerate(pred):
for p in pp:
sys.stdout.write(",%f" % p)
print ""
assert(len(sys.argv) > 1)
imgDir = sys.argv[1]
if len(sys.argv) > 2:
tasks, imgs = process.readGT(sys.argv[2], False, False)
else:
tasks = None
imgs = [ f[:-4] for f in listdir(imgDir) \
if isfile(join(imgDir, f)) and f[-4:] == '.jpg' ]
nTasks = 11
cnns = [None] * nTasks
for i in range(nTasks):
cnns[i] = cnn.loadConvNet('task%d.pkl' % i, 1)
printHeader(cnns)
if tasks:
sump = [0] * 11
count = [0] * 11
for imgName in imgs:
sys.stderr.write("Processing %s\n" % imgName)
img = process.readImg(imgDir, imgName)
pred = [None] * nTasks
for i in range(nTasks):
pred[i] = cnns[i].predict(img)[0]
pred = process.makePred(pred)
printPred(imgName,pred)
if tasks:
for i in range(nTasks):
if imgName in tasks[i]:
for p, g in zip(pred[i], tasks[i][imgName]):
sump[i] += (p - g) ** 2
count[i] += 1
else:
for p in pred[i]:
sump[i] += p ** 2
count[i] += 1
if tasks:
for i in range(11):
sys.stderr.write("MSE: " + str(math.sqrt(sump[i] / count[i])) + "\n")
sys.stderr.write("Average MSE: " + str(math.sqrt(sum(sump) / sum(count))) + "\n")