메뉴
BL
MarkTechPost 52일 전

ModelScope 실전 가이드: 검색부터 파인튜닝, 배포까지

IMP
7/10
핵심 요약

ModelScope 허브를 활용하여 AI 모델을 검색, 다운로드, 파인튜닝 및 배포하는 방법을 다루는 실전 튜토리얼입니다. IMDB 데이터셋을 이용한 감성 분류기 미세조정 과정을 통해 자연어처리(NLP) 파이프라인을 직접 구축해 봅니다. 허깅페이스(Hugging Face)와의 호환성을 강조하며 연구부터 프로덕션 환경까지 아우르는 통합 워크플로우를 제공합니다.

번역된 본문

인공지능(AI) 인프라 기술 및 튜토리얼 소식입니다. 이 튜토리얼에서는 구글 코랩(Colab) 환경에서 원활하게 실행되는 실전 엔드투엔드(End-to-End) 워크플로우를 통해 ModelScope를 탐구합니다. 먼저 환경을 설정하고 종속성을 확인하며 GPU 사용 가능 여부를 점검하여 프레임워크를 안정적으로 다룰 수 있는 기반을 마련합니다.

이어서 ModelScope 허브(Hub)와 상호 작용하여 모델을 검색하고, 스냅샷을 다운로드하며, 데이터셋을 로드하는 방법을 배웁니다. 또한 이 생태계가 허깅페이스 트랜스포머(Hugging Face Transformers)와 같은 익숙한 도구들과 어떻게 연결되는지 이해합니다.

나아가 자연어 처리(NLP) 및 컴퓨터 비전 작업에 사전 학습된 파이프라인을 적용해 보고, IMDB 데이터셋을 활용해 감성 분류기를 파인튜닝(Fine-tuning)한 뒤 성능을 평가하고 배포를 위해 모델을 내보내는(Export) 과정까지 진행합니다. 이 과정을 통해 단순히 작동하는 코드를 넘어, ModelScope가 연구, 실험 및 실제 서비스(Production) 중심의 AI 워크플로우를 어떻게 지원하는지 명확히 이해할 수 있습니다.

코드 복사 !pip install -q addict simplejson yapf gast oss2 sortedcontainers requests !pip install -q modelscope transformers>=4.37.0 datasets torch torchvision \ accelerate scikit-learn sentencepiece Pillow matplotlib evaluate optimum[exporters] import torch, os, sys, json, warnings, numpy as np warnings.filterwarnings("ignore") import addict; print("✅ addict OK") print(f"PyTorch: {torch.version}") print(f"CUDA available: {torch.cuda.is_available()}") if torch.cuda.is_available(): print(f"GPU: {torch.cuda.get_device_name(0)}") import modelscope print(f"ModelScope: {modelscope.version}") DEVICE = 0 if torch.cuda.is_available() else -1 from modelscope import snapshot_download from modelscope.hub.api import HubApi api = HubApi() print("\n🔍 ModelScope Hub에서 'bert' 모델 검색 중...\n") try: models = api.list_models(filter_dict={"Search": "bert"}, sort="StarCount") for i, m in enumerate(models): if i >= 5: break print(f" • {m.get('Name', m.get('id', 'N/A'))}") except Exception as e: print(f" (중국 외부에서는 허브 검색이 불가능할 수 있음 — {e})") model_dir = snapshot_download( "AI-ModelScope/bert-base-uncased", cache_dir="./ms_cache", ) print(f"\n✅ 모델 다운로드 완료: {model_dir}") print(" 파일 목록:", os.listdir(model_dir)[:8]) from modelscope.msdatasets import MsDataset print("\n📦 'imdb' 데이터셋 로딩 중...\n") try: ds = MsDataset.load("imdb", split="train") print(f" 데이터셋 크기: {len(ds)} 샘플") sample = next(iter(ds)) print(f" 키(Keys): {list(sample.keys())}") print(f" 텍스트 미리보기: {sample['text'][:120]}...") print(f" 라벨(Label): {sample['label']} (0=부정, 1=긍정)") except Exception as e: print(f" HuggingFace 데이터셋으로 대체(fallback) 중: {e}") from datasets import load_dataset ds = load_dataset("imdb", split="train") print(f" 데이터셋 크기: {len(ds)} 샘플") labels = [row["label"] for row in ds] print("\n 라벨 분포:") for label in sorted(set(labels)): count = labels.count(label) print(f" 라벨 {label}: {count}개 ({count/len(labels)*100:.1f}%)")

이렇게 우리는 코랩(Colab) 환경을 완전히 설정하고 튜토리얼에 필요한 모든 라이브러리를 설치했습니다. 본격적인 작업에 앞서 addict와 같은 중요한 종속성을 확인하고, 파이토치(PyTorch) 및 CUDA 설정을 점검하며 ModelScope가 올바르게 설치되었는지 확인했습니다. 그런 다음 ModelScope 생태계를 다루기 시작했는데, 허브에서 BERT 모델을 검색하고 모델 스냅샷을 로컬에 다운로드한 뒤 IMDB 데이터셋을 로딩하여 향후 사용할 데이터의 라벨 분포를 살펴보았습니다.

코드 복사 from transformers import pipeline as hf_pipeline print("\n🧠 NLP 파이프라인\n") print("── 4a. 감성 분석(Sentiment Analysis) ──") sentiment = hf_pipeline( "sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", device=DEVICE, ) test_texts = [ "ModelScope는 AI 모델에 대한 접근을 믿을 수 없을 만큼 쉽고 직관적으로 만들어 줍니다!", "문서가 혼란스럽고 API는 계속 오류를 반환했습니다.", "오늘 날씨는 약간 흐리지만 산들바람이 붑니다.", ] for text in test_texts: result = sentiment(text)[0] emoji = "🟢" if result['label'] == 'POSITIVE' else "🔴"

