Computes the list of anchor boxes by sending a fake image through the graph. Outputs an array of size (4, num_anchors) where each element is an anchor box given as [ycenter, xcenter, height, width] in normalized coordinates.
(graph)
| 6 | from coremltools.models import neural_network |
| 7 | |
| 8 | def get_anchors(graph): |
| 9 | """ |
| 10 | Computes the list of anchor boxes by sending a fake image through the graph. |
| 11 | Outputs an array of size (4, num_anchors) where each element is an anchor box |
| 12 | given as [ycenter, xcenter, height, width] in normalized coordinates. |
| 13 | """ |
| 14 | anchors_tensor = "Concatenate/concat:0" |
| 15 | with graph.as_default(): |
| 16 | with tf.Session(graph=graph) as sess: |
| 17 | image_tensor = graph.get_tensor_by_name("image_tensor:0") |
| 18 | box_corners_tensor = graph.get_tensor_by_name(anchors_tensor) |
| 19 | box_corners = sess.run(box_corners_tensor, feed_dict={image_tensor: np.zeros((1, 300, 300, 3))}) |
| 20 | |
| 21 | # The TensorFlow graph gives each anchor box as [ymin, xmin, ymax, xmax]. |
| 22 | # Convert these min/max values to a center coordinate, width and height. |
| 23 | ymin, xmin, ymax, xmax = np.transpose(box_corners) |
| 24 | width = xmax - xmin |
| 25 | height = ymax - ymin |
| 26 | ycenter = ymin + height / 2. |
| 27 | xcenter = xmin + width / 2. |
| 28 | return np.stack([ycenter, xcenter, height, width]) |
| 29 | |
| 30 | |
| 31 | def build_decoder(graph, num_classes, num_anchors): |