Technology & AI

ModelScope’s Complete Beginner’s Guide to Model Search, Inference, Tuning, Testing, and Exporting

print("n๐Ÿ“Š MODEL EVALUATIONn")


eval_results = trainer.evaluate()
print("  Evaluation Results:")
for key, value in eval_results.items():
   if isinstance(value, float):
       print(f"    {key:<25}: {value:.4f}")


from sklearn.metrics import classification_report, confusion_matrix


preds_output = trainer.predict(eval_ds)
preds = np.argmax(preds_output.predictions, axis=-1)
labels = preds_output.label_ids


print("n  Classification Report:")
print(classification_report(labels, preds, target_names=["NEGATIVE", "POSITIVE"]))


cm = confusion_matrix(labels, preds)
fig, ax = plt.subplots(figsize=(5, 4))
im = ax.imshow(cm, cmap="Blues")
ax.set_xticks([0, 1]); ax.set_yticks([0, 1])
ax.set_xticklabels(["NEGATIVE", "POSITIVE"])
ax.set_yticklabels(["NEGATIVE", "POSITIVE"])
ax.set_xlabel("Predicted"); ax.set_ylabel("Actual")
ax.set_title("Confusion Matrix โ€” Fine-Tuned DistilBERT")
for i in range(2):
   for j in range(2):
       ax.text(j, i, str(cm[i, j]), ha="center", va="center",
               color="white" if cm[i, j] > cm.max()/2 else "black", fontsize=18)
plt.colorbar(im)
plt.tight_layout()
plt.savefig("confusion_matrix.png", dpi=150)
plt.show()
print("  โœ… Saved confusion_matrix.png")


print("nโ”€โ”€ Testing Fine-Tuned Model on New Inputs โ”€โ”€")
ft_pipeline = hf_pipeline(
   "sentiment-analysis",
   model=trainer.model,
   tokenizer=tokenizer,
   device=DEVICE,
)


new_reviews = [
   "An absolutely breathtaking masterpiece with brilliant performances!",
   "Waste of two hours. Terrible script and wooden acting.",
   "Decent popcorn movie but nothing special. Had some fun moments.",
]


for review in new_reviews:
   res = ft_pipeline(review)[0]
   emoji = "๐ŸŸข" if res["label"] == "POSITIVE" else "๐Ÿ”ด"
   print(f'  {emoji} {res["label"]} ({res["score"]:.4f}): "{review}"')




print("n๐Ÿ’พ EXPORTING THE FINE-TUNED MODELn")


save_path = "./ms_finetuned_model/final"
trainer.save_model(save_path)
tokenizer.save_pretrained(save_path)
print(f"  โœ… Model saved to: {save_path}")
print(f"     Files: {os.listdir(save_path)}")


print("nโ”€โ”€ ONNX Export โ”€โ”€")
try:
   from optimum.exporters.onnx import main_export
   onnx_path = "./ms_finetuned_model/onnx"
   main_export(save_path, output=onnx_path, task="text-classification")
   print(f"  โœ… ONNX model exported to: {onnx_path}")
   print(f"     Files: {os.listdir(onnx_path)}")
except Exception as e:
   print(f"  โš ๏ธ  ONNX export skipped: {e}")


print("""
โ”€โ”€ Upload to ModelScope Hub (manual step) โ”€โ”€


 1. Get a token from 
 2. Run:


    from modelscope.hub.api import HubApi
    api = HubApi()
    api.login('YOUR_TOKEN')
    api.push_model(
        model_id='your-username/my-finetuned-distilbert',
        model_dir="./ms_finetuned_model/final",
    )
""")


print("""
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘                   ๐ŸŽ‰  TUTORIAL COMPLETE!  ๐ŸŽ‰                    โ•‘
โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ
โ•‘  โœ“ ModelScope Hub โ€” search, browse & download models            โ•‘
โ•‘  โœ“ MsDataset โ€” load datasets from the ModelScope ecosystem      โ•‘
โ•‘  โœ“ NLP pipelines โ€” sentiment, NER, zero-shot, generation, mask  โ•‘
โ•‘  โœ“ CV pipelines โ€” image classification, object detection, viz   โ•‘
โ•‘  โœ“ HuggingFace interop โ€” snapshot_download + Transformers       โ•‘
โ•‘  โœ“ Fine-tuning โ€” DistilBERT on IMDB with Trainer API            โ•‘
โ•‘  โœ“ Evaluation โ€” accuracy, F1, confusion matrix                  โ•‘
โ•‘  โœ“ Export โ€” local save, ONNX, Hub upload                        โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
""")

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button