SqueezeNet1_1@0.5 在CPU上运行速度异常的情况
老师您好,我在您的squeezenet.py的最后添加了一下的代码测试运行速度。(ResNet50也有类似情况)
您有遇到类似的情况吗?是如何处理的呢?
if __name__ == '__main__':
import time
rec_time = 10
device = 'cpu'
model = squeezenet1_1(num_classes=42, dropout_factor=.5)
chkpt = torch.load('../weights/squeezenet1_1-size-256-model_epoch-2999.pth', map_location=device)
# 是否加载权重
model.load_state_dict(chkpt)
model.to(device).eval()
print(model)
model(torch.randn(1, 3, 256, 256).to(device))
input = torch.randn(1, 3, 256, 256).to(device)
start_time = time.time()
with torch.no_grad():
for i in range(rec_time):
out = model(input)
end_time = time.time()
print(out.shape)
print('avg time cost: %.4f' % ((end_time - start_time) / rec_time))
发现在CPU模式下,当不加载权重时,SqueezeNet平均每次推理耗时为0.0157秒;而加载权重后,SqueezeNet平均每次推理耗时为0.0656,耗时多了4+倍。
但是在GPU模式下,是否加载权重对平均运行时间无影响。
模型已经正确加载并正确输出,不进行dropout时也有一致的情况
OS:Ubuntu20.04
CPU:i7-9700F
GPU:RTX2060
Python:3.7
Pytorch:1.6.0