| layout | hub_detail | ||
|---|---|---|---|
| background-class | hub-background | ||
| body-class | hub | ||
| title | ShuffleNet v2 | ||
| summary | An efficient ConvNet optimized for speed and memory, pre-trained on Imagenet | ||
| category | researchers | ||
| image | shufflenet_v2_1.png | ||
| author | Pytorch Team | ||
| tags |
|
||
| github-link | https://github.com/pytorch/vision/blob/main/torchvision/models/shufflenetv2.py | ||
| github-id | pytorch/vision | ||
| featured_image_1 | shufflenet_v2_1.png | ||
| featured_image_2 | shufflenet_v2_2.png | ||
| accelerator | cuda-optional | ||
| order | 10 | ||
| demo-model-link | https://huggingface.co/spaces/pytorch/ShuffleNet_v2 |
import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'shufflenet_v2_x1_0', 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 ์ฌ์ฉ์ด ๊ฐ๋ฅํ ๊ฒฝ์ฐ ์๋๋ฅผ ์ํด ์
๋ ฅ๊ณผ ๋ชจ๋ธ์ GPU๋ก ์ด๋
if torch.cuda.is_available():
input_batch = input_batch.to('cuda')
model.to('cuda')
with torch.no_grad():
output = model(input_batch)
# Imagnet์ 1000๊ฐ ํด๋์ค์ ๋ํ ์ ๋ขฐ๋ ์ ์๋ฅผ ๊ฐ์ง 1000 ํํ์ ํ
์ ์ถ๋ ฅ
print(output[0])
# ์ถ๋ ฅ์ ์ ๊ทํ๋์ด์์ง ์์ต๋๋ค. ์ํํธ๋งฅ์ค๋ฅผ ์คํํ์ฌ ํ๋ฅ ์ ์ป์ ์ ์์ต๋๋ค.
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()]
# ์ด๋ฏธ์ง๋ง๋ค ์์ ์นดํ
๊ณ ๋ฆฌ 5๊ฐ ๋ณด์ฌ์ฃผ๊ธฐ
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())
์ด์ ์๋ ์ ๊ฒฝ๋ง ์ํคํ ์ฒ ์ค๊ณ๋ ์ฃผ๋ก FLOP์ ๊ฐ์ ๊ณ์ฐ ๋ณต์ก์ฑ์ ๊ฐ์ ์ธก์ ๊ธฐ์ค์ ๋ฐ๋ผ ์งํ๋์์ต๋๋ค. ๊ทธ๋ฌ๋ ์๋์ ๊ฐ์ ์ง์ ์ ์ธ ์ธก์ ์ ๋ฉ๋ชจ๋ฆฌ ์ก์ธ์ค ๋น์ฉ ๋ฐ ํ๋ซํผ ํน์ฑ๊ณผ ๊ฐ์ ๋ค๋ฅธ ์์์๋ ์์กดํฉ๋๋ค. ์ผ๋ จ์ ํต์ ๋ ์คํ์ ๊ธฐ๋ฐ์ผ๋ก, ์ด ์์ ์ ํจ์จ์ ์ธ ๋คํธ์ํฌ ์ค๊ณ๋ฅผ ์ํ ๋ช ๊ฐ์ง ์ค์ฉ์ ์ธ ์ง์นจ์ ๋์ถํฉ๋๋ค. ๋ฐ๋ผ์ ShuffleNet V2๋ผ๋ ์๋ก์ด ์ํคํ ์ฒ๊ฐ ์ ์๋ฉ๋๋ค. ์กฐ๊ฑด ๋ณํ์ ๋ฐ๋ฅธ ๋ชจ๋ธ ํ๊ฐ๋ฅผ ํตํด ์๋์ ์ ํ๋ ํธ๋ ์ด๋์คํ ์ธก๋ฉด์์ ์ต๊ณ ์์ค์์ ํ์ธํ์ต๋๋ค.
| Model structure | Top-1 error | Top-5 error |
|---|---|---|
| shufflenet_v2 | 30.64 | 11.68 |