Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. from ngraph_onnx.onnx_importer.importer import import_onnx_file
  2. import ngraph as ng
  3. import numpy as np
  4.  
  5.  
  6. class EmotionModel(object):
  7. def __init__(self):
  8. model = import_onnx_file("model/model.onnx")
  9. runtime = ng.runtime(backend_name="CPU")
  10. self.inference = runtime.computation(model)
  11. self.emotion_table = {
  12. "0": "neutral",
  13. "1": "happiness",
  14. "2": "surprise",
  15. "3": "sadness",
  16. "4": "anger",
  17. "5": "disgust",
  18. "6": "fear",
  19. "7": "contempt",
  20. }
  21.  
  22. def _softmax(self, x):
  23. e_x = np.exp(x - np.max(x))
  24. return e_x / np.sum(e_x)
  25.  
  26. def _postprocess(self, x):
  27. prob = self._softmax(x)
  28. prob = np.squeeze(prob)
  29. classes = np.argsort(prob)[::-1]
  30. return {self.emotion_table[str(c)]: str(prob[c]) for c in classes}
  31.  
  32. def predict(self, X, feature_names):
  33. return self._postprocess(self.inference(X))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement