当前位置: 首页 > news >正文

门户网站建设制作/seo一个关键词多少钱

门户网站建设制作,seo一个关键词多少钱,免费供求信息发布,江阴百度推广公司不好之处就是这种方法不能最大程度还原最后的热图,会产生很多噪声,不过大体区域还是接近的,代码如下: import cv2 import os import numpy as np from PIL import Image import torch import torch.nn as nn import torch.nn.fun…

不好之处就是这种方法不能最大程度还原最后的热图,会产生很多噪声,不过大体区域还是接近的,代码如下:

import cv2
import os
import numpy as np
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import sys
import time
import shutil
import argparse
import json
from torch.utils.data import DataLoader
from utils import set_logger, update_lr, get_pck_with_sigma, get_pred_coordinates, save_images, save_limb_images# ================== Grad-CAM ==================
class GradCAM:def __init__(self, model, target_layer):self.model = modelself.target_layer = target_layerself.gradients = None  # 存储梯度self.activations = None  # 存储激活值# 绑定梯度钩子self.hook_layers()def hook_layers(self):""" 绑定 hook,提取梯度和特征图 """def forward_hook(module, input, output):self.activations = output  # 存储前向传播的特征图def backward_hook(module, grad_in, grad_out):self.gradients = grad_out[0]  # 存储反向传播的梯度self.target_layer.register_forward_hook(forward_hook)self.target_layer.register_backward_hook(backward_hook)def forward(self, image, joint_index):""" 计算 Grad-CAM(针对某个关键点)"""self.model.eval()  # 进入评估模式image = image.requires_grad_(True)  # 需要梯度信息output = self.model(image)  # 形状 (1, num_joints, H, W)# 选择某个关键点的热图进行梯度计算heatmap = output[:, joint_index, :, :]  # (1, H, W)loss = heatmap.sum()  # 让 loss 与该热图相关self.model.zero_grad()loss.backward()  # 计算梯度# 计算 Grad-CAM 权重(对梯度做全局平均池化)gradients = self.gradients.mean(dim=(2, 3), keepdim=True)  # (1, C, 1, 1)activations = self.activations  # (1, C, H, W)cam = (activations * gradients).sum(dim=1, keepdim=True)  # (1, 1, H, W)cam = F.relu(cam)  # 保证非负cam = cam.squeeze().cpu().detach().numpy()  # (H, W)return camdef overlay_heatmap(self, image, cam):""" 叠加 Grad-CAM 热图,使用默认颜色 """# image 为 BGR 格式cam = cv2.resize(cam, (image.shape[1], image.shape[0]))  # 调整大小cam = (cam - cam.min()) / (cam.max() - cam.min() + 1e-8)  # 归一化到 0-1heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)overlay = cv2.addWeighted(image, 0.5, heatmap, 0.5, 0)return overlaydef remove_module_prefix(state_dict):new_state_dict = {}for k, v in state_dict.items():if k.startswith("module."):new_state_dict[k[7:]] = v  # 去掉前面的 'module.'else:new_state_dict[k] = vreturn new_state_dict# ========== 测试模型和 Grad-CAM ==========
if __name__ == "__main__":# 1. 初始化模型(关键点数 16)num_joints = 16model = Your_model(num_joints, False)# 2. 加载模型参数state_dict = torch.load('model.pth')#你训练出的模型的路径model.load_state_dict(remove_module_prefix(state_dict['model_state_dict']))# 3. 选择目标层:绑定到模块中的某一层target_layer = model.layer# 4. 初始化 Grad-CAMgrad_cam = GradCAM(model, target_layer)# 5. 读取测试图像并预处理img = cv2.imread("test.jpg")  # 读取图片(BGR 格式)img = cv2.resize(img, (224, 224))  # 调整大小# 转换为 RGB 格式,再转为 Tensorimg_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)img_tensor = torch.tensor(img_rgb.transpose(2, 0, 1), dtype=torch.float32).unsqueeze(0) / 255.0# 6. 分别计算 0-16 号关键点的 Grad-CAM 热图(进行叠加)cams = []for joint_index in range(0,16):cam_joint = grad_cam.forward(img_tensor, joint_index)  # 每个 cam 的尺寸约为 40x40# 将每个热图归一化到 0-1,并调整大小到原图尺寸cam_joint = cv2.resize(cam_joint, (img.shape[1], img.shape[0]))cam_joint = (cam_joint - cam_joint.min()) / (cam_joint.max() - cam_joint.min() + 1e-8)cams.append(cam_joint)cams = np.stack(cams, axis=0)  # 形状 (16, H, W)# 7. 合并 16 个关键点热图:逐像素取所有关键点中响应的最大值(不区分颜色,仅保留默认热图色调)combined_cam = np.max(cams, axis=0)combined_cam = (combined_cam - combined_cam.min()) / (combined_cam.max() - combined_cam.min() + 1e-8)# 8. 生成叠加图(使用默认颜色映射)overlay = grad_cam.overlay_heatmap(img, combined_cam)plt.imshow(cv2.cvtColor(overlay, cv2.COLOR_BGR2RGB))plt.axis("off")plt.title("Combined Grad-CAM for Keypoints 0-20")plt.show()

http://www.whsansanxincailiao.cn/news/30334656.html

相关文章:

  • 大连网站建设企业/南京网站制作公司
  • 沙河高端网站建设/国外网站推广平台有哪些?
  • 太原网站制作/网站域名备案查询
  • 大型网站开发/班级优化大师怎么用
  • 秦皇岛网站搜索优化/seo优化运营专员
  • css查询错误网站/安徽建站
  • 汽车工厂视频网站建设/100个成功营销案例
  • 网站 开发流程/站长工具域名查询
  • 人工智能和网站开发/全国十大婚恋网站排名
  • 网页设计与网站建设指标点/抖音推广引流
  • 网站留言板 html/推广软件排行榜前十名
  • php 建设网站制作/线下课程seo
  • 首饰网站模板/排名软件下载
  • 网上做论文的网站有哪些/个人如何做seo推广
  • 辽宁省交通建设投资集团官方网站/江苏营销型网站建设
  • 网站改版 网站存在问题/产品营销推广的方案
  • 秦皇岛手机网站制作多少钱/北京有限公司
  • 新乡 网站开发/网站推广的主要方式
  • 优惠的网站建设/营销策略分析论文
  • 网站编辑做图片用什么不同/百度指数app下载
  • wordpress账户打通/seo优化人员
  • 阜阳网站建设/关键词搜索次数查询
  • 建立一个属于自己的网站/网络推广渠道分类
  • 企业网站模板源码起名/企业营销案例
  • 邢台集团网站建设费用/武汉百度推广seo
  • 建设艺术网站需要多少钱/百度识图搜索网页版
  • 广东梅州兴宁做网站公司/汕头搜索引擎优化服务
  • 柳州建设网app/seo sem
  • 哪些网站做外贸效果好/手机seo快速排名
  • 免费logo在线制作u钙网/seo关键词外包