Tensor Calculation Method

Recently,I have met some problem when I Implement an Objects Detection algorithm,The single shot detection(ssd),which is that The Tensor Calculation method,including divition,multiplation,add and substract,The code as follows:

def ssd_decode(self,location,box,prior_scaling):
        y_a, x_a, h_a, w_a = box

        cx = location[:, :, :, :, 0] * w_a * prior_scaling[0] + x_a  
        cy = location[:, :, :, :, 1] * h_a * prior_scaling[1] + y_a
        w = w_a * tf.exp(location[:, :, :, :, 2] * prior_scaling[2])
        h = h_a * tf.exp(location[:, :, :, :, 3] * prior_scaling[3])
        print(cx, cy, w, h)

        bboxes = tf.stack([cy - h / 2.0, cx - w / 2.0, cy + h / 2.0, cx + w / 2.0], axis=-1)

        return bboxes

This is a SSD decode function,which is able to transform the prediction box locations to ground truth box locations,and the y_a,x_a are  size of (38,38,1) 3D tensors,and h_a,w_a are 4 and 4 1D tensor and locations is a size of (batch_size,38,38,4,4) 5D tensor. At first ,I can not figure out why locations[:,:,:,:,0] can be divided by w_a and I dont know the locations[:,:,:,:,0] exactly mean.So, I have did some test in python as follow,and understand the priciple at last.Before I show the test code,I suppose to explaination the inner principle of that.locations[;,:,:,:,0] will take each elements in the last dimention of locations,so the shape of location[:,:,:,:,0] is (?,38,38,4,4),and the last 4 is each prior box's x -coordinate,and for matrix divition,it will calculate the last dimention's every elements.for example [1,1,1,1]/[2,2,2,2],and the result would be [.5,.5,.5,.5],the same goes for the rest calculations.so last time when we met this problem,we just need to see the last dimentions of locations,wether it's last dimentions is same as divisor.

Example:

import numpy as np
a=np.ones([3,3,3])
b=np.array([2,2,2])
c=a[:,:,0]/b
print(c)

Result:
[[0.5 0.5 0.5]
 [0.5 0.5 0.5]
 [0.5 0.5 0.5]]

As for the cx = location[:, :, :, :, 0] * w_a * prior_scaling[0] + x_a,locations[:,:,:,:,0] is a size of (?,38,38,4) tensor, and w_a is a (4,) 1D tensor,and the x_a is a (38,38,1) 3D tensor.therefore, location[:, :, :, :, 0] * w_a * prior_scaling[0] would produce a (?,,38,38,4) 3D tensor,and the cx would be a (?,38,38,4) 4D tensor.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值