Skip to content

Latest commit

ย 

History

History
102 lines (88 loc) ยท 4.25 KB

pytorch_vision_densenet.md

File metadata and controls

102 lines (88 loc) ยท 4.25 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
Densenet
Dense Convolutional Network (DenseNet), connects each layer to every other layer in a feed-forward fashion.
researchers
densenet1.png
Pytorch Team
vision
scriptable
pytorch/vision
densenet1.png
densenet2.png
cuda-optional
10
import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'densenet121', pretrained=True)
# or any of these variants
# model = torch.hub.load('pytorch/vision:v0.10.0', 'densenet169', pretrained=True)
# model = torch.hub.load('pytorch/vision:v0.10.0', 'densenet201', pretrained=True)
# model = torch.hub.load('pytorch/vision:v0.10.0', 'densenet161', 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๋กœ ์˜ฎ๊น๋‹ˆ๋‹ค
if torch.cuda.is_available():
    input_batch = input_batch.to('cuda')
    model.to('cuda')

with torch.no_grad():
    output = model(input_batch)
# shape์ด 1000์ด๋ฉฐ ImageNet์˜ 1000๊ฐœ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์‹ ๋ขฐ๋„ ์ ์ˆ˜๊ฐ€ ์žˆ๋Š” ํ…์„œ
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()]
# ์ด๋ฏธ์ง€ ๋ณ„ Top5 ์นดํ…Œ๊ณ ๋ฆฌ ์กฐํšŒ
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())

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

Dense Convolutional Network (DenseNet)๋Š” ์ˆœ์ „ํŒŒ(feed-forward) ๋ฐฉ์‹์œผ๋กœ ๊ฐ ๋ ˆ์ด์–ด๋ฅผ ๋‹ค๋ฅธ ๋ชจ๋“  ๋ ˆ์ด์–ด๊ณผ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. L ๊ณ„์ธต์˜ ๊ธฐ์กด ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์ด L๊ฐœ์˜ ์—ฐ๊ฒฐ - ๊ฐ ์ธต๊ณผ ๋‹ค์Œ ์ธต ์‚ฌ์ด์˜ ํ•˜๋‚˜ - ์ธ ๋ฐ˜๋ฉด ์šฐ๋ฆฌ์˜ ์‹ ๊ฒฝ๋ง์€ L(L+1)/2 ์ง์ ‘ ์—ฐ๊ฒฐ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ฐ ๊ณ„์ธต์—, ๋ชจ๋“  ์„ ํ–‰ ๊ณ„์ธต์˜ (feature-map)ํ˜•์ƒ ๋งต์€ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ž์ฒด ํ˜•์ƒ ๋งต์€ ๋ชจ๋“  ํ›„์† ๊ณ„์ธต์— ๋Œ€ํ•œ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. DenseNets๋Š” ๋ช‡ ๊ฐ€์ง€ ๊ฐ•๋ ฅํ•œ ์žฅ์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค: ๊ทธ๋ ˆ๋””์–ธํŠธ๊ฐ€ ์‚ฌ๋ผ์ง€๋Š” ๋ฌธ์ œ๋ฅผ ์™„ํ™”์‹œํ‚ค๊ณ , ํŠน์ง• ์ „ํŒŒ๋ฅผ ๊ฐ•ํ™”ํ•˜๋ฉฐ, ํŠน์ง• ์žฌ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜๋ฉฐ, ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ์ˆ˜๋ฅผ ํฌ๊ฒŒ ์ค„์ž…๋‹ˆ๋‹ค.

์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ imagenet ๋ฐ์ดํ„ฐ์…‹์˜ 1-crop ์˜ค๋ฅ˜์œจ์€ ๋‹ค์Œ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Model structure Top-1 error Top-5 error
densenet121 25.35 7.83
densenet169 24.00 7.00
densenet201 22.80 6.43
densenet161 22.35 6.20

์ฐธ๊ณ  ์ž๋ฃŒ