MCPcopy Create free account
hub / github.com/M-Nauta/ProtoTree / upsample_local

Function upsample_local

util/visualize_prediction.py:22–86  ·  view source on GitHub ↗
(tree: ProtoTree,
                 sample: torch.Tensor,
                 sample_dir: str,
                 folder_name: str,
                 img_name: str,
                 decision_path: list,
                 args: argparse.Namespace)

Source from the content-addressed store, hash-verified

20from prototree.node import Node
21
22def upsample_local(tree: ProtoTree,
23 sample: torch.Tensor,
24 sample_dir: str,
25 folder_name: str,
26 img_name: str,
27 decision_path: list,
28 args: argparse.Namespace):
29
30 dir = os.path.join(os.path.join(os.path.join(args.log_dir, folder_name),img_name), args.dir_for_saving_images)
31 if not os.path.exists(dir):
32 os.makedirs(dir)
33 with torch.no_grad():
34 _, distances_batch, _ = tree.forward_partial(sample)
35 sim_map = torch.exp(-distances_batch[0,:,:,:]).cpu().numpy()
36 for i, node in enumerate(decision_path[:-1]):
37 decision_node_idx = node.index
38 node_id = tree._out_map[node]
39 img = Image.open(sample_dir)
40 x_np = np.asarray(img)
41 x_np = np.float32(x_np)/ 255
42 if x_np.ndim == 2: #convert grayscale to RGB
43 x_np = np.stack((x_np,)*3, axis=-1)
44
45 img_size = x_np.shape[:2]
46 similarity_map = sim_map[node_id]
47
48 rescaled_sim_map = similarity_map - np.amin(similarity_map)
49 rescaled_sim_map= rescaled_sim_map / np.amax(rescaled_sim_map)
50 similarity_heatmap = cv2.applyColorMap(np.uint8(255*rescaled_sim_map), cv2.COLORMAP_JET)
51 similarity_heatmap = np.float32(similarity_heatmap) / 255
52 similarity_heatmap = similarity_heatmap[...,::-1]
53 plt.imsave(fname=os.path.join(dir,'%s_heatmap_latent_similaritymap.png'%str(decision_node_idx)), arr=similarity_heatmap, vmin=0.0,vmax=1.0)
54
55 upsampled_act_pattern = cv2.resize(similarity_map,
56 dsize=(img_size[1], img_size[0]),
57 interpolation=cv2.INTER_CUBIC)
58 rescaled_act_pattern = upsampled_act_pattern - np.amin(upsampled_act_pattern)
59 rescaled_act_pattern = rescaled_act_pattern / np.amax(rescaled_act_pattern)
60 heatmap = cv2.applyColorMap(np.uint8(255*rescaled_act_pattern), cv2.COLORMAP_JET)
61 heatmap = np.float32(heatmap) / 255
62 heatmap = heatmap[...,::-1]
63 overlayed_original_img = 0.5 * x_np + 0.2 * heatmap
64 plt.imsave(fname=os.path.join(dir,'%s_heatmap_original_image.png'%str(decision_node_idx)), arr=overlayed_original_img, vmin=0.0,vmax=1.0)
65
66 # save the highly activated patch
67 masked_similarity_map = np.ones(similarity_map.shape)
68 masked_similarity_map[similarity_map < np.max(similarity_map)] = 0 #mask similarity map such that only the nearest patch z* is visualized
69
70 upsampled_prototype_pattern = cv2.resize(masked_similarity_map,
71 dsize=(img_size[1], img_size[0]),
72 interpolation=cv2.INTER_CUBIC)
73 plt.imsave(fname=os.path.join(dir,'%s_masked_upsampled_heatmap.png'%str(decision_node_idx)), arr=upsampled_prototype_pattern, vmin=0.0,vmax=1.0)
74
75 high_act_patch_indices = find_high_activation_crop(upsampled_prototype_pattern, args.upsample_threshold)
76 high_act_patch = x_np[high_act_patch_indices[0]:high_act_patch_indices[1],
77 high_act_patch_indices[2]:high_act_patch_indices[3], :]
78 plt.imsave(fname=os.path.join(dir,'%s_nearest_patch_of_image.png'%str(decision_node_idx)), arr=high_act_patch, vmin=0.0,vmax=1.0)
79

Callers 1

gen_pred_visFunction · 0.85

Calls 3

imsave_with_bboxFunction · 0.90
forward_partialMethod · 0.80

Tested by

no test coverage detected