How to export model from the checkpoint and then load the same model for prediction?
State: closed | Pull request: no

Hi, 

I have trained my own dataset using NLP Joint Intent & slot classification NeMo models. I am able to train & predict the data. However i am not getting how can i save the model from that checkpoint and then reload the same model & predict on example test queries. I can understand using experiment manager, path has been defined in yaml file for saving checkpoints, but i am unable to see that checkpoint file in that location(Running in google Colab).

My requirement is to save that model at my own defined location, then reload that model and i should able to predict from the reloaded model.

I have tried these things:-
1) save_to method  (It's working. model has been saved)
2) restore_from() method (It's not working)
3) load_from_checkpoint (This is working, but i need to train always first, the i can load, But i dont want to train everytime. I want to just load model & predict from that trained model)

Need your help to understand the correct way of using saving reloading APIs of NeMo & why it is like not able to see that checkpoint in google Colab location & the same path i am able to reload from the checkpoint, but can't see that file.

**Environment overview (please complete the following information)**

 - Environment location: Colab
 - Method of NeMo install: 
BRANCH = 'v1.0.0b2'
!python -m pip install git+https://github.com/NVIDIA/NeMo.git@$BRANCH#egg=nemo_toolkit[nlp]

Thank you!


You should be able to:
```
mymodel.save_to(save_path="mymodel.nemo")
```
then (possibly in another python script)
```
mymodel=IntentSlotClassificationModel.restore_from(restore_path="mymodel.nemo")
```
Note that ```restore_from``` is a class method which returns class instance. Are you calling it from existing instance instead when you say it is not working?


This example from our code shows also how to restore this model and use it for Inference.
https://github.com/NVIDIA/NeMo/blob/main/examples/nlp/intent_slot_classification/intent_slot_classification.py

Yes, I have tried to call from existing instance as well as in another script as well. The error which i get is like these:-

FileNotFoundError: Make sure that your data follows the standard format supported by JointIntentSlotDataset. Your data must contain dict.intents.csv and dict.slots.csv.

I am assuming in this "intent_slot_classification_descriptor.py " file, this things has been defined in _init_ method constructor, but if i am restoring then i need to place intents & slots csv in some "DATA_DIR" folder? My question is if i am restoring model, really data needs to be there while restoring models?

Many Thanks!

Basically the model currently expects to get Dictionary of Intents and Slots as files from outside, from "data_dir" folder. The reason for that is that the model is self sufficient but to interpret the outputs (numbers) of the classified Intents and Slots coming from the top layer of the model you still need to have a dictionary of appropriate labels. Please let us know if it creates any difficulties for you?

Many Thanks for your response. I am still not able to get my problem solved.

Let me define the steps which i am following:-

Run in google colab session-1:-

1) Preprocess data into Nemo-format & create files (dict.intents.csv, dict.slots.csv, train.tsv, test.tsv, train_slots.tsv, test_slots.tsv)
2) Fetch config yaml file & update parameters 
3) Do training
4) model.save_to('model_education.nemo') # save the model at some drive location
5)  Evaluate from the checkpoint saved by model training:-
`# extract the path of the best checkpoint from the training, you may update it to any other saved checkpoint file
checkpoint_path = trainer.checkpoint_callback.best_model_path
eval_model = nemo_nlp.models.IntentSlotClassificationModel.load_from_checkpoint(checkpoint_path=checkpoint_path)`
6)  Predict queries:-
`queries = [
    'hello',
    'I would like to enroll for deep learning course',
    'I enrolled and paid for DLNLP course which starts from October 10th',
    'pycharm connection error'
]
pred_intents, pred_slots = eval_model.predict_from_examples(queries)
`

I am able to predict from the above method as the training is in the same session.

Now my `google colab session-1` is disconnected.

Another google colab session-2:-

1) Import libraries
2) Fetch config file
3) Update DATA_DIR & NEMO_DIR Folder path
4) Dict.intents.csv & dict.slots.csv files are placed at DATA_DIR Folder.
5) Load the model

`mymodel=nemo_nlp.models.IntentSlotClassificationModel.restore_from(restore_path="/content/drive/My Drive/Colab Notebooks/mymodel_edu.nemo")`

Then error comes:-
FileNotFoundError: Make sure that your data follows the standard format supported by JointIntentSlotDataset. Your data must contain dict.intents.csv and dict.slots.csv.

It will be very helpful if you can help me where i made mistake or any other  documentation i can refer which tells this kind of scenario?

Thanks.


hello @Khushali22  
error says file not found and from your explanation I'am seeing that you saved the nemo model with name as _'model_education.nemo')_   in step 4      and restoring it with name 'mymodel_edu.nemo' ?