-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_model.py
61 lines (49 loc) · 1.91 KB
/
run_model.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
'''
For test purpose only. Allows loading of a model and an image locally or from S3 bucket,
and making prediction of loaded image
'''
import os
import zipfile
import json
import settings
import utils
from gan_model import GANModel
def download_model_from_local_file(model_dir):
'''
Copy saved model locally
:param model_dir: Target directory to copy the model
'''
# check the model file for existence amd copy if needed
protobuf_file_name = utils.get_env_var_or_raise_exception(
settings.MODEL_PROTOBUF_FILE_NAME_ENV_VAR)
model_path = model_dir + '/' + protobuf_file_name
if not os.path.isfile(model_path):
current_directory = os.path.dirname(os.path.realpath(__file__))
model_zip_file_name = utils.get_env_var_or_raise_exception(
settings.MODEL_ZIP_FILE_NAME_ENV_VAR)
model_zip_file_path = current_directory + '/model' +\
'/' + model_zip_file_name
print 'Going to copy a model file from {}...'.format(model_zip_file_path)
with zipfile.ZipFile(model_zip_file_path, 'r') as zip_ref:
zip_ref.extractall(model_dir)
def main():
'''
Entry point
'''
model_dir = utils.create_model_dir()
# download_model_from_local_file(model_dir)
utils.download_model_from_bucket(model_dir)
# create model...
with GANModel(model_dir) as gan_model:
# ...load the image
# image_file_name = './svnh_test_images/image_3.jpg'
# with open(image_file_name, 'rb') as f:
# image = f.read()
image = utils.download_image_from_bucket('vb-tf-aws-lambda-images', 'image_3.jpg')
# ...make prediction
scores = gan_model.predict(image)
# print results
results_json = [{'digit': str(score[0]), 'probability': str(score[1])} for score in scores]
print "Scores: {}".format(json.dumps(results_json))
if __name__ == '__main__':
main()