def get_region_boxes(sp, reg2sp): x = np.arange(0, sp.shape[1]) y = np.arange(0, sp.shape[0]) xv, yv = np.meshgrid(x, y) maxsp = np.max(sp) sp1=sp.reshape(-1)-1 xv = xv.reshape(-1) yv = yv.reshape(-1) spxmin = accum.my_accumarray(sp1,xv, maxsp, 'min') spymin = accum.my_accumarray(sp1,yv, maxsp, 'min') spxmax = accum.my_accumarray(sp1,xv, maxsp, 'max') spymax = accum.my_accumarray(sp1,yv, maxsp, 'max') Z = reg2sp.astype(float, copy=True) Z[reg2sp==0] = np.inf xmin = np.nanmin(np.multiply(spxmin.reshape(-1,1), Z),0) ymin = np.nanmin(np.multiply(spymin.reshape(-1,1), Z),0) xmax = np.amax(np.multiply(spxmax.reshape(-1,1), reg2sp),0) ymax = np.amax(np.multiply(spymax.reshape(-1,1), reg2sp), 0) xmin[np.isinf(xmin)]=0 ymin[np.isinf(ymin)]=0 boxes = np.hstack((xmin.reshape(-1,1), ymin.reshape(-1,1), xmax.reshape(-1,1), ymax.reshape(-1,1))) return boxes
def get_region_boxes(sp, reg2sp): x = np.arange(0, sp.shape[1]) y = np.arange(0, sp.shape[0]) xv, yv = np.meshgrid(x, y) maxsp = np.max(sp) sp1 = sp.reshape(-1) - 1 xv = xv.reshape(-1) yv = yv.reshape(-1) spxmin = accum.my_accumarray(sp1, xv, maxsp, 'min') spymin = accum.my_accumarray(sp1, yv, maxsp, 'min') spxmax = accum.my_accumarray(sp1, xv, maxsp, 'max') spymax = accum.my_accumarray(sp1, yv, maxsp, 'max') Z = reg2sp.astype(float, copy=True) Z[reg2sp == 0] = np.inf xmin = np.nanmin(np.multiply(spxmin.reshape(-1, 1), Z), 0) ymin = np.nanmin(np.multiply(spymin.reshape(-1, 1), Z), 0) xmax = np.amax(np.multiply(spxmax.reshape(-1, 1), reg2sp), 0) ymax = np.amax(np.multiply(spymax.reshape(-1, 1), reg2sp), 0) xmin[np.isinf(xmin)] = 0 ymin[np.isinf(ymin)] = 0 boxes = np.hstack((xmin.reshape(-1, 1), ymin.reshape(-1, 1), xmax.reshape(-1, 1), ymax.reshape(-1, 1))) return boxes
def project_to_sp(sp, mask): sp1=sp.reshape(-1)-1 mask1 = mask.reshape(-1) maxsp = np.max(sp) reg2sp = accum.my_accumarray(sp1,mask1,maxsp) areas = accum.my_accumarray(sp1,1.,maxsp) reg2sp = reg2sp/areas # projected_mask = reg2sp[sp-1] return reg2sp
def project_to_sp(sp, mask): sp1 = sp.reshape(-1) - 1 mask1 = mask.reshape(-1) maxsp = np.max(sp) reg2sp = accum.my_accumarray(sp1, mask1, maxsp) areas = accum.my_accumarray(sp1, 1., maxsp) reg2sp = reg2sp / areas # projected_mask = reg2sp[sp-1] return reg2sp
def get_bboxes(inst): x = np.arange(0, inst.shape[1]) y = np.arange(0, inst.shape[0]) xv, yv = np.meshgrid(x, y) maxinst = np.max(inst) inst1 = inst.reshape(-1) xv = xv.reshape(-1) yv = yv.reshape(-1) idx = inst1>0 inst1=inst1[idx] xv=xv[idx] yv = yv[idx] instxmin = accum.my_accumarray(inst1-1, xv, maxinst, 'min') instymin = accum.my_accumarray(inst1-1, yv, maxinst, 'min') instxmax = accum.my_accumarray(inst1-1, xv, maxinst, 'max') instymax = accum.my_accumarray(inst1-1, yv, maxinst, 'max') boxes = np.hstack((instxmin.reshape(-1,1),instymin.reshape(-1,1),instxmax.reshape(-1,1), instymax.reshape(-1,1))) return boxes
def compute_overlap_sprep(sp, reg2sp, inst): #compute intersection between all instances and all sp intsp = accum.my_accumarray((inst.reshape(-1), sp.reshape(-1)-1),1, (np.max(inst)+1,np.max(sp))) #compute the total intersection R=reg2sp.astype(np.float32, copy=True) inters = np.dot(intsp, R) #to compute the union, compute region areas spareas = accum.my_accumarray(sp.reshape(-1)-1,1,np.max(sp)) totalareas = np.dot(spareas.reshape(1,-1),R) #compute instance areas instareas = accum.my_accumarray(inst.reshape(-1),1,np.max(inst)+1) #union is sum of areas - inters uni = instareas.reshape(-1,1)+totalareas.reshape(1,-1)-inters #union that is 0 should be set to 1 for safety uni[uni==0]=1. ov = inters/uni #the fist instance is bg so remove it ov = ov[1:,:] return ov
def compute_overlap_sprep(sp, reg2sp, inst): #compute intersection between all instances and all sp intsp = accum.my_accumarray((inst.reshape(-1), sp.reshape(-1) - 1), 1, (np.max(inst) + 1, np.max(sp))) #compute the total intersection R = reg2sp.astype(np.float32, copy=True) inters = np.dot(intsp, R) #to compute the union, compute region areas spareas = accum.my_accumarray(sp.reshape(-1) - 1, 1, np.max(sp)) totalareas = np.dot(spareas.reshape(1, -1), R) #compute instance areas instareas = accum.my_accumarray(inst.reshape(-1), 1, np.max(inst) + 1) #union is sum of areas - inters uni = instareas.reshape(-1, 1) + totalareas.reshape(1, -1) - inters #union that is 0 should be set to 1 for safety uni[uni == 0] = 1. ov = inters / uni #the fist instance is bg so remove it ov = ov[1:, :] return ov
def paste_output_sp(output, boxes, im_shape, sp, target_output_size = [50, 50]): pasted_output =np.zeros((output.shape[0], output.shape[1], np.max(sp))) #clip and round new_boxes = np.hstack((np.ceil(boxes[:,0:2]),np.ceil(boxes[:,2:4]))) new_boxes = clip_boxes(new_boxes, im_shape) xmin = new_boxes[:,0] ymin = new_boxes[:,1] w = new_boxes[:,2]-xmin+1. h = new_boxes[:,3]-ymin+1. for item in range(output.shape[0]): xmin_ = xmin[item] ymin_ = ymin[item] w_ = w[item] h_ = h[item] y_all = np.arange(ymin_, ymin_+h_) Y_all = (y_all-ymin_)*target_output_size[0]/h_ Y_all = np.maximum(0, np.minimum(target_output_size[0]-1, np.floor(Y_all))) x_all = np.arange(xmin_, xmin_+w_) X_all = (x_all-xmin_)*target_output_size[1]/w_ X_all = np.maximum(0, np.minimum(target_output_size[1]-1, np.floor(X_all))) x_all = x_all.astype(np.intp) y_all = y_all.astype(np.intp) X_all = X_all.astype(np.intp) Y_all = Y_all.astype(np.intp) spind = sp[np.ix_(y_all,x_all)]-1 for channel in range(output.shape[1]): pasted_output_this = pasted_output[item,channel] output_this = output[item, channel] vals = output_this[np.ix_(Y_all,X_all)] np.add.at(pasted_output_this, spind, vals) #finally divide by sum counts = accum.my_accumarray(sp.reshape(-1)-1,1,np.max(sp)) pasted_output = pasted_output/counts.reshape((1,1,-1)) return pasted_output