Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 KB | None | 0 0
  1. from time import perf_counter
  2. import os
  3.  
  4. os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
  5.  
  6. # import face_alignment
  7. # import face_alignment
  8. # import onnx
  9. # from onnx import optimizer
  10. import onnx
  11. import onnxruntime
  12. from onnxruntime import InferenceSession
  13. # import numpy as np
  14. from tqdm import tqdm
  15.  
  16. # os.environ['FACEALIGNMENT_USERDIR'] = 'models'
  17. # import torch
  18. # import cv2
  19. # import face_alignment
  20. from test import *
  21.  
  22. print(cv2.__version__)
  23.  
  24. img = cv2.imread(r"C:\Users\maxbe\Desktop\selfie_hd.jpg")
  25. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  26. face_box = [
  27.     160,
  28.     300,
  29.     550,
  30.     850,
  31. ]
  32. # model = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D, device='cpu', flip_input=False, verbose=True)
  33. # # warmup
  34. # model.get_landmarks_from_image(img, [face_box])
  35. #
  36. # # ltrb
  37. # times = []
  38. # for _ in tqdm(range(100)):
  39. #     a = perf_counter()
  40. #     landmarks = model.get_landmarks_from_image(img, [face_box])
  41. #     b = perf_counter()
  42. #     times.append(b - a)
  43. # #
  44. # net = model.face_alignment_net
  45. #
  46. # input_names = ["actual_input_1"]
  47. # output_names = ["output1"]
  48. # dummy_input = torch.randn(1, 3, 256, 256, device='cpu')
  49. # torch.onnx.export(net.cpu().eval(), dummy_input, "FAN.onnx", verbose=True, opset_version=11, export_params=True,
  50. #                   keep_initializers_as_inputs=True)
  51. #
  52. # #
  53. # # dnn = cv2.dnn.readNetFromONNX('FAN.onnx')
  54. # #
  55. # onnx_graph = onnx.load('FAN.onnx')
  56. #
  57. # # Check the model
  58. # onnx.checker.check_model(onnx_graph)
  59. # print('The model is checked!')
  60. # passes = """eliminate_deadend
  61. #         eliminate_identity
  62. #         eliminate_nop_dropout
  63. #         eliminate_nop_monotone_argmax
  64. #         eliminate_nop_pad
  65. #         eliminate_nop_transpose
  66. #         eliminate_unused_initializer
  67. #         extract_constant_to_initializer
  68. #         fuse_add_bias_into_conv
  69. #         fuse_bn_into_conv
  70. #         fuse_consecutive_concats
  71. #         fuse_consecutive_log_softmax
  72. #         fuse_consecutive_reduce_unsqueeze
  73. #         fuse_consecutive_squeezes
  74. #         fuse_consecutive_transposes
  75. #         fuse_matmul_add_bias_into_gemm
  76. #         fuse_pad_into_conv
  77. #         fuse_transpose_into_gemm""".split('\n')
  78. #
  79. # passes = [x.strip() for x in passes]
  80. # optimized_model = optimizer.optimize(
  81. #     onnx_graph,
  82. #     passes=passes
  83. # )
  84. # onnx.save(optimized_model, 'FAN_optimized.onnx')
  85. sessionOptions = onnxruntime.SessionOptions()
  86. # sessionOptions.log_severity_level = 0
  87. sessionOptions.inter_op_num_threads = 8
  88. sessionOptions.intra_op_num_threads = 1
  89. sessionOptions.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
  90. sessionOptions.enable_profiling = True
  91. # sessionOptions.optimized_model_filepath = 'FAN_opt_graph.onnx'
  92. session = InferenceSession('FAN.onnx', sess_options=sessionOptions)
  93.  
  94. # get the name of the first input of the model
  95. input_name = session.get_inputs()[0].name
  96.  
  97. times_onnx = []
  98. for _ in range(100):
  99.     a = perf_counter()
  100.     tensor, center, scale = [face[0] for face in preprocess_image(img, [face_box])]  # extracted preprocessing
  101.     b = perf_counter()
  102.     print("PRE: ", b - a)
  103.     a = perf_counter()
  104.     outputs = session.run([], {input_name: tensor})[0]
  105.     b = perf_counter()
  106.     print("INF: ", b - a)
  107.     a = perf_counter()
  108.     landmarks_onnx = postprocess_output(torch.tensor(outputs), center, scale)
  109.     b = perf_counter()
  110.     print("POST:", b - a)
  111.     print()
  112.  
  113. # print(f"ONNX: {np.mean(times_onnx):.2f}s avg.")
  114. session.end_profiling()
  115. #
  116. # onnx_graph = onnx.load('FAN.onnx')
  117. #
  118. # # Check the model
  119. # onnx.checker.check_model(onnx_graph)
  120. # print('The model is checked!')
  121. # passes = """eliminate_deadend
  122. #         eliminate_identity
  123. #         eliminate_nop_dropout
  124. #         eliminate_nop_monotone_argmax
  125. #         eliminate_nop_pad
  126. #         eliminate_nop_transpose
  127. #         eliminate_unused_initializer
  128. #         extract_constant_to_initializer
  129. #         fuse_add_bias_into_conv
  130. #         fuse_bn_into_conv
  131. #         fuse_consecutive_concats
  132. #         fuse_consecutive_log_softmax
  133. #         fuse_consecutive_reduce_unsqueeze
  134. #         fuse_consecutive_squeezes
  135. #         fuse_consecutive_transposes
  136. #         fuse_matmul_add_bias_into_gemm
  137. #         fuse_pad_into_conv
  138. #         fuse_transpose_into_gemm""".split('\n')
  139. #
  140. # passes = [x.strip() for x in passes]
  141. # optimized_model = optimizer.optimize(
  142. #     onnx_graph,
  143. #     passes=passes
  144. # )
  145. # onnx.save(optimized_model, 'FAN_optimized.onnx')
  146. #
  147. # session = InferenceSession('FAN_optimized.onnx')
  148. #
  149. # # get the name of the first input of the model
  150. # input_name = session.get_inputs()[0].name
  151. #
  152. # times_onnx_optimized = []
  153. # for _ in tqdm(range(100)):
  154. #     a = perf_counter()
  155. #     tensor, center, scale = [face[0] for face in preprocess_image(img, [face_box])]  # extracted preprocessing
  156. #     outputs = session.run([], {input_name: tensor})[0]
  157. #     landmarks_onnx = postprocess_output(torch.tensor(outputs), center, scale)
  158. #     b = perf_counter()
  159. #     times_onnx_optimized.append(b - a)
  160. # # print(f"PYTORCH: {np.mean(times):.2f}s avg.")
  161. # print(f"ONNX: {np.mean(times_onnx):.2f}s avg.")
  162. # print(f"ONNX (OPTIMIZED): {np.mean(times_onnx_optimized):.2f}s avg.")
  163. # print('Input Name:', input_name)
  164. # a = perf_counter()
  165. # tensor = model.preprocess_image(img, [face_box])  # extracted preprocessing
  166. # outputs = session.run([], {input_name: tensor})[0]
  167. # b = perf_counter()
  168. # print(b - a)
  169. #
  170. # a = perf_counter()
  171. # tensor = model.preprocess_image(img, [face_box])[0]  # extracted preprocessing
  172. # outputs = session.run([], {input_name: tensor})[0]
  173. # b = perf_counter()
  174. # print(b - a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement