def bbox3_to_mask(bbox3, shape): """ :param bbox3: [B, N, 4] y_from,x_from, y_size, x_size, normalized 0~1 :shape [H,W] :return: [B, N, H, W] mask """ assert bbox3.ndim == 3 and bbox3.dims[-1] == 4 x, y = coord2d(shape) # [H, W] x = x.expand_dims(0).expand_dims(0) # 1,1,H,W y = y.expand_dims(0).expand_dims(0) # 1,1,H,W bbox3 = bbox3.expand_dims(-1).expand_dims(-1) xfrom = bbox3[:, :, 1] # B,N,1,1 xto = xfrom + bbox3[:, :, 3] yfrom = bbox3[:, :, 0] # B,N,1,1 yto = yfrom + bbox3[:, :, 2] # broadcasting ix = tf.logical_and(x >= xfrom, x <= xto) # B,N,H,W iy = tf.logical_and(y >= yfrom, y <= yto) # B,N,H,W imask = tf.logical_and(ix, iy) return imask
def bbox2_to_mask(bbox2, shape): """ :param bbox2: [N, 4] y_from,x_from, y_size, x_size, normalized 0~1 :param shape: (H, W) :return: [N, H, W] mask """ # same to this # return tf.map_fn(lambda x: bbox1_to_mask(x, shape), bbox3, dtype=tf.bool) assert bbox2.ndim == 2 and bbox2.dims[-1] == 4 x, y = coord2d(shape) # H, W x = x.expand_dims(0) # 1HW y = y.expand_dims(0) # 1HW bbox2 = bbox2.expand_dims(-1).expand_dims(-1) xfrom = bbox2[:, 1] # N,1,1 xto = xfrom + bbox2[:, 3] yfrom = bbox2[:, 0] # N,1,1 yto = yfrom + bbox2[:, 2] ix = tf.logical_and(x >= xfrom, x <= xto) iy = tf.logical_and(y >= yfrom, y <= yto) imask = tf.logical_and(ix, iy) return imask
def bbox1_to_mask(bbox1, shape): """ bbox :param bbox1: [4] float32 value of (0~1) y_from,x_from, y_size, x_size, normalized 0~1 :param shape: [H,W] :return: mask of dtype tf.bool """ # ex: shape = (255, 255) # bbox = (N, 4) assert bbox1.ndim == 1 and bbox1.dims[-1] == 4 x, y = coord2d(shape) # check bbox x, bbox[1]~bbox[1]+bbox[3] # ix = bbox[1] <= x <= (bbox[1] + bbox[3]) # iy = bbox[0] <= y <= (bbox[0] + bbox[2]) ix = tf.logical_and(x >= bbox1[1], x <= (bbox1[1] + bbox1[3])) iy = tf.logical_and(y >= bbox1[0], y <= (bbox1[0] + bbox1[2])) imask = tf.logical_and(ix, iy) return imask
def sampling_xy_3r(img, xys, outsize=None, oob=None): """ differentiable image sampling (with interpolation) :param img: source image [HWC] :param xys: source coord [2, H'*W'] if outsize given :param outsize: [H',W'] or None, xys must has rank3 :return: [B,H',W',C] """ assert img.ndim == 3 oobv = oob if oobv is None: # oobv = tf.zeros(shape=(img.dims[-1]), dtype=tf.float32) # [0., 0., 0.] oobv = 0. # oobv = [0., 0., 0.] oobv = tf.convert_to_tensor(oobv) if outsize is None: outsize = tf.shape(xys)[1:] xys = xys.flat2d() H, W, C = img.shapes WH = tf.stack([W, H]).to_float().reshape((2, 1)) # XYf = (xys + 1.) * WH * 0.5 # scale to HW coord ( + 1 for start from 0) XYf = (xys + 0.5) * WH # * 0.5 # scale to HW coord ( + 1 for start from 0) XYS = tf.ceil(XYf) # left top weight # prepare weights w00 = XYS - XYf # [2, p] w11 = 1. - w00 # [2, p] # get near 4 pixels per pixel XYS = XYS.to_int32() # [2, p] # todo check xy order XYs = XYS - 1 Xs = tf.stack([XYs[0], XYS[0]]) Ys = tf.stack([XYs[1], XYS[1]]) # get mask of outof bound # leave option for filling value Xi = Xs.clip_by_value(0, W - 1) Yi = Ys.clip_by_value(0, H - 1) inb = tf.logical_and(Xi.equal(Xs), Yi.equal(Ys)) # [2, p] inb = tf.reduce_any(inb, axis=0, keepdims=True) # all oob? [1, p]- # inb = inb.expand_dims(2).to_float() # [1, p] inb = inb.reshape((-1, 1)).to_float() # [p, 1] 1 for channel # get 4 pixels [p, C] p00 = getpixel(img, tf.stack([Yi[0], Xi[0]]).T) p01 = getpixel(img, tf.stack([Yi[0], Xi[1]]).T) p10 = getpixel(img, tf.stack([Yi[1], Xi[0]]).T) p11 = getpixel(img, tf.stack([Yi[1], Xi[1]]).T) # stacked nearest : [4, p, C] near4 = tf.stack([p00, p01, p10, p11], axis=0) # XYw : 4 near point weights [4, pixel] w4 = tf.stack([ w00[1] * w00[0], # left top w00[1] * w11[0], # right top w11[1] * w00[0], # left bottom w11[1] * w11[0] ]) # right bottom # weighted sum of 4 nearest pixels broadcasting w4 = w4.reshape((4, -1, 1)) # interpolated = tf.sum(w4 * near4.to_float(), axis=1) # [p, C] interpolated = tf.sum(w4 * near4.to_float(), axis=0) # [p, C] # assign oob value # fill oob by broadcasting oobv = oobv.reshape((1, -1)) # [p, C] interpolated = interpolated * inb + oobv * (1. - inb) output = interpolated.reshape((outsize[0], outsize[1], C)) # reshape [p, C] => [H', W', C] return output