Writing/reading scANVI model error?

Hi all,

I’m just wondering if I’m missing something with saving/reading a trained scANVI model. Getting an error when loading.

Running scANVI and saving with something like:

# Load scVI model
vae_ref = scvi.model.SCVI.load("../output/scVI_model/",
                              adata = adata)

# Train scANVI model
vae_ref_scan = scvi.model.SCANVI.from_scvi_model(
    vae_ref,
    unlabeled_category="Unknown",
    labels_key="celltype_level2",
)

vae_ref_scan.train(max_epochs=400,
          early_stopping=True,
          early_stopping_monitor='elbo_validation',
          early_stopping_patience=10)

# save the model
dir_path = "../output/scANVI_celltype_level2"
vae_ref_scan.save(dir_path, overwrite=True)

# Embed data with scANVI latent rep
adata.obsm["X_scANVI"] = vae_ref_scan.get_latent_representation()
sc.pp.neighbors(adata, use_rep="X_scANVI")
sc.tl.umap(adata)

# Save h5ad
adata.write_h5ad(filename='./data_integrated.h5ad', compression="gzip")

And then in a different environment, when trying to load the scANVI model:

vae_ref_scan = scvi.model.SCANVI.load("../output/scANVI_celltype_level2/",
                              adata = adata)
INFO     File ../output/scANVI_celltype_level2/model.pt already        
         downloaded                                                                          
/Users/dpcook/mambaforge/envs/scvi-env/lib/python3.9/site-packages/scvi/model/base/_utils.py:142: UserWarning: var_names for adata passed in does not match var_names of adata used to train the model. For valid results, the vars need to be the same and in the same order as the adata used to train the model.
  warnings.warn(
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [14], line 1
----> 1 vae_ref_scan = scvi.model.SCANVI.load("../output/scANVI_celltype_level2/",
      2                               adata = adata)

File ~/mambaforge/envs/scvi-env/lib/python3.9/site-packages/scvi/model/base/_base_model.py:652, in BaseModelClass.load(cls, dir_path, adata, use_gpu, prefix, backup_url)
    650 registry = attr_dict.pop("registry_")
    651 if _MODEL_NAME_KEY in registry and registry[_MODEL_NAME_KEY] != cls.__name__:
--> 652     raise ValueError(
    653         "It appears you are loading a model from a different class."
    654     )
    656 if _SETUP_ARGS_KEY not in registry:
    657     raise ValueError(
    658         "Saved model does not contain original setup inputs. "
    659         "Cannot load the original setup."
    660     )

ValueError: It appears you are loading a model from a different class.

It loads without an error from scvi.model.SCVI.load, but will give an error later if, for example, using scvi.model.SCANVI.load_query_data for reference mapping.

Can you paste the outputs of vae_ref_scan.registry_ after creating that instance?

You should see model_name in there and hopefully it says scanvi

I’ve tried this workflow in the following colab notebook:

I don’t seem to have any issue so I’m wondering what versions of scvi-tools you have in each environment.

1 Like

Thanks for the reply Adam. Not sure what I was doing and thought I had tried several independent times, but I’m no longer having the issue. Had an older version of the script that mistakenly saved vae_ref (the scVI model) instead of the scANVI model, so maybe I didn’t push that fix. I appreciate the help!