Skip to content

Latest commit

ย 

History

History
97 lines (79 loc) ยท 3.78 KB

pytorch_vision_alexnet.md

File metadata and controls

97 lines (79 loc) ยท 3.78 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
AlexNet
The 2012 ImageNet winner achieved a top-5 error of 15.3%, more than 10.8 percentage points lower than that of the runner up.
researchers
alexnet2.png
Pytorch Team
vision
scriptable
pytorch/vision
alexnet1.png
alexnet2.png
cuda-optional
10
import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'alexnet', pretrained=True)
model.eval()

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

์ด๋ฏธ์ง€๋Š” [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 ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์‹œ ๋ชจ๋ธ๊ณผ ์ž…๋ ฅ๊ฐ’์„ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •
if torch.cuda.is_available():
    input_batch = input_batch.to('cuda')
    model.to('cuda')

with torch.no_grad():
    output = model(input_batch)
# Imagenet 1000๊ฐœ ํด๋ž˜์Šค์˜ ์‹ ๋ขฐ ์ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ…์„œ

print(output[0])

# ๊ฒฐ๊ณผ๋Š” ๋น„์ •๊ทœํ™”๋œ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค. 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())

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

AlexNet์€ 2012๋…„๋„ ImageNet Large Scale Visual Recognition Challenge (ILSVRC)์— ์ฐธ์—ฌํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋„คํŠธ์›Œํฌ๋Š” 15.3%์˜ top-5 ์—๋Ÿฌ์œจ์„ ๋‹ฌ์„ฑํ–ˆ๊ณ , ์ด๋Š” 2์œ„๋ณด๋‹ค 10.8%P ๋‚ฎ์€ ์ˆ˜์น˜์ž…๋‹ˆ๋‹ค. ์› ๋…ผ๋ฌธ์˜ ์ฃผ์š” ๊ฒฐ๋ก ์€ ๋†’์€ ์„ฑ๋Šฅ์„ ์œ„ํ•ด ๋ชจ๋ธ์˜ ๊นŠ์ด๊ฐ€ ํ•„์ˆ˜์ ์ด๋ผ๋Š” ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ณ„์‚ฐ ๋น„์šฉ์ด ๋งŽ์ด ๋“ค์ง€๋งŒ, ํ•™์Šต ๊ณผ์ •์—์„œ GPU์˜ ์‚ฌ์šฉ์œผ๋กœ ๊ฐ€๋Šฅํ•ด์กŒ์Šต๋‹ˆ๋‹ค.

์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์ด ์žˆ๋Š” ImageNet ๋ฐ์ดํ„ฐ์…‹์˜ 1-crop ์—๋Ÿฌ์œจ์€ ๋‹ค์Œ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ ๊ตฌ์กฐ Top-1 ์—๋Ÿฌ Top-5 ์—๋Ÿฌ
alexnet 43.45 20.91

์ฐธ๊ณ ๋ฌธํ—Œ

  1. One weird trick for parallelizing convolutional neural networks.