Last active
June 4, 2020 02:24
-
-
Save arunm8489/074af31c1d109d7117c2c80f31db53c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def detection_preprocess(x,inp_dim,anchors,num_classes,CUDA=False): | |
""" | |
This function will take input_dimension_of_image,anchors and number of classes as input | |
""" | |
# x --> 4D feature map | |
batch_size = x.size(0) | |
grid_size = x.size(2) | |
stride = inp_dim // x.size(2) # factor by which current feature map reduced from input | |
#grid_size = inp_dim // stride | |
bbox_attrs = 5 + num_classes #5 + 80 | |
num_anchors = len(anchors) #3 | |
#[1, 255, 13, 13] | |
prediction = x.view(batch_size, bbox_attrs*num_anchors, grid_size*grid_size) # 1x255x169 | |
prediction = prediction.transpose(1,2).contiguous() #1x169x255 | |
prediction = prediction.view(batch_size, grid_size*grid_size*num_anchors, bbox_attrs) #1x507x85 | |
# the dimension of anchors is wrt original image.We will make it corresponding to feature map | |
anchors = [(a[0]/stride, a[1]/stride) for a in anchors] | |
#Sigmoid the centre_X, centre_Y. and object confidencce | |
prediction[:,:,0] = torch.sigmoid(prediction[:,:,0]) | |
prediction[:,:,1] = torch.sigmoid(prediction[:,:,1]) | |
prediction[:,:,4] = torch.sigmoid(prediction[:,:,4]) | |
#Add the center offsets | |
grid = np.arange(grid_size) | |
a,b = np.meshgrid(grid, grid) | |
x_offset = torch.FloatTensor(a).view(-1,1) #(1,gridsize*gridsize,1) | |
y_offset = torch.FloatTensor(b).view(-1,1) | |
if CUDA: | |
x_offset = x_offset.cuda() | |
y_offset = y_offset.cuda() | |
x_y_offset = torch.cat((x_offset, y_offset), 1).repeat(1,num_anchors).view(-1,2).unsqueeze(0) | |
prediction[:,:,:2] += x_y_offset | |
#log space transform height and the width | |
anchors = torch.FloatTensor(anchors) | |
if CUDA: | |
anchors = anchors.cuda() | |
anchors = anchors.repeat(grid_size*grid_size, 1).unsqueeze(0) | |
prediction[:,:,2:4] = torch.exp(prediction[:,:,2:4])*anchors #width and height | |
prediction[:,:,5: 5 + num_classes] = torch.sigmoid((prediction[:,:, 5 : 5 + num_classes])) | |
prediction[:,:,:4] *= stride | |
return prediction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment