-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_models.py
More file actions
84 lines (59 loc) · 2.27 KB
/
create_models.py
File metadata and controls
84 lines (59 loc) · 2.27 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
"""
To test each one of the models separately execute:
```
MODEL=half_plus_two # or half_plus_ten
VERSION=1 # or 2
docker run -t --rm -p 8501:8501 \
--name=serving \
-v $(pwd)/models/${MODEL}/${VERSION}:/models/${MODEL}/${VERSION} \
-e MODEL_NAME=${MODEL} \
tensorflow/serving:2.11.0
curl -X POST http://localhost:8501/v1/models/${MODEL}/versions/${VERSION}:predict \
-H 'Content-type: application/json' \
-d '{"signature_name": "serving_default", "instances": [{"x": [0, 1, 2]}]}'
```
"""
import os
import logging
import tensorflow as tf
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
class HalfPlusTwo(tf.Module):
""" Map x -> 0.5 * x + 2 """
def __init__(self):
super(HalfPlusTwo, self).__init__()
@tf.function(input_signature=[tf.TensorSpec([None], tf.float32)])
def __call__(self, x):
return {'y': tf.constant(.5) * x + tf.constant(2.)}
class HalfPlusTen(tf.Module):
""" Map x -> 0.6 * x + 10 """
def __init__(self):
super(HalfPlusTen, self).__init__()
@tf.function(input_signature=[tf.TensorSpec([None], tf.float32)])
def __call__(self, x):
return {'y': tf.constant(.6) * x + tf.constant(10.)}
class HalfPlusTenAgain(tf.Module):
""" Map x -> 0.5 * x + 10 """
def __init__(self):
super(HalfPlusTenAgain, self).__init__()
@tf.function(input_signature=[tf.TensorSpec([None], tf.float32)])
def __call__(self, x):
return {'y': tf.constant(.5) * x + tf.constant(10.)}
def export(model_path: str):
""" Store a custom model """
os.makedirs(model_path, exist_ok=True)
module = HalfPlusTwo()
path = os.path.join(model_path, 'half_plus_two', '1')
tf.saved_model.save(module, path)
logger.info(f"Model exported in {path}")
module = HalfPlusTen()
path = os.path.join(model_path, 'half_plus_ten', '1')
tf.saved_model.save(module, path)
logger.info(f"Model exported in {path}")
module = HalfPlusTenAgain()
path = os.path.join(model_path, 'half_plus_ten', '2')
tf.saved_model.save(module, path)
logger.info(f"Model exported in {path}")
if __name__ == '__main__':
export(model_path='models')