Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

python - Calling forward function without .forward()

While looking at some pytorch code on pose estimation AlphaPose I noticed some unfamiliar syntax:

Basically, we define a Darknet class which inherits nn.Module properties like so: class Darknet(nn.Module)

This re-constructs the neural net from some config file and also defines functions to load pre-trained weights and a forward pass

Now, forward pass takes the following parameters:

def forward(self, x, CUDA)

I should note that in class definition forward is the only method that has a CUDA attribute (this will become important later on)

In the forward pass we get the predictions:

for i in range(number_of_modules):
     x = self.module[i](x)

where module[i] was constructed as:

module = nn.Sequential()
conv = nn.Conv2d(prev_fileters, filters, kernel_size, stride, pad, bias=bias)
module.add_module("conv_{0}".format(index), conv)

We then call invoke this model and (I presume) a forward method like so:

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)

I assume that the last line is the calling of the forward pass but why not use the .forward? Is this a pytorch specific syntax or am I missing some basic python principles?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is nothing torch specific. When you call something as class_object(fn params) it invokes the __call__ method of that class.

If you dig the code of torch, specifically nn.Module you will see that __call__ internally invokes forward but taking care of hooks and states that pytorch allows. So when you are calling self.det_model(img, cuda) you are still calling forward.

See the code for nn.module here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...