00001 """ 00002 Generate base anchors on index 0 00003 """ 00004 00005 import numpy as np 00006 00007 00008 def generate_anchors(base_size=16, ratios=[0.5, 1, 2], 00009 scales=2 ** np.arange(3, 6)): 00010 """ 00011 Generate anchor (reference) windows by enumerating aspect ratios X 00012 scales wrt a reference (0, 0, 15, 15) window. 00013 """ 00014 00015 base_anchor = np.array([1, 1, base_size, base_size]) - 1 00016 ratio_anchors = _ratio_enum(base_anchor, ratios) 00017 anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales) 00018 for i in xrange(ratio_anchors.shape[0])]) 00019 return anchors 00020 00021 00022 def _whctrs(anchor): 00023 """ 00024 Return width, height, x center, and y center for an anchor (window). 00025 """ 00026 00027 w = anchor[2] - anchor[0] + 1 00028 h = anchor[3] - anchor[1] + 1 00029 x_ctr = anchor[0] + 0.5 * (w - 1) 00030 y_ctr = anchor[1] + 0.5 * (h - 1) 00031 return w, h, x_ctr, y_ctr 00032 00033 00034 def _mkanchors(ws, hs, x_ctr, y_ctr): 00035 """ 00036 Given a vector of widths (ws) and heights (hs) around a center 00037 (x_ctr, y_ctr), output a set of anchors (windows). 00038 """ 00039 00040 ws = ws[:, np.newaxis] 00041 hs = hs[:, np.newaxis] 00042 anchors = np.hstack((x_ctr - 0.5 * (ws - 1), 00043 y_ctr - 0.5 * (hs - 1), 00044 x_ctr + 0.5 * (ws - 1), 00045 y_ctr + 0.5 * (hs - 1))) 00046 return anchors 00047 00048 00049 def _ratio_enum(anchor, ratios): 00050 """ 00051 Enumerate a set of anchors for each aspect ratio wrt an anchor. 00052 """ 00053 00054 w, h, x_ctr, y_ctr = _whctrs(anchor) 00055 size = w * h 00056 size_ratios = size / ratios 00057 ws = np.round(np.sqrt(size_ratios)) 00058 hs = np.round(ws * ratios) 00059 anchors = _mkanchors(ws, hs, x_ctr, y_ctr) 00060 return anchors 00061 00062 00063 def _scale_enum(anchor, scales): 00064 """ 00065 Enumerate a set of anchors for each scale wrt an anchor. 00066 """ 00067 00068 w, h, x_ctr, y_ctr = _whctrs(anchor) 00069 ws = w * scales 00070 hs = h * scales 00071 anchors = _mkanchors(ws, hs, x_ctr, y_ctr) 00072 return anchors