원문 보기
원문 보기 (영어)
Artificial Intelligence AI Infrastructure Technology Editors Pick Staff Tutorials In this tutorial, we explore ModelScope 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. Copy Code Copied Use a different Browser !pip install -q addict simplejson yapf gast oss2 sortedcontainers requests !pip install -q modelscope transformers>=4.37.0 datasets torch torchvision \ accelerate scikit-learn sentencepiece Pillow matplotlib evaluate optimum[exporters] import torch, os, sys, json, warnings, numpy as np warnings.filterwarnings("ignore") import addict; print("✅ addict OK") print(f"PyTorch: {torch.__version__}") print(f"CUDA available: {torch.cuda.is_available()}") if torch.cuda.is_available(): print(f"GPU: {torch.cuda.get_device_name(0)}") import modelscope print(f"ModelScope: {modelscope.__version__}") DEVICE = 0 if torch.cuda.is_available() else -1 from modelscope import snapshot_download from modelscope.hub.api import HubApi api = HubApi() print("\n🔍 Searching ModelScope Hub for 'bert' models...\n") try: models = api.list_models(filter_dict={"Search": "bert"}, sort="StarCount") for i, m in enumerate(models): if i >= 5: break print(f" • {m.get('Name', m.get('id', 'N/A'))}") except Exception as e: print(f" (Hub search may be unavailable outside China — {e})") model_dir = snapshot_download( "AI-ModelScope/bert-base-uncased", cache_dir="./ms_cache", ) print(f"\n✅ Model downloaded to: {model_dir}") print(" Files:", os.listdir(model_dir)[:8]) from modelscope.msdatasets import MsDataset print("\n📦 Loading 'imdb' dataset...\n") try: ds = MsDataset.load("imdb", split="train") print(f" Dataset size: {len(ds)} samples") sample = next(iter(ds)) print(f" Keys: {list(sample.keys())}") print(f" Text preview: {sample['text'][:120]}...") print(f" Label: {sample['label']} (0=neg, 1=pos)") except Exception as e: print(f" Falling back to HuggingFace datasets: {e}") from datasets import load_dataset ds = load_dataset("imdb", split="train") print(f" Dataset size: {len(ds)} samples") labels = [row["label"] for row in ds] print("\n Label distribution:") for label in sorted(set(labels)): count = labels.count(label) print(f" Label {label}: {count} ({count/len(labels)*100:.1f}%)") 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. Copy Code Copied Use a different Browser from transformers import pipeline as hf_pipeline print("\n🧠 NLP PIPELINES\n") print("── 4a. Sentiment Analysis ──") sentiment = hf_pipeline( "sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", device=DEVICE, ) test_texts = [ "ModelScope makes AI model access incredibly easy and intuitive!", "The documentation was confusing and the API kept returning errors.", "The weather today is partly cloudy with a slight breeze.", ] for text in test_texts: result = sentiment(text)[0] emoji = "🟢" if result["label"] == "POSITIVE" else "🔴" print(f' {emoji} {result["label"]} ({result["score"]:.4f}): "{text[:60]}..."') print("\n── 4b. Named Entity Recognition ──") ner = hf_pipeline( "ner", model="dbmdz/bert-large-cased-finetuned-conll03-english", aggregation_strategy="simple", device=DEVICE, ) ner_text = "Alibaba's ModelScope platform was developed in Hangzhou, China and competes with Hugging Face." entities = ner(ner_text) for ent in entities: print(f' 🏷️ {ent["word"]} → {ent["entity_group"]} (score: {ent["score"]:.3f})') print("\n── 4c. Zero-Shot Classification ──") zsc = hf_pipeline( "zero-shot-classification", model="facebook/bart-large-mnli", device=DEVICE, ) zsc_result = zsc( "ModelScope provides pretrained models for NLP, CV, and audio tasks.", candidate_labels=["technology", "sports", "politics", "science"], ) for label, score in zip(zsc_result["labels"], zsc_result["scores"]): bar = "█" * int(score * 30) print(f" {label:<12} {score:.3f} {bar}") print("\n── 4d. Text Generation (GPT-2) ──") generator = hf_pipeline( "text-generation", model="gpt2", device=DEVICE, ) gen_output = generator( "The future of open-source AI is", max_new_tokens=60, do_sample=True, temperature=0.8, top_p=0.9, num_return_sequences=1, ) print(f" 📝 {gen_output[0]['generated_text']}") print("\n── 4e. Fill-Mask (BERT) ──") fill_mask = hf_pipeline( "fill-mask", model=model_dir, device=DEVICE, ) mask_results = fill_mask("ModelScope is an open-source [MASK] for AI models.") for r in mask_results[:5]: print(f" ✏️ [MASK] → '{r['token_str']}' (score: {r['score']:.4f})") 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. Copy Code Copied Use a different Browser print("\n👁️ COMPUTER VISION PIPELINES\n") print("── 5a. Image Classification (ViT) ──") img_classifier = hf_pipeline( "image-classification", model="google/vit-base-patch16-224", device=DEVICE, ) img_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" img_results = img_classifier(img_url) for r in img_results[:5]: print(f" 🖼️ {r['label']:<30} ({r['score']:.4f})") print("\n── 5b. Object Detection (DETR) ──") detector = hf_pipeline( "object-detection", model="facebook/detr-resnet-50", device=DEVICE, ) detections = detector(img_url) for d in detections[:5]: box = d["box"] print(f" 📦 {d['label']:<15} score={d['score']:.3f} box=({box['xmin']:.0f},{box['ymin']:.0f},{box['xmax']:.0f},{box['ymax']:.0f})") print("\n── 5c. Visualising Detections ──") from PIL import Image, ImageDraw import requests, matplotlib.pyplot as plt from io import BytesIO img = Image.open(BytesIO(requests.get(img_url).content)) draw = ImageDraw.Draw(img) colors = ["#58a6ff", "#3fb950", "#d2a8ff", "#f78166", "#ff7b72"] for i, d in enumerate(detections[:5]): box = d["box"] color = colors[i % len(colors)] draw.rectangle([box["xmin"], box["ymin"], box["xmax"], box["ymax"]], outline=color, width=3) draw.text((box["xmin"]+4, box["ymin"]+2), f"{d['label']} {d['score']:.2f}", fill=color) plt.figure(figsize=(10, 7)) plt.imshow(img) plt.axis("off") plt.title("DETR Object Detection") plt.tight_layout() plt.savefig("detection_result.png", dpi=150, bbox_inches="tight") plt.show() print(" ✅ Saved detection_result.png") print("\n🔄 HUGGINGFACE INTEROP\n") from transformers import AutoTokenizer, AutoModelForSequenceClassification print("── Approach A: snapshot_download (works for models on ModelScope Hub) ──") print(f" We already downloaded bert-base-uncased in Section 2: {model_dir}") print("\n── Approach B: Direct HF loading (works globally for any HF model) ──") hf_model_name = "distilbert-ba