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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章