From 759e07d35c9d5ccc605647eaf09f20bdeb875a2a Mon Sep 17 00:00:00 2001 From: Zengyf-CVer <41098760+Zengyf-CVer@users.noreply.github.com> Date: Thu, 18 Nov 2021 20:38:21 +0800 Subject: [PATCH 1/6] Add feature map to save npy files Add feature map to save npy files,export npy files with 32 feature maps per layer. --- utils/plots.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/utils/plots.py b/utils/plots.py index 9919e4d9d88f..214e3304ae5e 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -133,7 +133,8 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec batch, channels, height, width = x.shape # batch, channels, height, width if height > 1 and width > 1: f = f"stage{stage}_{module_type.split('.')[-1]}_features.png" # filename - + f2 = f[:-4] # npy name + blocks = torch.chunk(x[0].cpu(), channels, dim=0) # select batch index 0, block by channels n = min(n, channels) # number of plots fig, ax = plt.subplots(math.ceil(n / 8), 8, tight_layout=True) # 8 rows x n/8 cols @@ -142,7 +143,11 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec for i in range(n): ax[i].imshow(blocks[i].squeeze()) # cmap='gray' ax[i].axis('off') - + + block_arrary = blocks[i].squeeze().numpy() # tensor2numpy + if not os.path.exists(f'{save_dir}/{f2}'): # mkdir npy + os.makedirs(f'{save_dir}/{f2}') + np.save(f'{save_dir}/{f2}/{f2}_{i}.npy', block_arrary) # npy save print(f'Saving {save_dir / f}... ({n}/{channels})') plt.savefig(save_dir / f, dpi=300, bbox_inches='tight') plt.close() From 631f9e75379476db241b280dca36b07535851a61 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 18 Nov 2021 13:06:49 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- utils/plots.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/plots.py b/utils/plots.py index 214e3304ae5e..9ad2f72ad383 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -134,7 +134,7 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec if height > 1 and width > 1: f = f"stage{stage}_{module_type.split('.')[-1]}_features.png" # filename f2 = f[:-4] # npy name - + blocks = torch.chunk(x[0].cpu(), channels, dim=0) # select batch index 0, block by channels n = min(n, channels) # number of plots fig, ax = plt.subplots(math.ceil(n / 8), 8, tight_layout=True) # 8 rows x n/8 cols @@ -143,7 +143,7 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec for i in range(n): ax[i].imshow(blocks[i].squeeze()) # cmap='gray' ax[i].axis('off') - + block_arrary = blocks[i].squeeze().numpy() # tensor2numpy if not os.path.exists(f'{save_dir}/{f2}'): # mkdir npy os.makedirs(f'{save_dir}/{f2}') From a6a0c76c03f1079ba6fe2dcfb4b2784fa07613ea Mon Sep 17 00:00:00 2001 From: Zengyf-CVer <41098760+Zengyf-CVer@users.noreply.github.com> Date: Thu, 18 Nov 2021 21:45:11 +0800 Subject: [PATCH 3/6] Update plots.py --- utils/plots.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/plots.py b/utils/plots.py index 9ad2f72ad383..9e93c43cd89f 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -144,10 +144,10 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec ax[i].imshow(blocks[i].squeeze()) # cmap='gray' ax[i].axis('off') - block_arrary = blocks[i].squeeze().numpy() # tensor2numpy - if not os.path.exists(f'{save_dir}/{f2}'): # mkdir npy + block_arrary = blocks[i].squeeze().numpy() # tensor2numpy + if not os.path.exists(f'{save_dir}/{f2}'): # mkdir npy os.makedirs(f'{save_dir}/{f2}') - np.save(f'{save_dir}/{f2}/{f2}_{i}.npy', block_arrary) # npy save + np.save(f'{save_dir}/{f2}/{f2}_{i}.npy', block_arrary) # npy save print(f'Saving {save_dir / f}... ({n}/{channels})') plt.savefig(save_dir / f, dpi=300, bbox_inches='tight') plt.close() From 78dd69b22d9c7b4216f3403a236ce560f2dfd239 Mon Sep 17 00:00:00 2001 From: Zengyf-CVer <41098760+Zengyf-CVer@users.noreply.github.com> Date: Sat, 20 Nov 2021 12:19:34 +0800 Subject: [PATCH 4/6] Update plots.py --- utils/plots.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/utils/plots.py b/utils/plots.py index 9e93c43cd89f..c945276fcef2 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -133,7 +133,8 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec batch, channels, height, width = x.shape # batch, channels, height, width if height > 1 and width > 1: f = f"stage{stage}_{module_type.split('.')[-1]}_features.png" # filename - f2 = f[:-4] # npy name + npy_name = f[:-4] # npy name + block_list = [] # block tmp list blocks = torch.chunk(x[0].cpu(), channels, dim=0) # select batch index 0, block by channels n = min(n, channels) # number of plots @@ -145,9 +146,9 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec ax[i].axis('off') block_arrary = blocks[i].squeeze().numpy() # tensor2numpy - if not os.path.exists(f'{save_dir}/{f2}'): # mkdir npy - os.makedirs(f'{save_dir}/{f2}') - np.save(f'{save_dir}/{f2}/{f2}_{i}.npy', block_arrary) # npy save + block_list.append(block_arrary.tolist()) # list add + block_arrarys = np.array(block_list) # list2arrary + np.save(f'{save_dir}/{npy_name}.npy', block_arrarys) # npy save print(f'Saving {save_dir / f}... ({n}/{channels})') plt.savefig(save_dir / f, dpi=300, bbox_inches='tight') plt.close() From 6c4267484ba7160b6602bfab0d29558af78391a5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 21 Nov 2021 18:28:50 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorial.ipynb b/tutorial.ipynb index 9440ca8b1788..4ce87c75aa64 100644 --- a/tutorial.ipynb +++ b/tutorial.ipynb @@ -1104,4 +1104,4 @@ "outputs": [] } ] -} \ No newline at end of file +} From 8509c523b0464069cfdf34e708b55684ad9c1ef8 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sun, 21 Nov 2021 19:53:38 +0100 Subject: [PATCH 6/6] Update plots.py --- utils/plots.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/utils/plots.py b/utils/plots.py index c945276fcef2..69037ee9af70 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -132,9 +132,7 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec if 'Detect' not in module_type: batch, channels, height, width = x.shape # batch, channels, height, width if height > 1 and width > 1: - f = f"stage{stage}_{module_type.split('.')[-1]}_features.png" # filename - npy_name = f[:-4] # npy name - block_list = [] # block tmp list + f = save_dir / f"stage{stage}_{module_type.split('.')[-1]}_features.png" # filename blocks = torch.chunk(x[0].cpu(), channels, dim=0) # select batch index 0, block by channels n = min(n, channels) # number of plots @@ -145,13 +143,10 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec ax[i].imshow(blocks[i].squeeze()) # cmap='gray' ax[i].axis('off') - block_arrary = blocks[i].squeeze().numpy() # tensor2numpy - block_list.append(block_arrary.tolist()) # list add - block_arrarys = np.array(block_list) # list2arrary - np.save(f'{save_dir}/{npy_name}.npy', block_arrarys) # npy save - print(f'Saving {save_dir / f}... ({n}/{channels})') - plt.savefig(save_dir / f, dpi=300, bbox_inches='tight') + print(f'Saving {f}... ({n}/{channels})') + plt.savefig(f, dpi=300, bbox_inches='tight') plt.close() + np.save(str(f.with_suffix('.npy')), x[0].cpu().numpy()) # npy save def hist2d(x, y, n=100):