| layout | hub_detail | |
|---|---|---|
| background-class | hub-background | |
| body-class | hub | |
| title | ResNeSt | |
| summary | A new ResNet variant. | |
| category | researchers | |
| image | resnest.jpg | |
| author | Hang Zhang | |
| tags |
|
|
| github-link | https://github.com/zhanghang1989/ResNeSt | |
| github-id | zhanghang1989/ResNeSt | |
| featured_image_1 | resnest.jpg | |
| featured_image_2 | no-image | |
| accelerator | cuda-optional | |
| order | 10 | |
| demo-model-link | https://huggingface.co/spaces/pytorch/ResNeSt |
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 |