【tensorflow】MTCNN网络基本函数bbox_ohem&landmark_ohem()

【tensorflow】MTCNN网络基本函数bbox_ohem&landmark_ohem()tf.gather:用一个一维的索引数组,将张量中对应索引的向量提取出来importtensorflowastfimportnumpyasnpa=tf.constant([1,2,3,4])b=tf.square(a)withtf.Session()assess:print(“b:%s”%sess.run(b))#b:[14916]…

大家好,又见面了,我是你们的朋友全栈君。

tf.gather:用一个一维的索引数组,将张量中对应索引的向量提取出来

import tensorflow as tf
import numpy as np
a = tf.constant([1,2,3,4])
b = tf.square(a)
with tf.Session() as sess:
    print("b:%s" % sess.run(b))
# b:[ 1  4  9 16]
import numpy as np
import tensorflow as tf
def bbox_ohem(bbox_pred,bbox_target,label):
    '''
    :param bbox_pred:
    :param bbox_target:
    :param label: class label
    :return: mean euclidean loss for all the pos and part examples
    '''
    zeros_index = tf.zeros_like(label, dtype=tf.float32)
    ones_index = tf.ones_like(label, dtype=tf.float32)
    #获取pos样本和part样本
    valid_inds = tf.where(tf.equal(tf.abs(label),1),ones_index,zeros_index)
    #(batch,)
    #计算平方和(按行)tf.square(bbox_pred-bbox_target): 求每个数的平方值
    square_error = tf.square(bbox_pred-bbox_target)
    square_error = tf.reduce_sum(square_error,axis=1)
    with tf.Session() as sess:
        print("bbox_pred-bbox_target:%s"%(sess.run(bbox_pred-bbox_target)))
        print("square_error:%s" % (sess.run(square_error)))
    # 计算pos样本和part样本的数量
    num_valid = tf.reduce_sum(valid_inds)
    keep_num = tf.cast(num_valid, dtype=tf.int32)
    # 去掉neg样本和landmark样本的平方和
    square_error = square_error*valid_inds
    # 获取前K个样本的索引,K为pos和part样本的数量
    _, k_index = tf.nn.top_k(square_error, k=keep_num)
    # 将所有pos样本和part样本的平方和提取出来
    square_error = tf.gather(square_error, k_index)
    # 返回均值
    return tf.reduce_mean(square_error)

bbox_pred = tf.random_uniform([2,4],10,100,seed = 100)
bbox_target = tf.random_uniform([2,4],15,150,seed = 100)
with tf.Session() as sess:
    print("cls_prob:%s"%(sess.run(bbox_pred)))
label = np.array([1,0])
bbox_ohem(bbox_pred,bbox_target,label)

在这里插入图片描述

landmark_ohem:作用就是返回landmark的损失,用的是landmark样本。

def landmark_ohem(landmark_pred,landmark_target,label):
    '''

    :param landmark_pred:
    :param landmark_target:
    :param label:
    :return: mean euclidean loss
    '''
    #keep label =-2  then do landmark detection
    ones = tf.ones_like(label,dtype=tf.float32)
    zeros = tf.zeros_like(label,dtype=tf.float32)
    valid_inds = tf.where(tf.equal(label,-2),ones,zeros)
    square_error = tf.square(landmark_pred-landmark_target)
    square_error = tf.reduce_sum(square_error,axis=1)
    num_valid = tf.reduce_sum(valid_inds)
    #keep_num = tf.cast(num_valid*num_keep_radio,dtype=tf.int32)
    keep_num = tf.cast(num_valid, dtype=tf.int32)
    square_error = square_error*valid_inds
    _, k_index = tf.nn.top_k(square_error, k=keep_num)
    square_error = tf.gather(square_error, k_index)
    return tf.reduce_mean(square_error)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139359.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • leveldb介绍「建议收藏」

    网上有很多关于leveldb的介绍文章,还不如直接看官方文档,直接上文档,希望自己以后有空翻译成中文版本。leveldbJeffDean,SanjayGhemawatTheleveldblibraryprovidesapersistentkeyvaluestore.Keysandvaluesarearbitrarybytearrays.Theke…

    2022年4月6日
    62
  • centos7.4安装docker_docker运行centos

    centos7.4安装docker_docker运行centos前言当我们在一台电脑上搭建了python3.6的环境,下次换台电脑,又得重新搭建一次,设置环境变量等操作。好不容易安装好,一会提示pip不是内部或外部命令,一会又提示pip:commandno

    2022年7月28日
    8
  • Random随机数nextInt(n)的使用

    Random随机数nextInt(n)的使用nextInt(intn) 方法用于获取一个伪随机,在0(包括)和指定值(不包括),从此随机数生成器的序列中取出均匀分布的int值。声明以下是对java.util.Random.nextInt()方法的声明。publicintnextInt(intn)参数n–这是结合于该随机数返回。必须为正数。返回值在方法调用返回

    2022年7月22日
    13
  • PyCharm 2021.5.3 x64激活码[在线序列号]

    PyCharm 2021.5.3 x64激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    56
  • Office 365提供安全且可靠的服务

    Office 365提供安全且可靠的服务

    2021年8月21日
    87
  • 解决Destroying ProtocolHandler [“ajp-apr-8009“]

    解决Destroying ProtocolHandler [“ajp-apr-8009“]今天刚开始调JSP网站,还好好的,后来中间注销了电脑一次,再打开调试它,就遇到了这个无法启动服务器的问题:DestroyingProtocolHandler[“ajp-apr-8009”]上网百度了下,说是端口号被占用了。解决办法:1、查看端口号被占用情况:步骤:调出命令窗:开始->运行->cmd,然后输入命令:netstat-ano如图(记下来占用该

    2025年8月26日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号