【性能比较】关于pytorch中的unsample和反卷积的耗时

这篇博客对比了PyTorch中使用`nn.ConvTranspose2d`进行上采样和`nn.Upsample`结合线性插值的效率。在CPU上,`nn.ConvTranspose2d`耗时约0.016秒,而`nn.Upsample`耗时约0.007秒;在GPU(1080ti)上,`nn.ConvTranspose2d`耗时0.269秒,而`nn.Upsample`仅需0.002秒。结果表明,在GPU环境下,`nn.Upsample`的性能更优。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import time


transpose_conv = nn.ConvTranspose2d(
                in_channels=3,
                out_channels=3,
                kernel_size=3,
                stride=2,
                padding=1,
                output_padding=1)
unsample = nn.Upsample(scale_factor=2)
xxxxx = torch.rand(1,3,448,256)

startTime = time.time()
ddd = transpose_conv(xxxxx)
print(ddd.shape)
endTime = time.time()
print(endTime-startTime)




startTime1 = time.time()
eee = unsample(xxxxx)
print(eee.shape)
endTime1 = time.time()
print(endTime1-startTime1)

CPU比较结果:

torch.Size([1, 3, 896, 512])
0.016954421997070312
torch.Size([1, 3, 896, 512])
0.006981372833251953

GPU比较结果(需要在各个模块最后加入.cuda()),平台为1080ti:

torch.Size([1, 3, 896, 512])
0.2690904140472412
torch.Size([1, 3, 896, 512])
0.0017828941345214844
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值