# Function defencoder(self,boxes,labels): ''' boxes (tensor) [[x1,y1,x2,y2],[]] labels (tensor) [...] return 7x7x30 ''' grid_num = 14# The number of grids, 网格数目 target = torch.zeros((grid_num,grid_num,30)) # Create a grid_num x grid_num x 30 array, 30 means Bx5+classes_num. 创建一个 网格数目x网格数目x30的数组 cell_size = 1./grid_num wh = boxes[:,2:]-boxes[:,:2] # Caculate ground true box width and height cxcy = (boxes[:,2:]+boxes[:,:2])/2# Caculate center x,y. Nx2 for i in range(cxcy.size()[0]): # Loop from first ground true box to the last. 从第一个ground true box循环到最后一个 cxcy_sample = cxcy[i] col_row = (cxcy_sample/cell_size).ceil()-1# i = column, j = row target[int(col_row[1]),int(col_row[0]),4] = 1# Set the confidence to 1. target[int(col_row[1]),int(col_row[0]),9] = 1# Set the confidence to 1. target[int(col_row[1]),int(col_row[0]),int(labels[i])+9] = 1# Set the class label to 1. delta_xy = cxcy_sample/cell_size - col_row #匹配到的网格的左上角相对坐标 target[int(col_row[1]),int(col_row[0]),2:4] = wh[i] target[int(col_row[1]),int(col_row[0]),:2] = delta_xy target[int(col_row[1]),int(col_row[0]),7:9] = wh[i] target[int(col_row[1]),int(col_row[0]),5:7] = delta_xy return target