Skip to content

Latest commit

ย 

History

History
122 lines (103 loc) ยท 5.36 KB

pytorch_vision_meal_v2.md

File metadata and controls

122 lines (103 loc) ยท 5.36 KB
layout background-class body-class title summary category image author tags github-link github-id featured_image_1 featured_image_2 accelerator order demo-model-link
hub_detail
hub-background
hub
MEAL_V2
Boosting Tiny and Efficient Models using Knowledge Distillation.
researchers
MEALV2.png
Carnegie Mellon University
vision
szq0214/MEAL-V2
MEALV2_method.png
MEALV2_results.png
cuda
10

timm ์ข…์† ํŒจํ‚ค์ง€ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

!pip install timm
import torch
# ๋ชจ๋ธ ์ข…๋ฅ˜: 'mealv1_resnest50', 'mealv2_resnest50', 'mealv2_resnest50_cutmix', 'mealv2_resnest50_380x380', 'mealv2_mobilenetv3_small_075', 'mealv2_mobilenetv3_small_100', 'mealv2_mobilenet_v3_large_100', 'mealv2_efficientnet_b0'
# ์‚ฌ์ „์— ํ•™์Šต๋œ "mealv2_resnest50_cutmix"์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.
model = torch.hub.load('szq0214/MEAL-V2','meal_v2', 'mealv2_resnest50_cutmix', pretrained=True)
model.eval()

์‚ฌ์ „์— ํ•™์Šต๋œ ๋ชจ๋“  ๋ชจ๋ธ์€ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ •๊ทœํ™”๋œ ์ž…๋ ฅ ์ด๋ฏธ์ง€, ์ฆ‰, H ์™€ W ๋Š” ์ตœ์†Œ 224 ์ด์ƒ์ธ (3 x H x W) ํ˜•ํƒœ์˜ 3-์ฑ„๋„ RGB ์ด๋ฏธ์ง€์˜ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ฅผ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ [0, 1] ๋ฒ”์œ„์—์„œ ๋ถˆ๋Ÿฌ์˜จ ๋‹ค์Œ mean = [0.485, 0.456, 0.406] ๊ณผ std = [0.229, 0.224, 0.225] ๋ฅผ ํ†ตํ•ด ์ •๊ทœํ™”ํ•ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

# ํŒŒ์ดํ† ์น˜ ์›น์‚ฌ์ดํŠธ์—์„œ ์˜ˆ์ œ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ
import urllib
url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# ์‹คํ–‰ ์˜ˆ์‹œ (torchvision ํ•„์š”)
from PIL import Image
from torchvision import transforms
input_image = Image.open(filename)
preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # ๋ชจ๋ธ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฏธ๋‹ˆ๋ฐฐ์น˜ ์ƒ์„ฑ

# ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์†๋„๋ฅผ ์œ„ํ•ด ์ž…๋ ฅ๊ณผ ๋ชจ๋ธ์„ GPU๋กœ ์˜ฎ๊น๋‹ˆ๋‹ค.
if torch.cuda.is_available():
    input_batch = input_batch.to('cuda')
    model.to('cuda')

with torch.no_grad():
    output = model(input_batch)
# 1000๊ฐœ์˜ ImageNet ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์‹ ๋ขฐ๋„ ์ ์ˆ˜(confidence score)๋ฅผ ๊ฐ€์ง„ 1000 ํฌ๊ธฐ์˜ Tensor
print(output[0])
# output์—” ์ •๊ทœํ™”๋˜์ง€ ์•Š์€ ์‹ ๋ขฐ๋„ ์ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™•๋ฅ ๊ฐ’์„ ์–ป์œผ๋ ค๋ฉด softmax๋ฅผ ์‹คํ–‰ํ•˜์„ธ์š”.
probabilities = torch.nn.functional.softmax(output[0], dim=0)
print(probabilities)
# ImageNet ๋ ˆ์ด๋ธ” ๋‹ค์šด๋กœ๋“œ
!wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt
# ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ๊ธฐ
with open("imagenet_classes.txt", "r") as f:
    categories = [s.strip() for s in f.readlines()]
