Skip to content

Latest commit

ย 

History

History
104 lines (90 loc) ยท 4.08 KB

pytorch_vision_wide_resnet.md

File metadata and controls

104 lines (90 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
Wide ResNet
Wide Residual Networks
researchers
wide_resnet.png
Sergey Zagoruyko
vision
scriptable
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

์ฐธ๊ณ ๋ฌธํ—Œ