使用举例:
model = model()model.load_state_dict(torch.load(weight_path))device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model = model.to(device)input_size = (384,288)Torch2Onnx(model,input_size,"test.onnx") 获取model中的params:请注意:不同的方法默认model在cpu还是在cuda上是不一样的,如果出现类似RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same的报错,请检查weight是否应该在cuda上。
花痴的大白:使用torchsummary使用pip安装torchsummary:
pip install torchsummary
代码片段:
from torchsummary import summarymodel = model()model.load_state_dict(torch.load(weight_path))device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model = model.to(device)summary(model,(3,384,288)) ajdjm:使用torchstat使用pip安装torchstat:
pip install torchstat
代码片段(和summary差不多)
from torchstat import stat model = model() model.load_state_dict(torch.load(weight_path)) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") stat(model,(3,384,288)) 方法三:使用thop(不太推荐)使用pip安装thop:
pip install thop
代码片段:
from thop import profile,clever_formatmodel = model() model.load_state_dict(torch.load(weight_path)) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") flops, params = profile(model,inputs=()) flops,params = clever_format(flops,params,"%.3f")