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 |
Wide ResNet |
Wide Residual Networks |
researchers |
wide_resnet.png |
Sergey Zagoruyko |
|
pytorch/vision |
wide_resnet.png |
no-image |
cuda-optional |
10 |
import torch
# load WRN-50-2:
model = torch.hub.load('pytorch/vision:v0.10.0', 'wide_resnet50_2', pretrained=True)
# or WRN-101-2
model = torch.hub.load('pytorch/vision:v0.10.0', 'wide_resnet101_2', 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) # ๋ชจ๋ธ์์ ์๊ตฌํ๋ ๋ฏธ๋๋ฐฐ์น ์์ฑ
# 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())
Wide Residual ๋คํธ์ํฌ๋ ResNet์ ๋นํด ๋จ์ํ ์ฑ๋ ์๊ฐ ์ฆ๊ฐํ์ต๋๋ค. ์ด์ธ์ ์ํคํ ์ฒ๋ ResNet๊ณผ ๋์ผํฉ๋๋ค. ๋ณ๋ชฉ(bottleneck) ๋ธ๋ก์ด ์๋ ์ฌ์ธต ImageNet ๋ชจ๋ธ์ ๋ด๋ถ 3x3 ํฉ์ฑ๊ณฑ ์ฑ๋ ์๋ฅผ ์ฆ๊ฐ ์์ผฐ์ต๋๋ค.
wide_resnet50_2
๋ฐ wide_resnet101_2
๋ชจ๋ธ์ Warm Restarts๊ฐ ์๋ SGD(SGDR)๋ฅผ ์ฌ์ฉํ์ฌ ํผํฉ ์ ๋ฐ๋(Mixed Precision) ๋ฐฉ์์ผ๋ก ํ์ต๋์์ต๋๋ค.
์ฒดํฌ ํฌ์ธํธ๋ ํฌ๊ธฐ๊ฐ ์์ ๊ฒฝ์ฐ ์ ๋ฐ ์ ๋ฐ๋(batch norm ์ ์ธ)์ ๊ฐ์ค์น๋ฅผ ๊ฐ์ง๋ฉฐ FP32 ๋ชจ๋ธ์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
Model structure | Top-1 error | Top-5 error | # parameters |
---|---|---|---|
wide_resnet50_2 | 21.49 | 5.91 | 68.9M |
wide_resnet101_2 | 21.16 | 5.72 | 126.9M |