C++ 类的成员函数指针(用作函数参数) tcy

本文详细讲解了C++中的类方法指针和普通函数指针,包括如何定义、使用以及在实际编程中的应用场景,通过实例演示了如何通过指针调用类的成员函数。
这个概念主要用在C++中去实现"委托"的特性. 
但现在C++11 中有了 更好用的function/bind 功能.
但对于类的成员函数指针的概念我们还是应该掌握的.

类函数指针 就是要确定由哪个 类的实例 去调用 类函数指针所指的函数

第一部分:函数指针 

float add(float a, float b) { return a + b; }
float sub(float a, float b) { return a - b; }

typedef float(*pf)(float, float);

float foo(float a, float b, pf op){
     float y = op(a, b);
     return y;
}

pf bar(const char* mode = "add") {
    if (mode == "add")
        return add;
    else
        return sub;
}

第二部分:类方法指针: 

class A {
public:
    int add(int x, int y) { return x + y; }
    int sub(int x, int y) { return x + y; }
    int mul(int x, int y)const { return x * y; }
    int div(int x, int y)const { return x / y; }
};

typedef int (A::*pfunc)(int, int) const;
//pfunc div1 = &A::div;

int Div(int x, int y,const A& self, pfunc op){
    return (self.*op)(x,y);
}

void test() {
    //测试1:
    int (A::*fp)(int, int) = &A::add;
    A a;
    cout << "1.1.add= " << (a.*fp)(10, 20) << endl;

    //测试2:
    typedef int (A::*f)(int, int);
    f add = &A::add;

    cout << "2.1.add=" << (a.*add)(10, 20) << endl;
    cout << "2.2.add=" << (a.*&A::add)(10, 20) << endl;
    cout << "2.3.add= " << (a.A::add)(10, 20) << endl;

    //测试3:
    int (A::*fp1)(int, int)const = &A::mul;
    cout << "3.1.add= " << (a.*fp1)(10, 20) << endl;

    //测试4:
    typedef int (A::*f1)(int, int) const;
    f1 mul = &A::mul;

    cout << "4.1.add=" << (a.*mul)(10, 20) << endl;
    cout << "4.2.add=" << (a.*&A::mul)(10, 20) << endl;
    cout << "4.3.add= " << (a.A::mul)(10, 20) << endl;

    //测试5:
    cout << "5.1.Div=" << Div(100, 2, a, &A::div) << endl;
}

int main(){
    test();
}
### DETR 模型中的 CIoU 损失函数 #### CIoU 损失函数的作用 CIoU (Complete Intersection over Union) 是一种用于边界框回归的任务特定损失函数,旨在解决 IoU 及其变体(如 GIoU 和 DIoU)未能充分考虑预测框和真实框之间几何关系的问题。具体来说,CIoU 不仅考虑了重叠面积的比例,还引入了两个额外的惩罚项来提高定位精度: - **比例一致性**:通过比较宽高比差异来衡量形状相似度。 - **中心点距离**:测量两矩形中心之间的相对位置偏差。 这种设计使得 CIoU 更加关注于提升检测框的质量,在实际应用中能够带来更好的收敛速度以及更高的 mAP 值[^1]。 #### CIoU 损失函数的具体实现 在 `metrics.py` 文件或其他似的模块中定义了模型使用的各种损失函数及其计算逻辑。对于 CIoU 的实现通常会涉及到以下几个方面: 1. 计算交并比 IoU; 2. 添加上述提到的距离与尺度因子作为附加约束条件; 3. 将这些成分组合成最终形式返回给训练过程优化使用。 以下是基于 PyTorch 实现的一个简化版 CIoU 函数示例: ```python import torch def ciou_loss(preds, targets, eps=1e-7): # 获取边界的坐标信息 px1, py1, px2, py2 = preds[:, 0], preds[:, 1], preds[:, 2], preds[:, 3] tx1, ty1, tx2, ty2 = targets[:, 0], targets[:, 1], targets[:, 2], targets[:, 3] # 计算宽度高度 pw = px2 - px1 ph = py2 - py1 tw = tx2 - tx1 th = ty2 - ty1 # 中心点坐标 pcx = (px1 + px2) / 2 pcy = (py1 + py2) / 2 tcx = (tx1 + tx2) / 2 tcy = (ty1 + ty2) / 2 # 边界框间的最小外接矩形对角线长度平方 c_squared = ((torch.max(px2, tx2)-torch.min(px1, tx1))**2 + (torch.max(py2, ty2)-torch.min(py1, ty1))**2) # 预测框和目标框中心点间欧氏距离平方 rho_squared = (pcx-tcx)**2 + (pcy-tcy)**2 # 宽高比率 v = (4 / math.pi ** 2) * \ torch.pow(torch.atan(tw/th+eps) - torch.atan(pw/ph+eps), 2) alpha = v / (1-iou+v+eps) iou_term = bbox_overlaps_iou(preds, targets)+eps distance_penalty = rho_squared/c_squared+eps aspect_ratio_penalty = v*alpha+eps return 1 - iou_term + distance_penalty + aspect_ratio_penalty ``` 此代码片段展示了如何构建一个完整的 CIoU 损失计算器,并将其应用于边界框回归任务之中。需要注意的是这里假设输入张量已经包含了正确的维度顺序即 `[batch_size, num_boxes, 4]` 形式的 `(x_min,y_min,x_max,y_max)` 格式化后的数据[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值