-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
116 lines (104 loc) · 3.74 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import sys
#from memory_profiler import profile
#@profile
def main():
import caffe
import numpy as np
caffe_dir = "../caffe"
MODEL_FILE = caffe_dir + "/models/bvlc_reference_caffenet/deploy.prototxt"
PRETRAINED = caffe_dir + "/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"
IMAGE_FILE = "../cat.jpg"
with open("synset_words.txt") as f:
words = f.readlines()
words = map(lambda x: x.strip(), words)
net = caffe.Classifier(MODEL_FILE, PRETRAINED,
mean=np.load(caffe_dir + '/python/caffe/imagenet/ilsvrc_2012_mean.npy'),
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
caffe.set_phase_test()
caffe.set_mode_gpu()
input_image = caffe.io.load_image(IMAGE_FILE)
#prediction = net.predict([input_image])
prediction = net.forward_all(data=np.asarray([net.preprocess('data', input_image)]))
i = prediction["prob"].argmax()
print(i)
print(words[i])
# del net
# prediction = net2.predict([input_image])
# i = prediction[0].argmax()
# print(i)
# print(words[i])
SETTINGS = {
"imagenet": {
"model_file": "var6.8.Model.prototxt",
"pretrained": "var6.8.Model.caffemodel",
"image_dims": (256, 256),
"input_dims": [224, 224],
"oversample": False,
"raw": "fish.rawinput.txt",
"sep": ",",
},
"deepface": {
#"model_file": "lconv/context_specific.14.Model.prototxt",
#"pretrained": "lconv/context_specific.14.Model.caffemodel",
"model_file": "models/D6.prototxt",
"pretrained": "models/D6.caffemodel",
#"model_file": "lconv/cs.rm2.prototxt",
#"pretrained": "lconv/cs.rm2.caffemodel",
"image_dims": (152, 152),
"input_dims": [152, 152],
"oversample": False,
"raw": "face.rawinput.txt",
"sep": ",",
},
"test": {
"model_file": "testnn/local_test_mid.prototxt",
"pretrained": "testnn/local_test_mid.caffemodel",
"image_dims": (3, 3),
"input_dims": [3, 3],
"oversample": False,
"raw": "testnn/local_input_mid.txt",
"sep": "\t",
}
}
def main_tlc():
import caffe
import numpy as np
import skimage.io
np.set_printoptions(threshold='nan')
options = SETTINGS["imagenet"]
IMAGE_FILE = "../cat.jpg"
#IMAGE_FILE = "../fish.jpg"
mean = np.zeros([3] + list(options['input_dims']))
mean.fill(128.0)
with open("/home/haichen/datasets/imagenet/meta/2010/synset_words.txt") as f:
words = f.readlines()
words = map(lambda x: x.strip(), words)
net = caffe.Classifier(options["model_file"], options["pretrained"],
mean=mean, input_scale=0.0078125,
image_dims=options["image_dims"])
sys.stderr.write("model file: %s\n" % options["model_file"])
sys.stderr.write("pretrained: %s\n" % options["pretrained"])
caffe.set_phase_test()
caffe.set_mode_gpu()
#net.set_mode_cpu()
with open(options["raw"]) as f:
content = f.read()
rawinput = content.strip(' ,\t\r\n').split(options["sep"])
rawinput = map(lambda x: eval(x), rawinput)
rawinput = np.asarray(rawinput).reshape([1,3] + options['input_dims'])
prediction = net.predict_raw(rawinput)
return
input_image = skimage.io.imread(IMAGE_FILE)
prediction = net.predict([input_image], oversample=True)
#prediction = net.forward_all(data=np.asarray([net.preprocess('data', input_image)]))
#return
label = prediction.argmax()
#for i,v in enumerate(prediction[0]):
# print i, v
print label
print words[label]
#print input_image
#main()
main_tlc()