【发布时间】:2019-03-06 01:14:18
【问题描述】:
在查看有关姿势估计的一些 pytorch 代码AlphaPose 时,我注意到一些不熟悉的语法:
基本上,我们定义了一个Darknet 类,它继承nn.Module 属性,如下所示:class Darknet(nn.Module)
这会从一些配置文件重新构建神经网络,并定义函数来加载预训练的权重和前向传递
现在,前向传递采用以下参数:
def forward(self, x, CUDA)
我应该注意,在类定义中,forward 是唯一具有 CUDA 属性的方法(这将在稍后变得重要)
在前向传递中,我们得到预测:
for i in range(number_of_modules):
x = self.module[i](x)
module[i] 构造为:
module = nn.Sequential()
conv = nn.Conv2d(prev_fileters, filters, kernel_size, stride, pad, bias=bias)
module.add_module("conv_{0}".format(index), conv)
然后我们调用这个模型和(我假设)一个转发方法,如下所示:
self.det_model = Darknet("yolo/cfg/yolov3-spp.cfg")
self.det_model.load_weights('models/yolo/yolov3-spp.weights')
self.det_model.cpu()
self.det_model.eval()
image = image.cpu()
prediction = self.det_model(img, CUDA = False)
我假设最后一行是前向传递的调用,但为什么不使用.forward?这是特定于 pytorch 的语法还是我缺少一些基本的 python 原则?
【问题讨论】: