-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathyhat-deploy.py
140 lines (114 loc) · 4.49 KB
/
yhat-deploy.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
cd ~/workspace/github.com/yhat/demo-image-recognizer
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
import cv2
import numpy as np
import pprint as pp
import matplotlib.pyplot as plt
# http://keras.io/
def VGG_16(weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
if weights_path:
model.load_weights(weights_path)
return model
plt.imshow(cv2.imread('images/pig.jpg'))
labels = [" ".join(row.split(' ')[1:]) for row in open("./labels.txt").read().strip().split('\n')]
df_labels = pd.DataFrame({"label": labels})
im = cv2.resize(cv2.imread('images/pig.jpg'), (224, 224)).astype(np.float32)
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0)
# Test pretrained model
model = VGG_16('data/vgg16_weights.h5')
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
out = model.predict(im)
pred = dict(zip(labels, model.predict_proba(im)[0]))
best_guess = labels[np.argmax(out)]
output = []
guesses = np.array(labels)[np.argsort(out[0])].tolist()
guesses.reverse()
for item in guesses[:10]:
output.append(item)
output = ", ".join(output)
best_guess = ",".join(output.split(", ")[:10])
print "It's a %s" % best_guess
{ "guess": best_guess, "prob": pred }
from yhat import Yhat, YhatModel, preprocess
from PIL import Image
from StringIO import StringIO
import base64
class ImageRecognizer(YhatModel):
REQUIREMENTS = [
"opencv"
]
@preprocess(in_type=dict, out_type=dict)
def execute(self, data):
img64 = data['image64']
binaryimg = base64.decodestring(img64)
pilImage = Image.open(StringIO(binaryimg))
image = np.array(pilImage)
resized_image = cv2.resize(image, (224, 224)).astype(np.float32)
resized_image[:,:,0] -= 103.939
resized_image[:,:,1] -= 116.779
resized_image[:,:,2] -= 123.68
resized_image = resized_image.transpose((2,0,1))
resized_image = np.expand_dims(resized_image, axis=0)
out = model.predict(resized_image)
# pred = dict(zip(labels, model.predict_proba(im)[0]))
output = []
guesses = np.array(labels)[np.argsort(out[0])].tolist()
guesses.reverse()
for item in guesses[:10]:
output.append(item)
output = ", ".join(output)
guesses = ",".join(output.split(", ")[:5])
print "It's a %s" % guesses
return { "guess": guesses }
testdata = {
"image64": open('./test-image.base64', 'rb').read()
}
print ImageRecognizer().execute(testdata)
yh = Yhat(USERNAME, APIKEY, URL)
yh.deploy("ImageRecognizer", ImageRecognizer, globals(), True)