데이터과학 삼학년

tf.keras serving function을 이용한 feature transform 적용 방법 본문

Machine Learning

tf.keras serving function을 이용한 feature transform 적용 방법

Dan-k 2020. 11. 25. 19:17
반응형
serving function을 이용한 featuretransform 적용 예시
In [1]:
import pandas as pd
import numpy as np

# Make numpy values easier to read.
np.set_printoptions(precision=3, suppress=True)

import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
In [2]:
PROJECT_ID = "project_id"
BUCKET_NAME = "bucket_name" 
REGION = "us-central1"
In [3]:
!gcloud config set project $PROJECT_ID
!gcloud config set compute/region $REGION
Updated property [core/project].
Updated property [compute/region].
In [4]:
abalone_train = pd.read_csv(
    "https://storage.googleapis.com/download.tensorflow.org/data/abalone_train.csv",
    names=["Length", "Diameter", "Height", "Whole weight", "Shucked weight",
           "Viscera weight", "Shell weight", "Age"])

abalone_train.head()
Out[4]:
Length Diameter Height Whole weight Shucked weight Viscera weight Shell weight Age
0 0.435 0.335 0.110 0.334 0.1355 0.0775 0.0965 7
1 0.585 0.450 0.125 0.874 0.3545 0.2075 0.2250 6
2 0.655 0.510 0.160 1.092 0.3960 0.2825 0.3700 14
3 0.545 0.425 0.125 0.768 0.2940 0.1495 0.2600 16
4 0.545 0.420 0.130 0.879 0.3740 0.1695 0.2300 13
In [5]:
abalone_features = abalone_train.copy()
abalone_labels = abalone_features.pop('Age')
In [6]:
abalone_features = np.array(abalone_features)
abalone_features
Out[6]:
array([[0.435, 0.335, 0.11 , ..., 0.136, 0.077, 0.097],
       [0.585, 0.45 , 0.125, ..., 0.354, 0.207, 0.225],
       [0.655, 0.51 , 0.16 , ..., 0.396, 0.282, 0.37 ],
       ...,
       [0.53 , 0.42 , 0.13 , ..., 0.374, 0.167, 0.249],
       [0.395, 0.315, 0.105, ..., 0.118, 0.091, 0.119],
       [0.45 , 0.355, 0.12 , ..., 0.115, 0.067, 0.16 ]])
In [7]:
abalone_model = tf.keras.Sequential([
  layers.Dense(64),
  layers.Dense(1)
])

abalone_model.compile(loss = tf.losses.MeanSquaredError(),
                      optimizer = tf.optimizers.Adam())
In [8]:
abalone_model.fit(abalone_features, abalone_labels, epochs=10)
Epoch 1/10
104/104 [==============================] - 0s 3ms/step - loss: 59.0822
Epoch 2/10
104/104 [==============================] - 0s 2ms/step - loss: 12.3370
Epoch 3/10
104/104 [==============================] - 0s 3ms/step - loss: 9.0293
Epoch 4/10
104/104 [==============================] - 0s 3ms/step - loss: 8.4060
Epoch 5/10
104/104 [==============================] - 0s 3ms/step - loss: 7.8532
Epoch 6/10
104/104 [==============================] - 0s 3ms/step - loss: 7.3990
Epoch 7/10
104/104 [==============================] - 0s 2ms/step - loss: 7.0498
Epoch 8/10
104/104 [==============================] - 0s 3ms/step - loss: 6.8201
Epoch 9/10
104/104 [==============================] - 0s 3ms/step - loss: 6.6385
Epoch 10/10
104/104 [==============================] - 0s 3ms/step - loss: 6.5113
Out[8]:
<tensorflow.python.keras.callbacks.History at 0x7fd778681f50>
In [9]:
abalone_test = abalone_features[10:15]
abalone_model.predict(abalone_test)
Out[9]:
array([[11.004],
       [11.102],
       [ 8.697],
       [ 8.619],
       [ 7.77 ]], dtype=float32)
In [10]:
column_lst = list(abalone_train).copy()
column_lst.remove('Age')
column_lst
Out[10]:
['Length',
 'Diameter',
 'Height',
 'Whole weight',
 'Shucked weight',
 'Viscera weight',
 'Shell weight']
In [13]:
input_signature=[tf.TensorSpec([None], dtype=tf.string, name='user'),tf.TensorSpec([None, 7], dtype=tf.float64,name='data')]

이 아래 serving_func 함수 안을 바꾸면 됨!!!!!!

1. data에서 feature engineering 및 normalization 해서 loaded_model에 넣으면 됨
2. 주의할 점은, data가 tensor라서 tensor 연산으로 FE, 노멀라이징 해야함
In [14]:
@tf.function(input_signature=input_signature)
def serving_func(user, data):
    """
    여기서
    raw data를
    feature eng, normalization  해서
    최종 data로 만들면 끝!
    (tensor 연산으로 진행해야함)
    """
    pred = loaded_model(data, training=False)
    predictions = {
        'user':user,
        'pred':pred
    }
    

    return predictions
In [15]:
MODEL_EXPORT_PATH = "./"
tf.saved_model.save(abalone_model, MODEL_EXPORT_PATH)
WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py:1817: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
INFO:tensorflow:Assets written to: ./assets
In [16]:
loaded_model = tf.keras.models.load_model(MODEL_EXPORT_PATH)
In [17]:
SERVING_MODEL_EXPORT_PATH = './serving'
In [18]:
loaded_model(abalone_test)
Out[18]:
<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[11.004],
       [11.102],
       [ 8.697],
       [ 8.619],
       [ 7.77 ]], dtype=float32)>
In [19]:
loaded_model.save(SERVING_MODEL_EXPORT_PATH, signatures={'serving_default': serving_func})
INFO:tensorflow:Assets written to: ./serving/assets
In [20]:
keyed_model = tf.keras.models.load_model(SERVING_MODEL_EXPORT_PATH)
In [21]:
keyed_model(abalone_test)
Out[21]:
<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[11.004],
       [11.102],
       [ 8.697],
       [ 8.619],
       [ 7.77 ]], dtype=float32)>
In [22]:
abalone_test[1]
Out[22]:
array([0.615, 0.495, 0.16 , 1.255, 0.582, 0.32 , 0.323])
In [23]:
import os
os.environ["MODEL_LOCATION"] = SERVING_MODEL_EXPORT_PATH
In [ ]:
## 최초 함수 시작시에만 실행
!gcloud ai-platform models create test_keyed_model \
  --regions us-central1
In [24]:
!gcloud ai-platform versions create v12\
       --model test_keyed_model --origin  ${MODEL_LOCATION} --staging-bucket gs://daehwan \
       --runtime-version 2.1
Using endpoint [https://ml.googleapis.com/]
Creating version (this might take a few minutes)......done.                    
In [25]:
with open("keyed_input.json", "w") as file:
    print('{"data": [1,2,3,4,5,6,7], "user": "id_1234"}', file=file)
In [26]:
!gcloud ai-platform predict --model test_keyed_model --json-instances keyed_input.json --version v12 --signature-name serving_default
Using endpoint [https://ml.googleapis.com/]
PRED                 USER
[52.49372100830078]  id_1234
728x90
반응형
LIST

'Machine Learning' 카테고리의 다른 글

[Clustering] DBSCAN  (0) 2021.01.01
Anomaly Detection 종류(Point, Contextual, Collective)  (0) 2020.12.01
Hierarchical temporal memory (HTM networks)  (0) 2020.08.11
AutoML for Text Classification  (0) 2020.08.10
GANs 간략한 소개  (0) 2020.08.10
Comments