# ์ด๋ฏธ์ง€๋ณ„ Top5 ์นดํ…Œ๊ณ ๋ฆฌ ์กฐํšŒ
top5_prob, top5_catid = torch.topk(probabilities, 5)
for i in range(top5_prob.size(0)):
    print(categories[top5_catid[i]], top5_prob[i].item())

๋ชจ๋ธ ์„ค๋ช…

MEAL V2 ๋ชจ๋ธ๋“ค์€ MEAL V2: Boosting Vanilla ResNet-50 to 80%+ Top-1 Accuracy on ImageNet without Tricks ๋…ผ๋ฌธ์— ๊ธฐ๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค.

MEAL V2์˜ ์ฃผ์š” ๊ด€์ ์€ distillation ๊ณผ์ •์— One-Hot ๋ ˆ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. MEAL V2๋Š” ํŒ๋ณ„์ž๋ฅผ ์ด์šฉํ•œ knowledge distillation ์•™์ƒ๋ธ” ๊ธฐ๋ฒ•์ธ MEAL์— ๊ธฐ์ดˆํ•˜๋ฉฐ, MEAL์„ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์˜ ์ˆ˜์ •์„ ๊ฑฐ์ณค์Šต๋‹ˆ๋‹ค. 1) ํŒ๋ณ„์ž ์ž…๋ ฅ, ์œ ์‚ฌ๋„ ์†์‹ค ๊ณ„์‚ฐ์— ์ตœ์ข… ์ถœ๋ ฅ๋งŒ์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. 2) ๋ชจ๋“  teacher๋“ค์˜ ์˜ˆ์ธก ํ™•๋ฅ ์„ ํ‰๊ท  ๋‚ด์–ด distillation์— ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด MEAL V2๋Š” ์–ด๋– ํ•œ ํŠธ๋ฆญ ์‚ฌ์šฉ ์—†์ด๋„ ResNet-50์˜ ImageNet Top-1 ์ •ํ™•๋„๋ฅผ 80% ์ด์ƒ ๊ธฐ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (ํŠธ๋ฆญ : 1) ๋ชจ๋ธ ๊ตฌ์กฐ ๋ณ€๊ฒฝ; 2) ImageNet ์™ธ ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ ํ™œ์šฉ; 3) autoaug/randaug; 4) cosine learning rate; 5) mixup/cutmix; 6) label smoothing; etc)

| Models | Resolution| #Parameters | Top-1/Top-5 | | :---: | :-: | :-: | :------:| :------: | | MEAL-V1 w/ ResNet50 | 224 | 25.6M |78.21/94.01 | GitHub | | MEAL-V2 w/ ResNet50 | 224 | 25.6M | 80.67/95.09 | | MEAL-V2 w/ ResNet50| 380 | 25.6M | 81.72/95.81 | | MEAL-V2 + CutMix w/ ResNet50| 224 | 25.6M | 80.98/95.35 | | MEAL-V2 w/ MobileNet V3-Small 0.75| 224 | 2.04M | 67.60/87.23 | | MEAL-V2 w/ MobileNet V3-Small 1.0| 224 | 2.54M | 69.65/88.71 | | MEAL-V2 w/ MobileNet V3-Large 1.0 | 224 | 5.48M | 76.92/93.32 | | MEAL-V2 w/ EfficientNet-B0| 224 | 5.29M | 78.29/93.95 |

์ฐธ์กฐ

์ž์„ธํ•œ ์‚ฌํ•ญ์€ MEAL V2, MEAL์„ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@article{shen2020mealv2,
    title={MEAL V2: Boosting Vanilla ResNet-50 to 80%+ Top-1 Accuracy on ImageNet without Tricks},
    author={Shen, Zhiqiang and Savvides, Marios},
    journal={arXiv preprint arXiv:2009.08453},
    year={2020}
}

@inproceedings{shen2019MEAL,
	title = {MEAL: Multi-Model Ensemble via Adversarial Learning},
	author = {Shen, Zhiqiang and He, Zhankui and Xue, Xiangyang},
	booktitle = {AAAI},
	year = {2019}
}