Skip to content

Latest commit

ย 

History

History
95 lines (79 loc) ยท 3.71 KB

pytorch_vision_googlenet.md

File metadata and controls

95 lines (79 loc) ยท 3.71 KB
layout background-class body-class title summary category image author tags github-link github-id featured_image_1 featured_image_2 accelerator demo-model-link order
hub_detail
hub-background
hub
GoogLeNet
GoogLeNet was based on a deep convolutional neural network architecture codenamed "Inception" which won ImageNet 2014.
researchers
googlenet1.png
Pytorch Team
vision
pytorch/vision
googlenet1.png
googlenet2.png
cuda-optional
10
import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'googlenet', pretrained=True)
model.eval()

์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋“ค์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ •๊ทœํ™”๋œ ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฏธ๋‹ˆ ๋ฐฐ์น˜(mini-batch)์˜ 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]์„ ์ด์šฉํ•ด ์ •๊ทœํ™”๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์‹คํ–‰ ์˜ˆ์ œ ์ž…๋‹ˆ๋‹ค.

# ํŒŒ์ดํ† ์น˜ ์›น ์‚ฌ์ดํŠธ์—์„œ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ
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)
# output์€ shape๊ฐ€ [1000]์ธ Tensor ์ž๋ฃŒํ˜•์ด๋ฉฐ, ์ด๋Š” ImageNet ๋ฐ์ดํ„ฐ์…‹์˜ 1000๊ฐœ์˜ ๊ฐ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋ชจ๋ธ์˜ ํ™•์‹ ๋„(confidence)๋ฅผ ๋‚˜ํƒ€๋ƒ„
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()]
# ๊ฐ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ top 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())

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

GoogLeNet์€ ์ฝ”๋“œ๋„ค์ž„ "Inception"์œผ๋กœ ๋ถˆ๋ฆฌ๋Š” ์‹ ๊ฒฝ๋ง ์•„ํ‚คํ…์ฒ˜์— ๊ธฐ๋ฐ˜ํ•œ ๊นŠ์€ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ImageNet Large-Scale Visual Recognition Challenge 2014 (ILSVRC 2014) ์—์„œ ์ƒˆ๋กœ์šด SOTA(state of the art)๋ฅผ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋กœ ImageNet ๋ฐ์ดํ„ฐ์…‹์—์„œ์˜ ๋‹จ์ผ-ํฌ๋กญ ๋ฐฉ์‹์œผ๋กœ ์˜ค๋ฅ˜ ๋น„์œจ์„ ์ธก์ •ํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ ๊ตฌ์กฐ Top-1 ์˜ค๋ฅ˜ Top-5 ์˜ค๋ฅ˜
googlenet 30.22 10.47

์ฐธ๊ณ ๋ฌธํ—Œ