计算flops、参数量等 以及 计算miou

 计算FLOPs 的4种方法

import torch
from torchvision import models

from thop import profile
from torchstat import stat
from torchsummary import summary
from fvcore.nn import FlopCountAnalysis, parameter_count_table


# 方法1
model = models.resnet50(pretrained=True)
tensor = (torch.rand(1, 3, 224, 224),)
flops, params = profile(model, tensor)
print(flops, params)                        # 4133742592, 25557032



# 方法2
model = models.resnet50(pretrained=True)
print(parameter_count_table(model))         # 计算参数      25.6M
tensor = (torch.rand(1, 3, 224, 224),)
flops = FlopCountAnalysis(model, tensor)    # 计算FLOPs    4144854528
print("FLOPs: ", flops.total())




# 方法3
model = models.resnet50(pretrained=True)
stat(model, (3, 224, 224))                  # Total params: 25,557,032  Total Flops: 4.12GFlops





# 方法4
model = models.resnet50(pretrained=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
summary(model, (3, 224, 224))               # Total params: 25,557,032



计算mIOU 

def compute_iou(box, boxes, box_area, boxes_area):
    """Calculates IoU of the given box with the array of the given boxes.
    box: 1D vector [y1, x1, y2, x2]
    boxes: [boxes_count, (y1, x1, y2, x2)]
    box_area: float. the area of 'box'
    boxes_area: array of length boxes_count.

    Note: the areas are passed in rather than calculated here for
          efficency. Calculate once in the caller to avoid duplicate work.
    """
    # Calculate intersection areas
    y1 = np.maximum(box[0], boxes[:, 0])
    y2 = np.minimum(box[2], boxes[:, 2])
    x1 = np.maximum(box[1], boxes[:, 1])
    x2 = np.minimum(box[3], boxes[:, 3])
    intersection = np.maximum(x2 - x1, 0) * np.maximum(y2 - y1, 0)
    union = box_area + boxes_area[:] - intersection[:]
    iou = intersection / union
    return iou

(61条消息) pytorch: 计算网络模型的计算量(FLOPs)和参数量(Params)_Caesar6666的博客-CSDN博客_计算网络模型的参数量https://blog.csdn.net/Caesar6666/article/details/109842379

(61条消息) pytorch转为onnx格式,以及加载模型的params和GFLOPs方法_六六六六神的博客-CSDN博客_pytorch加载onnxhttps://blog.csdn.net/weixin_41848012/article/details/114588854

PyTorch计算模型的参数量以及FLOPs - 知乎 (zhihu.com)

(73条消息) pytorch计算FLOPs - CSDN

(73条消息) pytorch计算FLOPs_haima1998的博客-CSDN博客_pytorch计算flops

(73条消息) 【DL】torch小技巧之网络参数统计 torchstat & torchsummary_张林克的博客-CSDN博客_torchsummary参数

(73条消息) PyTorch查看网络模型的参数量params和FLOPs等_一个菜鸟的奋斗的博客-CSDN博客_pytorch查看参数数量

你可能感兴趣的:(python,pytorch,深度学习,人工智能)