Skip to content

Latest commit

ย 

History

History
93 lines (80 loc) ยท 4.28 KB

pytorch_vision_inception_v3.md

File metadata and controls

93 lines (80 loc) ยท 4.28 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
Inception_v3
Also called GoogleNetv3, a famous ConvNet trained on Imagenet from 2015
researchers
inception_v3.png
Pytorch Team
vision
scriptable
pytorch/vision
inception_v3.png
no-image
cuda-optional
10
import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'inception_v3', 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(299),
    transforms.CenterCrop(299),
    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())

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

Inception v3๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ ์ ˆํžˆ ๋ถ„ํ•ดํ•˜๊ณ  ์ ๊ทน์ ์ธ ์ •๊ทœํ™”๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€๋œ ๊ณ„์‚ฐ์„ ๊ฐ€๋Šฅํ•œ ํ•œ ํšจ์œจ์ ์œผ๋กœ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ๋„คํŠธ์›Œํฌ๋ฅผ ํ™•์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ํƒ์ƒ‰์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ILSVRC 2012 (ImageNet) ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ ๋ณธ ๋…ผ๋ฌธ์€ ๋‹น์‹œ ๊ธฐ์ค€์˜ SOTA(state of the art) ๋ชจ๋ธ๋ณด๋‹ค ์ƒ๋‹นํ•œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์–ป์—ˆ๊ณ , ๋‹จ์ผ ํ”„๋ ˆ์ž„ ํ‰๊ฐ€์—์„œ 21.2%์˜ top-1 ์˜ค๋ฅ˜์™€ 5.6%์˜ top-5 ์˜ค๋ฅ˜๋ฅผ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฐ๊ณผ๋Š” 2500๋งŒ๊ฐœ ์ดํ•˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋‹จ์ผ ์ถ”๋ก  ๋‹น 50์–ต๋ฒˆ์˜ ๊ณฑ์…ˆ-๋ง์…ˆ ์—ฐ์‚ฐ์˜ ๊ณ„์‚ฐ ๋น„์šฉ์œผ๋กœ ๋‹ฌ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ 4๊ฐœ ๋ชจ๋ธ์˜ ์•™์ƒ๋ธ”(ensemble)๊ณผ ๋‹ค์ค‘-ํฌ๋กญ ํ‰๊ฐ€(multi-crop evaluation)์„ ์ด์šฉํ•˜์—ฌ, 17.3%์˜ top-1 ์˜ค๋ฅ˜์™€ 3.6%์˜ top-5 ์˜ค๋ฅ˜๋ฅผ ํ‰๊ฐ€(validation) ๋ฐ์ดํ„ฐ์…‹์—์„œ ๋‹ฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋กœ ImageNet ๋ฐ์ดํ„ฐ์…‹์—์„œ์˜ ๋‹จ์ผ-ํฌ๋กญ ๋ฐฉ์‹์œผ๋กœ ์˜ค๋ฅ˜ ๋น„์œจ์„ ์ธก์ •ํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ ๊ตฌ์กฐ Top-1 ์˜ค๋ฅ˜ Top-5 ์˜ค๋ฅ˜
inception_v3 22.55 6.44

์ฐธ๊ณ ๋ฌธํ—Œ