forked from uhfband/keras2caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_InceptionV3.py
47 lines (29 loc) · 890 Bytes
/
convert_InceptionV3.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
import caffe
import cv2
import numpy as np
from keras.applications.inception_v3 import InceptionV3
import keras2caffe
#converting
keras_model = InceptionV3(weights='imagenet', include_top=True)
keras2caffe.convert(keras_model, 'deploy.prototxt', 'InceptionV3.caffemodel')
#testing the model
caffe.set_mode_gpu()
net = caffe.Net('deploy.prototxt', 'InceptionV3.caffemodel', caffe.TEST)
img = cv2.imread('bear.jpg')
img = cv2.resize(img, (299, 299))
img = img[...,::-1] #RGB 2 BGR
data = np.array(img, dtype=np.float32)
data = data.transpose((2, 0, 1))
data.shape = (1,) + data.shape
data -= 128
data /= 128
net.blobs['data'].data[...] = data
out = net.forward()
pred = out['predictions']
#softmax function
pred = np.exp(pred - np.max(pred))
pred /= pred.sum()
prob = np.max(pred)
cls = pred.argmax()
lines=open('synset_words.txt').readlines()
print prob, cls, lines[cls]