自己写了一个globalStorage类

本文介绍了一种兼容IE和非IE浏览器的客户端存储解决方案,利用IE的userdata和HTML5的globalStorage特性,实现了跨浏览器的数据存储功能,并提供了具体的JavaScript实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

非IE浏览器“userdata”的解决方案

globalStorage

这个也是html5中提出来,在浏览器关闭以后,使用globalStorage存储的信息仍能够保留下来,并且存储容量比IE的userdata大得多,一个域下面是5120k。和sessionStorage一样,域中任何一个页面存储的信息都能被所有的页面共享。

作用域

globalStorage['z.baidu.com'] 所有z.baidu.com下面的页面都可以使用这块空间
globalStorage['baidu.com'] 所有baidu.com下面的页面都可以使用这块空间
globalStorage['com']:所有com域名都可以 共享的使用这一块空间
globalStorage[''] :所有页面都可以使用的空间

现在Firefox只支持当前域下的globalStorage存储, 如果使用公用域会导致一个这样一个类似的错误“Security error” code: “1000”。

过期时间

按照HTML5的描述,globalStorage只在安全问题或者当用户要求时才会过期,浏览器应该避免删除那些正在被脚本访问的数据,并且userdata应该是用户可写的。

因此我们的脚本要能够控制过期时间,可以在globalStorage的某个区域存储过期时间,在load的时候判断是否过期,可以在一定程度上解决过期时间的问题。

存储时,同时存储过期时间

以上是我从网上查询到的资料,为了兼容非IE浏览器“userdata”,我改进了之前我自己写的一个“userdata”(见 UserData使用总结) ,现在是兼容IE和支持globalStorage的浏览器了。

其中CMInfo类见 一些常用的JS功能函数(一) (2009-06-04更新)

需要说明的是因为还没用到实际项目中,因此还不知其兼容性和稳定性如何,如果网友发现了BUG,还望指出。谢谢

### GIoU 的计算公式 GIoU (Generalized Intersection over Union) 是一种扩展的 IoU 损失函数,解决了传统 IoU 在边界框无重叠情况下的不足。其核心思想是在 IoU 基础上引入了一个额外项,用于衡量预测框和真实框之间的相对位置关系。 #### 定义与公式 设 \( B \) 和 \( B^{gt} \) 分别表示预测框和真实框,\( C \) 表示能够覆盖 \( B \) 和 \( B^{gt} \) 的最小闭合区域(即包围盒)。则: \[ \text{IoU}(B, B^{gt}) = \frac{\text{Area}(B \cap B^{gt})}{\text{Area}(B \cup B^{gt})} \] 而 GIoU 的定义为: \[ \text{GIoU}(B, B^{gt}) = \text{IoU}(B, B^{gt}) - \frac{\text{Area}(C \setminus (B \cup B^{gt}))}{\text{Area}(C)} \] 其中: - \( \text{Area}(B \cap B^{gt}) \) 表示预测框和真实框交集面积。 - \( \text{Area}(B \cup B^{gt}) \) 表示预测框和真实框并集面积。 - \( \text{Area}(C) \) 表示最小闭包矩形 \( C \) 的总面积。 - \( \text{Area}(C \setminus (B \cup B^{gt})) \) 表示最小闭包矩形减去预测框和真实框并集部分的剩余面积。 通过这一公式,即使预测框和真实框完全不相交,也可以得到一个有意义的损失值[^1]。 --- ### 通用 IoU 损失函数定义 传统的 IoU 损失函数可以被定义为: \[ L_{\text{IoU}}(B, B^{gt}) = 1 - \text{IoU}(B, B^{gt}) \] 然而,这种形式在预测框和真实框完全没有重叠的情况下会失效,因为此时 \( \text{IoU}(B, B^{gt}) = 0 \),导致梯度消失问题。 相比之下,GIoU 提供了一种更鲁棒的方式处理这种情况,使得即使没有重叠也能提供有效的梯度方向[^2]。 --- ### Python 实现代码 以下是基于上述公式的 GIoU 计算实现: ```python import torch def giou_loss(pred_boxes, target_boxes): """ Calculate the Generalized IoU loss between predicted and target bounding boxes. Args: pred_boxes: Tensor of shape (N, 4), where N is the number of boxes, each box represented as [x1, y1, x2, y2]. target_boxes: Tensor of shape (N, 4). Returns: A scalar tensor representing the mean GIoU loss across all boxes. """ # Compute intersection coordinates xA = torch.max(pred_boxes[:, 0], target_boxes[:, 0]) yA = torch.max(pred_boxes[:, 1], target_boxes[:, 1]) xB = torch.min(pred_boxes[:, 2], target_boxes[:, 2]) yB = torch.min(pred_boxes[:, 3], target_boxes[:, 3]) # Compute area of intersection rectangle I inter_area = torch.clamp(xB - xA + 1, min=0.0) * torch.clamp(yB - yA + 1, min=0.0) # Compute areas of prediction and ground truth rectangles pred_area = (pred_boxes[:, 2] - pred_boxes[:, 0] + 1) * \ (pred_boxes[:, 3] - pred_boxes[:, 1] + 1) target_area = (target_boxes[:, 2] - target_boxes[:, 0] + 1) * \ (target_boxes[:, 3] - target_boxes[:, 1] + 1) # Compute union area U union_area = pred_area + target_area - inter_area # Compute IOU iou = inter_area / union_area # Compute smallest enclosing box C c_xA = torch.min(pred_boxes[:, 0], target_boxes[:, 0]) c_yA = torch.min(pred_boxes[:, 1], target_boxes[:, 1]) c_xB = torch.max(pred_boxes[:, 2], target_boxes[:, 2]) c_yB = torch.max(pred_boxes[:, 3], target_boxes[:, 3]) # Area of enclosing box C enclose_area = (c_xB - c_xA + 1) * (c_yB - c_yA + 1) # Compute GIoU giou = iou - ((enclose_area - union_area) / enclose_area) # Return GIoU loss return 1 - giou.mean() ``` 此代码实现了 GIoU 损失的计算过程,并支持批量输入的张量操作。 --- ### 总结 GIoU 改进了传统 IoU 损失函数存在的缺陷,特别是在预测框和真实框无重叠的情况下的表现更为优越。它不仅保留了 IoU 对于重叠区域的有效评估能力,还通过引入外部闭包区域进一步增强了模型的学习效果。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值