下载darknet
git clone https://github.com/AlexeyAB/darknet.git
终端输入:
sudo vim Makefile
按“a”进入插入模式,将如下内容更改:
GPU=1
CUDNN=1
CUDNN_HALF=0
OPENCV=1
按“esc”,再输入“:wq!”
在src/network_kernels.cu内,添加内容
1.找到void forward_network_gpu(network net, network_state state)函数,在对应位置添加如下内容:
if(net.wait_stream)
cudaStreamSynchronize(get_cuda_stream());
state.input = l.output_gpu;
bool show_featuremap = true;
if (show_featuremap){
//这个函数是新加的,用来得到图片保存图片
image tmp = get_network_cow_image_layer(&net,i);
}
2.添加如下函数:
/*****************************************************
*func: 主要是为了将output结果能够映射到0-255区间(初始化,使用sigmoid函数进行归一化,),便于进行可视化操作。 将所有维度合成到一个维度,然后取平均,×255,便于查看
*****************************************************/
image float_to_cow_image(int w, int h, int c, float *data,int ai)
{
char tp[1000];
//保存文件到特定文件夹(feature_txt)中并根据ai大小命名
sprintf(tp,"/home/hui/feature_txt/out_%d.txt",ai);
FILE * stream = fopen(tp,"w+");
//创建一个1维的空图片
image out = make_empty_image(w,h,1);
int i, j;
//设计一个数组保存该图片内容
float *tempData = (float*)calloc(w*h,sizeof(float));
//初始化
for(i = 0 ; i < w*h ; i++)
{
tempData[i] = 0;
}
//归一化,sigmoid
for(i = 0 ; i < w*h*c ; i++)
{
data[i] = 1.0/(1+exp(-1*data[i]));
}
//合并通道
for(i = 0 ; i < w*h ; i++)
{
for(j = 0 ; j < c ; j++)
{
tempData[i] += data[i+j*w*h];
}
}
//保存到文件
for(i = 0 ; i < w*h; i++)
{
tempData[i] /= c;
tempData[i] *= 255;
fprintf(stream," %f",tempData[i]);
if((i+1)%w==0)
fprintf(stream,"\n");
}
//关闭文件流
fclose(stream);
out.data = tempData;
return out;
}
image get_network_cow_image_layer(network *net, int i)
{
// image a;
layer l = net->layers[i];
#ifdef GPU
cuda_pull_array(l.output_gpu, l.output, l.outputs);
#endif
printf("w:%d,h:%d,c:%d\n",l.out_w,l.out_h,l.out_c);
if (l.out_w && l.out_h && l.out_c){
// return a;
return float_to_cow_image(l.out_w,l.out_h,l.out_c,l.output,i);
}
image def = {0};
return def;
}
在include/darknet.h中,添加内容
LIB_API image float_to_cow_image(int w, int h, int c, float *data,int ai);
LIB_API image get_network_cow_image_layer(network *net, int i);
在/home/hui/创建三个文件夹
终端输入:
make
完成编译后,输入如下命令,生成文件在feature_txt中
./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights data/eagle.jpg
在darknet根目录创建1.py,并执行。生成内容可在feature_pic和feature_joint文件夹内查看
import os
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
outpath = "/home/hui/feature_pic/"
outpath_joint = "/home/hui/feature_joint/"
filepath = "/home/hui/feature_txt/"
final_size = 1024
# https://www.cnblogs.com/pprp/p/10146355.html
def process(filepath, outpath):
feature_map_list = []
for fileName in os.listdir(filepath):
a = np.loadtxt(filepath + "/" + fileName)
im = Image.fromarray(np.uint8(a))
feature_map_list.append(im)
plt.title(fileName)
plt.imshow(im), plt.axis('off')
im.save(outpath + "/" + fileName[:-4] + ".jpg")
return feature_map_list
# 定义图像拼接函数
# https://blog.youkuaiyun.com/lxk2017/article/details/97911371
# 输入10张图片,输出4*4的方格,每一个方格放入相应图片
def image_compose1(feature_map_list,IMAGE_ROW,IMAGE_COLUMN, outpath_joint):
IMAGE_SIZE = feature_map_list[0].size[0]
to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE)) # 创建一个新图
for x in range(IMAGE_ROW):
for y in range(IMAGE_COLUMN):
img_index = x * IMAGE_ROW + y
if img_index < len(feature_map_list):
from_image = feature_map_list[img_index]
to_image.paste(from_image, (x * IMAGE_SIZE, y * IMAGE_SIZE))
to_image = to_image.resize((final_size, final_size), Image.ANTIALIAS)
saveimg_path = str(IMAGE_SIZE) + "_" + str(IMAGE_COLUMN) + ".jpg"
to_image.save(os.path.join(outpath_joint, saveimg_path)) # 保存新图
# 输入162张特征图,方格的大小
def image_compose(feature_map_list,feature_index,IMAGE_ROW_t,IMAGE_COLUMN_t, outpath_joint):
for i in range(len(feature_index)-1):
feature_map_list1 = []
for j in range(feature_index[i], feature_index[i + 1]):
feature_map_list1.append(feature_map_list[j])
image_compose1(feature_map_list1, IMAGE_ROW_t[i], IMAGE_COLUMN_t[i], outpath_joint)
if __name__ == "__main__":
feature_map_list = process(filepath, outpath)
IMAGES_FORMAT = ['.jpg', '.JPG'] # 图片格式
feature_index = [0, 10, 23, 54, 85, 117, 127, 140, 151, 161]
IMAGE_ROW_t = [1, 4, 4, 6, 6, 6, 4, 4, 4, 4] # 图片间隔,也就是合并成一张图后,一共有几行
IMAGE_COLUMN_t = [1, 4, 4, 6, 6, 6, 4, 4, 4, 4] # 图片间隔,也就是合并成一张图后,一共有几列
image_compose(feature_map_list,feature_index,IMAGE_ROW_t,IMAGE_COLUMN_t, outpath_joint) # 调用函数