Skip to content

Latest commit

ย 

History

History
101 lines (86 loc) ยท 4.08 KB

pytorch_vision_resnest.md

File metadata and controls

101 lines (86 loc) ยท 4.08 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
ResNeSt
A new ResNet variant.
researchers
resnest.jpg
Hang Zhang
vision
zhanghang1989/ResNeSt
resnest.jpg
no-image
cuda-optional
10
import torch
# ๋ชจ๋ธ ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
torch.hub.list('zhanghang1989/ResNeSt', force_reload=True)
# ์˜ˆ์‹œ๋กœ ResNeSt-50์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค๊ธฐ
model = torch.hub.load('zhanghang1989/ResNeSt', 'resnest50', pretrained=True)
model.eval()

๋ชจ๋“  ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์€ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ •๊ทœํ™”๋œ ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, H์™€ W๊ฐ€ ์ตœ์†Œ 224์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” (3 x H x W)ํ˜•ํƒœ์˜ 3์ฑ„๋„ RGB ์ด๋ฏธ์ง€์˜ ๋ฏธ๋‹ˆ๋ฐฐ์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ [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) # create a mini-batch as expected by the model

# 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๊ฐœ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์‹ ๋ขฐ๋„ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ 1000 ํ˜•ํƒœ์˜ Tensor ์ถœ๋ ฅ
print(output[0])
# ์ถœ๋ ฅ์€ ์ •๊ทœํ™”๋˜์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํ™•๋ฅ ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
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()]
# ์ด๋ฏธ์ง€๋งˆ๋‹ค ์ƒ์œ„ ์นดํ…Œ๊ณ ๋ฆฌ 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())

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

ResNeSt ๋ชจ๋ธ์€ ResNeSt: Split-Attention Networks ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ตœ๊ทผ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์ด ๊ณ„์† ๋ฐœ์ „ํ•˜๊ณ  ์žˆ์ง€๋งŒ ๊ฐ์ฒด ๊ฐ์ง€ ๋ฐ ์˜๋ฏธ ๋ถ„ํ• ๊ณผ ๊ฐ™์€ ๋Œ€๋ถ€๋ถ„์˜ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(downstream applications)์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ชจ๋“ˆํ™”๋œ ๊ตฌ์กฐ๋กœ ์ธํ•ด ์—ฌ์ „ํžˆ ResNet ๋ณ€ํ˜•์„ ๋ฐฑ๋ณธ ๋„คํŠธ์›Œํฌ(backbone network)๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋Šฅ ๋งต ๊ทธ๋ฃน ์ „๋ฐ˜์— ๊ฑธ์ณ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ผ ์ˆ˜ ์žˆ๋Š” Split-Attention ๋ธ”๋ก์„ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ Split-Attention ๋ธ”๋ก์„ ResNet ์Šคํƒ€์ผ๋กœ ์Œ“์•„์„œ ResNeSt๋ผ๊ณ  ํ•˜๋Š” ์ƒˆ๋กœ์šด ResNet ๋ณ€ํ˜•์„ ์–ป์Šต๋‹ˆ๋‹ค. ResNeSt ๋ชจ๋ธ์€ ์œ ์‚ฌํ•œ ๋ชจ๋ธ ๋ณต์žก์„ฑ์„ ๊ฐ€์ง„ ๋‹ค๋ฅธ ๋„คํŠธ์›Œํฌ๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•˜๋ฉฐ ๊ฐ์ฒด ๊ฐ์ง€, ์ธ์Šคํ„ด์Šค ๋ถ„ํ• (instance segmentation) ๋ฐ ์˜๋ฏธ ๋ถ„ํ• ์„ ํฌํ•จํ•œ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

crop size PyTorch
ResNeSt-50 224 81.03
ResNeSt-101 256 82.83
ResNeSt-200 320 83.84
ResNeSt-269 416 84.54

์ฐธ๊ณ ๋ฌธํ—Œ