Skip to content

Latest commit

ย 

History

History
94 lines (79 loc) ยท 3.78 KB

File metadata and controls

94 lines (79 loc) ยท 3.78 KB
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
vision
scriptable
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

์ฐธ๊ณ ๋ฌธํ—Œ