// 定义池化操作 void pool(float *input, int input_width, int input_height, int width, int height, int stride, float *output, int output_width, int output_height) { for (int i = 0; i < output_height; i++) { for (int j = 0; j < output_width; j++) { float max_val = -INFINITY; for (int k = 0; k < height; k++) { for (int l = 0; l < width; l++) { int x = j * stride + l; int y = i * stride + k; if (x >= 0 && x < input_width && y >= 0 && y < input_height) { float val = input[y * input_width + x]; if (val > max_val) { max_val = val; } } } } output[i * output_width + j] = max_val; } } }改为平均池化

时间: 2024-02-10 19:10:41 浏览: 20
可以将池化操作的求最大值改为求平均值即可,具体实现如下: ``` // 定义平均池化操作 void avg_pool(float *input, int input_width, int input_height, int width, int height, int stride, float *output, int output_width, int output_height) { for (int i = 0; i < output_height; i++) { for (int j = 0; j < output_width; j++) { float sum_val = 0.0f; int count = 0; for (int k = 0; k < height; k++) { for (int l = 0; l < width; l++) { int x = j * stride + l; int y = i * stride + k; if (x >= 0 && x < input_width && y >= 0 && y < input_height) { float val = input[y * input_width + x]; sum_val += val; count++; } } } output[i * output_width + j] = sum_val / count; } } } ``` 在这个函数中,我们使用了一个 `sum_val` 变量来累加每个池化窗口内的像素值,然后再除以像素数来求平均值。注意,为了避免除以 0 的情况,我们在每个池化窗口内记录了像素数,并用变量 `count` 来累加。最后,将求得的平均值存储到输出数组中即可。

相关推荐

