Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. from PIL import Image, ImageFont, ImageDraw
  2. import numpy as np
  3. from keras import backend as K
  4. from keras.models import load_model
  5. from keras.layers import Input
  6. from yolo3.model import yolo_eval, yolo_body, tiny_yolo_body
  7. from yolo3.utils import letterbox_image
  8. import os
  9. import tensorflow as tf
  10. from timeit import default_timer as timer
  11. from yolo3.utils import rgb2gray
  12.  
  13. def sigmoid(x):
  14. return 1./(1.+np.exp(-x))
  15.  
  16. sigmoid = np.vectorize(sigmoid)
  17.  
  18. #loading of model and printing of summary
  19. num_classes = 2
  20. anchors = np.array(
  21. [np.array([55, 116]), np.array([67, 120]), np.array([69, 144]), np.array([85, 132]), np.array([107, 184]), np.array([203, 206])])
  22. anchor_mask = [[3, 4, 5], [1, 2, 3]]
  23. t_yolo = tiny_yolo_body(Input(shape=(256, 256, 3)), 6 // 2, num_classes)
  24. t_yolo.load_weights("app4/trained/round2/trained_weights_final.h5")
  25. t_yolo.summary()
  26.  
  27. #preparations of two images
  28. image = Image.open("img1.png")
  29. boxed_image = letterbox_image(image, tuple(reversed((416, 416))))
  30. image_datas = rgb2gray(boxed_image, triplet=True)
  31. image2 = Image.open("img2.png")
  32. boxed_image2 = letterbox_image(image2, tuple(reversed((416, 416))))
  33. image_datas2 = rgb2gray(boxed_image2, triplet=True)
  34. image_data = np.array([image_datas, image_datas2])
  35.  
  36. #inference
  37. features = t_yolo.predict(image_data)
  38. length = len(features)
  39. proto_box = []
  40. proto_scores = []
  41.  
  42. #processing of results
  43. for idx, val in enumerate(features):
  44. anchors = np.array([np.array([55,116]), np.array([67,120]), np.array([69,144]), np.array([85,132]), np.array([107,184]), np.array([203,216])])
  45. anchor_mask = [[3,4,5], [1,2,3]]
  46. input_shape = np.asarray(np.shape(features[0])[1 : 3]) * 32
  47.  
  48. first = anchors[anchor_mask[idx]]
  49. image_size = (256, 256)
  50.  
  51. num_anchors = len(first)
  52. anchors_tensor = np.reshape(first, [1, 1, 1, num_anchors, 2])
  53. grid_shape = np.shape(val)[1 : 3]
  54. b = np.reshape(np.arange(0, stop=grid_shape[0]), [-1, 1, 1, 1])
  55. grid_y = np.tile(np.reshape(np.arange(0, stop=grid_shape[0]), [-1, 1, 1, 1]), [1, grid_shape[1], 1, 1])
  56. grid_x = np.tile(np.reshape(np.arange(0, stop=grid_shape[1]), [1, -1, 1, 1]),
  57. [grid_shape[0], 1, 1, 1])
  58. grid = np.concatenate([grid_x, grid_y], axis=3)
  59.  
  60. feats = np.reshape(
  61. val, [-1, grid_shape[0], grid_shape[1], num_anchors, num_classes + 5])
  62.  
  63. box_xy = (sigmoid(feats[..., :2]) + grid) / grid_shape[::-1]
  64. pre_box_wh = feats[..., 2:4] * anchors_tensor / input_shape[::-1]
  65. box_wh = np.exp(feats[..., 2:4]) * anchors_tensor / input_shape[::-1]
  66. box_confidence = sigmoid(feats[..., 4:5])
  67. box_class_probs = sigmoid(feats[..., 5:])
  68.  
  69. box_yx = box_xy[..., ::-1]
  70. box_hw = box_wh[..., ::-1]
  71. image_shape = np.array([256, 256])
  72. new_shape = np.round((image_shape * np.min(input_shape/image_shape)))
  73. offset = (input_shape-new_shape)/2./input_shape
  74. scale = input_shape/new_shape
  75. box_yx = (box_yx - offset) * scale
  76. box_hw *= scale
  77.  
  78. box_mins = box_yx - (box_hw / 2.)
  79. box_maxes = box_yx + (box_hw / 2.)
  80.  
  81. boxes = np.concatenate([
  82. box_mins[..., 0:1], # y_min
  83. box_mins[..., 1:2], # x_min
  84. box_maxes[..., 0:1], # y_max
  85. box_maxes[..., 1:2] # x_max
  86. ], axis=4)
  87.  
  88. # Scale boxes back to original image shape.
  89. scaler = np.concatenate([image_shape, image_shape])
  90. boxes *= scaler
  91. #here at original implementation is loosing of data, because batch size is ignored
  92. boxes = np.reshape(boxes, [boxes.shape[0], -1, 4])
  93. box_scores = box_confidence * box_class_probs
  94. box_scores = np.reshape(box_scores, [box_scores.shape[0], -1, num_classes])
  95. proto_box.append(boxes)
  96. proto_scores.append(box_scores)
  97.  
  98. proto_box = np.concatenate(proto_box, axis=1)
  99. proto_scores = np.concatenate(proto_scores, axis=1)
  100. mask = proto_scores >= 0.6
  101. _boxes = []
  102.  
  103. #there is need for non maxima supression algorithm implementation
  104. for idx, batch in enumerate(proto_scores):
  105. final_classes = []
  106. final_boxes = []
  107. final_scores = []
  108. for c in range(num_classes):
  109. class_boxes = proto_box[idx, mask[idx, :, c]]
  110. class_box_scores = proto_scores[idx, :, c][mask[idx, :, c]]
  111. classes = np.ones_like(class_box_scores, dtype="int32") * c
  112. final_boxes.append(class_boxes)
  113. final_scores.append(class_box_scores)
  114. final_boxes = np.concatenate(final_boxes, axis=0)
  115. final_scores = np.concatenate(final_scores, axis=0)
  116. _boxes.append(final_boxes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement