-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lambda.py
136 lines (96 loc) · 5.75 KB
/
Lambda.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
# --------------------------------------------
# Serialize Image
# --------------------------------------------
"""Input
{
"s3_bucket": "sagemaker-us-east-1-613011470122",
"s3_key": "test/bicycle_s_000030.png"
}
"""
import json
import boto3
import base64
s3 = boto3.resource('s3')
def lambda_handler(event, context):
"""A function to serialize target data from S3"""
# Get the s3 address from the Step Function event input
key = event['s3_key'] ## TODO: fill in
bucket = event['s3_bucket'] ## TODO: fill in
# Download the data from s3 to /tmp/image.png
## TODO: fill in
s3.Bucket(bucket).download_file(key, '/tmp/image.png')
# We read the data from a file
with open("/tmp/image.png", "rb") as f:
image_data = base64.b64encode(f.read())
# Pass the data back to the Step Function
print("Event:", event.keys())
return {
'statusCode': 200,
'body': {
"image_data": image_data,
"s3_bucket": bucket,
"s3_key": key,
"inferences": []
}
}
# --------------------------------------------
# Classify Image
# --------------------------------------------
"""Input
{
"body": {
"image_data": "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAB9dJREFUWIXNl0mTXEcRx3+ZVe/1Oj37PhpZMpblCCRADoMD4+DEgQjgc/Ax/CXgwIkLFw7czAWzhDFrYOMIZAnJCmsZaUaz9PQ23a/fUpUceowMhG8Yky8qKiIPlZn/f1W+/IuZGZ+jKYCZwf8yDTuPCfhnvlkS8tnE+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UxyTls1JDNVU5agVsf3Wbj8Rj56Suv2uraOr3uCeO8ZCSeMnWERkKmEZcbG9GzkyTUxSi9Yio0qkitqii8IyQpSRCiGa1x5FhL3mFAsbTA1tY6jUTxMTIsAqf9AeMnj9meBLYLh/z829+1kHpcFambknpPUCgsYl6ppSl1rwgFWILRBlW8r1CpiNERJUWtJCkqzrRCYyRMI3vFlBMXKJs1UIfLJvhJxnpMWIiOacsh45u3LA8VarNRICQzuaQGymw0NzVMIhodzmqz37mLmERmw6CCRJzB1EVcNHwFwTuiCVYZLgASQAwXobJInKv9HwgTmI1in4Ukg/+UZfIJv4g804b/HAz/y/app54D/7lT8A8hijL99C6y1wAAAABJRU5ErkJggg==",
"s3_bucket": "sagemaker-us-east-1-613011470122",
"s3_key": "test/bicycle_s_000030.png",
"inferences": []
}
}
"""
import json
import base64
from sagemaker.serializers import IdentitySerializer
from sagemaker.predictor import Predictor
# Fill this in with the name of your deployed model
ENDPOINT = 'image-classification-2023-09-04-02-25-17-599' ## TODO: fill in
def lambda_handler(event, context):
# Decode the image data
image = base64.b64decode(event['image_data'])
# Instantiate a Predictor
predictor = Predictor(ENDPOINT) ## TODO: fill in
# For this model the IdentitySerializer needs to be "image/png"
predictor.serializer = IdentitySerializer("image/png")
# Make a prediction:
inferences = predictor.predict(payload) ## TODO: fill in
# We return the data back to the Step Function
event["inferences"] = inferences.decode('utf-8')
return {
'statusCode': 200,
'body': json.dumps(event)
}
# --------------------------------------------
# Filter Inference Confidence
# --------------------------------------------
"""Input
{
"body": {
"inferences": [0.9770092368125916, 0.02299083210527897]
}
}
"""
import json
THRESHOLD = .93
def lambda_handler(event, context):
# Grab the inferences from the event
inferences = event["inferences"] ## TODO: fill in
# Check if any values in our inferences are above THRESHOLD
meets_threshold = (max(inferences) > THRESHOLD) ## TODO: fill in
# If our threshold is met, pass our data back out of the
# Step Function, else, end the Step Function with an error
if meets_threshold:
pass
else:
raise("THRESHOLD_CONFIDENCE_NOT_MET")
return {
'statusCode': 200,
'body': json.dumps(event)
}