Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMPL body models can't be loaded #54

Open
gngdb opened this issue Jan 23, 2022 · 2 comments
Open

SMPL body models can't be loaded #54

gngdb opened this issue Jan 23, 2022 · 2 comments

Comments

@gngdb
Copy link

gngdb commented Jan 23, 2022

I downloaded the SMPL body models from the website and they're .pkl files. The if statement in the main body model class doesn't check for .pkl suffix: https://github.com/nghorbani/human_body_prior/blob/master/src/human_body_prior/body_model/body_model.py#L57-L61

I tried rewriting the SMPL body model to an .npz archive but that won't load either because it doesn't contain "shapedirs":

  File "/home/gngdb/miniconda3/envs/gait/lib/python3.9/site-packages/human_body_prior/body_model/body_model.py", line 89, in __init__
    num_total_betas = smpl_dict['shapedirs'].shape[-1]
  File "/home/gngdb/miniconda3/envs/gait/lib/python3.9/site-packages/numpy/lib/npyio.py", line 260, in __getitem__
    raise KeyError("%s is not a file in the archive" % key)
KeyError: 'shapedirs is not a file in the archive'
@gngdb
Copy link
Author

gngdb commented Jan 23, 2022

Here's a workaround, it also deals with some other issues when trying to load using the SMPL body models that are on the website (some arrays are actually chumpy or scipy.sparse arrays). It writes an .npz archive to a temporary directory with the same contents after cleaning up the arrays so they're all numpy arrays, then passes that path to BodyModel to be loaded.

with tempfile.TemporaryDirectory() as tmpdirname:
    # this is a hack to make it possible to load from pkl
    if bm_path.suffix == ".pkl":
        hack_bm_path = Path(tmpdirname) / (bm_path.stem + ".npz")
        with open(bm_path, "rb") as f:
            try:
                data = pkl.load(f, encoding="latin1")
            except ModuleNotFoundError as e:
                if "chumpy" in str(e):
                    message = ("Failed to load pickle file because "
                        "chumpy is not installed.\n"
                        "The original SMPL body model archives store some arrays as chumpy arrays, these are cast back to numpy arrays before use but it is not possible to unpickle the data without chumpy installed.")
                    raise ModuleNotFoundError(message) from e
                else:
                    raise e
            def clean(x):
                if 'chumpy' in str(type(x)):
                    return np.array(x)
                elif type(x) == scipy.sparse.csc.csc_matrix:
                    return x.toarray()
                else:
                    return x
            data = {k: clean(v) for k,v in data.items() if type(v)}
            data = {k: v for k,v in data.items() if type(v) == np.ndarray}
            np.savez(hack_bm_path, **data)
    else:
        hack_bm_path = bm_path
    bm = BodyModel(
        bm_fname=hack_bm_path,
        num_betas=num_betas,
        # model_type=model_type
    )

@AlyanQ
Copy link

AlyanQ commented Jan 28, 2022

Thanks for the code, I was trying to accomplish the same thing but thankfully I found your comment. By any chance, are you doing this to edit the pose parameters of SMPL model to manually edit the pose of SMPL models created with VPoser? I have a project that uses smplify-x to generate human meshes, but to extract some data I need for the models to be in specific poses. I've been trying to find a way to edit the poses but haven't had any luck because the models are created using VPoser and it being a prior is not easy to edit. If you are trying to do the same thing and have found any luck let me know, thanks again for your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants