forked from huggingface/transformers
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐ [i18n-KO] Translated `knowledge_distillation_for_image_classificatiโฆ
โฆon.md to Korean" (huggingface#32334) * docs: ko: tasks/knowledge_distillation_for_image_classification.md * feat: nmt draft * fix: manual edits * Apply suggestions from code review Co-authored-by: Chulhwa (Evan) Han <[email protected]> * Apply suggestions from code review Co-authored-by: Chulhwa (Evan) Han <[email protected]> * Apply suggestions from code review Co-authored-by: Ahnjj_DEV <[email protected]> * Apply suggestions from code review Co-authored-by: Ahnjj_DEV <[email protected]> * Apply suggestions from code review Co-authored-by: Ahnjj_DEV <[email protected]> * Apply suggestions from code review Co-authored-by: Chulhwa (Evan) Han <[email protected]> * Apply suggestions from code review Co-authored-by: Chulhwa (Evan) Han <[email protected]> * Apply suggestions from code review Co-authored-by: Chulhwa (Evan) Han <[email protected]> * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: Chulhwa (Evan) Han <[email protected]> Co-authored-by: Ahnjj_DEV <[email protected]>
- Loading branch information
Showing
2 changed files
with
195 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
docs/source/ko/tasks/knowledge_distillation_for_image_classification.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<!--Copyright 2023 The HuggingFace Team. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
rendered properly in your Markdown viewer. | ||
--> | ||
# ์ปดํจํฐ ๋น์ ์ ์ํ ์ง์ ์ฆ๋ฅ[[Knowledge-Distillation-for-Computer-Vision]] | ||
|
||
[[open-in-colab]] | ||
|
||
์ง์ ์ฆ๋ฅ(Knowledge distillation)๋ ๋ ํฌ๊ณ ๋ณต์กํ ๋ชจ๋ธ(๊ต์ฌ)์์ ๋ ์๊ณ ๊ฐ๋จํ ๋ชจ๋ธ(ํ์)๋ก ์ง์์ ์ ๋ฌํ๋ ๊ธฐ์ ์ ๋๋ค. ํ ๋ชจ๋ธ์์ ๋ค๋ฅธ ๋ชจ๋ธ๋ก ์ง์์ ์ฆ๋ฅํ๊ธฐ ์ํด, ํน์ ์์ (์ด ๊ฒฝ์ฐ ์ด๋ฏธ์ง ๋ถ๋ฅ)์ ๋ํด ํ์ต๋ ์ฌ์ ํ๋ จ๋ ๊ต์ฌ ๋ชจ๋ธ์ ์ฌ์ฉํ๊ณ , ๋๋ค์ผ๋ก ์ด๊ธฐํ๋ ํ์ ๋ชจ๋ธ์ ์ด๋ฏธ์ง ๋ถ๋ฅ ์์ ์ ๋ํด ํ์ตํฉ๋๋ค. ๊ทธ๋ค์, ํ์ ๋ชจ๋ธ์ด ๊ต์ฌ ๋ชจ๋ธ์ ์ถ๋ ฅ์ ๋ชจ๋ฐฉํ์ฌ ๋ ๋ชจ๋ธ์ ์ถ๋ ฅ ์ฐจ์ด๋ฅผ ์ต์ํํ๋๋ก ํ๋ จํฉ๋๋ค. ์ด ๊ธฐ๋ฒ์ Hinton ๋ฑ ์ฐ๊ตฌ์ง์ [Distilling the Knowledge in a Neural Network](https://arxiv.org/abs/1503.02531)์์ ์ฒ์ ์๊ฐ๋์์ต๋๋ค. ์ด ๊ฐ์ด๋์์๋ ํน์ ์์ ์ ๋ง์ถ ์ง์ ์ฆ๋ฅ๋ฅผ ์ํํ ๊ฒ์ ๋๋ค. ์ด๋ฒ์๋ [beans dataset](https://huggingface.co/datasets/beans)์ ์ฌ์ฉํ ๊ฒ์ ๋๋ค. | ||
|
||
์ด ๊ฐ์ด๋๋ [๋ฏธ์ธ ์กฐ์ ๋ ViT ๋ชจ๋ธ](https://huggingface.co/merve/vit-mobilenet-beans-224) (๊ต์ฌ ๋ชจ๋ธ)์ [MobileNet](https://huggingface.co/google/mobilenet_v2_1.4_224) (ํ์ ๋ชจ๋ธ)์ผ๋ก ์ฆ๋ฅํ๋ ๋ฐฉ๋ฒ์ ๐ค Transformers์ [Trainer API](https://huggingface.co/docs/transformers/en/main_classes/trainer#trainer) ๋ฅผ ์ฌ์ฉํ์ฌ ๋ณด์ฌ์ค๋๋ค. | ||
|
||
์ฆ๋ฅ์ ๊ณผ์ ํ๊ฐ๋ฅผ ์ํด ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํด ๋ด ์๋ค. | ||
|
||
|
||
```bash | ||
pip install transformers datasets accelerate tensorboard evaluate --upgrade | ||
``` | ||
|
||
์ด ์์ ์์๋ `merve/beans-vit-224` ๋ชจ๋ธ์ ๊ต์ฌ ๋ชจ๋ธ๋ก ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ์ด ๋ชจ๋ธ์ beans ๋ฐ์ดํฐ์ ์์ ํ์ธ ํ๋๋ `google/vit-base-patch16-224-in21k` ๊ธฐ๋ฐ์ ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชจ๋ธ์ ๋๋ค. ์ด ๋ชจ๋ธ์ ๋ฌด์์๋ก ์ด๊ธฐํ๋ MobileNetV2๋ก ์ฆ๋ฅํด๋ณผ ๊ฒ์ ๋๋ค. | ||
|
||
์ด์ ๋ฐ์ดํฐ์ ์ ๋ก๋ํ๊ฒ ์ต๋๋ค. | ||
|
||
```python | ||
from datasets import load_dataset | ||
|
||
dataset = load_dataset("beans") | ||
``` | ||
|
||
์ด ๊ฒฝ์ฐ ๋ ๋ชจ๋ธ์ ์ด๋ฏธ์ง ํ๋ก์ธ์๊ฐ ๋์ผํ ํด์๋๋ก ๋์ผํ ์ถ๋ ฅ์ ๋ฐํํ๊ธฐ ๋๋ฌธ์, ๋๊ฐ์ง๋ฅผ ๋ชจ๋ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ฐ์ดํฐ์ ์ ๋ชจ๋ ๋ถํ ๋ง๋ค ์ ์ฒ๋ฆฌ๋ฅผ ์ ์ฉํ๊ธฐ ์ํด `dataset`์ `map()` ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๊ฒ ์ ๋๋ค. | ||
|
||
|
||
```python | ||
from transformers import AutoImageProcessor | ||
teacher_processor = AutoImageProcessor.from_pretrained("merve/beans-vit-224") | ||
|
||
def process(examples): | ||
processed_inputs = teacher_processor(examples["image"]) | ||
return processed_inputs | ||
|
||
processed_datasets = dataset.map(process, batched=True) | ||
``` | ||
|
||
ํ์ ๋ชจ๋ธ(๋ฌด์์๋ก ์ด๊ธฐํ๋ MobileNet)์ด ๊ต์ฌ ๋ชจ๋ธ(ํ์ธ ํ๋๋ ๋น์ ํธ๋์คํฌ๋จธ)์ ๋ชจ๋ฐฉํ๋๋ก ํ ๊ฒ ์ ๋๋ค. ์ด๋ฅผ ์ํด ๋จผ์ ๊ต์ฌ์ ํ์ ๋ชจ๋ธ์ ๋ก์ง ์ถ๋ ฅ๊ฐ์ ๊ตฌํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ๊ฐ ์ถ๋ ฅ๊ฐ์ ๋งค๊ฐ๋ณ์ `temperature` ๊ฐ์ผ๋ก ๋๋๋๋ฐ, ์ด ๋งค๊ฐ๋ณ์๋ ๊ฐ ์ํํธ ํ๊ฒ์ ์ค์๋๋ฅผ ์กฐ์ ํ๋ ์ญํ ์ ํฉ๋๋ค. ๋งค๊ฐ๋ณ์ `lambda` ๋ ์ฆ๋ฅ ์์ค์ ์ค์๋์ ๊ฐ์ค์น๋ฅผ ์ค๋๋ค. ์ด ์์ ์์๋ `temperature=5`์ `lambda=0.5`๋ฅผ ์ฌ์ฉํ ๊ฒ์ ๋๋ค. ํ์๊ณผ ๊ต์ฌ ๊ฐ์ ๋ฐ์ฐ์ ๊ณ์ฐํ๊ธฐ ์ํด Kullback-Leibler Divergence ์์ค์ ์ฌ์ฉํฉ๋๋ค. ๋ ๋ฐ์ดํฐ P์ Q๊ฐ ์ฃผ์ด์ก์ ๋, KL Divergence๋ Q๋ฅผ ์ฌ์ฉํ์ฌ P๋ฅผ ํํํ๋ ๋ฐ ์ผ๋งํผ์ ์ถ๊ฐ ์ ๋ณด๊ฐ ํ์ํ์ง๋ฅผ ๋งํด์ค๋๋ค. ๋ ๋ฐ์ดํฐ๊ฐ ๋์ผํ๋ค๋ฉด, KL Divergence๋ 0์ด๋ฉฐ, Q๋ก P๋ฅผ ์ค๋ช ํ๋ ๋ฐ ์ถ๊ฐ ์ ๋ณด๊ฐ ํ์ํ์ง ์์์ ์๋ฏธํฉ๋๋ค. ๋ฐ๋ผ์ ์ง์ ์ฆ๋ฅ์ ๋งฅ๋ฝ์์ KL Divergence๋ ์ ์ฉํฉ๋๋ค. | ||
|
||
|
||
```python | ||
from transformers import TrainingArguments, Trainer | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
|
||
|
||
class ImageDistilTrainer(Trainer): | ||
def __init__(self, teacher_model=None, student_model=None, temperature=None, lambda_param=None, *args, **kwargs): | ||
super().__init__(model=student_model, *args, **kwargs) | ||
self.teacher = teacher_model | ||
self.student = student_model | ||
self.loss_function = nn.KLDivLoss(reduction="batchmean") | ||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | ||
self.teacher.to(device) | ||
self.teacher.eval() | ||
self.temperature = temperature | ||
self.lambda_param = lambda_param | ||
|
||
def compute_loss(self, student, inputs, return_outputs=False): | ||
student_output = self.student(**inputs) | ||
|
||
with torch.no_grad(): | ||
teacher_output = self.teacher(**inputs) | ||
|
||
# ๊ต์ฌ์ ํ์์ ์ํํธ ํ๊ฒ(soft targets) ๊ณ์ฐ | ||
|
||
soft_teacher = F.softmax(teacher_output.logits / self.temperature, dim=-1) | ||
soft_student = F.log_softmax(student_output.logits / self.temperature, dim=-1) | ||
|
||
# ์์ค(loss) ๊ณ์ฐ | ||
distillation_loss = self.loss_function(soft_student, soft_teacher) * (self.temperature ** 2) | ||
|
||
# ์ค์ ๋ ์ด๋ธ ์์ค ๊ณ์ฐ | ||
student_target_loss = student_output.loss | ||
|
||
# ์ต์ข ์์ค ๊ณ์ฐ | ||
loss = (1. - self.lambda_param) * student_target_loss + self.lambda_param * distillation_loss | ||
return (loss, student_output) if return_outputs else loss | ||
``` | ||
|
||
์ด์ Hugging Face Hub์ ๋ก๊ทธ์ธํ์ฌ `Trainer`๋ฅผ ํตํด Hugging Face Hub์ ๋ชจ๋ธ์ ํธ์ํ ์ ์๋๋ก ํ๊ฒ ์ต๋๋ค. | ||
|
||
|
||
```python | ||
from huggingface_hub import notebook_login | ||
|
||
notebook_login() | ||
``` | ||
|
||
์ด์ `TrainingArguments`, ๊ต์ฌ ๋ชจ๋ธ๊ณผ ํ์ ๋ชจ๋ธ์ ์ค์ ํ๊ฒ ์ต๋๋ค. | ||
|
||
|
||
```python | ||
from transformers import AutoModelForImageClassification, MobileNetV2Config, MobileNetV2ForImageClassification | ||
|
||
training_args = TrainingArguments( | ||
output_dir="my-awesome-model", | ||
num_train_epochs=30, | ||
fp16=True, | ||
logging_dir=f"{repo_name}/logs", | ||
logging_strategy="epoch", | ||
eval_strategy="epoch", | ||
save_strategy="epoch", | ||
load_best_model_at_end=True, | ||
metric_for_best_model="accuracy", | ||
report_to="tensorboard", | ||
push_to_hub=True, | ||
hub_strategy="every_save", | ||
hub_model_id=repo_name, | ||
) | ||
|
||
num_labels = len(processed_datasets["train"].features["labels"].names) | ||
|
||
# ๋ชจ๋ธ ์ด๊ธฐํ | ||
teacher_model = AutoModelForImageClassification.from_pretrained( | ||
"merve/beans-vit-224", | ||
num_labels=num_labels, | ||
ignore_mismatched_sizes=True | ||
) | ||
|
||
# MobileNetV2 ๋ฐ๋ฐ๋ฅ๋ถํฐ ํ์ต | ||
student_config = MobileNetV2Config() | ||
student_config.num_labels = num_labels | ||
student_model = MobileNetV2ForImageClassification(student_config) | ||
``` | ||
|
||
`compute_metrics` ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ ์คํธ ์ธํธ์์ ๋ชจ๋ธ์ ํ๊ฐํ ์ ์์ต๋๋ค. ์ด ํจ์๋ ํ๋ จ ๊ณผ์ ์์ ๋ชจ๋ธ์ `accuracy`์ `f1`์ ๊ณ์ฐํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. | ||
|
||
|
||
```python | ||
import evaluate | ||
import numpy as np | ||
|
||
accuracy = evaluate.load("accuracy") | ||
|
||
def compute_metrics(eval_pred): | ||
predictions, labels = eval_pred | ||
acc = accuracy.compute(references=labels, predictions=np.argmax(predictions, axis=1)) | ||
return {"accuracy": acc["accuracy"]} | ||
``` | ||
|
||
์ ์ํ ํ๋ จ ์ธ์๋ก `Trainer`๋ฅผ ์ด๊ธฐํํด๋ด ์๋ค. ๋ํ ๋ฐ์ดํฐ ์ฝ๋ ์ดํฐ(data collator)๋ฅผ ์ด๊ธฐํํ๊ฒ ์ต๋๋ค. | ||
|
||
```python | ||
from transformers import DefaultDataCollator | ||
|
||
data_collator = DefaultDataCollator() | ||
trainer = ImageDistilTrainer( | ||
student_model=student_model, | ||
teacher_model=teacher_model, | ||
training_args=training_args, | ||
train_dataset=processed_datasets["train"], | ||
eval_dataset=processed_datasets["validation"], | ||
data_collator=data_collator, | ||
tokenizer=teacher_processor, | ||
compute_metrics=compute_metrics, | ||
temperature=5, | ||
lambda_param=0.5 | ||
) | ||
``` | ||
|
||
์ด์ ๋ชจ๋ธ์ ํ๋ จํ ์ ์์ต๋๋ค. | ||
|
||
```python | ||
trainer.train() | ||
``` | ||
|
||
๋ชจ๋ธ์ ํ ์คํธ ์ธํธ์์ ํ๊ฐํ ์ ์์ต๋๋ค. | ||
|
||
```python | ||
trainer.evaluate(processed_datasets["test"]) | ||
``` | ||
|
||
|
||
ํ ์คํธ ์ธํธ์์ ๋ชจ๋ธ์ ์ ํ๋๋ 72%์ ๋๋ฌํ์ต๋๋ค. ์ฆ๋ฅ์ ํจ์จ์ฑ์ ๊ฒ์ฆํ๊ธฐ ์ํด ๋์ผํ ํ์ดํผํ๋ผ๋ฏธํฐ๋ก beans ๋ฐ์ดํฐ์ ์์ MobileNet์ ์ฒ์๋ถํฐ ํ๋ จํ์๊ณ , ํ ์คํธ ์ธํธ์์์ ์ ํ๋๋ 63% ์์ต๋๋ค. ๋ค์ํ ์ฌ์ ํ๋ จ๋ ๊ต์ฌ ๋ชจ๋ธ, ํ์ ๊ตฌ์กฐ, ์ฆ๋ฅ ๋งค๊ฐ๋ณ์๋ฅผ ์๋ํด๋ณด์๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ํ๊ธฐ๋ฅผ ๊ถ์ฅํฉ๋๋ค. ์ฆ๋ฅ๋ ๋ชจ๋ธ์ ํ๋ จ ๋ก๊ทธ์ ์ฒดํฌํฌ์ธํธ๋ [์ด ์ ์ฅ์](https://huggingface.co/merve/vit-mobilenet-beans-224)์์ ์ฐพ์ ์ ์์ผ๋ฉฐ, ์ฒ์๋ถํฐ ํ๋ จ๋ MobileNetV2๋ ์ด [์ ์ฅ์](https://huggingface.co/merve/resnet-mobilenet-beans-5)์์ ์ฐพ์ ์ ์์ต๋๋ค. |