• Pytorch特征图heat map热力图


    savepath = r'features'
    if not os.path.exists(savepath):
        os.mkdir(savepath)
    
    • 1
    • 2
    • 3
    def draw_features(width, height, x, savename):
        tic = time.time()
        fig = plt.figure(figsize=(16, 16))
        fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95, wspace=0.05, hspace=0.05)
        for i in range(width * height):
            #8*8网格
            plt.subplot(height, width, i + 1)
            plt.axis('off')
            img = x[0, i, :, :]
            pmin = np.min(img)
            pmax = np.max(img)
            img = ((img - pmin) / (pmax - pmin + 0.000001)) * 255  # float在[0,1]之间,转换成0-255
            img = img.astype(np.uint8)  # 转成unit8
            #函数applycolormap产生伪彩色图像
            #COLORMAP_JET模式,就常被用于生成我们所常见的 热力图
            img = cv2.applyColorMap(img, cv2.COLORMAP_JET)  # 生成heat map
            img = img[:, :, ::-1]  # 注意cv2(BGR)和matplotlib(RGB)通道是相反的
            plt.imshow(img)
            print("{}/{}".format(i, width * height))
        fig.savefig(savename, dpi=100)
        fig.clf()
        plt.close()
        print("time:{}".format(time.time() - tic))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    class PFLDInference(nn.Module):
        def __init__(self):
            super(PFLDInference, self).__init__()
            self.conv1 = nn.Conv2d(3,
                                   64,
                                   kernel_size=3,
                                   stride=2,
                                   padding=1,
                                   bias=False)
            self.bn1 = nn.BatchNorm2d(64)
            self.relu = nn.ReLU(inplace=True)
    
            self.conv2 = nn.Conv2d(64,
                                   64,
                                   kernel_size=3,
                                   stride=1,
                                   padding=1,
                                   bias=False)
            self.bn2 = nn.BatchNorm2d(64)
            self.relu = nn.ReLU(inplace=True)
    
            self.conv3_1 = InvertedResidual(64, 64, 2, False, 2)
    
            self.block3_2 = InvertedResidual(64, 64, 1, True, 2)
            self.block3_3 = InvertedResidual(64, 64, 1, True, 2)
            self.block3_4 = InvertedResidual(64, 64, 1, True, 2)
            self.block3_5 = InvertedResidual(64, 64, 1, True, 2)
    
            self.conv4_1 = InvertedResidual(64, 128, 2, False, 2)
    
            self.conv5_1 = InvertedResidual(128, 128, 1, False, 4)
            self.block5_2 = InvertedResidual(128, 128, 1, True, 4)
            self.block5_3 = InvertedResidual(128, 128, 1, True, 4)
            self.block5_4 = InvertedResidual(128, 128, 1, True, 4)
            self.block5_5 = InvertedResidual(128, 128, 1, True, 4)
            self.block5_6 = InvertedResidual(128, 128, 1, True, 4)
    
            self.conv6_1 = InvertedResidual(128, 16, 1, False, 2)  # [16, 14, 14]
    
            self.conv7 = conv_bn(16, 32, 3, 2)  # [32, 7, 7]
            self.conv8 = nn.Conv2d(32, 128, 7, 1, 0)  # [128, 1, 1]
            self.bn8 = nn.BatchNorm2d(128)
    
            self.avg_pool1 = nn.AvgPool2d(14)
            self.avg_pool2 = nn.AvgPool2d(7)
            self.fc = nn.Linear(176, 136)
            #self.fc = nn.Linear(176, 196)
    
        def forward(self, x):  # x: 3, 112, 112
            x = self.conv1(x)
            x = self.bn1(x)
            x = self.relu(x)  # [64, 56, 56]
            x = self.relu(self.bn2(self.conv2(x)))  # [64, 56, 56]
            x = self.conv3_1(x)
            x = self.block3_2(x)
            x = self.block3_3(x)
            x = self.block3_4(x)
            out1 = self.block3_5(x)
            draw_features(8, 8, out1.cpu().numpy(), "{}/f1_conv1.png".format(savepath))
    
            x = self.conv4_1(out1)
            x = self.conv5_1(x)
            x = self.block5_2(x)
            x = self.block5_3(x)
            x = self.block5_4(x)
            x = self.block5_5(x)
            x = self.block5_6(x)
            x = self.conv6_1(x)
            x1 = self.avg_pool1(x)
            x1 = x1.view(x1.size(0), -1)
    
            x = self.conv7(x)
            x2 = self.avg_pool2(x)
            x2 = x2.view(x2.size(0), -1)
    
            x3 = self.relu(self.conv8(x))
            x3 = x3.view(x3.size(0), -1)
    
            multi_scale = torch.cat([x1, x2, x3], 1)
            landmarks = self.fc(multi_scale)
    
            return out1, landmarks
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    基于heatmap

        顾名思义,其核心思想是将输出的特征层利用卷积来得到各个关键点的置信度,即,每个通道代表了某个
    
    关键点在输入图片上各个位置的置信度,随后在每个通道上取置信度最大值和对应位置即可;heatmap的生成
    
    方式较多样,如使用高斯分布将距离关键点中心远的位置设置低点,呈现出辐射状。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    
    
    • 1
  • 相关阅读:
    Spring整合MyBatis
    张大哥笔记:普通人如何搞钱?
    与创新者同行,Apache Doris in 2023
    .NET 8 IEndpointRouteBuilder详解
    多头风险管理和空头风险管理
    * 玩转数据魔方Plotly Express实战8例
    SUSE 12 SP5 安装MySQL后第一次修改mysql密码
    ElasticSearch(九):ELK 架构
    为啥要用C艹不用C?
    手机转接器实现原理,低成本方案讲解
  • 原文地址:https://blog.csdn.net/qq_16792139/article/details/126744145