说明
本篇文章针对AB版DarkNet源代码进行了修改,添加了一些函数,进行全通道特征图可视化。主要针对YOLO-v4推理过程中,对中间计算结果的特征图数据进行转换,将转换结果用表示成图片进行保存。
DarkNet源代码修改
在network_kernel.cu文件中加入以下代码:
image float_to_cow_image(int w, int h, int c, float *data, int ai)
{
char tp[1000];
//保存文件到特定文件夹(feature_txt)中并根据ai大小命名
//sprintf(tp, "E:\\yolov4\\feature_save\\out_%d.txt", ai);
//FILE * stream = fopen(tp, "w+");
//创建一个1维的空图片
image out = make_empty_image(w, h, 1);
for (int k = 0; k < c; k++) {
if (ai < 10)
sprintf(tp, "E:\\yolov4\\feature_pic\\out_000%d", ai);
else
sprintf(tp, "E:\\yolov4\\feature_pic\\out_00%d", ai);
mkdir(tp);
if (ai < 10)
sprintf(tp, "E:\\yolov4\\feature_txt\\out_000%d", ai);
else
sprintf(tp, "E:\\yolov4\\feature_txt\\out_00%d", ai);
mkdir(tp);
if (ai < 10)
sprintf(tp, "E:\\yolov4\\feature_txt\\out_000%d\\out_000%d_%d.txt", ai, ai, k);
else
sprintf(tp, "E:\\yolov4\\feature_txt\\out_00%d\\out_00%d_%d.txt", ai, ai, k);
FILE * stream = fopen(tp, "w+");
int i, j;
//设计一个数组保存该图片内容
float * tempData = (float*)calloc(w*h, sizeof(float));
//初始化
for (i = 0; i < w*h; i++)
{
tempData[i] = 0;
}
for (i = 0; i < w*h; i++)
{
tempData[i] = 1.0 / (1 + exp(-1 * data[k*w*h + i]));
}
//保存到文件
for (i = 0; i < w*h; i++)
{
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;
}
在network_kernel.cu文件的forward_network_gpu函数中加入如下代码:
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);
}
重新生成darknet.exe
进行推理
新建目录,准备文件夹
进入darknet.exe所在文件夹,执行cmd,输入命令:
darknet.exe detector test cfg/coco.data yolov4.cfg yolov4.weights -i 0 -thresh 0.25 -ext_output dog.jpg
pause
推理执行中,显示如下:
E:\yolov4\darknet\build\darknet\x64>darknet.exe detector test cfg/coco.data yolov4.cfg yolov4.weights -i 0 -thresh 0.25 -ext_output dog.jpg
CUDA-version: 9000 (9000), cuDNN: 7.1.4, GPU count: 1
OpenCV version: 4.0.1
0 : compute_capability = 610, cudnn_half = 0, GPU: Quadro P6000
net.optimized_memory = 0
mini_batch = 1, batch = 8, time_steps = 1, train = 0
layer filters size/strd(dil) input output
0 conv 32 3 x 3/ 1 608 x 608 x 3 -> 608 x 608 x 32 0.639 BF
1 conv 64 3 x 3/ 2 608 x 608 x 32 -> 304 x 304 x 64 3.407 BF
2 conv 64 1 x 1/ 1 304 x 304 x 64 -> 304 x 304 x 64 0.757 BF
3 route 1 -> 304 x 304 x 64
4 conv 64 1 x 1/ 1 304 x 304 x 64 -> 304 x 304 x 64 0.757 BF
5 conv 32 1 x 1/ 1 304 x 304 x 64 -> 304 x 304 x 32 0.379 BF
6 conv 64 3 x 3/ 1 304 x 304 x 32 -> 304 x 304 x 64 3.407 BF
7 Shortcut Layer: 4, wt = 0, wn = 0, outputs: 304 x 304 x 64 0.006 BF
8 conv 64 1 x 1/ 1 304 x 304 x 64 -> 304 x 304 x 64 0.757 BF
9 route 8 2 -> 304 x 304 x 128
10 conv 64 1 x 1/ 1 304 x 304 x 128 -> 304 x 304 x 64 1.514 BF
11 conv 128 3 x 3/ 2 304 x 304 x 64 -> 152 x 152 x 128 3.407 BF
12 conv 64 1 x 1/ 1 152 x 152 x 128 -> 152 x 152 x 64 0.379 BF
13 route 11 -> 152 x 152 x 128
14 conv 64 1 x 1/ 1 152 x 152 x 128 -> 152 x 152 x 64 0.379 BF
15 conv 64 1 x 1/ 1 152 x 152 x 64 -> 152 x 152 x 64 0.189 BF
16 conv 64 3 x 3/ 1 152 x 152 x 64 -> 152 x 152 x 64 1.703 BF
17 Shortcut Layer: 14, wt = 0, wn = 0, outputs: 152 x 152 x 64 0.001 BF
18 conv 64 1 x 1/ 1 152 x 152 x 64 -> 152 x 152 x 64 0.189 BF
19 conv 64 3 x 3/ 1 152 x 152 x 64 -> 152 x 152 x 64 1.703 BF
20 Shortcut Layer: 17, wt = 0, wn = 0, outputs: 152 x 152 x 64 0.001 BF
21 conv 64 1 x 1/ 1 152 x 152 x 64 -> 152 x 152 x 64 0.189 BF
22 route 21 12 -> 152 x 152 x 128
23 conv 128 1 x 1/ 1 152 x 152 x 128 -> 152 x 152 x 128 0.757 BF
24 conv 256 3 x 3/ 2 152 x 152 x 128 -> 76 x 76 x 256 3.407 BF
25 conv 128 1 x 1/ 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BF
26 route 24 -> 76 x 76 x 256
27 conv 128 1 x 1/ 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BF
28 conv 128 1 x 1/ 1 76 x 76 x 128 -> 76 x 76 x 128 0.189 BF
29 conv 128 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 128 1.703 BF
30 Shortcut Layer: 27, wt = 0, wn = 0, outputs: 76 x 76 x 128 0.001 BF
31 conv 128 1 x 1/ 1 76 x 76 x 128 -> 76 x 76 x 128 0.189 BF
32 conv 128 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 128 1.703 BF
33 Shortcut Layer: 30, wt = 0, wn = 0, outputs: 76 x 76 x 128 0.001 BF
34 conv 128 1 x 1/ 1 76 x 76 x 128 -> 76 x 76 x 128 0.189 BF
35 conv 128 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 128 1.703 BF
36 Shortcut Layer: 33, wt = 0, wn = 0, outputs: 76 x 76 x 128 0.001 BF
37 conv 128 1 x 1/ 1 76 x 76 x 128 -> 76 x 76 x 128 0.189 BF
38 conv 128 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 128 1.703 BF
39 Shortcut Layer: 36, wt = 0, wn = 0, outputs: 76 x 76 x 128 0.001 BF
40 conv 128 1 x 1/ 1 76 x 76 x 128 -> 76 x 76 x 128 0.189 BF
41 conv 128 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 128 1.703 BF
42 Shortcut Layer: 39, wt = 0, wn = 0, outputs: 76 x 76 x 128 0.001 BF
43 conv 128 1 x 1/ 1 76 x 76 x 128 -> 76 x 76 x 128 0.189 BF
44 conv 128 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 128 1.703 BF
45 Shortcut Layer: 42, wt = 0, wn = 0, outputs: 76 x 76 x 128 0.001 BF
46 conv 128 1 x 1/ 1 76 x 76 x 128 -> 76 x 76 x 128 0.189 BF
47 conv 128 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 128 1.703 BF
48 Shortcut Layer: 45, wt = 0, wn = 0, outputs: 76 x 76 x 128 0.001 BF
49 conv 128 1 x 1/ 1 76 x 76 x 128 -> 76 x 76 x 128 0.189 BF
50 conv 128 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 128 1.703 BF
51 Shortcut Layer: 48, wt = 0, wn = 0, outputs: 76 x 76 x 128 0.001 BF
52 conv 128 1 x 1/ 1 76 x 76 x 128 -> 76 x 76 x 128 0.189 BF
53 route 52 25 -> 76 x 76 x 256
54 conv 256 1 x 1/ 1 76 x 76 x 256 -> 76 x 76 x 256 0.757 BF
55 conv 512 3 x 3/ 2 76 x 76 x 256 -> 38 x 38 x 512 3.407 BF
56 conv 256 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BF
57 route 55 -> 38 x 38 x 512
58 conv 256 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BF
59 conv 256 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 256 0.189 BF
60 conv 256 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 256 1.703 BF
61 Shortcut Layer: 58, wt = 0, wn = 0, outputs: 38 x 38 x 256 0.000 BF
62 conv 256 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 256 0.189 BF
63 conv 256 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 256 1.703 BF
64 Shortcut Layer: 61, wt = 0, wn = 0, outputs: 38 x 38 x 256 0.000 BF
65 conv 256 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 256 0.189 BF
66 conv 256 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 256 1.703 BF
67 Shortcut Layer: 64, wt = 0, wn = 0, outputs: 38 x 38 x 256 0.000 BF
68 conv 256 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 256 0.189 BF
69 conv 256 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 256 1.703 BF
70 Shortcut Layer: 67, wt = 0, wn = 0, outputs: 38 x 38 x 256 0.000 BF
71 conv 256 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 256 0.189 BF
72 conv 256 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 256 1.703 BF
73 Shortcut Layer: 70, wt = 0, wn = 0, outputs: 38 x 38 x 256 0.000 BF
74 conv 256 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 256 0.189 BF
75 conv 256 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 256 1.703 BF
76 Shortcut Layer: 73, wt = 0, wn = 0, outputs: 38 x 38 x 256 0.000 BF
77 conv 256 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 256 0.189 BF
78 conv 256 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 256 1.703 BF
79 Shortcut Layer: 76, wt = 0, wn = 0, outputs: 38 x 38 x 256 0.000 BF
80 conv 256 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 256 0.189 BF
81 conv 256 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 256 1.703 BF
82 Shortcut Layer: 79, wt = 0, wn = 0, outputs: 38 x 38 x 256 0.000 BF
83 conv 256 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 256 0.189 BF
84 route 83 56 -> 38 x 38 x 512
85 conv 512 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 512 0.757 BF
86 conv 1024 3 x 3/ 2 38 x 38 x 512 -> 19 x 19 x1024 3.407 BF
87 conv 512 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BF
88 route 86 -> 19 x 19 x1024
89 conv 512 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BF
90 conv 512 1 x 1/ 1 19 x 19 x 512 -> 19 x 19 x 512 0.189 BF
91 conv 512 3 x 3/ 1 19 x 19 x 512 -> 19 x 19 x 512 1.703 BF
92 Shortcut Layer: 89, wt = 0, wn = 0, outputs: 19 x 19 x 512 0.000 BF
93 conv 512 1 x 1/ 1 19 x 19 x 512 -> 19 x 19 x 512 0.189 BF
94 conv 512 3 x 3/ 1 19 x 19 x 512 -> 19 x 19 x 512 1.703 BF
95 Shortcut Layer: 92, wt = 0, wn = 0, outputs: 19 x 19 x 512 0.000 BF
96 conv 512 1 x 1/ 1 19 x 19 x 512 -> 19 x 19 x 512 0.189 BF
97 conv 512 3 x 3/ 1 19 x 19 x 512 -> 19 x 19 x 512 1.703 BF
98 Shortcut Layer: 95, wt = 0, wn = 0, outputs: 19 x 19 x 512 0.000 BF
99 conv 512 1 x 1/ 1 19 x 19 x 512 -> 19 x 19 x 512 0.189 BF
100 conv 512 3 x 3/ 1 19 x 19 x 512 -> 19 x 19 x 512 1.703 BF
101 Shortcut Layer: 98, wt = 0, wn = 0, outputs: 19 x 19 x 512 0.000 BF
102 conv 512 1 x 1/ 1 19 x 19 x 512 -> 19 x 19 x 512 0.189 BF
103 route 102 87 -> 19 x 19 x1024
104 conv 1024 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x1024 0.757 BF
105 conv 512 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BF
106 conv 1024 3 x 3/ 1 19 x 19 x 512 -> 19 x 19 x1024 3.407 BF
107 conv 512 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BF
108 max 5x 5/ 1 19 x 19 x 512 -> 19 x 19 x 512 0.005 BF
109 route 107 -> 19 x 19 x 512
110 max 9x 9/ 1 19 x 19 x 512 -> 19 x 19 x 512 0.015 BF
111 route 107 -> 19 x 19 x 512
112 max 13x13/ 1 19 x 19 x 512 -> 19 x 19 x 512 0.031 BF
113 route 112 110 108 107 -> 19 x 19 x2048
114 conv 512 1 x 1/ 1 19 x 19 x2048 -> 19 x 19 x 512 0.757 BF
115 conv 1024 3 x 3/ 1 19 x 19 x 512 -> 19 x 19 x1024 3.407 BF
116 conv 512 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BF
117 conv 256 1 x 1/ 1 19 x 19 x 512 -> 19 x 19 x 256 0.095 BF
118 upsample 2x 19 x 19 x 256 -> 38 x 38 x 256
119 route 85 -> 38 x 38 x 512
120 conv 256 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BF
121 route 120 118 -> 38 x 38 x 512
122 conv 256 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BF
123 conv 512 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 512 3.407 BF
124 conv 256 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BF
125 conv 512 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 512 3.407 BF
126 conv 256 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BF
127 conv 128 1 x 1/ 1 38 x 38 x 256 -> 38 x 38 x 128 0.095 BF
128 upsample 2x 38 x 38 x 128 -> 76 x 76 x 128
129 route 54 -> 76 x 76 x 256
130 conv 128 1 x 1/ 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BF
131 route 130 128 -> 76 x 76 x 256
132 conv 128 1 x 1/ 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BF
133 conv 256 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BF
134 conv 128 1 x 1/ 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BF
135 conv 256 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BF
136 conv 128 1 x 1/ 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BF
137 conv 256 3 x 3/ 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BF
138 conv 255 1 x 1/ 1 76 x 76 x 256 -> 76 x 76 x 255 0.754 BF
139 yolo
[yolo] params: iou loss: ciou (4), iou_norm: 0.07, cls_norm: 1.00, scale_x_y: 1.20
nms_kind: greedynms (1), beta = 0.600000
140 route 136 -> 76 x 76 x 128
141 conv 256 3 x 3/ 2 76 x 76 x 128 -> 38 x 38 x 256 0.852 BF
142 route 141 126 -> 38 x 38 x 512
143 conv 256 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BF
144 conv 512 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 512 3.407 BF
145 conv 256 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BF
146 conv 512 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 512 3.407 BF
147 conv 256 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BF
148 conv 512 3 x 3/ 1 38 x 38 x 256 -> 38 x 38 x 512 3.407 BF
149 conv 255 1 x 1/ 1 38 x 38 x 512 -> 38 x 38 x 255 0.377 BF
150 yolo
[yolo] params: iou loss: ciou (4), iou_norm: 0.07, cls_norm: 1.00, scale_x_y: 1.10
nms_kind: greedynms (1), beta = 0.600000
151 route 147 -> 38 x 38 x 256
152 conv 512 3 x 3/ 2 38 x 38 x 256 -> 19 x 19 x 512 0.852 BF
153 route 152 116 -> 19 x 19 x1024
154 conv 512 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BF
155 conv 1024 3 x 3/ 1 19 x 19 x 512 -> 19 x 19 x1024 3.407 BF
156 conv 512 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BF
157 conv 1024 3 x 3/ 1 19 x 19 x 512 -> 19 x 19 x1024 3.407 BF
158 conv 512 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BF
159 conv 1024 3 x 3/ 1 19 x 19 x 512 -> 19 x 19 x1024 3.407 BF
160 conv 255 1 x 1/ 1 19 x 19 x1024 -> 19 x 19 x 255 0.189 BF
161 yolo
[yolo] params: iou loss: ciou (4), iou_norm: 0.07, cls_norm: 1.00, scale_x_y: 1.05
nms_kind: greedynms (1), beta = 0.600000
Total BFLOPS 128.459
avg_outputs = 1068395
Allocate additional workspace_size = 33.55 MB
Loading weights from yolov4.weights...
seen 64, trained: 32032 K-images (500 Kilo-batches_64)
Done! Loaded 162 layers from weights-file
w:608,h:608,c:32
w:304,h:304,c:64
w:304,h:304,c:64
w:304,h:304,c:64
w:304,h:304,c:64
w:304,h:304,c:32
w:304,h:304,c:64
w:304,h:304,c:64
w:304,h:304,c:64
w:304,h:304,c:128
w:304,h:304,c:64
w:152,h:152,c:128
w:152,h:152,c:64
w:152,h:152,c:128
w:152,h:152,c:64
w:152,h:152,c:64
w:152,h:152,c:64
w:152,h:152,c:64
w:152,h:152,c:64
w:152,h:152,c:64
w:152,h:152,c:64
w:152,h:152,c:64
w:152,h:152,c:128
w:152,h:152,c:128
w:76,h:76,c:256
w:76,h:76,c:128
w:76,h:76,c:256
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:128
w:76,h:76,c:256
w:76,h:76,c:256
w:38,h:38,c:512
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:512
w:19,h:19,c:1024
w:19,h:19,c:512
w:19,h:19,c:1024
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:1024
w:19,h:19,c:1024
w:19,h:19,c:512
w:19,h:19,c:1024
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:512
w:19,h:19,c:2048
w:19,h:19,c:512
w:19,h:19,c:1024
w:19,h:19,c:512
w:19,h:19,c:256
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:256
w:38,h:38,c:128
w:76,h:76,c:128
w:76,h:76,c:256
w:76,h:76,c:128
w:76,h:76,c:256
w:76,h:76,c:128
w:76,h:76,c:256
w:76,h:76,c:128
w:76,h:76,c:256
w:76,h:76,c:128
w:76,h:76,c:256
w:76,h:76,c:255
w:76,h:76,c:255
w:76,h:76,c:128
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:256
w:38,h:38,c:512
w:38,h:38,c:255
w:38,h:38,c:255
w:38,h:38,c:256
w:19,h:19,c:512
w:19,h:19,c:1024
w:19,h:19,c:512
w:19,h:19,c:1024
w:19,h:19,c:512
w:19,h:19,c:1024
w:19,h:19,c:512
w:19,h:19,c:1024
w:19,h:19,c:255
w:19,h:19,c:255
dog.jpg: Predicted in 90334.152000 milli-seconds.
bicycle: 92% (left_x: 114 top_y: 128 width: 458 height: 299)
dog: 98% (left_x: 129 top_y: 225 width: 184 height: 317)
truck: 92% (left_x: 464 top_y: 77 width: 221 height: 93)
pottedplant: 33% (left_x: 681 top_y: 109 width: 37 height: 45)
推理完毕,生成txt:
将txt转换为图片
使用python实现,代码如下:
import os
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import matplotlib.image as mpimg
def process(filepath,outpath):
for fileName in os.listdir(filepath):
a=np.loadtxt(filepath+"/"+fileName)
im = Image.fromarray(np.uint8(a))
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)
# 定义图像拼接函数
def image_compose(IMAGE_SIZE,IMAGE_ROW,IMAGE_COLUMN,image_names,IMAGE_SAVE_PATH):
to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE)) #创建一个新图
# 循环遍历,把每张图片按顺序粘贴到对应位置上
for y in range(1, IMAGE_ROW + 1):
for x in range(1, IMAGE_COLUMN + 1):
from_image = Image.open(IMAGES_PATH + image_names[IMAGE_COLUMN * (y - 1) + x - 1]).resize(
(IMAGE_SIZE, IMAGE_SIZE),Image.ANTIALIAS)
to_image.paste(from_image, ((x - 1) * IMAGE_SIZE, (y - 1) * IMAGE_SIZE))
return to_image.save(IMAGE_SAVE_PATH) # 保存新图
if __name__ == "__main__":
outpath = "E:\\yolov4\\feature_pic"
filepath="E:\\yolov4\\feature_txt"
IMAGES_FORMAT = ['.jpg', '.JPG'] # 图片格式
IMAGE_SIZE_t = [512,256,256,128,128,64,64,32,32,32,32,32,32,32,32] # 每张小图片的大小
IMAGE_ROW_t = [4,4,4,4,8,8,8,8,8,8,8,8,8,5,5] # 图片间隔,也就是合并成一张图后,一共有几行
IMAGE_COLUMN_t = [4,4,4,4,4,4,4,4,8,8,8,8,4,6,6] # 图片间隔,也就是合并成一张图后,一共有几列
count = 0
plt.figure()
for fpathe,fpathe1 in zip(os.walk(filepath),os.walk(outpath)):
#for f in fs:
if(count!=0):
plt.subplot(4,4,count)
IMAGE_SAVE_PATH = 'E:\\yolov4\\feature_pic_final\\final_00'+str(count-1)+'.jpg' # 图片转换后的地址
process(fpathe[0],fpathe1[0])
IMAGES_PATH = fpathe1[0] + "\\"
# 获取图片集地址下的所有图片名称
image_names = [name for name in os.listdir(IMAGES_PATH) for item in IMAGES_FORMAT if os.path.splitext(name)[1] == item]
# 简单的对于参数的设定和实际图片集的大小进行数量判断
if len(image_names) != IMAGE_ROW_t[count -1] * IMAGE_COLUMN_t[count-1]:
#raise ValueError("合成图片的参数和要求的数量不能匹配!")
image_compose(IMAGE_SIZE_t[count-1],IMAGE_ROW_t[count-1],IMAGE_COLUMN_t[count-1],image_names,IMAGE_SAVE_PATH) #调用函数
lena = mpimg.imread(IMAGE_SAVE_PATH)
img = plt.imshow(lena) # 显示图片
count = count + 1
plt.show()
生成图片如下