Spaces:
Sleeping
Sleeping
Rename app.py to appxxxx.py
Browse files- app.py → appxxxx.py +39 -25
app.py → appxxxx.py
RENAMED
|
@@ -149,50 +149,64 @@ demo.launch()
|
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
"""
|
| 154 |
-
生成
|
| 155 |
"""
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
heatmap = np.maximum(heatmap.detach().cpu().numpy(), 0)
|
| 164 |
heatmap /= np.max(heatmap)
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
heatmap[heatmap < 0.5] = 0 # 只保留高信心区域
|
| 168 |
-
|
| 169 |
return heatmap
|
| 170 |
|
| 171 |
|
| 172 |
-
|
|
|
|
| 173 |
"""
|
| 174 |
-
|
| 175 |
"""
|
| 176 |
-
# 调整热力图大小
|
| 177 |
heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))
|
| 178 |
heatmap = np.uint8(255 * heatmap)
|
|
|
|
| 179 |
|
| 180 |
-
# 应用
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
# 创建一个只有热力图区域的蒙版
|
| 184 |
-
mask = heatmap > 0
|
| 185 |
-
|
| 186 |
-
# 创建叠加图像
|
| 187 |
superimposed_img = image.copy()
|
| 188 |
superimposed_img[mask] = superimposed_img[mask] * (1 - intensity) + heatmap[mask] * intensity
|
| 189 |
|
| 190 |
superimposed_img = np.clip(superimposed_img, 0, 255).astype(np.uint8)
|
| 191 |
-
superimposed_img = Image.fromarray(superimposed_img)
|
| 192 |
-
|
| 193 |
return superimposed_img
|
| 194 |
|
| 195 |
|
|
|
|
| 196 |
def predict(image):
|
| 197 |
"""
|
| 198 |
預測並生成熱力圖。
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
+
import torch.nn.functional as F
|
| 153 |
+
|
| 154 |
+
def generate_heatmap(model, last_conv_layer, input_tensor, pred_class):
|
| 155 |
"""
|
| 156 |
+
使用 Grad-CAM 生成更精确的热力图。
|
| 157 |
"""
|
| 158 |
+
# 获取最后卷积层的特征图和模型输出
|
| 159 |
+
model.eval()
|
| 160 |
+
features = []
|
| 161 |
+
def hook_function(module, input, output):
|
| 162 |
+
features.append(output)
|
| 163 |
+
|
| 164 |
+
hook = last_conv_layer.register_forward_hook(hook_function)
|
| 165 |
+
model_output = model(input_tensor)
|
| 166 |
+
hook.remove()
|
| 167 |
+
|
| 168 |
+
# 获取预测类别的得分
|
| 169 |
+
class_score = model_output[:, pred_class]
|
| 170 |
+
|
| 171 |
+
# 计算梯度
|
| 172 |
+
model.zero_grad()
|
| 173 |
+
class_score.backward(retain_graph=True)
|
| 174 |
+
|
| 175 |
+
# 获取梯度和特征图
|
| 176 |
+
gradients = features[0].grad # 获取最后卷积层的梯度
|
| 177 |
+
pooled_gradients = torch.mean(gradients, dim=[0, 2, 3]) # 全局平均池化
|
| 178 |
+
|
| 179 |
+
# 获取特征图,并应用梯度加权
|
| 180 |
+
features = features[0]
|
| 181 |
+
for i in range(features.shape[1]):
|
| 182 |
+
features[:, i, :, :] *= pooled_gradients[i]
|
| 183 |
+
|
| 184 |
+
# 创建热力图
|
| 185 |
+
heatmap = torch.mean(features, dim=1).squeeze()
|
| 186 |
heatmap = np.maximum(heatmap.detach().cpu().numpy(), 0)
|
| 187 |
heatmap /= np.max(heatmap)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
return heatmap
|
| 189 |
|
| 190 |
|
| 191 |
+
|
| 192 |
+
def overlay_heatmap(image, heatmap, intensity=0.5, threshold=0.5):
|
| 193 |
"""
|
| 194 |
+
优化热力图叠加逻辑。
|
| 195 |
"""
|
|
|
|
| 196 |
heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))
|
| 197 |
heatmap = np.uint8(255 * heatmap)
|
| 198 |
+
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
| 199 |
|
| 200 |
+
# 只在高信心区域应用热力图
|
| 201 |
+
mask = heatmap > np.max(heatmap) * threshold
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
superimposed_img = image.copy()
|
| 203 |
superimposed_img[mask] = superimposed_img[mask] * (1 - intensity) + heatmap[mask] * intensity
|
| 204 |
|
| 205 |
superimposed_img = np.clip(superimposed_img, 0, 255).astype(np.uint8)
|
|
|
|
|
|
|
| 206 |
return superimposed_img
|
| 207 |
|
| 208 |
|
| 209 |
+
|
| 210 |
def predict(image):
|
| 211 |
"""
|
| 212 |
預測並生成熱力圖。
|