Skip to content

Latest commit

ย 

History

History
94 lines (79 loc) ยท 3.66 KB

pytorch_vision_mobilenet_v2.md

File metadata and controls

94 lines (79 loc) ยท 3.66 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
MobileNet v2
์ž”์ฐจ ๋ธ”๋ก์— ๊ธฐ๋ฐ˜ํ•œ ์†๋„์™€ ๋ฉ”๋ชจ๋ฆฌ์— ์ตœ์ ํ™”๋œ ํšจ์œจ์ ์ธ ๋„คํŠธ์›Œํฌ
researchers
mobilenet_v2_1.png
Pytorch Team
vision
scriptable
pytorch/vision
mobilenet_v2_1.png
mobilenet_v2_2.png
cuda-optional
10
import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'mobilenet_v2', pretrained=True)
model.eval()

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

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

# pytorch ์›น์‚ฌ์ดํŠธ์—์„œ ์˜ˆ์ œ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ
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)
# output์€ 1000๊ฐœ์˜ Tensor ํ˜•ํƒœ์ด๋ฉฐ, ์ด๋Š” Imagenet ๋ฐ์ดํ„ฐ ์…‹์˜ 1000๊ฐœ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์‹ ๋ขฐ๋„ ์ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒฐ๊ณผ
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_prob, top5_catid = torch.topk(probabilities, 5)
for i in range(top5_prob.size(0)):
    print(categories[top5_catid[i]], top5_prob[i].item())

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

MobileNet v2 ๊ตฌ์กฐ๋Š” ์ž”์ฐจ ๋ธ”๋ก์˜ ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ์ด ์–‡์€ ๋ณ‘๋ชฉ ๊ณ„์ธต ํ˜•ํƒœ์ธ ๋ฐ˜์ „๋œ ์ž”์ฐจ ๊ตฌ์กฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜์ „๋œ ์ž”์ฐจ ๊ตฌ์กฐ๋Š” ์ž…๋ ฅ๋‹จ์—์„œ ํ™•์žฅ๋œ ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์กด์˜ ์ž”์ฐจ ๋ชจ๋ธ๊ณผ ๋ฐ˜๋Œ€๋˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. MobileNet v2๋Š” ๊ฒฝ๋Ÿ‰ํ™”๋œ depthwise ํ•ฉ์„ฑ๊ณฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๊ฐ„ ํ™•์žฅ ๊ณ„์ธต์˜ ํŠน์ง•๋“ค์„ ํ•„ํ„ฐ๋งํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ํ‘œํ˜„๋ ฅ ์œ ์ง€๋ฅผ ์œ„ํ•ด ์ข์€ ๊ณ„์ธต์˜ ๋น„์„ ํ˜•์„ฑ์€ ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ ๊ตฌ์กฐ Top-1 ์˜ค๋ฅ˜ Top-5 ์˜ค๋ฅ˜
mobilenet_v2 28.12 9.71

์ฐธ๊ณ ๋ฌธํ—Œ