Пример #1
0
    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
Пример #2
0
 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 forward(self, feats, xyens):
        logName = 'cyc_r: %s, out_cyc_r: %s' % (self.cyc_r, self.out_cyc_r
                                                or 'N')
        logTag = timegap(self.log_freq, logName)
        if logTag:
            logDic = dicto()
        if self.layerNorma:
            feats = layerNormaFun(feats)
        tensorType = feats.type()
        shape = self.shape = feats.shape
        feats = feats.view(-1, *shape[-2:])
        xyens = xyens.view(-1, 3)
        pgts = xyens[..., [1, 0]].cpu().numpy()
        existMask = xyens[..., -1].type(tensorType)

        #        with timeit(logName):
        masks = map2(getMaskOfPgt, [
            dict(pgt=pgt,
                 cycle_tmpl=self.cycle_tmpl,
                 shape=shape,
                 out_cyc_r=self.out_cyc_r) for pgt in pgts
        ])
        #            masks = mapmp(self.getMask, pgts, pool=4)
        masks = np.array(masks)
        masks = th.from_numpy(np.uint8(masks)).type(tensorType).cuda()

        loss = 0

        #(lambda a,b,t=1:e**(t*a)/(e**(t*a)+e**(t*b)))(2,1,5)

        def softmaxFgBgOld(fg, bg, t=1):
            fge = th.exp(fg * t)
            bge = th.exp(bg * t)
            prob = fge / (fge + bge + eps)
            return prob

        def softmaxFgBgSubMax(fg, bg, t=1):
            fg = fg * t
            bg = bg * t
            maxx = max(float(fg.max()), float(bg.max()))
            fge = th.exp(fg - maxx)
            bge = th.exp(bg - maxx)
            prob = fge / (fge + bge + eps)
            return prob

        def softmaxFgBgSub(fg, bg, t=1):
            diff = bg - fg
            toExp = t * diff
            if (toExp > 80).sum() and timegap(1, 'toExp'):
                from boxx import prettyFrameLocation, pred
                print(prettyFrameLocation())
                pred - "toExp.max() is %.2f > 80, diff.max() is %.2f" % (
                    toExp.max(), diff.max())

            prob = 1 / (1 + th.exp(toExp))
            return prob

        softmaxFgBg = softmaxFgBgSubMax

        def CE(fg, bg):
            prob = softmaxFgBg(fg, bg)
            avgLosses = -th.log(prob + eps)
            return avgLosses

        if 'avg' in self.poolings:
            bgAvgPool = (feats * masks[..., 0, :, :]).sum(-1).sum(-1) / (
                masks[..., 0, :, :].sum(-1).sum(-1) + eps)
            fgAvgPool = (feats * masks[..., 1, :, :]).sum(-1).sum(-1) / (
                masks[..., 1, :, :].sum(-1).sum(-1) + eps)
            avgProbs = softmaxFgBg(fgAvgPool, bgAvgPool, self.temper)
            avgLosses = -th.log(avgProbs + eps)

            indexMask = existMask * (avgProbs < self.probMargin).type(
                tensorType) if self.probMargin else existMask

            avgLoss = (avgLosses * indexMask).sum() / (indexMask.sum() + eps)
            loss += avgLoss

            if logTag:
                logDic.avgLoss = float(avgLoss)
                logDic.avgProb = float(avgProbs.mean())

        if 'max' in self.poolings:
            bgMaxPool = (feats * masks[..., 0, :, :]).max(-1)[0].max(-1)[0]
            fgMaxPool = (feats * masks[..., 1, :, :]).max(-1)[0].max(-1)[0]
            maxProbs = softmaxFgBg(fgMaxPool, bgMaxPool, self.temper)
            maxLosses = -th.log(maxProbs + eps)

            indexMask = existMask * (maxProbs < self.probMargin).type(
                tensorType) if self.probMargin else existMask

            maxLoss = (maxLosses * indexMask).sum() / (indexMask.sum() + eps)
            loss += maxLoss

            if logTag:
                logDic.maxLoss = float(maxLoss)
                logDic.maxProb = float(maxProbs.mean())
        if logTag:
            print("%s | %s" % (logName, ', '.join(
                map(lambda kv: "%s: %.3f" % kv, logDic.items()))))


#            print(Markdown([{k:strnum(v) for k,v in logDic.items()}]))
#        g()
        return loss
    hw = Vector((9, 9))
    channl = 1
    batch = 1
    multi_batch = False
    multi_batch = True
    if multi_batch:
        gpun = 1
        batchPerGpu = 1
        channl = 1
        if cloud:
            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()
