Guest User

Untitled

a guest
Jan 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #!/usr/bin/env owl
  2.  
  3. open Owl
  4.  
  5. let gist_id = "51eaf74c65fa14c8c466ecfab2351bbd"
  6.  
  7. (* input: 1x1000 ndarray; output: top-N inference result list,
  8. each element in the form of [class: string; propability: float] *)
  9. let to_tuples ?(top=5) preds =
  10. let dict_path = Sys.getenv "HOME" ^ "/.owl/zoo/" ^ gist_id ^ "/imagenet1000.dict" in
  11. let h = Owl_utils.marshal_from_file dict_path in
  12. let tp = Dense.Matrix.S.top preds top in
  13.  
  14. let results = Array.make top ("type", 0.) in
  15. Array.iteri (fun i x ->
  16. let cls = Hashtbl.find h x.(1) in
  17. let prop = Dense.Ndarray.S.get preds [|x.(0); x.(1)|] in
  18. Array.set results i (cls, prop);
  19. ) tp;
  20. results
  21.  
  22. (* input: 1x1000 ndarray; output: top-N inference result as a json string *)
  23. let to_json ?(top=5) preds =
  24. let dict_path = Sys.getenv "HOME" ^ "/.owl/zoo/" ^ gist_id ^ "/imagenet1000.dict" in
  25. let h = Owl_utils.marshal_from_file dict_path in
  26. let tp = Dense.Matrix.S.top preds top in
  27.  
  28. let assos = Array.make top "" in
  29. Array.iteri (fun i x ->
  30. let cls = Hashtbl.find h x.(1) in
  31. let prop = Dense.Matrix.S.get preds x.(0) x.(1) in
  32. let p = "{\"class\":\"" ^ cls ^ "\", \"prop\": " ^ (string_of_float prop) ^ "}," in
  33. Array.set assos i p
  34. ) tp;
  35.  
  36. let str = Array.fold_left (^) "" assos in
  37. let str = String.sub str 0 ((String.length str) - 1) in
  38. let json = "[" ^ str ^ " ]" in
  39. json
Add Comment
Please, Sign In to add comment