-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassify.py
More file actions
67 lines (47 loc) · 1.2 KB
/
classify.py
File metadata and controls
67 lines (47 loc) · 1.2 KB
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
# -*- coding: utf-8 -*-
import sys
import numpy as np
import chainer
import chainer.links as L
from chainer import serializers
import net
def load_image(path):
from PIL import Image
img = Image.open(path)
if img.size != (28, 28):
img.resize((28, 28))
gray_img = img.convert('L')
#gray_img.save('sample-gray.png')
y=[]
for x in img.getdata():
y.append(255-x[0])
y = np.asarray(y)
y = y.astype(np.float32)
y /= 255
return y
def classify(model, x):
return model.predictor(x)
def main(argv):
if len(argv) < 4:
print "Usage: %s [sp|mlp|cnn] model_path image_path" % argv[0]
sys.exit()
type = argv[1]
model_path = argv[2]
image_path = argv[3]
if type == "sp":
model = L.Classifier(net.MnistSP())
elif type == "cnn":
model = L.Classifier(net.MnistCNN())
else:
model = L.Classifier(net.MnistMLP())
serializers.load_npz(model_path, model)
print("input:\t%s" % image_path)
x = load_image(image_path)
x = chainer.Variable(np.asarray([x]))
r = classify(model, x)
print("output:")
for i in range(len(r.data[0])):
print "\t%d: %f" % (i , r.data[0][i])
print("class:\t%d" % np.argmax(r.data[0]))
if __name__ == "__main__":
main(sys.argv)