Example #1
0
def draw_uomap3d(fig, ax, vxcnt_crop, uomap):
    crop_size = vxcnt_crop.shape[0]
    hmap_size = uomap.shape[0]
    map_scale = crop_size / hmap_size
    vxcnt_hmap = vxcnt_crop[::2, ::2, ::2]
    grid = regu_grid(step=32)
    coord = grid.slice_ortho(vxcnt_hmap)
    grid.draw_slice(ax, coord, 2.)
    xx, yy = np.meshgrid(np.arange(0, crop_size, map_scale),
                         np.arange(0, crop_size, map_scale))
    ax.quiver(xx,
              yy,
              np.squeeze(uomap[..., 0]),
              -np.squeeze(uomap[..., 1]),
              color='r',
              width=0.004,
              scale=20)
    # xx, yy, zz = np.meshgrid(
    #     np.arange(0, crop_size, map_scale),
    #     np.arange(0, crop_size, map_scale),
    #     np.arange(0, crop_size, map_scale))
    # ax.quiver(
    #     xx, yy, zz,
    #     uomap[..., 0], uomap[..., 1], uomap[..., 2],
    #     color='r', length=1.2, arrow_length_ratio=0.4)
    ax.set_xlim([0, crop_size])
    ax.set_ylim([0, crop_size])
    # ax.view_init(azim=180, elev=-90)
    ax.set_aspect('equal', adjustable='box')
    ax.invert_yaxis()
Example #2
0
def draw_vxmap(fig,
               ax,
               vxcnt_crop,
               vxmap,
               voxize_hmap,
               reduce_fn=np.sum,
               roll=0):
    crop_size = vxcnt_crop.shape[0]
    hmap_size = vxmap.shape[0]
    map_scale = crop_size / hmap_size
    vxcnt_hmap = vxcnt_crop[::map_scale, ::map_scale, ::map_scale]
    grid = regu_grid(step=voxize_hmap)
    coord = grid.slice_ortho(vxcnt_hmap, roll=roll)
    grid.draw_slice(ax, coord, 1.)
    vxmap_axis = reduce_fn(vxmap, axis=(2 - roll))
    if 1 != roll:
        vxmap_axis = np.swapaxes(vxmap_axis, 0, 1)  # swap xy
    img_hit = ax.imshow(vxmap_axis, cmap=transparent_cmap(mpplot.cm.jet))
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    fig.colorbar(img_hit, cax=cax)
    ax.set_xlim([0, voxize_hmap])
    ax.set_ylim([0, voxize_hmap])
    ax.set_aspect('equal', adjustable='box')
    ax.invert_yaxis()
Example #3
0
 def raw_to_vxlab(cls, pose_raw, cube, caminfo):
     """ 01-voxel heatmap converted to labels """
     step = caminfo.hmap_size
     grid = regu_grid()
     grid.from_cube(cube, step)
     indices = grid.putit(pose_raw)
     return np.ravel_multi_index(indices.T, (step, step, step))
Example #4
0
 def to_vxhit(cls, img, cube, caminfo):
     step = caminfo.crop_size
     points3_pick = cube.pick(cls.img_to_raw(img, caminfo))
     grid = regu_grid()
     grid.from_cube(cube, step)
     pcnt3 = grid.hit(points3_pick)
     return pcnt3
Example #5
0
 def prop_edt3(cls, vxhit, pose_raw, cube, caminfo):
     from scipy.ndimage.morphology import distance_transform_edt
     step = caminfo.hmap_size
     scale = int(vxhit.shape[0] / step)
     vol_shape = (step, step, step)
     if 1 == scale:
         vxhit_hmap = vxhit
     else:
         vxhit_hmap = np.zeros(vol_shape)
         for i0 in np.mgrid[0:scale, 0:scale, 0:scale].reshape(3, -1).T:
             vxhit_hmap += vxhit[i0[0]::scale, i0[1]::scale, i0[2]::scale]
         # print(np.sum(vxhit_hmap) - np.sum(vxhit))
     mask = (1e-4 > vxhit_hmap)
     masked_edt = np.ma.masked_array(distance_transform_edt(vxhit_hmap),
                                     mask)
     # masked_edt = np.ma.masked_array(
     #     vxhit_hmap,
     #     mask)
     grid = regu_grid()
     grid.from_cube(cube, step)
     indices = grid.putit(pose_raw)
     vol_l = []
     for index in indices:
         # phi = np.zeros_like(masked_edt)
         # phi[index[0], index[1], index[2]] = 1.
         phi = masked_edt.copy()
         phi[index[0], index[1], index[2]] = 0.
         df = skfmm.distance(phi, dx=1e-1)
         df_max = np.max(df)
         df = (df_max - df) / df_max
         df[mask] = 0.
         vol_l.append(df)
     return np.stack(vol_l, axis=3)
Example #6
0
 def raw_to_vxhit(cls, pose_raw, cube, caminfo):
     """ 01-voxel heatmap """
     step = caminfo.hmap_size
     grid = regu_grid()
     grid.from_cube(cube, step)
     indices = grid.putit(pose_raw)
     vol_l = []
     for index in indices:
         vol = np.zeros((step, step, step))
         vol[index[0], index[1], index[2]] = 1.
         vol_l.append(vol)
     return np.stack(vol_l, axis=3)
Example #7
0
 def raw_to_vxoff_flat(cls, vxcnt, pose_raw, cube, caminfo):
     """ offset map from voxel center to each joint
     """
     step = caminfo.hmap_size
     grid = regu_grid()
     grid.from_cube(cube, step)
     # pose_raw = grid.voxen(np.array([[0, 0, 0], [1, 1, 1]]))
     # print(pose_raw)
     voxcens = grid.voxen(np.mgrid[0:step, 0:step, 0:step].reshape(3, -1).T)
     # print(voxcens)
     offset = pose_raw - voxcens[:, None]  # (S*S*S)xJx3
     return offset
Example #8
0
 def voxelize_depth(cls, img, pose_raw, step, anchor_num, caminfo):
     halflen = caminfo.crop_range
     points3 = cls.img_to_raw(img, caminfo, halflen)
     grid = regu_grid(np.array([-halflen, -halflen, caminfo.z_range[0]]),
                      step, halflen * 2 / step)
     pcnt = grid.fill(points3)
     cube = iso_cube(
         (np.max(pose_raw, axis=0) + np.min(pose_raw, axis=0)) / 2,
         caminfo.region_size)
     grid.step = anchor_num
     grid.cellen = halflen * 2 / anchor_num
     anchors = grid.prow_anchor_single(cube.cen, caminfo.region_size)
     cubecen = grid.fill([cube.cen])
     cubecen_anchors = np.append(cubecen.flatten(), anchors)
     resce = cube.dump()
     # mpplot = import_module('matplotlib.pyplot')
     # print(np.histogram(pcnt))
     # grid.show_dims()
     # cube.show_dims()
     # index = np.array(np.unravel_index(np.argmax(cubecen), cubecen.shape))
     # print(index)
     # print(grid.yank_anchor_single(
     #     index,
     #     anchors
     # ))
     # ax = mpplot.subplot(projection='3d')
     # numpts = points3.shape[0]
     # if 1000 < numpts:
     #     samid = np.random.choice(numpts, 1000, replace=False)
     #     points3_sam = points3[samid, :]
     # else:
     #     points3_sam = points3
     # ax.scatter(
     #     points3_sam[:, 0], points3_sam[:, 1], points3_sam[:, 2])
     # ax.view_init(azim=-90, elev=-75)
     # ax.set_zlabel('depth (mm)', labelpad=15)
     # corners = cube.get_corners()
     # iso_cube.draw_cube_wire(ax, corners)
     # from mayavi import mlab
     # mlab.figure(size=(800, 800))
     # mlab.pipeline.volume(mlab.pipeline.scalar_field(pcnt))
     # mlab.pipeline.image_plane_widget(
     #     mlab.pipeline.scalar_field(pcnt),
     #     plane_orientation='z_axes',
     #     slice_index=halflen)
     # np.set_printoptions(precision=4)
     # mlab.outline()
     # mpplot.show()
     return pcnt, cubecen_anchors, resce
Example #9
0
 def vxlab_to_raw(cls, vxlab, cube, caminfo):
     """ vxlab: sequential number """
     step = caminfo.hmap_size
     grid = regu_grid()
     grid.from_cube(cube, step)
     num_joint = vxlab.shape[-1]
     pose_out = np.empty([num_joint, 3])
     vol_shape = (step, step, step)
     for joint in range(num_joint):
         vh = vxlab[..., joint]
         # index = np.array(np.unravel_index(
         #     np.argmax(vh), vh.shape))
         index = np.array(np.unravel_index(int(vh), vol_shape))
         pose_out[joint, :] = grid.voxen(index)
     return pose_out
Example #10
0
    def draw_random(self, thedata, args):
        import matplotlib.pyplot as mpplot
        from mpl_toolkits.mplot3d import Axes3D
        from mayavi import mlab

        # mlab.figure(size=(800, 800))
        # # cube = iso_cube()
        # # points3_trans = np.hstack(
        # #     (np.zeros((10, 2)), np.arange(-1, 1, 0.2).reshape(10, 1)))
        # # grid = regu_grid()
        # # grid.from_cube(cube, 6)
        # # pcnt = grid.fill(points3_trans)
        #
        # pcnt = np.zeros((6, 6, 6))
        # pcnt[2:4, 2:4, 3] = 1
        # frame = args.data_ops.prop_dist(pcnt)
        # mlab.pipeline.volume(mlab.pipeline.scalar_field(frame))
        # mlab.pipeline.image_plane_widget(
        #     mlab.pipeline.scalar_field(frame),
        #     plane_orientation='z_axes',
        #     slice_index=self.crop_size / 2)
        # print(pcnt[..., 3])
        # print(frame[..., 3])
        # print(frame[0, 0, 3], type(frame[0, 0, 3]))
        # mlab.outline()
        # mlab.show()
        # os._exit(0)

        # mode = 'train'
        mode = 'test'
        store_handle = self.store_handle[mode]
        index_h5 = store_handle['index']
        store_size = index_h5.shape[0]
        frame_id = np.random.choice(store_size)
        # frame_id = 0  # frame_id = img_id - 1
        # frame_id = 239
        img_id = index_h5[frame_id, ...]
        frame_h5 = store_handle['pcnt3'][frame_id, ...]
        poses_h5 = store_handle['pose_c'][frame_id, ...].reshape(-1, 3)
        resce_h5 = store_handle['resce'][frame_id, ...]

        print('[{}] drawing image #{:d} ...'.format(self.name_desc, img_id))
        print(np.min(frame_h5), np.max(frame_h5))
        print(np.histogram(frame_h5, range=(1e-4, np.max(frame_h5))))
        from colour import Color
        colors = [Color('orange').rgb, Color('red').rgb, Color('lime').rgb]
        resce3 = resce_h5[0:4]
        cube = iso_cube()
        cube.load(resce3)
        fig, _ = mpplot.subplots(nrows=2, ncols=2, figsize=(2 * 5, 2 * 5))

        ax = mpplot.subplot(2, 2, 1)
        img_name = args.data_io.index2imagename(img_id)
        img = args.data_io.read_image(
            self.data_inst.images_join(img_name, mode))
        ax.imshow(img, cmap=mpplot.cm.bone_r)
        pose_raw = self.yanker(poses_h5, resce_h5, self.caminfo)
        args.data_draw.draw_pose2d(
            ax, thedata, args.data_ops.raw_to_2d(pose_raw, self.caminfo))
        rects = cube.proj_rects_3(args.data_ops.raw_to_2d, self.caminfo)
        for ii, rect in enumerate(rects):
            rect.draw(ax, colors[ii])

        ax = mpplot.subplot(2, 2, 3, projection='3d')
        resce3 = resce_h5[0:4]
        cube = iso_cube()
        cube.load(resce3)
        cube.show_dims()
        points3 = args.data_ops.img_to_raw(img, self.caminfo)
        points3_trans = cube.pick(points3)
        points3_trans = cube.transform_to_center(points3_trans)
        numpts = points3_trans.shape[0]
        if 1000 < numpts:
            points3_trans = points3_trans[
                np.random.choice(numpts, 1000, replace=False), :]
        ax.scatter(points3_trans[:, 0],
                   points3_trans[:, 1],
                   points3_trans[:, 2],
                   color=Color('lightsteelblue').rgb)
        pose_c = cube.transform_to_center(pose_raw)
        args.data_draw.draw_raw3d_pose(ax, thedata, pose_c)
        corners = cube.transform_to_center(cube.get_corners())
        cube.draw_cube_wire(ax, corners)
        ax.view_init(azim=-120, elev=-150)

        ax = mpplot.subplot(2, 2, 2, projection='3d')
        numpts = points3.shape[0]
        if 1000 < numpts:
            samid = np.random.choice(numpts, 1000, replace=False)
            points3_sam = points3[samid, :]
        else:
            points3_sam = points3
        ax.scatter(points3_sam[:, 0],
                   points3_sam[:, 1],
                   points3_sam[:, 2],
                   color=Color('lightsteelblue').rgb)
        ax.view_init(azim=-90, elev=-75)
        ax.set_zlabel('depth (mm)', labelpad=15)
        args.data_draw.draw_raw3d_pose(ax, thedata, pose_raw)
        corners = cube.get_corners()
        iso_cube.draw_cube_wire(ax, corners)

        voxize_crop = self.crop_size
        grid = regu_grid()
        grid.from_cube(cube, voxize_crop)
        vxcnt_crop = frame_h5

        def draw_voxel_pose(ax, poses, roll=0):
            pose3d = cube.transform_center_shrink(poses)
            pose2d, _ = cube.project_ortho(pose3d, roll=roll, sort=False)
            pose2d *= voxize_crop
            args.data_draw.draw_pose2d(
                ax,
                thedata,
                pose2d,
            )
            coord = grid.slice_ortho(vxcnt_crop, roll=roll)
            grid.draw_slice(ax, coord, 1.)
            ax.set_xlim([0, voxize_crop])
            ax.set_ylim([0, voxize_crop])
            ax.set_aspect('equal', adjustable='box')
            ax.invert_yaxis()

        ax = mpplot.subplot(2, 2, 4)
        draw_voxel_pose(ax, pose_raw, roll=0)

        # if not self.args.show_draw:
        #     mlab.options.offscreen = True
        #     mlab.figure(size=(800, 800))
        #     points3_trans = cube.transform_to_center(points3_sam)
        #     mlab.points3d(
        #         points3_trans[:, 0], points3_trans[:, 1], points3_trans[:, 2],
        #         scale_factor=8,
        #         color=Color('lightsteelblue').rgb)
        #     mlab.outline()

        if not self.args.show_draw:
            mlab.options.offscreen = True
        else:
            mlab.figure(size=(800, 800))
            # mlab.contour3d(frame)
            mlab.pipeline.volume(mlab.pipeline.scalar_field(frame_h5))
            mlab.pipeline.image_plane_widget(
                mlab.pipeline.scalar_field(frame_h5),
                plane_orientation='z_axes',
                slice_index=self.crop_size / 2)
            np.set_printoptions(precision=4)
            # print(frame[12:20, 12:20, 16])
            mlab.outline()
            from utils.image_ops import draw_dist3
            draw_dist3(vxcnt_crop, voxize_crop, 2)
            mlab.draw()
            mlab.savefig(
                os.path.join(self.predict_dir,
                             'draw3d_{}_{}.png'.format(self.name_desc,
                                                       img_id)))

        fig.tight_layout()
        mpplot.savefig(
            os.path.join(self.predict_dir,
                         'draw_{}_{}.png'.format(self.name_desc, img_id)))
        if self.args.show_draw:
            mpplot.show()
            mlab.close(all=True)
        print('[{}] drawing image #{:d} - done.'.format(
            self.name_desc, img_id))
Example #11
0
    def draw_random(self, thedata, args):
        import matplotlib.pyplot as mpplot
        from mpl_toolkits.mplot3d import Axes3D
        from mayavi import mlab

        # mode = 'train'
        mode = 'test'
        store_handle = self.store_handle[mode]
        index_h5 = store_handle['index']
        store_size = index_h5.shape[0]
        frame_id = np.random.choice(store_size)
        # frame_id = 0  # frame_id = img_id - 1
        # frame_id = 239
        img_id = index_h5[frame_id, ...]
        frame_h5 = store_handle['vxhit'][frame_id, ...]
        poses_h5 = store_handle['poses'][frame_id, ...].reshape(-1, 3)
        pose_lab_h5 = store_handle['pose_lab'][frame_id, ...]
        resce_h5 = store_handle['resce'][frame_id, ...]

        print('[{}] drawing image #{:d} ...'.format(self.name_desc, img_id))
        print(np.min(frame_h5), np.max(frame_h5))
        print(np.histogram(frame_h5, range=(1e-4, np.max(frame_h5))))
        print(resce_h5)
        resce3 = resce_h5[0:4]
        cube = iso_cube()
        cube.load(resce3)
        cube.show_dims()
        img_name = args.data_io.index2imagename(img_id)
        img = args.data_io.read_image(
            self.data_inst.images_join(img_name, mode))
        from colour import Color
        colors = [Color('orange').rgb, Color('red').rgb, Color('lime').rgb]
        fig, _ = mpplot.subplots(nrows=2, ncols=4, figsize=(4 * 5, 2 * 5))
        voxize_crop = self.crop_size
        voxize_hmap = self.hmap_size
        scale = self.map_scale

        ax = mpplot.subplot(2, 4, 3, projection='3d')
        points3 = args.data_ops.img_to_raw(img, self.caminfo)
        points3_trans = cube.pick(points3)
        # points3_trans = cube.transform_to_center(points3_trans)
        numpts = points3_trans.shape[0]
        if 1000 < numpts:
            points3_trans = points3_trans[
                np.random.choice(numpts, 1000, replace=False), :]
        ax.scatter(points3_trans[:, 0],
                   points3_trans[:, 1],
                   points3_trans[:, 2],
                   color=Color('lightsteelblue').rgb)
        args.data_draw.draw_raw3d_pose(ax, thedata, poses_h5)
        corners = cube.get_corners()
        # corners = cube.transform_to_center(corners)
        cube.draw_cube_wire(ax, corners)
        ax.view_init(azim=-90, elev=-75)

        ax = mpplot.subplot(2, 4, 1)
        ax.imshow(img, cmap=mpplot.cm.bone_r)
        pose_raw = poses_h5
        args.data_draw.draw_pose2d(
            ax, thedata, args.data_ops.raw_to_2d(pose_raw, self.caminfo))
        rects = cube.proj_rects_3(args.data_ops.raw_to_2d, self.caminfo)
        for ii, rect in enumerate(rects):
            rect.draw(ax, colors[ii])

        ax = mpplot.subplot(2, 4, 2, projection='3d')
        numpts = points3.shape[0]
        if 1000 < numpts:
            samid = np.random.choice(numpts, 1000, replace=False)
            points3_sam = points3[samid, :]
        else:
            points3_sam = points3
        ax.scatter(points3_sam[:, 0],
                   points3_sam[:, 1],
                   points3_sam[:, 2],
                   color=Color('lightsteelblue').rgb)
        ax.view_init(azim=-90, elev=-75)
        ax.set_zlabel('depth (mm)', labelpad=15)
        args.data_draw.draw_raw3d_pose(ax, thedata, pose_raw)
        corners = cube.get_corners()
        iso_cube.draw_cube_wire(ax, corners)

        ax = mpplot.subplot(2, 4, 4, projection='3d')
        # grid = regu_grid()
        # grid.from_cube(cube, self.crop_size)
        # grid.draw_map(ax, frame_h5)
        args.data_draw.draw_raw3d_pose(ax, thedata, pose_raw)
        # from cv2 import resize as cv2resize
        # import cv2
        # img_hmap = cv2resize(img, (241, 241))
        # img_hmap = (img_hmap / np.max(img_hmap)) * 255
        # # fig2d, _ = mpplot.subplots()
        # # ax2d = mpplot.subplot(1, 1, 1)
        # # ax2d.imshow(img)
        # # img_hmap = fig2data(fig2d)
        # # img_hmap = (255. * img_hmap / np.max(img_hmap)).astype(int)
        # # img_hmap = img_hmap / np.max(img_hmap)
        # img_hmap = cv2.cvtColor(img_hmap, cv2.COLOR_GRAY2RGB)
        # # x, y = np.ogrid[0:cube.sidelen, 0:cube.sidelen]
        # x, y = np.mgrid[-cube.sidelen:cube.sidelen, -cube.sidelen:cube.sidelen]
        # ax.plot_surface(
        #     # x, y, cube.sidelen,
        #     x, y,
        #     np.ones_like(x) * cube.sidelen,
        #     rstride=2, cstride=2,
        #     facecolors=img_hmap)
        ax.view_init(azim=-90, elev=-75)
        # axlim = cube.sidelen * 0.8
        # ax.set_xlim([-axlim, axlim])
        # ax.set_ylim([-axlim, axlim])
        # ax.set_zlim([-axlim, axlim])
        # ax.set_aspect('equal', adjustable='box')
        ax.set_zlabel('depth (mm)', labelpad=15)

        pose_yank = self.yanker_hmap(pose_lab_h5, resce3, self.caminfo)
        diff = np.abs(pose_raw - pose_yank)
        print(diff)
        print(np.min(diff, axis=0), np.max(diff, axis=0))
        grid = regu_grid()
        grid.from_cube(cube, voxize_crop)
        vxcnt_crop = frame_h5

        def draw_voxel_pose(ax, poses, roll=0):
            pose3d = cube.transform_center_shrink(poses)
            pose2d, _ = cube.project_ortho(pose3d, roll=roll, sort=False)
            pose2d *= voxize_crop
            args.data_draw.draw_pose2d(
                ax,
                thedata,
                pose2d,
            )
            coord = grid.slice_ortho(vxcnt_crop, roll=roll)
            grid.draw_slice(ax, coord, 1.)
            ax.set_xlim([0, voxize_crop])
            ax.set_ylim([0, voxize_crop])
            ax.set_aspect('equal', adjustable='box')
            ax.invert_yaxis()

        ax = mpplot.subplot(2, 4, 5)
        draw_voxel_pose(ax, pose_raw, roll=0)

        roll = 1
        ax = mpplot.subplot(2, 4, 6)
        draw_voxel_pose(ax, pose_yank, roll=roll)

        from utils.image_ops import draw_vxlab
        ax = mpplot.subplot(2, 4, 7)
        draw_vxlab(fig, ax, vxcnt_crop, pose_lab_h5, voxize_hmap, roll=0)

        ax = mpplot.subplot(2, 4, 8)
        draw_vxlab(fig, ax, vxcnt_crop, pose_lab_h5, voxize_hmap, roll=roll)

        # if not self.args.show_draw:
        #     mlab.options.offscreen = True
        # else:
        #     mlab.figure(size=(800, 800))
        #     # mlab.contour3d(frame_h5)
        #     mlab.pipeline.volume(mlab.pipeline.scalar_field(frame_h5))
        #     mlab.pipeline.image_plane_widget(
        #         mlab.pipeline.scalar_field(frame_h5),
        #         plane_orientation='z_axes',
        #         slice_index=self.crop_size / 2)
        #     np.set_printoptions(precision=4)
        #     # print(frame_h5[12:20, 12:20, 16])
        #     mlab.outline()

        # if not self.args.show_draw:
        #     mlab.options.offscreen = True
        # else:
        #     from utils.image_ops import draw_dist3
        #     vxmap = np.zeros(voxize_hmap * voxize_hmap * voxize_hmap)
        #     vxmap[pose_lab_h5[-1].astype(int)] = 1
        #     vxmap = vxmap.reshape((voxize_hmap, voxize_hmap, voxize_hmap))
        #     draw_dist3(vxmap, voxize_crop, scale)
        #     mlab.draw()
        #     mlab.savefig(os.path.join(
        #         self.predict_dir,
        #         'draw3d_{}_{}.png'.format(self.name_desc, img_id)))
        #
        fig.tight_layout()
        mpplot.savefig(
            os.path.join(self.predict_dir,
                         'draw_{}_{}.png'.format(self.name_desc, img_id)))
        if self.args.show_draw:
            mpplot.show()
            # mlab.close(all=True)
        print('[{}] drawing image #{:d} - done.'.format(
            self.name_desc, img_id))
Example #12
0
    def vxudir_to_raw(cls, vxhit, vxudir, cube, caminfo, nn=5):
        """ recover 3d from weight avarage """
        from sklearn.preprocessing import normalize
        step = caminfo.hmap_size
        theta = caminfo.region_size * 2
        grid = regu_grid()
        grid.from_cube(cube, step)
        num_joint = caminfo.join_num
        offlen = vxudir[..., :num_joint].reshape(-1, num_joint).T
        top_id = np.argpartition(offlen, -nn, axis=1)[:, -nn:]  # top elements
        conf3 = np.take(offlen, top_id)
        voxcens = grid.voxen(np.mgrid[0:step, 0:step, 0:step].reshape(3, -1).T)
        pose_out = np.empty((num_joint, 3))
        for jj in range(num_joint):
            unit_off = vxudir[..., num_joint + 3 * jj:num_joint + 3 *
                              (jj + 1)].reshape(-1, 3)
            unit_off = normalize(unit_off[top_id[jj], :])
            d = theta - offlen[jj, top_id[jj]] * theta
            p0 = voxcens[top_id[jj], :]
            pred3 = p0 + unit_off * d[:, None]
            c = conf3[jj]
            pred32 = np.sum(pred3 * c[:, None], axis=0) / np.sum(c)
            pose_out[jj, :] = pred32

            # print(conf3.shape)
            # unit = vxudir[..., num_joint:].reshape(num_joint, -1, 3)
            # print(np.take(unit, top_id).shape)
            # ux = np.take(unit[..., 0], top_id)
            # uy = np.take(unit[..., 1], top_id)
            # uz = np.take(unit[..., 2], top_id)
            # unit = normalize(
            #     np.stack((ux, uy, uz), axis=2),
            #     axis=2)
            # print(unit.shape)
            # ux = np.take(voxcens[..., 0], top_id)
            # uy = np.take(voxcens[..., 1], top_id)
            # uz = np.take(voxcens[..., 2], top_id)
            # pose_pred = voxcens + offset
            # vol_shape = (step, step, step)
            # pose_out = np.empty([num_joint, 3])
            # for joint in range(num_joint):
            #     # restore from 3d
            #     hm = vxhit[..., joint]
            #     # hm = softmax(vxhit[..., joint].flatten()).flatten()
            #     # hm[np.where(1e-2 > vxcnt_hmap)] = 0  # mask out void is wrong - joint not on the surface
            #     hm[np.where(0 > hm)] = 0  # not training goal
            #     top_id = hm.argpartition(-nn, axis=None)[-nn:]  # top elements
            #     x3, y3, z3 = np.unravel_index(top_id, vol_shape)
            #     conf3 = hm[x3, y3, z3]
            #     # conf3 = hm[top_id]
            #     print(conf3)
            #     dist = olmap[x3, y3, z3, joint]
            #     dist = theta - dist * theta  # inverse propotional
            #     uom = uomap[..., 3 * joint:3 * (joint + 1)]
            #     unit_off = uom[x3, y3, z3, :]
            #     unit_off = normalize(unit_off, norm='l2')
            #     offset = unit_off * np.tile(dist, [3, 1]).T
            #     p0 = grid.voxen(np.vstack([x3, y3, z3]).astype(float).T)
            #     pred3 = p0 + offset
            #     pred32 = np.sum(
            #         pred3 * np.tile(conf3, [3, 1]).T, axis=0
            #     ) / np.sum(conf3)
            pose_out[jj, :] = pred32
        return pose_out
Example #13
0
    def draw_random(self, thedata, args):
        import matplotlib.pyplot as mpplot
        from mpl_toolkits.mplot3d import Axes3D
        from mayavi import mlab

        index_h5 = self.store_handle['index']
        store_size = index_h5.shape[0]
        frame_id = np.random.choice(store_size)
        # frame_id = 0  # frame_id = img_id - 1
        frame_id = 239
        img_id = index_h5[frame_id, ...]
        frame_h5 = self.store_handle['pcnt3'][frame_id, ...]
        poses_h5 = self.store_handle['poses'][frame_id, ...].reshape(-1, 3)
        resce_h5 = self.store_handle['resce'][frame_id, ...]
        vxedt_h5 = self.store_handle['vxedt'][frame_id, ...]
        print(self.store_handle['vxedt'])

        print('[{}] drawing image #{:d} ...'.format(self.name_desc, img_id))
        print(np.min(frame_h5), np.max(frame_h5))
        print(np.histogram(frame_h5, range=(1e-4, np.max(frame_h5))))
        print(np.min(poses_h5, axis=0), np.max(poses_h5, axis=0))
        print(
            np.histogram(vxedt_h5, range=(np.min(vxedt_h5), np.max(vxedt_h5))))
        print(resce_h5)
        resce3 = resce_h5[0:4]
        cube = iso_cube()
        cube.load(resce3)
        cube.show_dims()
        img_name = args.data_io.index2imagename(img_id)
        img = args.data_io.read_image(os.path.join(self.image_dir, img_name))
        from colour import Color
        colors = [Color('orange').rgb, Color('red').rgb, Color('lime').rgb]
        fig, _ = mpplot.subplots(nrows=2, ncols=3, figsize=(3 * 5, 2 * 5))
        vxcnt_crop = frame_h5
        voxize_crop = self.crop_size
        voxize_hmap = self.hmap_size
        scale = self.map_scale
        num_joint = self.join_num
        joint_id = num_joint - 1
        vxdist = vxedt_h5[..., joint_id]
        print(np.histogram(vxdist, range=(np.min(vxdist), np.max(vxdist))))

        ax = mpplot.subplot(2, 3, 1)
        ax.imshow(img, cmap=mpplot.cm.bone_r)
        pose_raw = poses_h5
        args.data_draw.draw_pose2d(
            ax, thedata, args.data_ops.raw_to_2d(pose_raw, self.caminfo))
        rects = cube.proj_rects_3(args.data_ops.raw_to_2d, self.caminfo)
        for ii, rect in enumerate(rects):
            rect.draw(ax, colors[ii])

        grid = regu_grid()
        grid.from_cube(cube, voxize_crop)

        def draw_voxel_pose(ax, poses, roll=0):
            pose3d = cube.transform_center_shrink(poses)
            pose2d, _ = cube.project_ortho(pose3d, roll=roll, sort=False)
            pose2d *= voxize_crop
            args.data_draw.draw_pose2d(
                ax,
                thedata,
                pose2d,
            )
            coord = grid.slice_ortho(vxcnt_crop, roll=roll)
            grid.draw_slice(ax, coord, 1.)
            ax.set_xlim([0, voxize_crop])
            ax.set_ylim([0, voxize_crop])
            ax.set_aspect('equal', adjustable='box')
            ax.invert_yaxis()

        ax = mpplot.subplot(2, 3, 2)
        draw_voxel_pose(ax, pose_raw, roll=0)

        from utils.image_ops import draw_vxmap

        ax = mpplot.subplot(2, 3, 4)
        draw_vxmap(fig,
                   ax,
                   vxcnt_crop,
                   vxdist,
                   voxize_hmap,
                   reduce_fn=np.max,
                   roll=0)
        ax = mpplot.subplot(2, 3, 5)
        draw_vxmap(fig,
                   ax,
                   vxcnt_crop,
                   vxdist,
                   voxize_hmap,
                   reduce_fn=np.max,
                   roll=1)
        ax = mpplot.subplot(2, 3, 6)
        draw_vxmap(fig,
                   ax,
                   vxcnt_crop,
                   vxdist,
                   voxize_hmap,
                   reduce_fn=np.max,
                   roll=2)

        if not self.args.show_draw:
            mlab.options.offscreen = True
        else:
            from utils.image_ops import draw_dist3
            draw_dist3(vxdist, voxize_crop, scale)
            mlab.draw()
            mlab.savefig(
                os.path.join(self.predict_dir,
                             'draw3d_{}_{}.png'.format(self.name_desc,
                                                       img_id)))
            # joint_id = 0
            # vxdist = vxedt_h5[..., joint_id]
            # vxunit = vxedt_h5[..., num_joint + 3 * joint_id:num_joint + 3 * (joint_id + 1)]
            # draw_dist3(vxdist, vxunit, voxize_crop, scale)
            # mlab.draw()

        fig.tight_layout()
        mpplot.savefig(
            os.path.join(
                self.predict_dir,
                # 'draw_{}.png'.format(self.name_desc)))
                'draw_{}_{}.png'.format(self.name_desc, img_id)))
        if self.args.show_draw:
            mpplot.show()
            mlab.close(all=True)
        print('[{}] drawing image #{:d} - done.'.format(
            self.name_desc, img_id))