# 在需要查看时间的函数上使用装饰器,比如我想要查看网络哪层更耗时 classSRResNet(nn.Module): def__init__(self,in_channels,n_block,scale_factor,hidden_channels): super(SRResNet, self).__init__() self.num_blocks=n_block self.conv1=nn.Conv2d(in_channels,hidden_channels,kernel_size=(9,9),stride=(1,1),padding=4) self.ac1=nn.PReLU(num_parameters=1,init=0.25) self.blocks=nn.Sequential(*[Block(in_channels=hidden_channels,hidden=hidden_channels) for i inrange(self.num_blocks)])
self.conv2=nn.Conv2d(hidden_channels,hidden_channels,kernel_size=3,stride=1,padding=1) self.bn=nn.BatchNorm2d(hidden_channels) self.pixel_shuffle=nn.Sequential(*[SubPixelShuffleConvBlock(in_channels=hidden_channels) for i inrange(scale_factor//2)]) self.conv3=nn.Conv2d(hidden_channels,3,kernel_size=(9,9),stride=1,padding=4)
definit_weights(self): for m in self.modules(): ifisinstance(m,nn.Conv2d): n=m.kernel_size[0]*m.kernel_size[1]*m.out_channels m.weight.data.normal_(0,math.sqrt(2./n)) if m.bias!=None: m.bias.data.zero_() @profile defforward(self,x): out1=self.conv1(x) # B,64,w,h out1=self.ac1(out1) out2=self.blocks(out1) # B,64,w,h out3=self.conv2(out2) # B,64,w,h out3=self.bn(out3) out4=out1+out3 out5=self.pixel_shuffle(out4) # B,64,w*4,h*4 out6=self.conv3(out5) # B,3,w*4,h*4 return out6
positional arguments: script The python script file to run args Optional script arguments
optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -l, --line-by-line Use the line-by-line profiler instead of cProfile. Implies --builtin. -b, --builtin Put 'profile' in the builtins. Use 'profile.enable()'/'.disable()', '@profile' to decorate functions, or 'with profile:' to profile a section of code. -o OUTFILE, --outfile OUTFILE Save stats to <outfile> (default: 'scriptname.lprof' with --line-by-line, 'scriptname.prof' without) -s SETUP, --setup SETUP Code to execute before the code to profile -v, --view View the results of the profile in addition to saving it -u UNIT, --unit UNIT Output unit (in seconds) in which the timing info is displayed (default: 1e-6) -z, --skip-zero Hide functions which have not been called
通常都是下面的参数运行
kernprof -l -v -z -u 1e-3 xxx.py
结果:
Wrote profile results to eval.py.lprof Timer unit: 0.001 s
Total time: 89.0344 s File: E:\XXX\SRResNet.py Function: forward at line 30