Skip to content

Latest commit

ย 

History

History
100 lines (85 loc) ยท 3.77 KB

pytorch_vision_resnext.md

File metadata and controls

100 lines (85 loc) ยท 3.77 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
ResNext
Next generation ResNets, more efficient and accurate
researchers
resnext.png
Pytorch Team
vision
scriptable
pytorch/vision
resnext.png
no-image
cuda-optional
10
import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'resnext50_32x4d', pretrained=True)
# or
# model = torch.hub.load('pytorch/vision:v0.10.0', 'resnext101_32x8d', 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())

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

Resnext ๋ชจ๋ธ์€ ๋…ผ๋ฌธ [Aggregated Residual Transformations for Deep Neural Networks]์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. (https://arxiv.org/abs/1611.05431). ์—ฌ๊ธฐ์„œ๋Š” 50๊ฐœ์˜ ๊ณ„์ธต๊ณผ 101๊ฐœ์˜ ๊ณ„์ธต์„ ๊ฐ€์ง€๋Š” 2๊ฐœ์˜ resnet ๋ชจ๋ธ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. resnet50๊ณผ resnext50์˜ ์•„ํ‚คํ…์ฒ˜ ์ฐจ์ด๋Š” ๋…ผ๋ฌธ์˜ Table 1์„ ์ฐธ๊ณ ํ•˜์‹ญ์‹œ์˜ค. ImageNet ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์˜ ์—๋Ÿฌ(์„ฑ๋Šฅ)์€ ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ ๊ตฌ์กฐ Top-1 ์˜ค๋ฅ˜ Top-5 ์˜ค๋ฅ˜
resnext50_32x4d 22.38 6.30
resnext101_32x8d 20.69 5.47

์ฐธ๊ณ ๋ฌธํ—Œ