ShusenTang / Dive-into-DL-PyTorch

本项目将《动手学深度学习》(Dive into Deep Learning)原书中的MXNet实现改为PyTorch实现。
http://tangshusen.me/Dive-into-DL-PyTorch
Apache License 2.0
18.17k stars 5.38k forks source link

9.4 锚框一节里的MultiBoxPrior方法 #107

Open alqbib opened 4 years ago

alqbib commented 4 years ago

MultiBoxPrior方法中的ss1赋值建议改为ss1 = pairs[:, 0] pairs[:, 1] h / w ,出来的结果与原书更贴切. 我修改过的MultiBoxPrior方法如下,经与mxnet的MultiBoxPrior测试比较,输出的结果类似: def MultiBoxPrior(feature_map: torch.Tensor, sizes, ratios): pairs = [] h, w = feature_map.shape[-2:] for s in sizes: pairs.append([s, math.sqrt(ratios[0])]) for r in ratios[1:]: pairs.append([sizes[0], math.sqrt(r)]) pairs = torch.tensor(pairs, device=feature_map.device) ss1 = pairs[:, 0] pairs[:, 1] h / w ss2 = pairs[:, 0] / pairs[:, 1] base_anchors = torch.stack([-ss1, -ss2, ss1, ss2], dim=1) / 2 shifts_x = torch.arange(0, w, dtype=torch.float, device=feature_map.device) / w shifts_y = torch.arange(0, h, dtype=torch.float, device=feature_map.device) / h shift_y, shift_x = torch.meshgrid(shifts_y, shifts_x) shift_x = shift_x.reshape(-1) shift_y = shift_y.reshape(-1) shifts = torch.stack((shift_x, shift_y, shift_x, shift_y), dim=1) anchors = shifts.reshape((-1, 1, 4)) + base_anchors.reshape((1, -1, 4)) offset_x, offset_y = 1.0 / w, 1.0 / h return anchors.view(1, -1, 4) + torch.tensor([offset_x / 2, offset_y / 2, offset_x / 2, offset_y / 2], device=feature_map.device)

happyong366 commented 4 years ago

书上给出的程序没问题,楼主可以参考这个解释的网站:https://discuss.gluon.ai/t/topic/7024/48 里面讲解了由s和r推导出w和h的过程

alqbib commented 3 years ago

@happyong366 你好,但是我的方法和mxnet自带的MultiBoxPrior的输出结果更为接近,你可以测试下

happyong366 commented 3 years ago

OK 谢谢 ----- 原始邮件 ----- 发件人:alqbib notifications@github.com 收件人:ShusenTang/Dive-into-DL-PyTorch Dive-into-DL-PyTorch@noreply.github.com 抄送人:Wang Yong ygdx1122@sina.com, Mention mention@noreply.github.com 主题:Re:_[ShusenTang/Dive-into-DL-PyTorch]_9.4锚框一节里的MultiBoxPrior方法(#107) 日期:2020年10月10日 20点27分

@happyong366 你好,但是我的方法和mxnet自带的MultiBoxPrior的输出结果更为接近,你可以测试下

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.