效果图
- 数据根据我写的脚本,默认保存在主路径下的image_featuremap\radar_featuremap文件夹下:
融合特征图
雷达特征图
特征输出模型注册
- 在主文件夹下新建data_visualization.py文件:
import argparse
import os
import sys
import keras
import keras.preprocessing.image
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from keras.models import Model
import keras.preprocessing.image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import cv2
import progressbar
def main():
FILE_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default=os.path.join(FILE_DIRECTORY, "configs/temp.cfg"))
parser.add_argument('--model', type=str, default="./saved_models/convwith10epochs.h5")
parser.add_argument('--st', type=float, default=0.3)
parser.add_argument('--render', default=False, action='store_true')
parser.add_argument('--eval_from_detection_pickle', default=False, action='store_true')
parser.add_argument('--gpu', type=str, default='0')
args = parser.parse_args()
cfg = get_config(args.config)
model_name = args.config.split('/')[-1]
model_name = model_name.split('.')[0]
cfg.model_name = model_name
backbone = architectures.backbone(cfg.network)
model = keras.models.load_model(args.model, custom_objects=backbone.custom_objects)
radar_layers = [model.get_layer(name).output for name in ['rad_block1_pool', 'rad_block2_pool', 'rad_block3_pool','rad_block4_pool','rad_block5_pool','rad_block6_pool','rad_block7_pool']]
vision_layers = [model.get_layer(name).output for name in ['block1_conv2', 'block2_conv2', 'block3_conv2', 'block4_conv2', 'block5_conv2']]
- 定义特征输出模型:我定义了新的model,把原来的数据作为输入,将模型需要可视化的层作为输出,方便后面可视化
layer_model = Model(inputs=model.input, outputs=radar_layers)
image_layer_model = Model(inputs = model.input, outputs = vision_layers)
提取数据
- 这里根据generator这个数据加载器,遍历并提取所有的数据到inputs列表中,方便后面输入
generators = create_generators(cfg, backbone)
test_generator = generators[2]
inputs = []
for i in progressbar.progressbar(range(test_generator.size())):
input, _ = test_generator.compute_input_output([i])
inputs.append(input)
print(len(inputs), inputs[0].shape)
提取中间特征并批量可视化
for i in range(len(inputs)):
img_idx = i
data = inputs[img_idx]
feature_map = image_layer_model.predict(data)
source_img = np.mean(np.squeeze(data)[:,:,:3],axis=2)
plt.subplot(3,3,1)
plt.imshow(source_img)
for i in progressbar.progressbar(range(len(feature_map))):
layer_map = np.squeeze(feature_map[i])
plt.subplot(3,3,i+2)
plt.imshow(np.mean(layer_map, axis=2))
plt.title('{}'.format(img_idx))
if not os.path.exists(os.path.join(FILE_DIRECTORY, 'image_featuremap')):
os.mkdir(os.path.join(FILE_DIRECTORY, 'image_featuremap'))
plt.savefig(os.path.join(FILE_DIRECTORY, 'image_featuremap\\{}'.format(img_idx)))
for i in range(len(inputs)):
img_idx = i
data = inputs[img_idx]
feature_map = layer_model.predict(data)
source_img = np.mean(np.squeeze(data)[:,:,:3],axis=2)
plt.subplot(3,3,1)
plt.imshow(source_img)
for i in progressbar.progressbar(range(len(feature_map))):
layer_map = np.squeeze(feature_map[i])
plt.subplot(3,3,i+2)
plt.imshow(np.mean(layer_map, axis=2))
plt.title('{}'.format(img_idx))
if not os.path.exists(os.path.join(FILE_DIRECTORY, 'radar_featuremap')):
os.mkdir(os.path.join(FILE_DIRECTORY, 'radar_featuremap'))
plt.savefig(os.path.join(FILE_DIRECTORY, 'radar_featuremap\\{}'.format(img_idx)))
池化与卷积对雷达滤波的影响
- 可以看出,我将雷达的pooling对雷达处理的方式改编为卷积,在特征图可视化中,卷积(左边)能够对图中的杂点进行滤波,集中在车辆这种前景物体上。
|