Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

service.py 1.0 KB

You have to be logged in to leave a comment. Sign In
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
  1. import numpy as np
  2. import pickle
  3. import yaml
  4. import bentoml
  5. from bentoml.io import NumpyNdarray
  6. def calculate_loss(reconstructions,data,type_loss):
  7. if type_loss=="mse":
  8. train_loss = np.mean((reconstructions - data)**2, axis=1)
  9. return train_loss
  10. elif type_loss=="mae":
  11. train_loss = np.mean(np.abs(reconstructions - data), axis=1)
  12. return train_loss
  13. BENTO_MODEL_TAG = "keras_model:latest"
  14. ad_runner = bentoml.keras.get(BENTO_MODEL_TAG).to_runner()
  15. service = bentoml.Service("keras_model_ad", runners=[ad_runner])
  16. f = open('src/threshold.pickle', 'rb')
  17. threshold = pickle.load(f)
  18. f.close()
  19. f = open('src/parameters.yaml','rb')
  20. params = yaml.load(f, Loader=yaml.FullLoader)
  21. f.close()
  22. @service.api(input=NumpyNdarray(), output=NumpyNdarray())
  23. def predict(input_data: np.ndarray) -> np.ndarray:
  24. rec = ad_runner.predict.run(input_data)
  25. print(type(rec))
  26. loss = calculate_loss(np.array(rec),input_data,params['type_loss'])
  27. return np.array(loss>threshold).astype(int)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...