在model.py文件中可以看到以下几行代码: from retinanet.anchors import Anchors self.anchors = Anchors() img_batch, annotations = inputs anchors = self.anchors(img_batch)
1
找到Anchors函数,开始生成锚框操作
根据以上代码, 可以得到相关数据。
2 image_shape = image.shape[2:] 从刚才的实例中得出实参为img_batch(个人理解是我们输入的数据图像)。 image_shape = np.array(image_shape) 将形式改变为数组形式 image_shapes = [(image_shape + 2 ** x - 1) // (2 ** x) for x in self.pyramid_levels] 不同的特征图尺寸(看别的解析是说,这一公式的含义是原图//2**x, 但是我还是没能明白)
3 for idx, p in enumerate(self.pyramid_levels): 进行遍历,以下只以P3为例,后续P4-P7操作相同。 anchors = generate_anchors(base_size=self.sizes[idx], ratios=self.ratios, scales=self.scales) 参数1:32; 参数2:0.5,1,2; 参数3:2**0,2**(1/3),2**(2/3) 导入generate_anchors函数部分:
锚框的生成以后,进行转换操作 shifted_anchors = shift(image_shapes[idx], self.strides[idx], anchors) 参数1:P3,参数2:2**3=8,参数3:所有锚框。
导入shift函数部分:
最后经过融合以及维度转换等操作,得到最终的锚框。