{"id":688,"date":"2026-04-09T06:43:24","date_gmt":"2026-04-08T22:43:24","guid":{"rendered":"https:\/\/connectword.dpdns.org\/?p=688"},"modified":"2026-04-09T06:43:24","modified_gmt":"2026-04-08T22:43:24","slug":"a-comprehensive-implementation-guide-to-modelscope-for-model-search-inference-fine-tuning-evaluation-and-export","status":"publish","type":"post","link":"https:\/\/connectword.dpdns.org\/?p=688","title":{"rendered":"A Comprehensive Implementation Guide to ModelScope for Model Search, Inference, Fine-Tuning, Evaluation, and Export"},"content":{"rendered":"<p>In this tutorial, we explore <a href=\"https:\/\/github.com\/modelscope\/modelscope\"><strong>ModelScope<\/strong><\/a> through a practical, end-to-end workflow that runs smoothly on Colab. We begin by setting up the environment, verifying dependencies, and confirming GPU availability so we can work with the framework reliably from the start. From there, we interact with the ModelScope Hub to search for models, download snapshots, load datasets, and understand how its ecosystem connects with familiar tools such as Hugging Face Transformers. As we move forward, we apply pretrained pipelines across NLP and computer vision tasks, then fine-tune a sentiment classifier on IMDB, evaluate its performance, and export it for deployment. Through this process, we build not only a working implementation but also a clear understanding of how ModelScope can support research, experimentation, and production-oriented AI workflows.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">!pip install -q addict simplejson yapf gast oss2 sortedcontainers requests\n!pip install -q modelscope transformers&gt;=4.37.0 datasets torch torchvision \n   accelerate scikit-learn sentencepiece Pillow matplotlib evaluate optimum[exporters]\n\n\nimport torch, os, sys, json, warnings, numpy as np\nwarnings.filterwarnings(\"ignore\")\n\n\nimport addict; print(\"<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> addict OK\")\n\n\nprint(f\"PyTorch: {torch.__version__}\")\nprint(f\"CUDA available: {torch.cuda.is_available()}\")\nif torch.cuda.is_available():\n   print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n\n\nimport modelscope\nprint(f\"ModelScope: {modelscope.__version__}\")\n\n\nDEVICE = 0 if torch.cuda.is_available() else -1\n\n\n\n\nfrom modelscope import snapshot_download\nfrom modelscope.hub.api import HubApi\n\n\napi = HubApi()\nprint(\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f50d.png\" alt=\"\ud83d\udd0d\" class=\"wp-smiley\" \/> Searching ModelScope Hub for 'bert' models...n\")\ntry:\n   models = api.list_models(filter_dict={\"Search\": \"bert\"}, sort=\"StarCount\")\n   for i, m in enumerate(models):\n       if i &gt;= 5:\n           break\n       print(f\"  \u2022 {m.get('Name', m.get('id', 'N\/A'))}\")\nexcept Exception as e:\n   print(f\"  (Hub search may be unavailable outside China \u2014 {e})\")\n\n\nmodel_dir = snapshot_download(\n   \"AI-ModelScope\/bert-base-uncased\",\n   cache_dir=\".\/ms_cache\",\n)\nprint(f\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> Model downloaded to: {model_dir}\")\nprint(\"   Files:\", os.listdir(model_dir)[:8])\n\n\n\n\nfrom modelscope.msdatasets import MsDataset\n\n\nprint(\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f4e6.png\" alt=\"\ud83d\udce6\" class=\"wp-smiley\" \/> Loading 'imdb' dataset...n\")\ntry:\n   ds = MsDataset.load(\"imdb\", split=\"train\")\n   print(f\"  Dataset size: {len(ds)} samples\")\n   sample = next(iter(ds))\n   print(f\"  Keys: {list(sample.keys())}\")\n   print(f\"  Text preview: {sample['text'][:120]}...\")\n   print(f\"  Label: {sample['label']} (0=neg, 1=pos)\")\nexcept Exception as e:\n   print(f\"  Falling back to HuggingFace datasets: {e}\")\n   from datasets import load_dataset\n   ds = load_dataset(\"imdb\", split=\"train\")\n   print(f\"  Dataset size: {len(ds)} samples\")\n\n\nlabels = [row[\"label\"] for row in ds]\nprint(\"n  Label distribution:\")\nfor label in sorted(set(labels)):\n   count = labels.count(label)\n   print(f\"    Label {label}: {count} ({count\/len(labels)*100:.1f}%)\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We set up the complete Colab environment and install all the libraries required for the tutorial. We verify important dependencies such as addict, check the PyTorch and CUDA setup, and confirm that ModelScope is installed correctly before moving forward. We then begin working with the ModelScope ecosystem by searching the hub for BERT models, downloading a model snapshot locally, loading the IMDB dataset, and examining its label distribution to understand the data we will use later.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">from transformers import pipeline as hf_pipeline\n\n\nprint(\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f9e0.png\" alt=\"\ud83e\udde0\" class=\"wp-smiley\" \/> NLP PIPELINESn\")\n\n\nprint(\"\u2500\u2500 4a. Sentiment Analysis \u2500\u2500\")\nsentiment = hf_pipeline(\n   \"sentiment-analysis\",\n   model=\"distilbert-base-uncased-finetuned-sst-2-english\",\n   device=DEVICE,\n)\n\n\ntest_texts = [\n   \"ModelScope makes AI model access incredibly easy and intuitive!\",\n   \"The documentation was confusing and the API kept returning errors.\",\n   \"The weather today is partly cloudy with a slight breeze.\",\n]\n\n\nfor text in test_texts:\n   result = sentiment(text)[0]\n   emoji = \"<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f7e2.png\" alt=\"\ud83d\udfe2\" class=\"wp-smiley\" \/>\" if result[\"label\"] == \"POSITIVE\" else \"<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f534.png\" alt=\"\ud83d\udd34\" class=\"wp-smiley\" \/>\"\n   print(f'  {emoji} {result[\"label\"]} ({result[\"score\"]:.4f}): \"{text[:60]}...\"')\n\n\n\n\nprint(\"n\u2500\u2500 4b. Named Entity Recognition \u2500\u2500\")\nner = hf_pipeline(\n   \"ner\",\n   model=\"dbmdz\/bert-large-cased-finetuned-conll03-english\",\n   aggregation_strategy=\"simple\",\n   device=DEVICE,\n)\n\n\nner_text = \"Alibaba's ModelScope platform was developed in Hangzhou, China and competes with Hugging Face.\"\nentities = ner(ner_text)\nfor ent in entities:\n   print(f'  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f3f7.png\" alt=\"\ud83c\udff7\" class=\"wp-smiley\" \/>  {ent[\"word\"]} \u2192 {ent[\"entity_group\"]} (score: {ent[\"score\"]:.3f})')\n\n\n\n\nprint(\"n\u2500\u2500 4c. Zero-Shot Classification \u2500\u2500\")\nzsc = hf_pipeline(\n   \"zero-shot-classification\",\n   model=\"facebook\/bart-large-mnli\",\n   device=DEVICE,\n)\n\n\nzsc_result = zsc(\n   \"ModelScope provides pretrained models for NLP, CV, and audio tasks.\",\n   candidate_labels=[\"technology\", \"sports\", \"politics\", \"science\"],\n)\nfor label, score in zip(zsc_result[\"labels\"], zsc_result[\"scores\"]):\n   bar = \"\u2588\" * int(score * 30)\n   print(f\"  {label:&lt;12} {score:.3f} {bar}\")\n\n\n\n\nprint(\"n\u2500\u2500 4d. Text Generation (GPT-2) \u2500\u2500\")\ngenerator = hf_pipeline(\n   \"text-generation\",\n   model=\"gpt2\",\n   device=DEVICE,\n)\n\n\ngen_output = generator(\n   \"The future of open-source AI is\",\n   max_new_tokens=60,\n   do_sample=True,\n   temperature=0.8,\n   top_p=0.9,\n   num_return_sequences=1,\n)\nprint(f\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f4dd.png\" alt=\"\ud83d\udcdd\" class=\"wp-smiley\" \/> {gen_output[0]['generated_text']}\")\n\n\n\n\nprint(\"n\u2500\u2500 4e. Fill-Mask (BERT) \u2500\u2500\")\nfill_mask = hf_pipeline(\n   \"fill-mask\",\n   model=model_dir,\n   device=DEVICE,\n)\n\n\nmask_results = fill_mask(\"ModelScope is an open-source [MASK] for AI models.\")\nfor r in mask_results[:5]:\n   print(f\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/270f.png\" alt=\"\u270f\" class=\"wp-smiley\" \/>  [MASK] \u2192 '{r['token_str']}' (score: {r['score']:.4f})\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We focus on natural language processing pipelines and explore how easily we can run multiple tasks with pretrained models. We perform sentiment analysis, named entity recognition, zero-shot classification, text generation, and fill-mask prediction, providing a broad view of ModelScope-compatible inference workflows. As we test these tasks on sample inputs, we see how quickly we can move from raw text to meaningful model outputs in a unified pipeline.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">print(\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f441.png\" alt=\"\ud83d\udc41\" class=\"wp-smiley\" \/>  COMPUTER VISION PIPELINESn\")\n\n\nprint(\"\u2500\u2500 5a. Image Classification (ViT) \u2500\u2500\")\nimg_classifier = hf_pipeline(\n   \"image-classification\",\n   model=\"google\/vit-base-patch16-224\",\n   device=DEVICE,\n)\n\n\nimg_url = \"https:\/\/huggingface.co\/datasets\/huggingface\/documentation-images\/resolve\/main\/pipeline-cat-chonk.jpeg\"\nimg_results = img_classifier(img_url)\n\n\nfor r in img_results[:5]:\n   print(f\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f5bc.png\" alt=\"\ud83d\uddbc\" class=\"wp-smiley\" \/>  {r['label']:&lt;30} ({r['score']:.4f})\")\n\n\n\n\nprint(\"n\u2500\u2500 5b. Object Detection (DETR) \u2500\u2500\")\ndetector = hf_pipeline(\n   \"object-detection\",\n   model=\"facebook\/detr-resnet-50\",\n   device=DEVICE,\n)\n\n\ndetections = detector(img_url)\nfor d in detections[:5]:\n   box = d[\"box\"]\n   print(f\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f4e6.png\" alt=\"\ud83d\udce6\" class=\"wp-smiley\" \/> {d['label']:&lt;15} score={d['score']:.3f}  box=({box['xmin']:.0f},{box['ymin']:.0f},{box['xmax']:.0f},{box['ymax']:.0f})\")\n\n\n\n\nprint(\"n\u2500\u2500 5c. Visualising Detections \u2500\u2500\")\nfrom PIL import Image, ImageDraw\nimport requests, matplotlib.pyplot as plt\nfrom io import BytesIO\n\n\nimg = Image.open(BytesIO(requests.get(img_url).content))\ndraw = ImageDraw.Draw(img)\ncolors = [\"#58a6ff\", \"#3fb950\", \"#d2a8ff\", \"#f78166\", \"#ff7b72\"]\n\n\nfor i, d in enumerate(detections[:5]):\n   box = d[\"box\"]\n   color = colors[i % len(colors)]\n   draw.rectangle([box[\"xmin\"], box[\"ymin\"], box[\"xmax\"], box[\"ymax\"]], outline=color, width=3)\n   draw.text((box[\"xmin\"]+4, box[\"ymin\"]+2), f\"{d['label']} {d['score']:.2f}\", fill=color)\n\n\nplt.figure(figsize=(10, 7))\nplt.imshow(img)\nplt.axis(\"off\")\nplt.title(\"DETR Object Detection\")\nplt.tight_layout()\nplt.savefig(\"detection_result.png\", dpi=150, bbox_inches=\"tight\")\nplt.show()\nprint(\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> Saved detection_result.png\")\n\n\n\n\nprint(\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f504.png\" alt=\"\ud83d\udd04\" class=\"wp-smiley\" \/> HUGGINGFACE INTEROPn\")\n\n\nfrom transformers import AutoTokenizer, AutoModelForSequenceClassification\n\n\nprint(\"\u2500\u2500 Approach A: snapshot_download (works for models on ModelScope Hub) \u2500\u2500\")\nprint(f\"  We already downloaded bert-base-uncased in Section 2: {model_dir}\")\n\n\nprint(\"n\u2500\u2500 Approach B: Direct HF loading (works globally for any HF model) \u2500\u2500\")\n\n\nhf_model_name = \"distilbert-base-uncased-finetuned-sst-2-english\"\ntokenizer = AutoTokenizer.from_pretrained(hf_model_name)\nmodel = AutoModelForSequenceClassification.from_pretrained(hf_model_name)\nmodel.eval()\nprint(f\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> Loaded '{hf_model_name}' directly from HuggingFace\")\n\n\nprint(\"n\u2500\u2500 Manual inference without pipeline \u2500\u2500\")\ntexts = [\n   \"This open-source framework is a game changer for researchers!\",\n   \"I encountered multiple bugs during installation.\",\n]\n\n\ninputs = tokenizer(texts, padding=True, truncation=True, return_tensors=\"pt\")\n\n\nwith torch.no_grad():\n   outputs = model(**inputs)\n   probs = torch.softmax(outputs.logits, dim=-1)\n\n\nid2label = model.config.id2label\nfor text, prob in zip(texts, probs):\n   pred_id = prob.argmax().item()\n   print(f\"  \u2726 {id2label[pred_id]} ({prob[pred_id]:.4f}): '{text[:55]}...'\")\n\n\nprint(\"n\u2500\u2500 Loading Section 2's ModelScope-downloaded BERT with Transformers \u2500\u2500\")\nms_tokenizer = AutoTokenizer.from_pretrained(model_dir)\nms_model = AutoModelForSequenceClassification.from_pretrained(\n   model_dir, num_labels=2, ignore_mismatched_sizes=True\n)\nprint(f\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> bert-base-uncased from ModelScope loaded into Transformers AutoModel\")\nprint(f\"     Vocab size: {ms_tokenizer.vocab_size}, Hidden: {ms_model.config.hidden_size}\")\ndel ms_model<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We shift from text to computer vision and run image classification and object detection on a sample image. We also visualize the detection results by drawing bounding boxes and labels, which helps us inspect the model\u2019s predictions more intuitively and practically. After that, we explore Hugging Face interoperability by loading models and tokenizers directly, performing manual inference, and demonstrating that a model downloaded from ModelScope can also be used seamlessly with Transformers.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">print(\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f3af.png\" alt=\"\ud83c\udfaf\" class=\"wp-smiley\" \/> FINE-TUNING (DistilBERT on IMDB subset)n\")\n\n\nfrom datasets import load_dataset\nfrom transformers import (\n   AutoTokenizer,\n   AutoModelForSequenceClassification,\n   TrainingArguments,\n   Trainer,\n   DataCollatorWithPadding,\n)\nimport evaluate\n\n\nprint(\"  Loading IMDB subset...\")\nfull_train = load_dataset(\"imdb\", split=\"train\").shuffle(seed=42)\nfull_test  = load_dataset(\"imdb\", split=\"test\").shuffle(seed=42)\ntrain_ds = full_train.select(range(1000))\neval_ds  = full_test.select(range(500))\nprint(f\"  Train: {len(train_ds)}, Eval: {len(eval_ds)}\")\n\n\nckpt = \"distilbert-base-uncased\"\ntokenizer = AutoTokenizer.from_pretrained(ckpt)\n\n\ndef tokenize_fn(batch):\n   return tokenizer(batch[\"text\"], truncation=True, max_length=256)\n\n\ntrain_ds = train_ds.map(tokenize_fn, batched=True)\neval_ds  = eval_ds.map(tokenize_fn, batched=True)\n\n\nmodel = AutoModelForSequenceClassification.from_pretrained(\n   ckpt,\n   num_labels=2,\n   id2label={0: \"NEGATIVE\", 1: \"POSITIVE\"},\n   label2id={\"NEGATIVE\": 0, \"POSITIVE\": 1},\n)\n\n\naccuracy_metric = evaluate.load(\"accuracy\")\nf1_metric = evaluate.load(\"f1\")\n\n\ndef compute_metrics(eval_pred):\n   logits, labels = eval_pred\n   preds = np.argmax(logits, axis=-1)\n   acc = accuracy_metric.compute(predictions=preds, references=labels)\n   f1 = f1_metric.compute(predictions=preds, references=labels, average=\"weighted\")\n   return {**acc, **f1}\n\n\ntraining_args = TrainingArguments(\n   output_dir=\".\/ms_finetuned_model\",\n   num_train_epochs=2,\n   per_device_train_batch_size=16,\n   per_device_eval_batch_size=32,\n   learning_rate=2e-5,\n   weight_decay=0.01,\n   eval_strategy=\"epoch\",\n   save_strategy=\"epoch\",\n   load_best_model_at_end=True,\n   metric_for_best_model=\"accuracy\",\n   logging_steps=50,\n   report_to=\"none\",\n   fp16=torch.cuda.is_available(),\n   dataloader_num_workers=2,\n)\n\n\ntrainer = Trainer(\n   model=model,\n   args=training_args,\n   train_dataset=train_ds,\n   eval_dataset=eval_ds,\n   processing_class=tokenizer,\n   data_collator=DataCollatorWithPadding(tokenizer),\n   compute_metrics=compute_metrics,\n)\n\n\nprint(\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f680.png\" alt=\"\ud83d\ude80\" class=\"wp-smiley\" \/> Starting training...n\")\ntrain_result = trainer.train()\nprint(f\"n  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> Training complete!\")\nprint(f\"     Train loss: {train_result.training_loss:.4f}\")\nprint(f\"     Train time: {train_result.metrics['train_runtime']:.1f}s\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We move into fine-tuning by preparing a smaller IMDB subset so that training remains practical inside Google Colab. We tokenize the text, load a pretrained DistilBERT classification model, define evaluation metrics, and configure the training process with suitable arguments for a lightweight but realistic demonstration. We then launch training and observe how a pretrained checkpoint is adapted into a task-specific sentiment classifier through the Trainer workflow.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">print(\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f4ca.png\" alt=\"\ud83d\udcca\" class=\"wp-smiley\" \/> MODEL EVALUATIONn\")\n\n\neval_results = trainer.evaluate()\nprint(\"  Evaluation Results:\")\nfor key, value in eval_results.items():\n   if isinstance(value, float):\n       print(f\"    {key:&lt;25}: {value:.4f}\")\n\n\nfrom sklearn.metrics import classification_report, confusion_matrix\n\n\npreds_output = trainer.predict(eval_ds)\npreds = np.argmax(preds_output.predictions, axis=-1)\nlabels = preds_output.label_ids\n\n\nprint(\"n  Classification Report:\")\nprint(classification_report(labels, preds, target_names=[\"NEGATIVE\", \"POSITIVE\"]))\n\n\ncm = confusion_matrix(labels, preds)\nfig, ax = plt.subplots(figsize=(5, 4))\nim = ax.imshow(cm, cmap=\"Blues\")\nax.set_xticks([0, 1]); ax.set_yticks([0, 1])\nax.set_xticklabels([\"NEGATIVE\", \"POSITIVE\"])\nax.set_yticklabels([\"NEGATIVE\", \"POSITIVE\"])\nax.set_xlabel(\"Predicted\"); ax.set_ylabel(\"Actual\")\nax.set_title(\"Confusion Matrix \u2014 Fine-Tuned DistilBERT\")\nfor i in range(2):\n   for j in range(2):\n       ax.text(j, i, str(cm[i, j]), ha=\"center\", va=\"center\",\n               color=\"white\" if cm[i, j] &gt; cm.max()\/2 else \"black\", fontsize=18)\nplt.colorbar(im)\nplt.tight_layout()\nplt.savefig(\"confusion_matrix.png\", dpi=150)\nplt.show()\nprint(\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> Saved confusion_matrix.png\")\n\n\nprint(\"n\u2500\u2500 Testing Fine-Tuned Model on New Inputs \u2500\u2500\")\nft_pipeline = hf_pipeline(\n   \"sentiment-analysis\",\n   model=trainer.model,\n   tokenizer=tokenizer,\n   device=DEVICE,\n)\n\n\nnew_reviews = [\n   \"An absolutely breathtaking masterpiece with brilliant performances!\",\n   \"Waste of two hours. Terrible script and wooden acting.\",\n   \"Decent popcorn movie but nothing special. Had some fun moments.\",\n]\n\n\nfor review in new_reviews:\n   res = ft_pipeline(review)[0]\n   emoji = \"<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f7e2.png\" alt=\"\ud83d\udfe2\" class=\"wp-smiley\" \/>\" if res[\"label\"] == \"POSITIVE\" else \"<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f534.png\" alt=\"\ud83d\udd34\" class=\"wp-smiley\" \/>\"\n   print(f'  {emoji} {res[\"label\"]} ({res[\"score\"]:.4f}): \"{review}\"')\n\n\n\n\nprint(\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f4be.png\" alt=\"\ud83d\udcbe\" class=\"wp-smiley\" \/> EXPORTING THE FINE-TUNED MODELn\")\n\n\nsave_path = \".\/ms_finetuned_model\/final\"\ntrainer.save_model(save_path)\ntokenizer.save_pretrained(save_path)\nprint(f\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> Model saved to: {save_path}\")\nprint(f\"     Files: {os.listdir(save_path)}\")\n\n\nprint(\"n\u2500\u2500 ONNX Export \u2500\u2500\")\ntry:\n   from optimum.exporters.onnx import main_export\n   onnx_path = \".\/ms_finetuned_model\/onnx\"\n   main_export(save_path, output=onnx_path, task=\"text-classification\")\n   print(f\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> ONNX model exported to: {onnx_path}\")\n   print(f\"     Files: {os.listdir(onnx_path)}\")\nexcept Exception as e:\n   print(f\"  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/26a0.png\" alt=\"\u26a0\" class=\"wp-smiley\" \/>  ONNX export skipped: {e}\")\n\n\nprint(\"\"\"\n\u2500\u2500 Upload to ModelScope Hub (manual step) \u2500\u2500\n\n\n 1. Get a token from https:\/\/modelscope.cn\/my\/myaccesstoken\n 2. Run:\n\n\n    from modelscope.hub.api import HubApi\n    api = HubApi()\n    api.login('YOUR_TOKEN')\n    api.push_model(\n        model_id='your-username\/my-finetuned-distilbert',\n        model_dir='.\/ms_finetuned_model\/final',\n    )\n\"\"\")\n\n\nprint(\"\"\"\n\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\n\u2551                   <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f389.png\" alt=\"\ud83c\udf89\" class=\"wp-smiley\" \/>  TUTORIAL COMPLETE!  <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/1f389.png\" alt=\"\ud83c\udf89\" class=\"wp-smiley\" \/>                    \u2551\n\u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563\n\u2551  \u2713 ModelScope Hub \u2014 search, browse &amp; download models            \u2551\n\u2551  \u2713 MsDataset \u2014 load datasets from the ModelScope ecosystem      \u2551\n\u2551  \u2713 NLP pipelines \u2014 sentiment, NER, zero-shot, generation, mask  \u2551\n\u2551  \u2713 CV pipelines \u2014 image classification, object detection, viz   \u2551\n\u2551  \u2713 HuggingFace interop \u2014 snapshot_download + Transformers       \u2551\n\u2551  \u2713 Fine-tuning \u2014 DistilBERT on IMDB with Trainer API            \u2551\n\u2551  \u2713 Evaluation \u2014 accuracy, F1, confusion matrix                  \u2551\n\u2551  \u2713 Export \u2014 local save, ONNX, Hub upload                        \u2551\n\u255a\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255d\n\"\"\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We evaluate the fine-tuned model in detail and inspect its performance using standard metrics, a classification report, and a confusion matrix. We also test the trained model on fresh review examples to see how it behaves on unseen inputs in a realistic inference setting. Also, we save the model locally, export it to ONNX when possible, and review how we can upload the final checkpoint to the ModelScope Hub for sharing and deployment.<\/p>\n<p>In conclusion, we built a complete, hands-on pipeline that demonstrates how ModelScope fits into a real machine learning workflow rather than serving solely as a model repository. We searched and downloaded models, loaded datasets, ran inference across NLP and vision tasks, connected ModelScope assets with Transformers, fine-tuned a text classifier, evaluated it with meaningful metrics, and exported it for later use. By going through each stage of the code, we saw how the framework supports both experimentation and practical deployment, while also providing flexibility through interoperability with the broader Hugging Face ecosystem. In the end, we came away with a reusable Colab-ready workflow and a much stronger understanding of how to use ModelScope as a serious toolkit for building, testing, and sharing AI systems.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>Check out\u00a0the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\/blob\/main\/Deep%20Learning\/modelscope_end_to_end_model_hub_dataset_nlp_cv_finetuning_evaluation_export_marktechpost(1).py\" target=\"_blank\" rel=\"noreferrer noopener\">Full Codes here<\/a>. \u00a0<\/strong>Also,\u00a0feel free to follow us on\u00a0<strong><a href=\"https:\/\/x.com\/intent\/follow?screen_name=marktechpost\" target=\"_blank\" rel=\"noreferrer noopener\"><mark>Twitter<\/mark><\/a><\/strong>\u00a0and don\u2019t forget to join our\u00a0<strong><a href=\"https:\/\/www.reddit.com\/r\/machinelearningnews\/\" target=\"_blank\" rel=\"noreferrer noopener\">120k+ ML SubReddit<\/a><\/strong>\u00a0and Subscribe to\u00a0<strong><a href=\"https:\/\/www.aidevsignals.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">our Newsletter<\/a><\/strong>. Wait! are you on telegram?\u00a0<strong><a href=\"https:\/\/t.me\/machinelearningresearchnews\" target=\"_blank\" rel=\"noreferrer noopener\">now you can join us on telegram as well.<\/a><\/strong><\/p>\n<p>Need to partner with us for promoting your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar etc.?\u00a0<strong><a href=\"https:\/\/forms.gle\/MTNLpmJtsFA3VRVd9\" target=\"_blank\" rel=\"noreferrer noopener\"><mark>Connect with us<\/mark><\/a><\/strong><\/p>\n<p>The post <a href=\"https:\/\/www.marktechpost.com\/2026\/04\/08\/a-comprehensive-implementation-guide-to-modelscope-for-model-search-inference-fine-tuning-evaluation-and-export\/\">A Comprehensive Implementation Guide to ModelScope for Model Search, Inference, Fine-Tuning, Evaluation, and Export<\/a> appeared first on <a href=\"https:\/\/www.marktechpost.com\/\">MarkTechPost<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we explore M&hellip;<\/p>\n","protected":false},"author":1,"featured_media":29,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-688","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=688"}],"version-history":[{"count":0,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/688\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/media\/29"}],"wp:attachment":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}