def _check_threshold_non_max_suppression(self, probs, threshold, radius): """ Check the non maximum supression with threshold. Explanation: Karim proposed a precise procedure which is standard for this: sort the outputs (for different locations in the same image) in decreasing order of probability (keeping only those above a threshold). Traverse that list from highest face probability down. For each entry, remove the entries below that correspond to locations that are too close spatially (corresponding to more than 50% overlap of the bounding boxes). """ assert self.img_shape is not None assert radius < self.img_shape[0] batch_size = probs.shape[0] processed_probs = [] for i in xrange(batch_size): dop = np.copy(self.data_predictor_positions) #Make the predictions a column vector preds = np.reshape(probs[i], (probs[i].shape[0], 1)) pred_pos = np.hstack((dop, preds)) pred_pos = pred_pos[pred_pos[:, 1].argsort()] sorted_preds = pred_pos[:, 1] new_preds = np.zeros(preds.shape) for j in xrange(self.preds_size): #preds_view = preds.reshape(self.output_map_shp[:2]) if sorted_preds[j] < threshold: new_preds[pred_pos[j, 0]] = 0 else: #Remove the spatially close entries, hence check the, check the neighbours #within radius neighbourhood. #The problem is that the spatially close outputs might be corresponding to the same face. #Rows: r = j % self.output_map_shp[1] #Columns: c = j - r * self.output_map_shp[1] iterator = circle_around(r, c) #Move on the grid in a circular fashion for loc in iterator: if loc[0] == radius: break y, x = loc[1] n = y * self.output_map_shp[0] + self.output_map_shp[1] if (loc[1][0] >= 0 and loc[1][0] <= self.img_shape[0] and loc[1][1] <= self.img_shape[1] and loc[1][1] >= 0): if (new_preds[pred_pos[n, 0]] < new_preds[pred_pos[j, 0]]): new_preds[pred_pos[n, 0]] = 0 processed_probs.append(new_preds) processed_probs = np.asarray(processed_probs) return processed_probs
def _check_threshold_non_max_suppression(self, probs, threshold, radius): """ Check the non maximum supression with threshold. Explanation: Karim proposed a precise procedure which is standard for this: sort the outputs (for different locations in the same image) in decreasing order of probability (keeping only those above a threshold). Traverse that list from highest face probability down. For each entry, remove the entries below that correspond to locations that are too close spatially (corresponding to more than 50% overlap of the bounding boxes). """ assert self.img_shape is not None assert radius < self.img_shape[0] batch_size = probs.shape[0] processed_probs = [] for i in xrange(batch_size): dop = np.copy(self.data_predictor_positions) #Make the predictions a column vector preds = np.reshape(probs[i], (probs[i].shape[0], 1)) pred_pos = np.hstack((dop, preds)) pred_pos = pred_pos[pred_pos[: , 1].argsort()] sorted_preds = pred_pos[: , 1] new_preds = np.zeros(preds.shape) for j in xrange(self.preds_size): #preds_view = preds.reshape(self.output_map_shp[:2]) if sorted_preds[j] < threshold: new_preds[pred_pos[j , 0]] = 0 else: #Remove the spatially close entries, hence check the, check the neighbours #within radius neighbourhood. #The problem is that the spatially close outputs might be corresponding to the same face. #Rows: r = j % self.output_map_shp[1] #Columns: c = j - r * self.output_map_shp[1] iterator = circle_around(r, c) #Move on the grid in a circular fashion for loc in iterator: if loc[0] == radius: break y, x = loc[1] n = y * self.output_map_shp[0] + self.output_map_shp[1] if (loc[1][0] >= 0 and loc[1][0] <= self.img_shape[0] and loc[1][1] <= self.img_shape[1] and loc[1][1] >= 0): if (new_preds[pred_pos[n, 0]] < new_preds[pred_pos[j, 0]]): new_preds[pred_pos[n, 0]] = 0 processed_probs.append(new_preds) processed_probs = np.asarray(processed_probs) return processed_probs
def _check_threshold_non_max_suppression(self, probs, threshold, radius): assert self.img_shape is not None assert radius < self.img_shape[0] batch_size = self.output_map_shp[-1] processed_probs = [] for i in xrange(batch_size): dop = np.copy(self.data_predictor_positions) preds = np.reshape(probs[i], (probs[i].shape[0], 1)) pred_pos = np.vstack((dop, preds)) pred_pos = pred_pos[pred_pos[:, 1].argsort()] sorted_preds = pred_pos[:, 1] border = 0 new_preds = np.zeros() for j in xrange(self.preds_size): preds_view = preds.reshape(self.output_map_shp[:2]) if sorted_preds[j] < threshold: new_preds[pred_pos[j, 0]] = 0 else: #Remove the spatially close entries, hence check the, check the neighbours #within radius neighbourhood. #The problem is that the spatially close outputs might be #corresponding to the same face. #Rows: r = j % self.output_map_shp[1] #Columns: c = j - r * self.output_map_shp[1] iterator = circle_around(r, c) #Move on the grid in a circular fashion for loc in iterator: if loc[0] == radius: break y, x = loc[1] n = y * self.output_map_shp[0] + self.output_map_shp[1] if (loc[1][0] >= 0 and loc[1][0] <= self.img_shape[0] and loc[1][1] <= self.img_shape[1] and loc[1][1] >= 0): if new_preds[pred_pos[n, 0]] < new_preds[pred_pos[j, 0]]: new_preds[pred_pos[n, 0]] = 0 processed_probs = new_preds return new_preds
def _check_threshold_non_max_suppression(self, probs, threshold, radius): assert self.img_shape is not None assert radius < self.img_shape[0] batch_size = self.output_map_shp[-1] processed_probs = [] for i in xrange(batch_size): dop = np.copy(self.data_predictor_positions) preds = np.reshape(probs[i], (probs[i].shape[0], 1)) pred_pos = np.vstack((dop, preds)) pred_pos = pred_pos[pred_pos[:,1].argsort()] sorted_preds = pred_pos[:, 1] border = 0 new_preds = np.zeros() for j in xrange(self.preds_size): preds_view = preds.reshape(self.output_map_shp[:2]) if sorted_preds[j] < threshold: new_preds[pred_pos[j,0]] = 0 else: #Remove the spatially close entries, hence check the, check the neighbours #within radius neighbourhood. #The problem is that the spatially close outputs might be #corresponding to the same face. #Rows: r = j % self.output_map_shp[1] #Columns: c = j - r*self.output_map_shp[1] iterator = circle_around(r, c) #Move on the grid in a circular fashion for loc in iterator: if loc[0] == radius: break y, x = loc[1] n = y * self.output_map_shp[0] + self.output_map_shp[1] if (loc[1][0] >= 0 and loc[1][0] <= self.img_shape[0] and loc[1][1] <= self.img_shape[1] and loc[1][1] >= 0): if new_preds[pred_pos[n, 0]] < new_preds[pred_pos[j, 0]]: new_preds[pred_pos[n, 0]] = 0 processed_probs = new_preds return new_preds