解释代码 static int process(int8_t* input, int* anchor, int grid_h, int grid_w, int height, int width, int stride, std::vector<float>& boxes, std::vector<float>& objProbs, std::vector<int>& classId, float threshold, int32_t zp, float scale) { int validCount = 0; int grid_len = grid_h * grid_w; float thres = unsigmoid(threshold); int8_t thres_i8 = qnt_f32_to_affine(thres, zp, scale); for (int a = 0; a < 3; a++) { for (int i = 0; i < grid_h; i++) { for (int j = 0; j < grid_w; j++) { int8_t box_confidence = input[(PROP_BOX_SIZE * a + 4) * grid_len + i * grid_w + j]; if (box_confidence >= thres_i8) { int offset = (PROP_BOX_SIZE * a) * grid_len + i * grid_w + j; int8_t* in_ptr = input + offset; float box_x = sigmoid(deqnt_affine_to_f32(*in_ptr, zp, scale)) * 2.0 - 0.5; float box_y = sigmoid(deqnt_affine_to_f32(in_ptr[grid_len], zp, scale)) * 2.0 - 0.5; float box_w = sigmoid(deqnt_affine_to_f32(in_ptr[2 * grid_len], zp, scale)) * 2.0; float box_h = sigmoid(deqnt_affine_to_f32(in_ptr[3 * grid_len], zp, scale)) * 2.0; box_x = (box_x + j) * (float)stride; box_y = (box_y + i) * (float)stride; box_w = box_w * box_w * (float)anchor[a * 2]; box_h = box_h * box_h * (float)anchor[a * 2 + 1]; box_x -= (box_w / 2.0); box_y -= (box_h / 2.0); boxes.push_back(box_x); //push_back() 在Vector最后添加一个元素 boxes.push_back(box_y); boxes.push_back(box_w); boxes.push_back(box_h); int8_t maxClassProbs = in_ptr[5 * grid_len]; int maxClassId = 0; for (int k = 1; k < OBJ_CLASS_NUM; ++k) { int8_t prob = in_ptr[(5 + k) * grid_len]; if (prob > maxClassProbs) { maxClassId = k; maxClassProbs = prob; } } objProbs.push_back(sigmoid(deqnt_affine_to_f32(maxClassProbs, zp, scale))); classId.push_back(maxClassId); validCount++; } } } } return validCount; }

解释代码:static int process(int8_t* input, int* anchor, int grid_h, int grid_w, int height, int width, int stride, std::vector<float>& boxes, std::vector<float>& objProbs, std::vector<int>& classId, float threshold, int32_t zp, float scale) { int validCount = 0; int grid_len = grid_h * grid_w; float thres = unsigmoid(threshold); int8_t thres_i8 = qnt_f32_to_affine(thres, zp, scale); for (int a = 0; a < 3; a++) { for (int i = 0; i < grid_h; i++) { for (int j = 0; j < grid_w; j++) { int8_t box_confidence = input[(PROP_BOX_SIZE * a + 4) * grid_len + i * grid_w + j]; if (box_confidence >= thres_i8) { int offset = (PROP_BOX_SIZE * a) * grid_len + i * grid_w + j; int8_t* in_ptr = input + offset; float box_x = sigmoid(deqnt_affine_to_f32(*in_ptr, zp, scale)) * 2.0 - 0.5; float box_y = sigmoid(deqnt_affine_to_f32(in_ptr[grid_len], zp, scale)) * 2.0 - 0.5; float box_w = sigmoid(deqnt_affine_to_f32(in_ptr[2 * grid_len], zp, scale)) * 2.0; float box_h = sigmoid(deqnt_affine_to_f32(in_ptr[3 * grid_len], zp, scale)) * 2.0; box_x = (box_x + j) * (float)stride; box_y = (box_y + i) * (float)stride; box_w = box_w * box_w * (float)anchor[a * 2]; box_h = box_h * box_h * (float)anchor[a * 2 + 1]; box_x -= (box_w / 2.0); box_y -= (box_h / 2.0); int8_t maxClassProbs = in_ptr[5 * grid_len]; int maxClassId = 0; for (int k = 1; k < OBJ_CLASS_NUM; ++k) { int8_t prob = in_ptr[(5 + k) * grid_len]; if (prob > maxClassProbs) { maxClassId = k; maxClassProbs = prob; } } if (maxClassProbs>thres_i8){ objProbs.push_back(sigmoid(deqnt_affine_to_f32(maxClassProbs, zp, scale))* sigmoid(deqnt_affine_to_f32(box_confidence, zp, scale))); classId.push_back(maxClassId); validCount++; boxes.push_back(box_x); boxes.push_back(box_y); boxes.push_back(box_w); boxes.push_back(box_h); } } } } } return validCount; }

对上述代码进行如下修改,是否改变基本功能:tatic int process(int8_t* input, int point_cnt, int height, int width, int stride, std::vector<float>& boxes, std::vector<float>& objProbs, std::vector<int>& classId, float threshold, int32_t zp, float scale) { int validCount = 0; float thres = unsigmoid(threshold); int8_t thres_i8 = qnt_f32_to_affine(thres, zp, scale); for (int a = 0; a < point_cnt; a++){ int8_t maxClassProbs = 0; int maxClassId = 0; for (int k = 1; k < OBJ_CLASS_NUM; ++k) { int8_t prob = input[(3+k) * point_cnt + a]; if (prob > maxClassProbs) { maxClassId = k; maxClassProbs = prob; } } if (maxClassProbs >= thres_i8) { int8_t rx = input[0 * point_cnt + a]; int8_t ry = input[1 * point_cnt + a]; int8_t rw = input[2 * point_cnt + a]; int8_t rh = input[3 * point_cnt + a]; float box_x = sigmoid(deqnt_affine_to_f32(rx, zp, scale)) * 2.0 - 0.5; float box_y = sigmoid(deqnt_affine_to_f32(ry, zp, scale)) * 2.0 - 0.5; float box_w = sigmoid(deqnt_affine_to_f32(rw, zp, scale)) * 2.0; float box_h = sigmoid(deqnt_affine_to_f32(rh, zp, scale)) * 2.0; objProbs.push_back(sigmoid(deqnt_affine_to_f32(maxClassProbs, zp, scale))); classId.push_back(maxClassId); validCount++; boxes.push_back(box_x); boxes.push_back(box_y); boxes.push_back(box_w); boxes.push_back(box_h); } } return validCount; } int post_process(int8_t* input0, int model_in_h, int model_in_w, float conf_threshold, float nms_threshold, float scale_w, float scale_h, std::vector<int32_t>& qnt_zps, std::vector<float>& qnt_scales, detect_result_group_t* group) { static int init = -1; if (init == -1) { int ret = 0; ret = loadLabelName(LABEL_NALE_TXT_PATH, labels); if (ret < 0) { return -1; } init = 0; } memset(group, 0, sizeof(detect_result_group_t)); std::vector<float> filterBoxes; std::vector<float> objProbs; std::vector<int> classId; // stride 6 int stride0 = 4 + OBJ_CLASS_NUM; int point_cnt = 8400; int validCount0 = 0; validCount0 = process(input0, point_cnt, model_in_h, model_in_w, stride0, filterBoxes, objProbs, classId, conf_threshold, qnt_zps[0], qnt_scales[0]); int validCount = validCount0; // no object detect if (validCount <= 0) { return 0; } std::vector<int> indexArray; for (int i = 0; i < validCount; ++i) { indexArray.push_back(i); } quick_sort_indice_inverse(objProbs, 0, validCount - 1, indexArray); std::set<int> class_set(std::begin(classId), std::end(classId)); for (auto c : class_set) { nms(validCount, filterBoxes, classId, indexArray, c, nms_threshold); } int last_count = 0; group->count = 0; /* box valid detect target */ for (int i = 0; i < validCount; ++i) { if (indexArray[i] == -1 || last_count >= OBJ_NUMB_MAX_SIZE) { continue; } int n = indexArray[i]; float x1 = filterBoxes[n * 4 + 0]; float y1 = filterBoxes[n * 4 + 1]; float x2 = x1 + filterBoxes[n * 4 + 2]; float y2 = y1 + filterBoxes[n * 4 + 3]; int id = classId[n]; float obj_conf = objProbs[i]; group->results[last_count].box.left = (int)(clamp(x1, 0, model_in_w) / scale_w); group->results[last_count].box.top = (int)(clamp(y1, 0, model_in_h) / scale_h); group->results[last_count].box.right = (int)(clamp(x2, 0, model_in_w) / scale_w); group->results[last_count].box.bottom = (int)(clamp(y2, 0, model_in_h) / scale_h); group->results[last_count].prop = obj_conf; char* label = labels[id]; strncpy(group->results[last_count].name, label, OBJ_NAME_MAX_SIZE); // printf("result %2d: (%4d, %4d, %4d, %4d), %s\n", i, group->results[last_count].box.left, // group->results[last_count].box.top, // group->results[last_count].box.right, group->results[last_count].box.bottom, label); last_count++; } group->count = last_count; return 0; }

最新推荐

recommend-type

知识蒸馏-基于Tensorflow实现的无数据知识蒸馏-附项目源码+流程教程-优质项目分享.zip

知识蒸馏_基于Tensorflow实现的无数据知识蒸馏_附项目源码+流程教程_优质项目分享
recommend-type

基于人工智能模型U-net神经网络医学图像分割

这个完整工程给出基于人工智能模型U-net神经网络医学图像分割功能实现,适合基于机器学习和深度学习模型分析的学习资料,并有详细程序说明书。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

从键盘输入一段英文字符串,其中包含多个字母‘h',请编写程序利用正则表达式,将英文字符串中的’h'全部改为‘H’

好的,我可以帮您解决这个问题。 您可以使用 Python 编写一个程序来实现这个功能。具体步骤如下: 1. 首先导入 re 模块,这个模块是 Python 中用于处理正则表达式的模块。 2. 然后定义一个包含英文字符串的变量,例如: text = "This is a string containing multiple h's." 3. 接下来,使用 re.sub() 方法来进行替换操作。re.sub() 方法的第一个参数是正则表达式,第二个参数是替换的字符或字符串,第三个参数是被替换的字符串。在这个例子里,我们需要将所有的小写字母 h 替换成大写字母 H,所以正则表达式可以写成
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB柱状图在数据分析中的作用:从可视化到洞察

![MATLAB柱状图在数据分析中的作用:从可视化到洞察](https://img-blog.csdnimg.cn/img_convert/1a36558cefc0339f7836cca7680c0aef.png) # 1. MATLAB柱状图概述** 柱状图是一种广泛用于数据可视化的图表类型,它使用垂直条形来表示数据中不同类别或组别的值。在MATLAB中,柱状图通过`bar`函数创建,该函数接受数据向量或矩阵作为输入,并生成相应的高度条形。 柱状图的优点在于其简单性和易于理解性。它们可以快速有效地传达数据分布和组别之间的比较。此外,MATLAB提供了广泛的定制选项,允许用户调整条形颜色、
recommend-type

MySQL 什么情况下不会使用到索引

MySQL 不会使用索引的情况主要包括: 1. 查询的数据量太少,全表扫描比使用索引更快; 2. 使用了函数或表达式对索引字段进行运算; 3. 对索引字段进行了类型转换; 4. 索引列上出现了函数或者运算符; 5. 查询语句中使用了 OR 连接多个条件; 6. 查询条件中有 NULL 值; 7. 数据表的数据量过于庞大,索引失去了优势。