def sample_area(self, pred_box1, num, thre_max=0.5, thre_min=0): # sample area pred_box = pred_box1.squeeze() pred_box_w = pred_box[2] - pred_box[0] pred_box_h = pred_box[3] - pred_box[1] pred_box_cx = pred_box[0] + pred_box_w / 2.0 pred_box_cy = pred_box[1] + pred_box_h / 2.0 samples = np.ones((num, 4), dtype=np.float32) samples_wh = np.ones((num, 2), dtype=np.float32) # samples_wh[:,0]*=pred_box_w # samples_wh[:,1]*=pred_box_h cur_n = 0 # s:(0.3,0.7664) s = 0.23 + np.random.chisquare(3, num) * 0.09 s = 1 - s ind = np.where(s > 0.3)[0] s = s[ind[:num / 2]] samples_wh[:num / 2, 0] = pred_box_w * s samples_wh[:num / 2, 1] = pred_box_h * s # s:(1.3258,2) s = 1.325 + np.random.chisquare(3, num) * 0.125 ind = np.where(s <= 2)[0] s = s[ind[:num / 2]] samples_wh[num / 2:, 0] = pred_box_w * s samples_wh[num / 2:, 1] = pred_box_h * s # (x_c,y_c)-->(x1,y1,x2,y2) samples[:, 0] = pred_box_cx - samples_wh[:, 0] / 2.0 samples[:, 1] = pred_box_cy - samples_wh[:, 1] / 2.0 samples[:, 2] = pred_box_cx + samples_wh[:, 0] / 2.0 samples[:, 3] = pred_box_cy + samples_wh[:, 1] / 2.0 samples = utils.restrict_box(samples, self.width, self.height) iou = utils.calc_iou(pred_box, samples) iou = iou[:, np.newaxis] return samples, iou
def estimate_const(self,conf,k=10): cur_pos = np.zeros((6,), dtype=np.float32) # avreage cur_pos = np.average(self.particles, weights=conf, axis=0) # (cx,cy,s,r,dcx,dcy) if self.mltply: cur_pos = utils.state_to_bbox_m(cur_pos, self.area, self.ratio) else: cur_pos = utils.state_to_bbox(cur_pos, self.area, self.ratio) cur_pos = utils.restrict_box(cur_pos, self.width, self.height) return cur_pos
def sample_xy(self, pred_box1, num, thre_max=0.5, thre_min=0.0): # only perform xy shift # x alone pred_box = pred_box1.squeeze() alone = 1 / 3.0 both = 1 - np.sqrt(2 / 3.0) pred_box_w = pred_box[2] - pred_box[0] pred_box_h = pred_box[3] - pred_box[1] pred_box_cx = pred_box[0] + pred_box_w / 2.0 pred_box_cy = pred_box[1] + pred_box_h / 2.0 samples = np.ones((num, 4), dtype=np.float32) samples_c = np.ones((num, 2), dtype=np.float32) samples_c[:, 0] *= pred_box_cx samples_c[:, 1] *= pred_box_cy num1 = num / 2 # +-dx,+-dy dx = 0.25 + np.random.chisquare(3, num1) * 0.1 samples_c[:num1 / 4, 0] += (pred_box_w * dx[:num1 / 4]) # dx samples_c[num1 / 4:num1 / 2, 0] -= (pred_box_w * dx[num1 / 4:num1 / 2]) # -dx samples_c[num1 / 2:3 * num1 / 4, 1] += (pred_box_h * dx[num1 / 2:num1 * 3 / 4]) # dy samples_c[3 * num1 / 4:num1, 1] -= (pred_box_h * dx[3 * num1 / 4:]) # -dy # |dx2|=|dy2| dx2 = 0.15 + np.random.chisquare(3, num) * 0.03 # dx,dy samples_c[num1:(num1 + num1 / 4), 0] += (pred_box_w * dx2[:num1 / 4]) samples_c[num1:(num1 + num1 / 4), 1] += (pred_box_h * (dx2[num1 / 4:num1 / 2])) # -dx,dy samples_c[(num1 + num1 / 4):(num1 + num1 / 2), 0] -= (pred_box_w * dx2[num1 / 2:num1 * 3 / 4]) samples_c[(num1 + num1 / 4):(num1 + num1 / 2), 1] += (pred_box_h * (dx2[3 * num1 / 4:num1])) # dx,-dy samples_c[(num1 + num1 / 2):(num1 + 3 * num1 / 4), 0] += (pred_box_w * dx2[num1:(num1 + num1 / 4)]) samples_c[(num1 + num1 / 2):(num1 + 3 * num1 / 4), 1] -= ( pred_box_h * (dx2[(num1 + num1 / 4):(num1 + num1 / 2)])) # -dx,-dy samples_c[(num1 + 3 * num1 / 4):, 0] -= (pred_box_w * dx2[(num1 + num1 / 2):(num1 + 3 * num1 / 4)]) samples_c[(num1 + 3 * num1 / 4):, 1] -= ( pred_box_h * (dx2[(num1 + 3 * num1 / 4):])) # (x_c,y_c)-->(x1,y1,x2,y2) samples[:, 0] = samples_c[:, 0] - pred_box_w / 2.0 samples[:, 1] = samples_c[:, 1] - pred_box_h / 2.0 samples[:, 2] = samples_c[:, 0] + pred_box_w / 2.0 samples[:, 3] = samples_c[:, 1] + pred_box_h / 2.0 samples = utils.restrict_box(samples, self.width, self.height) iou = utils.calc_iou(pred_box, samples) iou = iou[:, np.newaxis] return samples, iou
def restrict_particles_extern(self,states,w,h): if self.mltply: bboxes = utils.state_to_bbox_m(states, self.area, self.ratio) else: bboxes = utils.state_to_bbox(states, self.area, self.ratio) # restrict x1,y1,x2,y2 # bboxes[:, 0] = np.minimum(np.maximum(0, bboxes[:, 0]), w) # bboxes[:, 2] = np.minimum(np.maximum(0, bboxes[:, 2]), w) # bboxes[:, 1] = np.minimum(np.maximum(0, bboxes[:, 1]), h) # bboxes[:, 3] = np.minimum(np.maximum(0, bboxes[:, 3]), h) if self.mltply: bboxes = utils.restrict_box_m(bboxes, w, h) else: bboxes = utils.restrict_box(bboxes, w, h) # prev_particles= self.particles if self.mltply: state_half = utils.bbox_to_states_m(bboxes, self.area, self.ratio) else: state_half = utils.bbox_to_states(bboxes, self.area, self.ratio) return state_half
def sample_iou_new(self, gt_box, Q, T, R, N, thre_min=0, thre_max=1): sample_boxN = [] sample_iouN = [] cur_n = 0 D = np.array([[self.box_w, 0], [0, self.box_h]]) QL = D * Q # QL = np.linalg.cholesky(Q) a = 1.5 # 1.5 gt_state = utils.bbox_to_states(gt_box, self.area, self.ratio) sample_times = 0 chg_i = 0 while cur_n < N: sample_particles = np.zeros((N, 6), dtype=np.float32) QL = D * Q sample_particles[:, :2] = gt_state[:, :2] + np.dot(QL, np.random.randn(2, N)).transpose() if self.mltply: dsn = np.random.randn(N) * T ds = np.power(a, dsn) else: ds = 1 + np.random.randn(N) * T ds = np.maximum(0.01, ds) # in case of ds<0 sample_particles[:, 2] = gt_state[:, 2] * ds if self.mltply: dr = np.random.randn(N) * R + 1 else: dr = 1 + np.random.randn(N) * R dr = np.maximum(0.01, dr) # in case of dr<0 sample_particles[:, 3] = gt_state[:, 3] * dr # get box if self.mltply: sample_box = utils.state_to_bbox_m(sample_particles, self.area, self.ratio) else: sample_box = utils.state_to_bbox(sample_particles, self.area, self.ratio) sample_box = utils.restrict_box(sample_box, self.width, self.height) # compute iou sample_iou = utils.calc_iou(gt_box, sample_box) # restrict iou ind = np.where((sample_iou >= thre_min) & (sample_iou <= thre_max)) sample_box = sample_box[ind[0]] sample_iou = sample_iou[ind[0]] cur_n += sample_box.shape[0] sample_boxN.append(sample_box) sample_iouN.append(sample_iou.reshape((-1, 1))) if ind[0].shape[0] < N / 2 and thre_max >= 0.8: if chg_i == 0: Q *= 0.5 chg_i = (chg_i + 1) % 3 else: if chg_i == 1: T *= 0.5 T = np.minimum(T, 0.5) chg_i = (chg_i + 1) % 3 else: R *= 0.5 R = np.minimum(R, 0.5) chg_i = (chg_i + 1) % 3 if ind[0].shape[0] < N / 2 and thre_min <= 0.5: if chg_i == 0: Q *= 2 chg_i = (chg_i + 1) % 3 else: if chg_i == 1: T *= 2 T = np.minimum(T, 0.5) chg_i = (chg_i + 1) % 3 else: R *= 2 R = np.minimum(R, 0.5) chg_i = (chg_i + 1) % 3 sample_times += 1 if sample_times >= 100: # and cur_n>N/2.0:#100 # print "Caution: too many loops in sampling" # break raise OverflowError() if cur_n >= N: sample_boxN = np.vstack(sample_boxN)[:N, :] sample_iouN = np.vstack(sample_iouN)[:N, :] else: diff_n = N - cur_n sample_iouN = np.vstack(sample_iouN) sample_boxN = np.vstack(sample_boxN) diff_ind = random.sample(range(cur_n), diff_n) # need to ensure diff_n<cur_n sample_boxN = np.vstack([sample_boxN, sample_boxN[diff_ind]]) sample_iouN = np.vstack([sample_iouN, sample_iouN[diff_ind]]) return sample_boxN, sample_iouN
def estimate(self, k=10): '''estimate current position''' np.copyto(self.prev_pos, self.cur_pos) np.copyto(self.prev_c, self.cur_c) cur_pos = np.zeros((6,), dtype=np.float32) # there are two methods to estimating cur_pos: average or max # avreage maxw = np.max(self.weights) inds = np.where(self.weights>0.5*maxw)[0] cur_pos = np.average(self.particles[inds], weights=self.weights.squeeze()[inds], axis=0) # (cx,cy,s,r,dcx,dcy) # max # cur_pos = self.particles[np.argmax(self.weights)] # max k pos ''' #k = 3 #print type(self.weights), self.weights.shape #print 'max: ',np.max(self.weights) #print 'min: ',np.min(self.weights) sort_ind = np.argsort(-self.weights.squeeze()) cur_pos = np.average(self.particles[sort_ind[:k]], weights=self.weights[sort_ind[:k]], axis=0) ''' ''' #hist estimate count_xy,edge_x,edge_y=np.histogram2d(self.particles[:,0],self.particles[:,1],bins=40,weights=self.weights.squeeze()) top3=(-count_xy).argsort(axis=None)[:2] ind_x=top3[:]/count_xy.shape[1] ind_y=top3[:]%count_xy.shape[1] if abs(max(ind_x)-min(ind_x))==1: #adjacent ind_right=max(ind_x) ind_left=min(ind_x) edge_x1=edge_x[ind_left] edge_x2=edge_x[ind_right+1] else: edge_x1=edge_x[ind_x[0]] edge_x2=edge_x[ind_x[0]+1] if abs(max(ind_y)-min(ind_y))==1: #adjacent ind_right = max(ind_y) ind_left = min(ind_y) edge_y1=edge_y[ind_left] edge_y2=edge_y[ind_right+1] else: edge_y1=edge_y[ind_y[0]] edge_y2=edge_y[ind_y[0]+1] cur_pos[0]=(edge_x1+edge_x2)/2.0 cur_pos[1]=(edge_y1+edge_y2)/2.0 #area and ratio count_sr, edge_s, edge_r = np.histogram2d(self.particles[:, 2], self.particles[:, 3], bins=20, weights=self.weights.squeeze()) top3 = (-count_sr).argsort(axis=None)[:2] ind_s = top3[:] / count_sr.shape[1] ind_r = top3[:] % count_sr.shape[1] if abs(max(ind_s) - min(ind_s)) == 1: # adjacent ind_right = max(ind_s) ind_left = min(ind_s) edge_s1 = edge_s[ind_left] edge_s2 = edge_s[ind_right + 1] else: edge_s1 = edge_s[ind_s[0]] edge_s2 = edge_s[ind_s[0] + 1] if abs(max(ind_r) - min(ind_r)) == 1: # adjacent ind_right = max(ind_r) ind_left = min(ind_r) edge_r1 = edge_r[ind_left] edge_r2 = edge_r[ind_right + 1] else: edge_r1 = edge_r[ind_r[0]] edge_r2 = edge_r[ind_r[0] + 1] cur_pos[2] = (edge_s1 + edge_s2) / 2.0 cur_pos[3] = (edge_r1 + edge_r2) / 2.0 ''' if self.mltply: self.cur_pos = utils.state_to_bbox_m(cur_pos, self.area, self.ratio) else: self.cur_pos = utils.state_to_bbox(cur_pos, self.area, self.ratio) self.cur_pos = utils.restrict_box(self.cur_pos, self.width, self.height) self.box_h = self.cur_pos[0, 3] - self.cur_pos[0, 1] self.box_w = self.cur_pos[0, 2] - self.cur_pos[0, 0] self.cur_c[0] = np.minimum(np.maximum(0, cur_pos[0]), self.width) self.cur_c[1] = np.minimum(np.maximum(0, cur_pos[1]), self.height) # update self.cur_a and self.prev_a self.prev_a = self.cur_a self.cur_a = self.box_w * self.box_h / self.area # self.cur_pos[0, 2] = np.minimum(np.maximum(0, cur_pos[2]), self.width) # self.cur_pos[0, 3] = np.minimum(np.maximum(0, cur_pos[3]), self.height) # print 'prev_pos = ', self.prev_pos # print 'cur_pos = ', self.cur_pos # calculate s and r s = self.particles[:, 2] r = self.particles[:, 3] return cur_pos, s, r