yolov1代码阅读
yolov1使用的backbone是由GoogLeNet启发而来,有24个卷积层,最后接2个全连接层,详细结构如下图:
检测网络的输入分辨率是448X448,最后的特征图大小为7X7。在特征图的每一个位置都预测如下数据项:
1、一个C维的向量,表示在该位置含有物体的条件下,含有的物体属于C个类别中每一类别的条件概率;
2、一个B维的向量,网络为每个位置预测了B个bounding boxes,每个bounding boxes都有一个“分数”,表示该box与真正的物体框的IOU,也可以理解成该bounding box含有物体的概率。这里的“分数”和1中的每个类别的分数相乘就是每个框含有每一类物体的概率;
3、一个B*4维的向量。前两个数是bbox的中心点坐标,坐标值是在7X7的尺度下,相对于feature map当前位置的偏移量,范围为[0, 1],当前位置坐标和偏移量相加即为实际的中心点坐标。这个中心点坐标的范围实际上就在以当前位置为左上角顶点的grid cell内。
综上输出的Tensor元素的排列顺序为:7*7*C-->7*7*B-->7*7*4*B,而标签Tensor元素的排列顺序为(1-->C-->4)*7*7。
yolov1的Loss由4部分组成,如下图:
yolov1最后一层的代码如下:
/*
l.coord_scale=5
l.object_scale=1
l.noobject_scale=0.5
l.class_scale=1
l.delta[box_index+0] = l.coord_scale*(net.truth[tbox_index + 0] - l.output[box_index + 0]);
l.delta[box_index+1] = l.coord_scale*(net.truth[tbox_index + 1] - l.output[box_index + 1]);
l.delta[box_index+2] = l.coord_scale*(sqrt(net.truth[tbox_index + 2]) - l.output[box_index + 2]);
l.delta[box_index+3] = l.coord_scale*(sqrt(net.truth[tbox_index + 3]) - l.output[box_index + 3]);
l.delta[p_index] = l.object_scale * (iou - l.output[p_index]);
l.delta[p_index] = l.noobject_scale*(0 - l.output[p_index]);
l.delta[class_index+j] = l.class_scale * (net.truth[truth_index+1+j] - l.output[class_index+j]);
*/
void forward_detection_layer(const detection_layer l, network net)
{
int locations = l.side*l.side;
int i,j;
memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float));
//if(l.reorg) reorg(l.output, l.w*l.h, size*l.n, l.batch, 1);
int b;
// l.softmax=0
if (l.softmax){
for(b = ; b < l.batch; ++b){
int index = b*l.inputs;
for (i = ; i < locations; ++i) {
int offset = i*l.classes;
softmax(l.output + index + offset, l.classes, , ,
l.output + index + offset);
}
}
}
if(net.train){
float avg_iou = ;
float avg_cat = ;
float avg_allcat = ;
float avg_obj = ;
float avg_anyobj = ;
int count = ;
*(l.cost) = ;
int size = l.inputs * l.batch;
memset(l.delta, , size * sizeof(float));
for (b = ; b < l.batch; ++b){
int index = b*l.inputs;
for (i = ; i < locations; ++i) {
int truth_index = (b*locations + i)*(+l.coords+l.classes);
int is_obj = net.truth[truth_index];
for (j = ; j < l.n; ++j) {
int p_index = index + locations*l.classes + i*l.n + j;
// l.noobject_scale=0.5
l.delta[p_index] = l.noobject_scale*( - l.output[p_index]);
*(l.cost) += l.noobject_scale*pow(l.output[p_index], );
avg_anyobj += l.output[p_index];
}
if (!is_obj){
continue;
} // l.class_scale=1
int class_index = index + i*l.classes;
for(j = ; j < l.classes; ++j) {
l.delta[class_index+j] = l.class_scale * (net.truth[truth_index++j] - l.output[class_index+j]);
*(l.cost) += l.class_scale * pow(net.truth[truth_index++j] - l.output[class_index+j], );
if(net.truth[truth_index + + j]) avg_cat += l.output[class_index+j];
avg_allcat += l.output[class_index+j];
}
int best_index = -;
float best_iou = ;
float best_rmse = ;
int row = i / l.side;
int col = i % l.side;
box truth = float_to_box(net.truth + truth_index + + l.classes, );
truth.x = (truth.x + col) / l.side;
truth.y = (truth.y + row) / l.side;
for(j = ; j < l.n; ++j){
int box_index = index + locations*(l.classes + l.n) + (i*l.n + j) * l.coords;
box out = float_to_box(l.output + box_index, );
out.x = (out.x + col) / l.side;
out.y = (out.y + row) / l.side;
// l.sqrt=1
if (l.sqrt){
out.w = out.w*out.w;
out.h = out.h*out.h;
}
float iou = box_iou(out, truth);
float rmse = box_rmse(out, truth);
if(best_iou > || iou > ){
if(iou > best_iou){
best_iou = iou;
best_index = j;
}
}else{
if(rmse < best_rmse){
best_rmse = rmse;
best_index = j;
}
}
}
// l.forced=0
if(l.forced){
if(truth.w*truth.h < .){
best_index = ;
}else{
best_index = ;
}
}
// l.random=0
if(l.random && *(net.seen) < ){
best_index = rand()%l.n;
} int box_index = index + locations*(l.classes + l.n) + (i*l.n + best_index) * l.coords;
int tbox_index = truth_index + + l.classes;
box out = float_to_box(l.output + box_index, );
out.x = (out.x + col) / l.side;
out.y = (out.y + row) / l.side;
if (l.sqrt) {
out.w = out.w*out.w;
out.h = out.h*out.h;
}
float iou = box_iou(out, truth);
// l.noobject_scale=0.5, l.object_scale=1
int p_index = index + locations*l.classes + i*l.n + best_index;
*(l.cost) -= l.noobject_scale * pow(l.output[p_index], );
*(l.cost) += l.object_scale * pow(-l.output[p_index], );
avg_obj += l.output[p_index];
l.delta[p_index] = l.object_scale * (.-l.output[p_index]);
// l.rescore=1
if(l.rescore){
l.delta[p_index] = l.object_scale * (iou - l.output[p_index]);
}
// l.coord_scale=5
l.delta[box_index+] = l.coord_scale*(net.truth[tbox_index + ] - l.output[box_index + ]);
l.delta[box_index+] = l.coord_scale*(net.truth[tbox_index + ] - l.output[box_index + ]);
l.delta[box_index+] = l.coord_scale*(net.truth[tbox_index + ] - l.output[box_index + ]);
l.delta[box_index+] = l.coord_scale*(net.truth[tbox_index + ] - l.output[box_index + ]);
if(l.sqrt){
l.delta[box_index+] = l.coord_scale*(sqrt(net.truth[tbox_index + ]) - l.output[box_index + ]);
l.delta[box_index+] = l.coord_scale*(sqrt(net.truth[tbox_index + ]) - l.output[box_index + ]);
} *(l.cost) += pow(-iou, );
avg_iou += iou;
++count;
}
}
*(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), );
printf("Detection Avg IOU: %f, Pos Cat: %f, All Cat: %f, Pos Obj: %f, Any Obj: %f, count: %d\n", avg_iou/count, avg_cat/count, avg_allcat/(count*l.classes), avg_obj/count, avg_anyobj/(l.batch*locations*l.n), count);
//if(l.reorg) reorg(l.delta, l.w*l.h, size*l.n, l.batch, 0);
}
} void backward_detection_layer(const detection_layer l, network net)
{
axpy_cpu(l.batch*l.inputs, , l.delta, , net.delta, );
}
预测时根据最终的输出得到bbox的代码如下:(值得注意的是,在yolov1的训练阶段,会使用原图的宽、高将标注的bbox归一化,在预测阶段输出的bbox坐标也是归一化的。)
void get_detection_detections(layer l, int w, int h, float thresh, detection *dets)
{
int i,j,n;
float *predictions = l.output;
//int per_cell = 5*num+classes;
for (i = ; i < l.side*l.side; ++i){
int row = i / l.side;
int col = i % l.side;
for(n = ; n < l.n; ++n){
int index = i*l.n + n;
int p_index = l.side*l.side*l.classes + i*l.n + n;
float scale = predictions[p_index];
int box_index = l.side*l.side*(l.classes + l.n) + (i*l.n + n)*;
box b;
// b.x = (predictions[box_index + 0] + col) / l.side * w;
// b.y = (predictions[box_index + 1] + row) / l.side * h;
// b.w = pow(predictions[box_index + 2], (l.sqrt?2:1)) * w;
// b.h = pow(predictions[box_index + 3], (l.sqrt?2:1)) * h;
b.x = (predictions[box_index + ] + col) / l.side;
b.y = (predictions[box_index + ] + row) / l.side;
b.w = pow(predictions[box_index + ], (l.sqrt?:));
b.h = pow(predictions[box_index + ], (l.sqrt?:));
dets[index].bbox = b;
dets[index].objectness = scale;
for(j = ; j < l.classes; ++j){
int class_index = i*l.classes;
float prob = scale*predictions[class_index+j];
dets[index].prob[j] = (prob > thresh) ? prob : ;
}
}
}
}
生成net.truth的关键代码如下:
void fill_truth_region(char *path, float *truth, int classes, int num_boxes, int flip, float dx, float dy, float sx, float sy)
{
char labelpath[];
find_replace(path, "images", "labels", labelpath);
find_replace(labelpath, "JPEGImages", "labels", labelpath); find_replace(labelpath, ".jpg", ".txt", labelpath);
find_replace(labelpath, ".png", ".txt", labelpath);
find_replace(labelpath, ".JPG", ".txt", labelpath);
find_replace(labelpath, ".JPEG", ".txt", labelpath);
int count = ;
box_label *boxes = read_boxes(labelpath, &count);
randomize_boxes(boxes, count);
correct_boxes(boxes, count, dx, dy, sx, sy, flip);
float x,y,w,h;
int id;
int i; for (i = ; i < count; ++i) {
x = boxes[i].x;
y = boxes[i].y;
w = boxes[i].w;
h = boxes[i].h;
id = boxes[i].id; if (w < . || h < .) continue; // num_boxes is S in article, yolov1 divides the input image into SxS grid
int col = (int)(x*num_boxes);
int row = (int)(y*num_boxes); x = x*num_boxes - col;
y = y*num_boxes - row; int index = (col+row*num_boxes)*(+classes);
if (truth[index]) continue;
truth[index++] = ; if (id < classes) truth[index+id] = ;
index += classes; truth[index++] = x;
truth[index++] = y;
truth[index++] = w;
truth[index++] = h;
}
free(boxes);
}
yolov1代码阅读的更多相关文章
- 代码阅读分析工具Understand 2.0试用
Understand 2.0是一款源代码阅读分析软件,功能强大.试用过一段时间后,感觉相当不错,确实可以大大提高代码阅读效率.由于Understand功能十分强大,本文不可能详尽地介绍它的所有功能,所 ...
- Android 上的代码阅读器 CoderBrowserHD 修改支持 go 语言代码
我在Android上的代码阅读器用的是 https://github.com/zerob13/CoderBrowserHD 改造的版本,改造后的版本我放在 https://github.com/ghj ...
- Linux协议栈代码阅读笔记(二)网络接口的配置
Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...
- [置顶] Linux协议栈代码阅读笔记(一)
Linux协议栈代码阅读笔记(一) (基于linux-2.6.21.7) (一)用户态通过诸如下面的C库函数访问协议栈服务 int socket(int domain, int type, int p ...
- 图形化代码阅读工具——Scitools Understand
Scitools出品的Understand 2.0.用了很多年了,比Source Insight强大很多.以前的名字叫Understand for C/C++,Understand for Java, ...
- Python - 关于代码阅读的一些建议
初始能力 让阅读思路保持清晰连贯,主力关注在流程架构和逻辑实现上,不被语法.技巧和业务流程等频繁地阻碍和打断. 建议基本满足以下条件,再开始进行代码阅读: 具备一定的语言基础:熟悉基础语法,常用的函数 ...
- MediaInfo代码阅读
MediaInfo是一个用来分析媒体文件的开源工具. 支持的文件非常全面,基本上支持所有的媒体文件. 最近是在做HEVC开发,所以比较关注MediaInfo中关于HEVC的分析与处理. 从Meid ...
- Tools - 一些代码阅读的方法
1 初始能力 让阅读思路清晰连贯,保持在程序的流程架构和逻辑实现上,不被语法.编程技巧和业务流程等频繁地阻碍和打断. 语言基础:熟悉基础语法,常用的函数.库.编程技巧等: 了解设计模式.构建工具.代码 ...
- Bleve代码阅读(二)——Index Mapping
引言 Bleve是Golang实现的一个全文检索库,类似Lucene之于Java.在这里通过阅读其代码,来学习如何使用及定制检索功能.也是为了通过阅读代码,学习在具体环境下Golang的一些使用方式. ...
随机推荐
- plsql初次连接oracle报错解决方案
windows7 64bit Oracle win64 11gR2(两个文件) PL/SQL v9.0 详细错误信息 Initialization error Could not initialize ...
- python re模块使用
re.findall() 查找字符 从字符串中找出符合模式的字符序列:findall(模式(正则表达式),目标字符串), 返回值为list类型,list元素为匹配出的各个字符串如: import re ...
- systemctl命令配置系统服务
1.systemd的配置文件目录 systemd将daemon执行的脚本视作服务单位(unit),服务依据功能区分时,分为不同的类型(type). 常见的systemd服务类型如下表: 后缀名称 ...
- Feign 系列(01)最简使用姿态
目录 Feign 系列(01)最简使用姿态 1. 引入 maven 依赖 2. 基本用法 3. Feign 声明式注解 Feign 系列(01)最简使用姿态 Spring Cloud 系列目录(htt ...
- 关于Ms Sql server 表列等是否存在
select object_id('名称') ,object_id('名称','类型') 1. 等价于 select * from sys.objects where name ='名称' selec ...
- DNF邀请码开发再开发方案需求
一.原因分析: 1.现实原因:主播粉丝量级有限,一定规模粉丝注册消耗完后无法进 行之后合作 2.主播资源有限,能合作主播数量少 3.直播粉丝真实接近核心用户,但是不能将其有效转化为平台流水 ...
- Shell 编程综合案例
Shell编程综合案例 Shell也学习了大概的知识,现在这篇文章就大概讲述下如何使用shell编写一个脚本呢?下面就展示一个大家常用的数据库备份案例来进行展示. 需求分析 1)每天凌晨2:10分备份 ...
- 微信小程序控件
1 scrollview 窗口view的滑动 <scroll-view scroll-y class='scroll-view-y' bindscrolltoupper="uppe ...
- 换盘符cd的用法
如果是在本盘内切换文件夹,直接使用cd 后面跟地址即可. 如果是跨区切换地址,cd 后面就需要跟/d,斜杠d, /d就代表着跨分区切换地址. cd /d d:\ C:\ProgramData\Anac ...
- webapi JWT 认证
第一步 使用ng安装JWT组件 第二步 编写登录和生成token代码 byte[] key = Encoding.UTF8.GetBytes("123456789aaaaaaa") ...