-
Notifications
You must be signed in to change notification settings - Fork 700
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
Add example model package #486
Comments
Hi, in a MONAI deploy WG meeting last week @ericspod mentioned that it might be useful to comment here on experiences with other app packaging frameworks. I just wanted to share my experiences with the MLFlow python_function "flavor", Unfortunately, I'm not able to share all the source code at this time, but I'll try to describe it best I can with a short excerpt. The model itself is an NLP application for classifying endoscopy reports based on transformers pre-trained BERT model. The basis of the package is this wrapper class which just requires """
mlflow python_model wrapper class for Barretts model
"""
import mlflow
import pandas as pd
from torch import topk
from project.BarrettsDataset import BarrettsDataset
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class BarrettsWrapper(mlflow.pyfunc.PythonModel):
def __init__(self, model, tokenizer):
self.model = model
self.tokenizer = tokenizer
self.dataset = BarrettsDataset(tokenizer=tokenizer)
def predict(self, context, model_input):
logger.info('Running prediction service ')
encoding = self.dataset.encoder(model_input.diag_final.tolist())
_, test_prediction = self.model(encoding["input_ids"], encoding["attention_mask"])
res = topk(test_prediction, 1).indices.tolist()
confidences = pd.DataFrame(test_prediction.tolist(), columns=self.dataset.label_columns)
prediction = pd.DataFrame({'Predicition': [self.dataset.label_columns[x[0]] for x in res]})
results = pd.concat([prediction, confidences], axis=1)
return results
# *** training code goes here, "model" is trained LightningModule, test_df is example input dataframe ***
wrappedModel = BarrettsWrapper(model, tokenizer)
signature = mlflow.models.signature.infer_signature(test_df, wrappedModel.predict(None, test_df))
mlflow.pyfunc.log_model('barretts_nlp', python_model=wrappedModel, signature=signature, code_path=['.']) Some things I think might be useful to mention:
I hope some of this rambling is useful, these are just a few things which come to mind, very happy to discuss if you have any questions! |
Hi @laurencejackson , Thanks so much for detailed sharing and feedback!
Thanks. |
Is your feature request related to a problem? Please describe.
Sub task of ticket Project-MONAI/MONAI#3482
Create an example of model package.
The text was updated successfully, but these errors were encountered: