Darknet浅层可视化教程

说明

针对YOLO官方提供的c语言版的darknet进行了修改,添加了一些函数,进行可视化处理。

建议使用visual studio code进行代码的跟踪和调试。

可视化内容是针对一下命令,对一张图片进行可视化:

./darknet detector test cfg/voc.data data/yolov3-voc.cfg backup/yolov3-voc_40000.weights

处理步骤

  • 入口: darknet.c的main文件,找到以下声明:
} else if (0 == strcmp(argv[1], "detector")){
run_detector(argc, argv);
  • 进入run_detector函数,在detector.c文件中找到以下代码:
if(0==strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, outfile, fullscreen);
else if(0==strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear);
else if(0==strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights, outfile);
else if(0==strcmp(argv[2], "valid2")) validate_detector_flip(datacfg, cfg, weights, outfile);
else if(0==strcmp(argv[2], "recall")) validate_detector_recall(datacfg, cfg, weights);
else if(0==strcmp(argv[2], "demo")) {
  • 找到第二个参数“test”对应的函数: test_detector,进入该函数进行修改:
while(1){
if(filename){
strncpy(input, filename, 256);
image im = load_image_color(input,0,0);
image sized = letterbox_image(im, net->w, net->h);
layer l = net->layers[net->n-1]; float *X = sized.data;
time=what_time_is_it_now();
network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time);
int nboxes = 0;
detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
  • 很明显,network_predict函数就是用来让图片在网络中forward_gpu一下,然后得到结果,所以进入该函数:
float *network_predict(network *net, float *input)
{
network orig = *net;
net->input = input;
net->truth = 0;
net->train = 0;
net->delta = 0;
forward_network(net);
float *out = net->output;
*net = orig;
return out;
}
  • 再继续找核心函数forward_network(net)
void forward_network(network *netp)
{
#ifdef GPU
if(netp->gpu_index >= 0){
forward_network_gpu(netp);
return;
}
#endif
network net = *netp;
printf("haha, net layer number: %d\n",net.n);
int i;
for(i = 0; i < net.n; ++i){
//image imi = get_network_image(netp);
//save_image(imi,"thiisisatest");
net.index = i;
layer l = net.layers[i];
if(l.delta){
fill_cpu(l.outputs * l.batch, 0, l.delta, 1);
}
l.forward(l, net);
net.input = l.output;
if(l.truth) {
net.truth = l.output;
}
}
calc_network_cost(netp);
}
  • 由于本项目是在有GPU支持的情况下,所以会执行#ifdef和#endif之间的内容,继续找forward_network_gpu()函数。
void forward_network_gpu(network *netp)
{
network net = *netp;
cuda_set_device(net.gpu_index);
cuda_push_array(net.input_gpu, net.input, net.inputs*net.batch);
if(net.truth){
cuda_push_array(net.truth_gpu, net.truth, net.truths*net.batch);
} int i;
for(i = 0; i < net.n; ++i){
net.index = i;
layer l = net.layers[i];
if(l.delta_gpu){
fill_gpu(l.outputs * l.batch, 0, l.delta_gpu, 1);
}
l.forward_gpu(l, net);
net.input_gpu = l.output_gpu;
net.input = l.output; if(l.truth) {
net.truth_gpu = l.output_gpu;
net.truth = l.output;
}
//这个函数是新加的,用来得到图片保存图片
image tmp = get_network_cow_image_layer(&net,i);
}
pull_network_output(netp);
calc_network_cost(netp);
}
  • 该函数在network.c文件中声明,需要在darknet.h中提前声明该函数:
image get_network_cow_image_layer(network *net, int i);

具体内容如下:

image get_network_cow_image_layer(network *net, int i)
{
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 float_to_cow_image(l.out_w,l.out_h,l.out_c,l.output,i);
}
image def = {0};
return def;
}
  • 可以发现,float_to_cow_image也是新加的函数,也需要加入到darknet.h中去:
image get_network_cow_image_layer(network *net, int i);

该函数具体内容如下(参考get_network_image_layer进行修改,添加i是为了识别这是第几层的可视化,用于保存文件):

/*****************************************************
*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/learner/darknet/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 = 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;
}

重新make,运行命令,会在指定目录下得到txt文件,之后的操作就是需要将txt文件可视化为图片。

使用python可视化txt文件

使用python读取图片有一个好处,就是可以将灰度图转化为热力图,这样更容易观察,否则会认为生成了一系列噪音点。

具体代码如下:(需要matplotlib,PIL, numpy库)

#!/home/learner/miniconda3/bin/python
# -*- coding: utf-8 -*-
"""
Spyder Editor This is a temporary script file.
"""
import os
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image def process(filepath,outpath):
for fileName in os.listdir(filepath):
print(fileName)
print(filepath+"/"+fileName)
a=np.loadtxt(filepath+"/"+fileName)
im = Image.fromarray(np.uint8(a))
#tmp_img = plt.imshow(im)
#tmp_img.set_cmap('hsv')
#plt.show()
print(im.size)
#im.set_cmap('hot') #plt.figure(figsize=(15,15))
plt.title(fileName)
plt.imshow(im),plt.axis('off')
im.save(outpath+"/"+fileName[:-4]+".jpg")
#plt.savefig(outpath+"/"+fileName[:-4]+".jpg",bbox_inches="tight",transparent=True,pad_inches=0) if __name__ == "__main__":
outpath = "/home/learner/darknet/feature_pic"
filepath="/home/learner/darknet/feature_txt"
process(filepath,outpath)

Darknet卷基层浅层特征可视化教程的更多相关文章

  1. caffe net 可视化工具,,层特征可视化

    1.只用网络在线结构绘制可视化网络模型 http://ethereon.github.io/netscope/#/editor 将对应的网络输入到里面,然后按shift+enter即可查看对应的网络结 ...

  2. 结合浅层高层特征的paper总结

    1.ION:在conv3.conv4.conv5和context features上分别进行roi_pooling,在channel那一维进行concat 2.Hypernet:在较浅层max_poo ...

  3. 深度学习笔记之关于基本思想、浅层学习、Neural Network和训练过程(三)

    不多说,直接上干货! 五.Deep Learning的基本思想 假设我们有一个系统S,它有n层(S1,…Sn),它的输入是I,输出是O,形象地表示为: I =>S1=>S2=>….. ...

  4. 矩池云 | 搭建浅层神经网络"Hello world"

    作为图像识别与机器视觉界的 "hello world!" ,MNIST ("Modified National Institute of Standards and Te ...

  5. deeplearning.ai 神经网络和深度学习 week3 浅层神经网络 听课笔记

    1. 第i层网络 Z[i] = W[i]A[i-1] + B[i],A[i] = f[i](Z[i]). 其中, W[i]形状是n[i]*n[i-1],n[i]是第i层神经元的数量: A[i-1]是第 ...

  6. MLDS笔记:浅层结构 vs 深层结构

    深度学习出现之前,机器学习方面的开发者通常需要仔细地设计特征.设计算法,且他们在理论上常能够得知这样设计的实际表现如何: 深度学习出现后,开发者常先尝试实验,有时候实验结果常与直觉相矛盾,实验后再找出 ...

  7. 神经网络:caffe特征可视化的代码例子

    caffe特征可视化的代码例子 不少读者看了我前面两篇文章 总结一下用caffe跑图片数据的研究流程 deep learning实践经验总结2--准确率再次提升,到达0.8.再来总结一下 之后.想知道 ...

  8. Tensorflow MNIST浅层神经网络的解释和答复

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51416540 看到之前的一篇博文:深入 ...

  9. deeplearning.ai 神经网络和深度学习 week3 浅层神经网络

    1. 第i层网络 Z[i] = W[i]A[i-1] + B[i],A[i] = f[i](Z[i]). 其中, W[i]形状是n[i]*n[i-1],n[i]是第i层神经元的数量: A[i-1]是第 ...

随机推荐

  1. DeepMind提出空间语言集成模型SLIM,有效编码自然语言的空间关系

    前不久,DeepMind 提出生成查询网络 GQN,具备从 2D 画面到 3D 空间的转换能力.近日.DeepMind 基于 GQN 提出一种新模型.可以捕捉空间关系的语义(如 behind.left ...

  2. Unity 补充安装

    当需要下载 安装Unity之时没勾选的一些组件时, 1.去Unity官网点开Unity旧版本 2.找到你的Unity版本,然后只要下载Unity安装程序 3.点开安装程序,去掉已安装组件的勾选,勾选你 ...

  3. 远程调用Spark平台中的程序

    用scala语言,开发好了在spark平台上可以一直运行的机器学习模型 现在有个需求: 要远程调用该模型的一些方法并获取结果 那么可以使用jetty在服务器端主节点占用一个端口然后对外提供http服务 ...

  4. ML实践详细经典教程----用例图、顺序图、状态图、类图、包图、协作图

    面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language?),这篇课程的目的是展示出UML ...

  5. Java-使用IO流对大文件进行分割和分割后的合并

    有的时候我们想要操作的文件很大,比如:我们想要上传一个大文件,但是收到上传文件大小的限制,无法上传,这是我们可以将一个大的文件分割成若干个小文件进行操作,然后再把小文件还原成源文件.分割后的每个小文件 ...

  6. windows下编译和安装boost库

    boost是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库. 获取方式 boost提供源码形式的安装包,可以从boost官方网站下载,目前最新版本是1.59.0. 本机上正好有boos ...

  7. VMware coding Challenge:Date of Weekday

    这道题再次证明了这种细节的题目,画个图容易搞清楚 import java.util.Scanner; public class Solution2 { static int DateOfWeekday ...

  8. Lower Power with CPF(一)

    CPF(Common Power Format):cadence推出的一种在设计中描述低功耗设计的文件.完全按Tcl的语言格式来定义. CPF文件在整个前端后端的过程中,需要的部分不一样,所以CPF文 ...

  9. 《Kalchbrenner N, Grefenstette E, Blunsom P. A convolutional neural network for modelling sentences》

    Kalchbrenner’s Paper Kal的这篇文章引用次数较高,他提出了一种名为DCNN(Dynamic Convolutional Neural Network)的网络模型,在上一篇(Kim ...

  10. uva11419 二分图--最小覆盖=最大匹配

    大白书355 // UVa11419 SAM I AM // Rujia Liu #include <cstdio> #include <cstring> #include & ...