概述
savepath = r'features'
if not os.path.exists(savepath):
os.mkdir(savepath)
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))
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
基于heatmap
顾名思义,其核心思想是将输出的特征层利用卷积来得到各个关键点的置信度,即,每个通道代表了某个
关键点在输入图片上各个位置的置信度,随后在每个通道上取置信度最大值和对应位置即可;heatmap的生成
方式较多样,如使用高斯分布将距离关键点中心远的位置设置低点,呈现出辐射状。
最后
以上就是烂漫饼干为你收集整理的Pytorch特征图heat map热力图的全部内容,希望文章能够帮你解决Pytorch特征图heat map热力图所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复