Created
January 17, 2019 23:24
-
-
Save LiamHz/956bb2b79078eaf2a496636d2bc1f7d2 to your computer and use it in GitHub Desktop.
This file contains 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
from __future__ import division | |
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
from torch.autograd import Variable | |
import numpy as np | |
import cv2 | |
def predict_transform(prediction, inp_dim, anchors, num_classes, CUDA = True): | |
batch_size = prediction.size(0) | |
stride = inp_dim // prediction.size(2) | |
grid_size = inp_dim // stride | |
bbox_attrs = 5 + num_classes | |
num_anchors = len(anchors) | |
prediction = prediction.view(batch_size, bbox_attrs*num_anchors, grid_size*grid_size) | |
prediction = prediction.transpose(1,2).contiguous() | |
prediction = prediction.view(batch_size, grid_size*grid_size*num_anchors, bbox_attrs) | |
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) | |
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 | |
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