def id_to_rgb(cls, id): """ Cover float[-2**20~2**20] to float32[0.~1.] rgb Parameters ---------- id : int, float or numpy float[-2**20~2**20]. Returns ------- (3,)float32 RGB for Blender. """ if isinstance(id, (float, int)): id = np.array(id) rgb = np.zeros(id.shape + (3, ), dtype=np.float64) rgb[..., 0] = id < 0 absf = np.abs(id) int_part = absf // 1 # rgb[..., 1] = 1 - 1 / (int_part + 1) poww = np.int32(np.log2(int_part + 1, dtype=np.float32)) + 1 denominator = (2**poww).round() rgb[..., 1] = ((int_part - denominator // 2 + 1) * 2 + 1) / denominator rgb[..., 2] = absf % 1 return rgb
def id_to_rgb(f): if isinstance(f, (float, int)): f = np.array(f) rgb = np.zeros(f.shape + (3, ), dtype=np.float64) rgb[..., 0] = f < 0 absf = np.abs(f) int_part = absf // 1 rgb[..., 1] = 1 - 1 / (int_part + 1) rgb[..., 2] = absf % 1 return rgb
def getMaskOfPgt(argkv): pgt, cycle_tmpl, shape, out_cyc_r = argkv.get('pgt'), argkv.get( 'cycle_tmpl'), argkv.get('shape'), argkv.get('out_cyc_r') if not isinstance(pgt, Vector): pgt = Vector(pgt) bg = np.zeros(shape[-2:], dtype=np.int32) if out_cyc_r: bg -= 1 mask = pasteImg(bg, cycle_tmpl, pgt - cycle_tmpl.shape[-1] // 2) # g() # print(pgt) # show-mask # tree-argkv return (mask == 0), (mask == 1),
def __init__(self, cyc_r=7, log_freq=60 * 5, poolings=['avg'], number_worker=8, out_cyc_r=None, layerNorma=True, probMargin=None, temper=1): super(SpatialSoftmax, self).__init__() self.out_cyc_r = out_cyc_r self.poolings = poolings self.log_freq = log_freq self.cyc_r = cyc_r self.layerNorma = layerNorma self.probMargin = probMargin self.temper = temper # raw_cycle_tmpl = getWeightCore(300, seta=.25) # raw_cycle_tmpl = raw_cycle_tmpl > raw_cycle_tmpl[150,0] # cycle_tmpl_np = resize(raw_cycle_tmpl, np.int8((cyc_r*2, cyc_r*2, ))) cyc_tmpl_np = getCycle(cyc_r) cycle_tmpl = np.int32(cyc_tmpl_np > .5) # g() if out_cyc_r is not None: # out_cycle_tmpl_np = resize(raw_cycle_tmpl, (out_cyc_r*2, out_cyc_r*2, )) # out_cycle_tmpl = np.int32(out_cycle_tmpl_np >.5) - 1 out_cycle_tmpl_np = getCycle(out_cyc_r) out_cycle_tmpl = np.int32(out_cycle_tmpl_np > .5) - 1 inCycIndx = np.zeros(out_cycle_tmpl.shape, np.bool) pasteImg(inCycIndx, cycle_tmpl, Vector([out_cyc_r - cyc_r] * 2)) out_cycle_tmpl[inCycIndx] = 1 cycle_tmpl = out_cycle_tmpl # self.cycle_tmpl = th.from_numpy(cycle_tmpl) self.cycle_tmpl = (cycle_tmpl)
@author: yl """ import cv2 from boxx import * import skimage.data as sda from boxx import sin, cos from boxx import Vector, np, timegap, intround import time img = sda.astronaut() imgs = [img] hw = Vector([800, 800]) imgHw = hw bg = np.zeros(list(imgHw) + [3]).astype(np.uint8) bgc = 250, 85, 70 bg[:] = bgc #bg[:] = 0 from skimage import draw line = draw.line(0, 0, 10, 10) def drawXyCycle(img, yx=(200, 300), r=8, color=255): cir = draw.circle(*yx, r) cir = list(cir) cir[0] = cir[0].clip(0, img.shape[0] - 1) cir[1] = cir[1].clip(0, img.shape[1] - 1)
batchPerGpu = 32 channl = 17 gpun = th.cuda.device_count() batch = batchPerGpu * gpun cyc_r = intround((25) / np.array([353, 257]).mean() * hw.mean()) pgt = hw // 2 pshift = (hw * .13).intround() pshift = Vector([0, 0]) pre = pgt + pshift coreShape = Vector(( cyc_r * 2, cyc_r * 2, )) feat = np.zeros([batch, channl] + list(hw)) raw_cycle_tmpl = getWeightCore(300, seta=.25) # raw_cycle_tmpl = raw_cycle_tmpl > raw_cycle_tmpl[150,0] core = resize(raw_cycle_tmpl, coreShape) msegt = feat.copy() msegt[..., (pgt - coreShape // 2).x:(pgt + coreShape // 2).x, (pgt - coreShape // 2).y:(pgt + coreShape // 2).y, ] = core feat[..., (pre - coreShape // 2).x:(pre + coreShape // 2).x, (pre - coreShape // 2).y:(pre + coreShape // 2).y, ] = core msegt = torch.tensor(msegt) feat = torch.tensor(feat) # show(core, feat)
from boxx import makedirs, np, npa, imread, pathjoin, glob, os, resize, p, uint8 ignoreWarning() celebA_path = os.path.expanduser('~/dataset/celeba') dataset = pathjoin(celebA_path, 'eyeglasses_stgan_dataset') st_gan_dataset = pathjoin(dataset, 'tf_st_gan_dataset') psa = sorted(glob(pathjoin(dataset, 'trainA/*')))[0::2] psb = sorted(glob(pathjoin(dataset, 'trainB/*')))[1::2] lena = len(psa) lenb = len(psb) attribute = np.zeros((lena + lenb, 40), np.bool) attribute[-lenb:, 15] = True img = imread(psa[0]) shape = img.shape makedirs(p / st_gan_dataset) imgns = psa + psb if shape[:2] != ( 218, 178, ): def f(imgn): return uint8(resize(imread(imgn), (
def visBboxList(rgb, bboxList, path='/tmp/visCanvas/tmp.pdf', classNames=None, box_alpha=0.8, show_class=True, thresh=0.1, show_mask=None, pltshow=False): if classNames is None: classNames = {i: "%d-class" % i for i in range(1111)} classn = len(classNames) dataset = dicto(classes=classNames) cls_segms = [[] for _ in range(classn)] cls_boxes = [np.zeros((0, 5), np.float32) for _ in range(classn)] #cls_segms = None #cls_boxes = [np.zeros((0,5), np.float32) for _ in range(classn)] bboxnps = bboxList.bbox extraFields = { k: v.cpu().numpy() for k, v in bboxList.extra_fields.items() } if show_mask is None: if 'mask' in extraFields: show_mask = True for ind, bboxnp in enumerate(bboxnps): # g() other = dicto({k: v[ind] for k, v in extraFields.items()}) if other.scores < thresh: continue c = other.labels if show_mask: rle = mask2rle(other.mask) cls_segms[c].append(rle) if bboxList.mode == 'xyxy': cls_boxes[c] = np.append(cls_boxes[c], [list(bboxnp) + [other.scores]], 0) cls_keyps = None if not show_mask: cls_segms = None # g() outputDir, name = dirname(path), filename(path) vis_one_image( rgb, # BGR -> RGB for visualization name, outputDir, cls_boxes, cls_segms, cls_keyps, dataset=dataset, box_alpha=0.8, show_class=True, thresh=thresh, kp_thresh=2, pltshow=pltshow, )