TF2.0 API学习(Python)六:函数compute_loss、函数bbox_giou、函数bbox_iou
函数compute_loss
for i in range(3):conv, pred = pred_result[i*2], pred_result[i*2+1]loss_items = compute_loss(pred, conv, *target[i], i)giou_loss += loss_items[0]conf_loss += loss_items[1]prob_loss += loss_items[2]
def compute_loss(pred, conv, label, bboxes, i=0):conv_shape = tf.shape(conv)batch_size = conv_shape[0]output_size = conv_shape[1]# __C.YOLO.STRIDES = [8, 16, 32]input_size = STRIDES[i] * output_sizeconv = tf.reshape(conv, (batch_size, output_size, output_size, 3, 5 + NUM_CLASS))conv_raw_conf = conv[:, :, :, :, 4:5]conv_raw_prob = conv[:, :, :, :, 5:]# 预测出的4个坐标值pred_xywh = pred[:, :, :, :, 0:4]# 预测含object的概率pred_conf = pred[:, :, :, :, 4:5]# 从标签文件中提取的坐标真值label_xywh = label[:, :, :, :, 0:4]respond_bbox = label[:, :, :, :, 4:5] # respond_bbox 猜测存在目标为1,不存在目标为0label_prob = label[:, :, :, :, 5:]# ①计算xy和wh上的loss,其计算的是实际上存在目标的,利用第三步真实框编码后的的结果和未处理的预测结果进行对比得到lossgiou = tf.expand_dims(bbox_giou(pred_xywh, label_xywh), axis=-1)input_size = tf.cast(input_size, tf.float32)bbox_loss_scale = 2.0 - 1.0 * label_xywh[:, :, :, :, 2:3] * label_xywh[:, :, :, :, 3:4] / (input_size ** 2)giou_loss = respond_bbox * bbox_loss_scale * (1- giou)# 对于每一幅图,计算其中所有真实框与预测框的IOU,取出每个网络点中IOU最大的先验框,# 如果这个最大的IOU都小于ignore_thresh,意味着这个网络点内不存在目标,可以被忽略。# 不明白这里为什么要加这么多np.newaxisiou = bbox_iou(pred_xywh[:, :, :, :, np.newaxis, :], bboxes[:, np.newaxis, np.newaxis, np.newaxis, :, :])max_iou = tf.expand_dims(tf.reduce_max(iou, axis=-1), axis=-1)respond_bgd = (1.0 - respond_bbox) * tf.cast( max_iou < IOU_LOSS_THRESH, tf.float32 )conf_focal = tf.pow(respond_bbox - pred_conf, 2)# 不明白conv_raw_conf和pred_conf的关系# ②计算置信度lossconf_loss = conf_focal * (respond_bbox * tf.nn.sigmoid_cross_entropy_with_logits(labels=respond_bbox, logits=conv_raw_conf)+respond_bgd * tf.nn.sigmoid_cross_entropy_with_logits(labels=respond_bbox, logits=conv_raw_conf))# ③计算类别条件概率的lossprob_loss = respond_bbox * tf.nn.sigmoid_cross_entropy_with_logits(labels=label_prob, logits=conv_raw_prob)giou_loss = tf.reduce_mean(tf.reduce_sum(giou_loss, axis=[1,2,3,4]))conf_loss = tf.reduce_mean(tf.reduce_sum(conf_loss, axis=[1,2,3,4]))prob_loss = tf.reduce_mean(tf.reduce_sum(prob_loss, axis=[1,2,3,4]))return giou_loss, conf_loss, prob_loss
1、tf.reshape
给定后tensor,此操作将返回一个新tf.Tensor值tensor,该值具有与相同顺序相同的值,但具有由给出的新形状 shape。
tf.reshape(tensor, shape, name=None
)
t1 = [[1, 2, 3],[4, 5, 6]]
print(tf.shape(t1).numpy())
[2 3]
t2 = tf.reshape(t1, [6])
t2
<tf.Tensor: id=8, shape=(6,), dtype=int32, numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>
2、tf.expand_dims
返回在index处插入长度为1的轴的张量axis。
tf.expand_dims(input, axis, name=None
)
给定的张量input,该操作插入尺寸指数在长度为1的尺寸axis的input的形状。维度索引遵循Python索引规则:从零开始,它是一个负索引,从末尾开始倒数。
image = tf.zeros([10,10,3])
tf.expand_dims(image, axis=0).shape.as_list()
[1, 10, 10, 3]
tf.expand_dims(image, axis=1).shape.as_list()
[10, 1, 10, 3]
tf.expand_dims(image, -1).shape.as_list()
[10, 10, 3, 1]
3、tf.math.reduce_max
计算张量维度上的元素最大值。
x = tf.constant([5, 1, 2, 4])
tf.reduce_max(x,axis=-1)
<tf.Tensor: id=14, shape=(), dtype=int32, numpy=5>
x = tf.constant([-5, -1, -2, -4])
tf.reduce_max(x)
<tf.Tensor: id=22, shape=(), dtype=int32, numpy=-1>
4、tf.math.pow
计算一个值对另一个值的幂。
tf.math.pow(x, y, name=None
)
x = tf.constant([[2, 2], [3, 3]])
y = tf.constant([[8, 16], [2, 3]])
tf.pow(x, y) # [[256, 65536], [9, 27]]
<tf.Tensor: id=26, shape=(2, 2), dtype=int32, numpy=
array([[ 256, 65536],[ 9, 27]], dtype=int32)>
5、tf.nn.sigmoid_cross_entropy_with_logits
计算给定的S形交叉熵logits
tf.nn.sigmoid_cross_entropy_with_logits(labels=None, logits=None, name=None
)
6、tf.math.reduce_sum
计算跨张量维度的元素之和。
x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x).numpy()
6
7、tf.math.reduce_mean
计算跨张量维度的元素的均值。
函数bbox_giou
pred_xywh = pred[:, :, :, :, 0:4]
label_xywh = label[:, :, :, :, 0:4]
bbox_giou(pred_xywh, label_xywh)
def bbox_giou(boxes1, boxes2):# 将(x,y,w,h)转化为(x-0.5*w, y-0.5*h, x+0.5*w, y+0.5*h)boxes1 = tf.concat([boxes1[..., :2] - boxes1[..., 2:] * 0.5,boxes1[..., :2] + boxes1[..., 2:] * 0.5], axis=-1)boxes2 = tf.concat([boxes2[..., :2] - boxes2[..., 2:] * 0.5,boxes2[..., :2] + boxes2[..., 2:] * 0.5], axis=-1)# 保证两个端点坐标值的大小顺序,但是这一步的必要性在哪里boxes1 = tf.concat([tf.minimum(boxes1[..., :2], boxes1[..., 2:]),tf.maximum(boxes1[..., :2], boxes1[..., 2:])], axis=-1)boxes2 = tf.concat([tf.minimum(boxes2[..., :2], boxes2[..., 2:]),tf.maximum(boxes2[..., :2], boxes2[..., 2:])], axis=-1)# 计算两个矩形框的面积boxes1_area = (boxes1[..., 2] - boxes1[..., 0]) * (boxes1[..., 3] - boxes1[..., 1])boxes2_area = (boxes2[..., 2] - boxes2[..., 0]) * (boxes2[..., 3] - boxes2[..., 1])# 交叠区域左上角坐标left_up = tf.maximum(boxes1[..., :2], boxes2[..., :2])# 交叠区域右下角坐标right_down = tf.minimum(boxes1[..., 2:], boxes2[..., 2:])# 交叠区域的宽高# 如果预测框与真实框不交叠则right_down - left_up是负值,所以这里与0对比,若为负值则取零inter_section = tf.maximum(right_down - left_up, 0.0)# 交叠区域面积inter_area = inter_section[..., 0] * inter_section[..., 1]# 并集区域面积union_area = boxes1_area + boxes2_area - inter_areaiou = inter_area / union_area# 计算最小闭合凸面 C 左上角和右下角的坐标enclose_left_up = tf.minimum(boxes1[..., :2], boxes2[..., :2])enclose_right_down = tf.maximum(boxes1[..., 2:], boxes2[..., 2:])# 计算最小闭合凸面 C的宽高enclose = tf.maximum(enclose_right_down - enclose_left_up, 0.0)# 计算最小闭合凸面 C的面积 = 宽 * 高enclose_area = enclose[..., 0] * enclose[..., 1]# 计算GIoUgiou = iou - 1.0 * (enclose_area - union_area) / enclose_areareturn giou
1、tf.math.minimum
返回元素的x和y的最小值
tf.math.minimum(x, y, name=None
)
x = tf.constant([0., 0., 0., 0.])
y = tf.constant([-5., -2., 0., 3.])
tf.math.minimum(x, y)
<tf.Tensor: id=31, shape=(4,), dtype=float32, numpy=array([-5., -2., 0., 0.], dtype=float32)>
x = tf.constant([-5., 0., 0., 0.])
y = tf.constant([-3.])
tf.math.minimum(x, y)
<tf.Tensor: id=35, shape=(4,), dtype=float32, numpy=array([-5., -3., -3., -3.], dtype=float32)>
x = tf.constant([-5., 0., 0., 0.])
y = tf.constant([-3., -1.])
tf.math.minimum(x, y)
InvalidArgumentError: Incompatible shapes: [4] vs. [2] [Op:Minimum]
2、tf.convert_to_tensor
将给定转换value为Tensor
tf.convert_to_tensor(value, dtype=None, dtype_hint=None, name=None
)
此函数将各种类型的Python对象转换为Tensor 对象。它接受Tensor对象,numpy数组,Python列表和Python标量
import numpy as np
def my_func(arg):arg = tf.convert_to_tensor(arg, dtype=tf.float32)return arg
value_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]]))
print(value_1)
tf.Tensor(
[[1. 2.][3. 4.]], shape=(2, 2), dtype=float32)
value_2 = my_func([[1.0, 2.0], [3.0, 4.0]])
print(value_2)
tf.Tensor(
[[1. 2.][3. 4.]], shape=(2, 2), dtype=float32)
value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32))
print(value_3)
tf.Tensor(
[[1. 2.][3. 4.]], shape=(2, 2), dtype=float32)
函数bbox_iou
iou = bbox_iou(pred_xywh[:, :, :, :, np.newaxis, :], bboxes[:, np.newaxis, np.newaxis, np.newaxis, :, :])
def bbox_iou(boxes1, boxes2):boxes1_area = boxes1[..., 2] * boxes1[..., 3]boxes2_area = boxes2[..., 2] * boxes2[..., 3]boxes1 = tf.concat([boxes1[..., :2] - boxes1[..., 2:] * 0.5,boxes1[..., :2] + boxes1[..., 2:] * 0.5], axis=-1)boxes2 = tf.concat([boxes2[..., :2] - boxes2[..., 2:] * 0.5,boxes2[..., :2] + boxes2[..., 2:] * 0.5], axis=-1)left_up = tf.maximum(boxes1[..., :2], boxes2[..., :2])right_down = tf.minimum(boxes1[..., 2:], boxes2[..., 2:])inter_section = tf.maximum(right_down - left_up, 0.0)inter_area = inter_section[..., 0] * inter_section[..., 1]union_area = boxes1_area + boxes2_area - inter_areareturn 1.0 * inter_area / union_area
TF2.0 API学习(Python)六:函数compute_loss、函数bbox_giou、函数bbox_iou相关推荐
- 从0基础学习Python(13)[面向对象思想]
从0基础学习Python (Day13) 面向对象思想 1.面向对象编程介绍 面向对象 概述 目前软件开发技术中两种主流思想:面向过程.面向对象 面向过程思想出现较早,C语言 面向对象思想 ...
- python 内存溢出能捕获吗_从0基础学习Python (19)[面向对象开发过程中的异常(捕获异常~相关)]...
从0基础学习Python (Day19) 面向对象开发过程中的=>异常 什么是异常 当程序在运行过程中出现的一些错误,或者语法逻辑出现问题,解释器此时无法继续正常执行了,反而出现了一些错误的 ...
- TF2.0深度学习实战(一):分类问题之手写数字识别
前言: 本专栏将分享从零开始搭建神经网络的学习过程,力争打造最易上手的小白教程.在这过程中,我将使用谷歌TensorFlow2 框架逐一复现经典的卷积神经网络:LeNet-5.AlexNet.VGG系 ...
- Lumerical Python API学习(六)——lumopt入门
Getting Started with lumopt - Python API 来自 https://support.lumerical.com/hc/en-us/articles/36005099 ...
- 0基础学习Python基础语法(0)-print()与变量
文章目录 Python[人工智能]时代的立身武器 print 无引号 单引号的用法 双引号的用法 三引号的用法 转义字符 变量和赋值 变量的命名规范 下关预告 Demo 例子:打印 例子:转义字符 要 ...
- 零基础入门学习Python(19)-内嵌函数和闭包
global关键字 全局变量的作用域是整个模块,也就是代码段内所有的函数内部都可以访问到全局变量 注意,在函数内部仅仅去访问全局变量就好,不要试图去修改它,如果随意修改全局变量的值,很容易牵一发而动全 ...
- 学习Python必会内容:print函数、赋值与代码块
1.神奇的print函数 print函数相信读者一定对它不陌生,因为在前面的章节,几乎每个例子都使用了print函数,这个函数的功能就是在控制台输出文本.不过print在输出文本时还可以进行一些设置, ...
- (原创) cocos2d-x 3.0+ lua 学习和工作(4) : 公共函数(5): 返回指定表格中的所有键(key):table.keys...
这里的函数主要用来做:返回指定表格中所有的键.参考资料为quick_cocos. 星月倾心贡献~~~ --[[ -- 返回指定表格中的所有键(key) -- example:local t = ( a ...
- 学习Python的利器:内置函数dir()和help()
(1)内置函数dir()用来查看对象的成员.在Python中所有的一切都是对象,除了整数.实数.复数.字符串.列表.元组.字典.集合等等,还有range对象.enumerate对象.zip对象.fil ...
最新文章
- 程序员的自我修养--链接、装载与库笔记:Windows PE/COFF
- win 对比工具-Araxis Merge
- Python深度学习之搭建小型卷积神经网络(Kaggle网站Dogs-vs-Cats数据集)
- 计算平方根【牛顿迭代法】
- 数据分析和作图用python好还是R!python真有那么神吗?
- linux 服务 启动 关闭 列表
- FrameLayout 测量过程
- 面向对象程序设计(OOP设计模式)-行为型模式之观察者模式的应用与实现
- 背景减法——自组织算法
- javascript 基础之手机端相关事件-touch(详细篇)(1)
- Cast-128 加密算法和 MyPassWord 的破解
- 2022-2028年中国智慧教育行业发展策略分析及投资前景研究报告
- 使用Layered分层窗口实现视频会议中的桌面区域共享
- 公司禁用QQ,破解方法(洋葱tor 安装配置)
- 简历中的star法则
- umi路由懒加载和权限验证(基于React)
- 制作ubuntu自定义系统镜像
- ufs2.1 android bench,小米6使用的是UFS2.1闪存吗?小米6闪存AndroBench跑分成绩
- 剑指高效编程之函数编程
- c++ notify_one()和notify_all()