MEAL_V2


我們需要一個額外的 Python 依賴項
!pip install timm
import torch
# list of models: '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'
# load pretrained models, using "mealv2_resnest50_cutmix" as an example
model = torch.hub.load('szq0214/MEAL-V2','meal_v2', 'mealv2_resnest50_cutmix', pretrained=True)
model.eval()
所有預訓練模型都要求輸入影像以相同的方式進行歸一化,即由形狀為 (3 x H x W) 的 3 通道 RGB 影像組成的小批次資料,其中 H 和 W 預計至少為 224。影像必須載入到 [0, 1] 範圍內,然後使用 mean = [0.485, 0.456, 0.406] 和 std = [0.229, 0.224, 0.225] 進行歸一化。
這是一個示例執行。
# Download an example image from the pytorch website
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)
# sample execution (requires 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) # create a mini-batch as expected by the model
# move the input and model to GPU for speed if available
if torch.cuda.is_available():
input_batch = input_batch.to('cuda')
model.to('cuda')
with torch.no_grad():
output = model(input_batch)
# Tensor of shape 1000, with confidence scores over ImageNet's 1000 classes
print(output[0])
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
probabilities = torch.nn.functional.softmax(output[0], dim=0)
print(probabilities)
# Download ImageNet labels
!wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt
# Read the categories
with open("imagenet_classes.txt", "r") as f:
categories = [s.strip() for s in f.readlines()]
# Show top categories per image
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: 無需技巧即可將 ImageNet 上的普通 ResNet-50 提升至 80% 以上 Top-1 準確率論文。
在本文中,我們介紹了一種簡單而有效的方法,可以將普通 ResNet-50 在 ImageNet 上的 Top-1 準確率提升到 80% 以上,而無需任何技巧。通常,我們的方法基於最近提出的MEAL,即透過判別器進行整合知識蒸餾。我們透過以下方式進一步簡化它:1) 僅在最終輸出上採用相似性損失和判別器;2) 使用所有教師整合模型的 softmax 機率平均值作為蒸餾的更強監督。我們方法的一個關鍵觀點是,在蒸餾過程中不應使用 one-hot/硬標籤。我們表明,這樣一個簡單的框架可以實現最先進的結果,而無需涉及任何常用技巧,例如:1) 架構修改;2) ImageNet 以外的外部訓練資料;3) autoaug/randaug;4) 餘弦學習率;5) mixup/cutmix 訓練;6) 標籤平滑;等。
| 模型 | 解析度 | #引數 | 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 |
參考文獻
@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}
}