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

demo_tensorflow.py 2.9 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  1. import sys
  2. import numpy as np
  3. import tensorflow as tf
  4. from tensorflow.python.platform import gfile
  5. import cv2
  6. from tool.utils import post_processing, load_class_names, plot_boxes_cv2
  7. def demo_tensorflow(tfpb_file="./weight/yolov4.pb", image_path=None, print_sensor_name=False):
  8. graph_name = 'yolov4'
  9. tf.compat.v1.disable_eager_execution()
  10. with tf.compat.v1.Session() as persisted_sess:
  11. print("loading graph...")
  12. with gfile.FastGFile(tfpb_file, 'rb') as f:
  13. graph_def = tf.compat.v1.GraphDef()
  14. graph_def.ParseFromString(f.read())
  15. persisted_sess.graph.as_default()
  16. tf.import_graph_def(graph_def, name=graph_name)
  17. # print all sensor_name
  18. if print_sensor_name:
  19. tensor_name_list = [tensor.name for tensor in tf.compat.v1.get_default_graph().as_graph_def().node]
  20. for tensor_name in tensor_name_list:
  21. print(tensor_name)
  22. inp = persisted_sess.graph.get_tensor_by_name(graph_name + '/' + 'input:0')
  23. print(inp.shape)
  24. out1 = persisted_sess.graph.get_tensor_by_name(graph_name + '/' + 'output_1:0')
  25. out2 = persisted_sess.graph.get_tensor_by_name(graph_name + '/' + 'output_2:0')
  26. out3 = persisted_sess.graph.get_tensor_by_name(graph_name + '/' + 'output_3:0')
  27. print(out1.shape, out2.shape, out3.shape)
  28. # image_src = np.random.rand(1, 3, 608, 608).astype(np.float32) # input image
  29. # Input
  30. image_src = cv2.imread(image_path)
  31. resized = cv2.resize(image_src, (inp.shape[2], inp.shape[3]), interpolation=cv2.INTER_LINEAR)
  32. img_in = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
  33. img_in = np.transpose(img_in, (2, 0, 1)).astype(np.float32)
  34. img_in = np.expand_dims(img_in, axis=0)
  35. img_in /= 255.0
  36. print("Shape of the network input: ", img_in.shape)
  37. feed_dict = {inp: img_in}
  38. outputs = persisted_sess.run([out1, out2, out3], feed_dict)
  39. print(outputs[0].shape)
  40. print(outputs[1].shape)
  41. print(outputs[2].shape)
  42. boxes = post_processing(img_in, 0.4, outputs)
  43. num_classes = 80
  44. if num_classes == 20:
  45. namesfile = 'data/voc.names'
  46. elif num_classes == 80:
  47. namesfile = 'data/coco.names'
  48. else:
  49. namesfile = 'data/names'
  50. class_names = load_class_names(namesfile)
  51. result = plot_boxes_cv2(image_src, boxes, savename=None, class_names=class_names)
  52. cv2.imshow("tensorflow predicted", result)
  53. cv2.waitKey()
  54. if __name__ == '__main__':
  55. if len(sys.argv) == 1:
  56. sys.argv.append('weight/yolov4.pb')
  57. sys.argv.append('data/dog.jpg')
  58. if len(sys.argv) == 3:
  59. tfpbfile = sys.argv[1]
  60. image_path = sys.argv[2]
  61. demo_tensorflow(tfpbfile, image_path)
  62. else:
  63. print('Please execute this script this way:\n')
  64. print(' python demo_tensorflow.py <tfpbfile> <imageFile>')
Tip!

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

Comments

Loading...