Created
September 26, 2020 02:17
-
-
Save Hamptonjc/c688c522cdcafd59c26d6268061fe692 to your computer and use it in GitHub Desktop.
FPN_Anchor_Box_Generator.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "FPN_Anchor_Box_Generator.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyPrh/bZ3xPUEp0sBAQS5QZ4", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/Hamptonjc/c688c522cdcafd59c26d6268061fe692/fpn_anchor_box_generator.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "CD9PC5y2594l" | |
}, | |
"source": [ | |
"# Anchor Box Generator for Feature Pyramid Network\n", | |
"***For use with RPN***" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "mhRdJnGF55nB" | |
}, | |
"source": [ | |
"# Imports\n", | |
"import numpy as np\n", | |
"from typing import List, NamedTuple" | |
], | |
"execution_count": 31, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "t0UsahSIDGps" | |
}, | |
"source": [ | |
"# Define some NamedTuple classes for clean, readable code :)\n", | |
"\n", | |
"class AnchorBox(NamedTuple):\n", | |
" x_1:float\n", | |
" y_1:float \n", | |
" x_2:float\n", | |
" y_2:float\n", | |
"\n", | |
"class PyramidAnchorBoxes(NamedTuple):\n", | |
" p2:List[AnchorBox]\n", | |
" p3:List[AnchorBox]\n", | |
" p4:List[AnchorBox]\n", | |
" p5:List[AnchorBox]\n", | |
" p6:List[AnchorBox]" | |
], | |
"execution_count": 32, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "r3s5jdOp6JEg" | |
}, | |
"source": [ | |
"def generate_anchor_boxes(ratios: List[float], anchor_box_area: int) -> List[AnchorBox]:\n", | |
" \"\"\"\n", | |
" ratios: list of aspect ratios (Example: [0.5,1,2])\n", | |
" anchor_box_area: area of the anchor box.\n", | |
" Returns: list of AnchorBox tuples, one for each aspect ratio.\n", | |
" Note: Anchor boxes are in x1,y1,x2,y2 coordinates,\n", | |
" where the center of the box = 0\n", | |
" \"\"\"\n", | |
" anchor_boxes = []\n", | |
" for ratio in ratios:\n", | |
" height = np.sqrt(anchor_box_area/ratio)\n", | |
" width = anchor_box_area/height\n", | |
" x_1, y_1 = (-width/2, height/2)\n", | |
" x_2, y_2 = (width/2, -height/2)\n", | |
" anchor_boxes.append(AnchorBox(x_1,y_1,x_2,y_2))\n", | |
" return anchor_boxes" | |
], | |
"execution_count": 39, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "2T2wYvWfCsBq" | |
}, | |
"source": [ | |
"def generate_pyramid_anchor_boxes(ratios: List[float], anchor_box_areas: List[int]):\n", | |
" \"\"\"\n", | |
" ratios: list of aspect ratios to use. (Ex: [0.5,1,2])\n", | |
" anchor_box_areas: list of areas where each area corresponds to a level in the feature pyramid (following the order [p2,p3,p4,p5,p6]).\n", | |
" Returns: PyramidAnchorBoxes tuple, where each item in tuple is a list of anchor boxes for that feature map size (p2,p3,etc.)\n", | |
" \"\"\"\n", | |
" pyramid_anchor_boxes = []\n", | |
" for area in anchor_box_areas:\n", | |
" anchor_boxes = generate_anchor_boxes(ratios, area)\n", | |
" pyramid_anchor_boxes.append(anchor_boxes)\n", | |
" pyramid_anchor_boxes = PyramidAnchorBoxes(p2=pyramid_anchor_boxes[0], p3=pyramid_anchor_boxes[1],\n", | |
" p4=pyramid_anchor_boxes[2], p5=pyramid_anchor_boxes[3],\n", | |
" p6=pyramid_anchor_boxes[4])\n", | |
" return pyramid_anchor_boxes" | |
], | |
"execution_count": 40, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "lAXInFnnLPAn" | |
}, | |
"source": [ | |
"# Example Usage" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "b3Eq2IjjG6Vz" | |
}, | |
"source": [ | |
"# Define ratios and anchor box areas. These are the values used in the FPN paper.\n", | |
"ratios = [0.5,1.,2.]\n", | |
"anchor_box_areas = [32**2, 64**2, 128**2, 256**2, 512**2]\n", | |
"pyramid_anchor_boxes = generate_pyramid_anchor_boxes(ratios, anchor_box_areas)" | |
], | |
"execution_count": 41, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ziq4yQ0CHi6q", | |
"outputId": "48d1a7b2-165f-485e-9553-cf00cd8ef818", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 68 | |
} | |
}, | |
"source": [ | |
"# p2 feature map list of anchor boxes\n", | |
"pyramid_anchor_boxes.p2" | |
], | |
"execution_count": 42, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[AnchorBox(x_1=-11.31370849898476, y_1=22.627416997969522, x_2=11.31370849898476, y_2=-22.627416997969522),\n", | |
" AnchorBox(x_1=-16.0, y_1=16.0, x_2=16.0, y_2=-16.0),\n", | |
" AnchorBox(x_1=-22.62741699796952, y_1=11.313708498984761, x_2=22.62741699796952, y_2=-11.313708498984761)]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 42 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "7gzalasMHkhq", | |
"outputId": "dcb88583-540c-4615-9592-c2c897d9f457", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"# the second anchor box of feature map p2.\n", | |
"pyramid_anchor_boxes.p2[1]" | |
], | |
"execution_count": 43, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"AnchorBox(x_1=-16.0, y_1=16.0, x_2=16.0, y_2=-16.0)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 43 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Zd0FqBJxH0in", | |
"outputId": "0aa2e93e-f778-4cc9-f054-b054ac242617", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"# the x_1 coordinate of the anchor box.\n", | |
"pyramid_anchor_boxes.p2[1].x_1" | |
], | |
"execution_count": 44, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"-16.0" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 44 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment