ranahanocka / MeshCNN

Convolutional Neural Network for 3D meshes in PyTorch
MIT License
1.61k stars 320 forks source link

How to prepare our own segmentation datasets #87

Open duhau opened 4 years ago

duhau commented 4 years ago

Hi,

Thank you so much for this repo. This is really helpful & very impressive work.

I encountered a few problems while preparing my own segmentation datasets 。First of all, I downloaded your scripts on matlab. I found two folders are required, namely , (1) .obj and .edges in bl_1500\test\, (2) .obj and .seg in \meshes\test. I couldn't find the relevant information how to prepare .edges and .seg files.

What's more, I run the example with MATLAB code, only .eseg files were generated, and without .seseg, so, I wnat to know how to papare .seseg. could you please tell me the how to papare .seseg files.

Thank you very much and look forward to receiving your reply.

Hua DU

ranahanocka commented 4 years ago

Hi @duhau ,

Check this answer, you basically need to export the .edges from the mesh class.

You can just create the seseg file by looking at the segmentation of the edges in the one ring. Here is the deprecated function that did it:


class FakeOptions:
        def __init__(self, flip_edges=0.1):
            self.flip_edges = flip_edges
            self.edge_ratio_epsilon=0.
            self.num_aug = 1

def make_soft_eseg(obj_path, eseg_path, seseg_path, nclasses=4):
    if not os.path.isdir(seseg_path): os.makedirs(seseg_path)
    files = glob.glob(os.path.join(obj_path, '*.obj'))
    from rewrite.mesh import Mesh
    opt = FakeOptions(flip_edges=0)
    for file in files:
        mesh = Mesh(file, shuffle=False, opt=opt)
        gemm_edges = np.array(mesh.gemm_edges)
        obj_id = os.path.splitext(os.path.basename(file))[0]
        seg_file = os.path.join(eseg_path, obj_id + '.eseg')
        edge_seg = np.array(read_seg(seg_file, True).squeeze(), dtype='int32') - 1
        s_eseg = -1 * np.ones((mesh.edges_count, nclasses), dtype='float64')
        for ei in range(mesh.edges_count):
            prob = np.zeros(nclasses)
            seg_ids, counts = np.unique(edge_seg[gemm_edges[ei]], return_counts=True)
            prob[seg_ids] = counts / float(len(gemm_edges[ei]))
            s_eseg[ei, :] = prob
        s_eseg_file = os.path.join(seseg_path, obj_id + '.seseg')
        np.savetxt(s_eseg_file, s_eseg, fmt='%f')