-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify_image_lambda.py
More file actions
144 lines (112 loc) · 4.4 KB
/
classify_image_lambda.py
File metadata and controls
144 lines (112 loc) · 4.4 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
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
141
142
143
144
# ImageClassification API
# authored by Scott Queen
# start date: 12/15/18
#
#
# Using snippets from Tensorflow code that is
# licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
import datetime
import json
import os
import base64
def timestamp_for_logging():
timestamp = datetime.datetime.now()
ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
return ts
def health_check():
print(timestamp_for_logging(), ': request made to health_check')
response = {}
response['status'] = 'All Good Here!'
return response
def load_graph(model_file):
print(timestamp_for_logging(), ': loading image-classification graph')
g = tf.Graph()
graph_def = tf.GraphDef()
with open(model_file, "rb") as f:
graph_def.ParseFromString(f.read())
with g.as_default():
tf.import_graph_def(graph_def)
return g
def load_labels(label_file):
print(timestamp_for_logging(), ': loading labels')
labels_list = []
proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines()
for l in proto_as_ascii_lines:
labels_list.append(l.rstrip())
return labels_list
def read_tensor_from_image_file(image,
input_height=299,
input_width=299,
input_mean=0,
input_std=255):
image_reader = tf.image.decode_image(image)
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0)
resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.Session()
result = sess.run(normalized)
return result
def classify_image(image):
print(timestamp_for_logging(), ': invoking classify_image')
graph = load_graph('/var/task/classification_model/pests_IncV3_2.pb')
print(timestamp_for_logging(), ': loaded image-classification graph')
labels = load_labels('/var/task/classification_model/pests_IncV3_2_labels.txt')
print(timestamp_for_logging(), ': loaded image-classification labels')
input_height = 299
input_width = 299
input_mean = 0
input_std = 255
input_layer = "Placeholder"
output_layer = "final_result"
t = read_tensor_from_image_file(
image,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
print(timestamp_for_logging(), ': successfully created tensor from image data')
input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = graph.get_operation_by_name(input_name)
output_operation = graph.get_operation_by_name(output_name)
with tf.Session(graph=graph) as sess:
results = sess.run(output_operation.outputs[0], {
input_operation.outputs[0]: t
})
results = np.squeeze(results)
top_k = results.argsort()[-3:][::-1]
response = {}
probabilities = {}
for i in top_k:
print(labels[i], results[i])
probabilities[labels[i]] = "{0:.3f}".format((results[i]))
response['probabilities'] = probabilities
return response
def classify_image(event, context):
LAMBDA_TASK_ROOT = os.environ.get('LAMBDA_TASK_ROOT', os.path.dirname(os.path.abspath(__file__)))
CURR_BIN_DIR = os.path.join(LAMBDA_TASK_ROOT, 'bin')
os.environ['PATH'] = os.environ['PATH'] + ':' + CURR_BIN_DIR
print("LAMBDA_TASK_ROOT: ", LAMBDA_TASK_ROOT)
print("CURR_BIN_DIR: ", CURR_BIN_DIR)
print("path: ", os.environ['PATH'])
base64_encoded_image = event.b64image
print("base64_encoded_image: ", base64_encoded_image)
print(timestamp_for_logging(), ': decoding received image')
image = base64.b64decode(base64_encoded_image)
print(timestamp_for_logging(), ': classifying image')
classification = classify_image(image)
response = json.dumps(classification)
print(timestamp_for_logging(), ': results of image classification: ', response)
return response