Пример #5
0
        colormap[a][0] + (colormap[b][0] - colormap[a][0]) * f,
        colormap[a][1] + (colormap[b][1] - colormap[a][1]) * f,
        colormap[a][2] + (colormap[b][2] - colormap[a][2]) * f,
    ]


def interpolate_or_clip(colormap, x):
    if x < 0.0:
        return [0.0, 0.0, 0.0]
    elif x > 1.0:
        return [1.0, 1.0, 1.0]
    else:
        return interpolate(colormap, x)


turbo_colormap_data_np = np.array(turbo_colormap_data)


def heatmap_to_pseudo_color(heatmap):
    x = heatmap

    x = x.clip(0, 1)
    a = (x * 255).astype(int)
    b = (a + 1).clip(max=255)
    f = x * 255.0 - a
    pseudo_color = (
        turbo_colormap_data_np[a] +
        (turbo_colormap_data_np[b] - turbo_colormap_data_np[a]) * f[..., None])

    pseudo_color[heatmap < 0.0] = 0.0
    pseudo_color[heatmap > 1.0] = 1.0
Пример #6
0
cube_size = 1
for inst_id in range(1, 3):
    location = [random.random() * 4 - 2 for _ in range(3)]
    rotation = [random.random() * 2 * pi for _ in range(3)]
    # add cube
    bpy.ops.mesh.primitive_cube_add(size=cube_size,
                                    location=location,
                                    rotation=rotation)
    obj = bpy.context.active_object
    # set each instance a unique inst_id, which is used to generate instance annotation.
    obj["inst_id"] = inst_id

# result["ycb_meta"] is 6d pose GT
result = bpycv.render_data()

meta = result["ycb_meta"]
img = result["image"]

# cube mesh xyz
cube_xyz_s = "111 110 101 011 100 010 001 000"
cube_xyz = np.array([[-cube_size / 2, cube_size / 2][int(s)]
                     for s in cube_xyz_s if s in "01"]).reshape(-1, 3)
cube_xyz = None
vis = vis_poses_by_meta(img, meta, cube_xyz)

cv2.imwrite("demo-vis_6d_pose.jpg", vis)

if __name__ == "__main__":
    pass
    def f(imgn):
        imread(imgn)


if __name__ == '__main__':
    imgns = imgns[:]
    #    with timeit():
    #        images = mapmp(f, imgns, pool=16)
    #    with timeit():
    #        images = mapmp(f, imgns, pool=8)
    #    with timeit():
    #        images = mapmt(f, imgns, pool=16)
    with timeit():
        images = mapmt(f, imgns, pool=8)
#    with timeit():
#        images = map2(f, imgns, )
    images = np.array((images))

    attrp = pathjoin(st_gan_dataset, 'attribute_train.npy')
    imagep = p / pathjoin(st_gan_dataset, 'image_train.npy')

    np.save(attrp, attribute)
    np.save(imagep, images)

#    os.link(attrp, attrp.replace('train.npy', 'test.npy'))
#    os.link(imagep, imagep.replace('train.npy', 'test.npy'))

if __name__ == "__main__":
    pass
Пример #8
0
    # add cube
    bpy.ops.mesh.primitive_cube_add(size=cube_size,
                                    location=location,
                                    rotation=rotation)
    obj = bpy.context.active_object
    # set each instance a unique inst_id, which is used to generate instance annotation.
    obj["inst_id"] = inst_id

result = bpycv.render_data()

# result["ycb_meta"] is 6d pose GT
meta = result["ycb_meta"]
img = result["image"]

# cube mesh xyz
cube_xyz = np.array([
    [1.0, 1.0, 1.0],
    [1.0, 1.0, -1.0],
    [1.0, -1.0, 1.0],
    [-1.0, 1.0, 1.0],
    [1.0, -1.0, -1.0],
    [-1.0, 1.0, -1.0],
    [-1.0, -1.0, 1.0],
    [-1.0, -1.0, -1.0],
])
cube_xyz *= cube_size / 2

vis = vis_poses_by_ycb_meta(img, meta, cube_xyz)

cv2.imwrite("demo-vis_6d_pose.jpg", vis)
Пример #9
0
 def adjustBrightnessPil(pil, target):
     new = adjustBrightnessInHsv(np.array(pil), adjustBrightness)
     newpil = Image.fromarray(new)
     return newpil, target