def get_zoom_negtive_samples(self): # feature map size n, h, w, ratio = len(self.masks), self.feat_h, self.feat_w, self.ratio match = np.ones((h * w, n), np.int) win_bbs = self.win_bbs objn_win_cens = self.objn_win_cens win_pts = self.win_pts ''' # condition 1: not too large or too small for i in range(n): match[:, i] = (self.bbs_hw[i].max() >= SLIDING_WINDOW_SIZE * ratio * OBJN_LOWER_BOUND_RATIO).all() & (self.bbs_hw[i].max() <= SLIDING_WINDOW_SIZE * ratio * OBJN_UPPER_BOUND_RATIO).all() ''' # condition 2: roughly contained for i in range(n): match[:, i] = match[:, i] & pts_in_bbs(self.centers[i], win_bbs) # condition 3: roughly centered for i in range(n): match[:, i] = match[:, i] & pts_in_bbs(self.centers[i], objn_win_cens) # choose the closest one dist = match * -1e9 for i in range(n): dist[:, i] += np.linalg.norm(win_pts - self.centers[i], axis=1) obj_ids = np.argmin(dist, axis=1) match[np.arange(h * w), obj_ids] += 1 match[match < 2] = 0 match[match == 2] = 1 self.zoom_negtive_match = match secondary_objns = np.zeros((h * w)) secondary_objns[match.any(axis=1)] = 1 objns = np.zeros((h * w)) objns[self.objn_match.any(axis=1)] = 1 negtive_samples = np.zeros((h * w)) negtive_samples[(secondary_objns == 1) & (objns == 0)] = 1 if self.zoom_negtive_samples is None: self.zoom_negtive_samples = negtive_samples else: self.zoom_negtive_samples = np.concatenate( (self.zoom_negtive_samples, negtive_samples), axis=0)
def get_zoom_negtive_samples(self): n, h, w, ratio = len(self.masks), self.feat_h, self.feat_w, self.ratio match = np.ones((h * w, n), np.int8) win_bbs = self.win_bbs objn_win_cens = self.objn_win_cens win_pts = self.win_pts # condition 2: roughly contained for i in range(n): match[:, i] = match[:, i] & pts_in_bbs(self.centers[i], win_bbs) # condition 3: roughly centered for i in range(n): match[:, i] = match[:, i] & pts_in_bbs(self.centers[i], objn_win_cens) # choose the closest one dist = match * -1e9 for i in range(n): dist[:, i] += np.linalg.norm(win_pts - self.centers[i], axis=1) obj_ids = np.argmin(dist, axis=1) match[np.arange(h * w), obj_ids] += 1 match[match < 2] = 0 match[match == 2] = 1 self.zoom_negtive_match = match secondary_objns = np.zeros((h * w)) secondary_objns[match.any(axis=1)] = 1 objns = np.zeros((h * w)) objns[self.objn_match.any(axis=1)] = 1 negtive_samples = np.zeros((h * w)) negtive_samples[(secondary_objns == 1) & (objns == 0)] = 1 if self.zoom_negtive_samples is None: self.zoom_negtive_samples = negtive_samples else: self.zoom_negtive_samples = np.concatenate( (self.zoom_negtive_samples, negtive_samples), axis=0)
def find_matched_masks(self): n, h, w, ratio = len(self.masks), self.feat_h, self.feat_w, self.ratio win_pts = np.array((np.arange(h * w, dtype=np.int) / w, np.arange(h * w, dtype=np.int) % w)) win_pts = win_pts.transpose((1, 0)).astype(np.float) win_pts *= ratio self.win_pts = win_pts objn_win_cens = np.hstack( (win_pts - (SLIDING_WINDOW_SIZE * ratio * OBJN_CENTER_RATIO / 2.0), win_pts + (SLIDING_WINDOW_SIZE * ratio * OBJN_CENTER_RATIO / 2.0))) mask_win_cens = np.hstack( (win_pts - (SLIDING_WINDOW_SIZE * ratio * MASK_CENTER_RATIO / 2.0), win_pts + (SLIDING_WINDOW_SIZE * ratio * MASK_CENTER_RATIO / 2.0))) self.objn_win_cens = objn_win_cens self.mask_win_cens = mask_win_cens win_bbs = np.hstack((win_pts - (SLIDING_WINDOW_SIZE * ratio / 2.0), win_pts + (SLIDING_WINDOW_SIZE * ratio / 2.0))) self.win_bbs = win_bbs self.objn_match = np.ones((h * w, n), np.int8) self.mask_match = np.ones((h * w, n), np.int8) # condition 1: neither too large nor too small for i in range(n): self.objn_match[:, i] = ( self.bbs_hw[i].max() >= SLIDING_WINDOW_SIZE * ratio * OBJN_LOWER_BOUND_RATIO).all() & ( self.bbs_hw[i].max() <= SLIDING_WINDOW_SIZE * ratio * OBJN_UPPER_BOUND_RATIO).all() for i in range(n): self.mask_match[:, i] = ( self.bbs_hw[i].max() >= SLIDING_WINDOW_SIZE * ratio * MASK_LOWER_BOUND_RATIO).all() & ( self.bbs_hw[i].max() <= SLIDING_WINDOW_SIZE * ratio * MASK_UPPER_BOUND_RATIO).all() # condition 2: roughly contained for i in range(n): self.objn_match[:, i] = self.objn_match[:, i] & pts_in_bbs( self.centers[i], win_bbs) for i in range(n): self.mask_match[:, i] = self.mask_match[:, i] & pts_in_bbs( self.centers[i], win_bbs) # condition 3: roughly centered for i in range(n): self.objn_match[:, i] = self.objn_match[:, i] & pts_in_bbs( self.centers[i], objn_win_cens) for i in range(n): self.mask_match[:, i] = self.mask_match[:, i] & pts_in_bbs( self.centers[i], mask_win_cens) # choose the closest one dist = self.objn_match * -1e9 for i in range(n): dist[:, i] += np.linalg.norm(win_pts - self.centers[i], axis=1) obj_ids = np.argmin(dist, axis=1) self.objn_match[np.arange(h * w), obj_ids] += 1 self.objn_match[self.objn_match < 2] = 0 self.objn_match[self.objn_match == 2] = 1 dist = self.mask_match * -1e9 for i in range(n): dist[:, i] += np.linalg.norm(win_pts - self.centers[i], axis=1) obj_ids = np.argmin(dist, axis=1) self.mask_match[np.arange(h * w), obj_ids] += 1 self.mask_match[self.mask_match < 2] = 0 self.mask_match[self.mask_match == 2] = 1 self.get_zoom_negtive_samples() self.get_shift_negtive_samples()
def find_matched_masks(self): # feature map size n, h, w, ratio = len(self.masks), self.feat_h, self.feat_w, self.ratio # win_pts: [h * w, 2] win_pts = np.array((np.arange(h * w, dtype=np.int) / w, np.arange(h * w, dtype=np.int) % w)) win_pts = win_pts.transpose((1, 0)).astype(np.float) win_pts *= ratio self.win_pts = win_pts # objn_win_cens: [h * w, 4] # mask_win_cens: [h * w, 4] objn_win_cens = np.hstack( (win_pts - (SLIDING_WINDOW_SIZE * ratio * OBJN_CENTER_RATIO / 2.0), win_pts + (SLIDING_WINDOW_SIZE * ratio * OBJN_CENTER_RATIO / 2.0))) mask_win_cens = np.hstack( (win_pts - (SLIDING_WINDOW_SIZE * ratio * MASK_CENTER_RATIO / 2.0), win_pts + (SLIDING_WINDOW_SIZE * ratio * MASK_CENTER_RATIO / 2.0))) self.objn_win_cens = objn_win_cens self.mask_win_cens = mask_win_cens # origin image img = self.image.copy() img += RGB_MEAN # visualize centers of windows ''' centers = win_cens + SLIDING_WINDOW_SIZE * CENTER_RATIO * ratio / 2 bbs = centers[:20, :] visualize_bbs(img, bbs) ''' # win_bbs: [h * w, 4] win_bbs = np.hstack((win_pts - (SLIDING_WINDOW_SIZE * ratio / 2.0), win_pts + (SLIDING_WINDOW_SIZE * ratio / 2.0))) self.win_bbs = win_bbs # [h * w, label_num) self.objn_match = np.ones((h * w, n), np.int) self.mask_match = np.ones((h * w, n), np.int) # condition 1: neither too large nor too small for i in range(n): self.objn_match[:, i] = ( self.bbs_hw[i].max() >= SLIDING_WINDOW_SIZE * ratio * OBJN_LOWER_BOUND_RATIO).all() & ( self.bbs_hw[i].max() <= SLIDING_WINDOW_SIZE * ratio * OBJN_UPPER_BOUND_RATIO).all() for i in range(n): self.mask_match[:, i] = ( self.bbs_hw[i].max() >= SLIDING_WINDOW_SIZE * ratio * MASK_LOWER_BOUND_RATIO).all() & ( self.bbs_hw[i].max() <= SLIDING_WINDOW_SIZE * ratio * MASK_UPPER_BOUND_RATIO).all() # condition 2: roughly contained for i in range(n): self.objn_match[:, i] = self.objn_match[:, i] & pts_in_bbs( self.centers[i], win_bbs) for i in range(n): self.mask_match[:, i] = self.mask_match[:, i] & pts_in_bbs( self.centers[i], win_bbs) # condition 3: roughly centered for i in range(n): self.objn_match[:, i] = self.objn_match[:, i] & pts_in_bbs( self.centers[i], objn_win_cens) for i in range(n): self.mask_match[:, i] = self.mask_match[:, i] & pts_in_bbs( self.centers[i], mask_win_cens) # choose the closest one dist = self.objn_match * -1e9 for i in range(n): dist[:, i] += np.linalg.norm(win_pts - self.centers[i], axis=1) obj_ids = np.argmin(dist, axis=1) self.objn_match[np.arange(h * w), obj_ids] += 1 self.objn_match[self.objn_match < 2] = 0 self.objn_match[self.objn_match == 2] = 1 dist = self.mask_match * -1e9 for i in range(n): dist[:, i] += np.linalg.norm(win_pts - self.centers[i], axis=1) obj_ids = np.argmin(dist, axis=1) self.mask_match[np.arange(h * w), obj_ids] += 1 self.mask_match[self.mask_match < 2] = 0 self.mask_match[self.mask_match == 2] = 1 # visualize matched sliding window ''' bbs = np.array((np.nonzero(self.objn_match)[0]/w, np.nonzero(self.objn_match)[0]%w)) * ratio bbs = np.vstack((bbs-SLIDING_WINDOW_SIZE/2*ratio, bbs+SLIDING_WINDOW_SIZE/2*ratio)).transpose((1, 0)) visualize_bbs(img, bbs[:16]) visualize_bbs(img, bbs[16:]) ''' # get hard training samples self.get_zoom_negtive_samples() self.get_shift_negtive_samples()