Skip to content

Latest commit

Β 

History

History
108 lines (91 loc) Β· 4.35 KB

pytorch_vision_proxylessnas.md

File metadata and controls

108 lines (91 loc) Β· 4.35 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
ProxylessNAS
Proxylessly specialize CNN architectures for different hardware platforms.
researchers
proxylessnas.png
MIT Han Lab
vision
mit-han-lab/ProxylessNAS
proxylessnas.png
no-image
cuda-optional
10
import torch
target_platform = "proxyless_cpu"
# proxyless_gpu, proxyless_mobile, proxyless_mobile14도 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
model = torch.hub.load('mit-han-lab/ProxylessNAS', target_platform, 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())

λͺ¨λΈ μ„€λͺ…

ProxylessNAS λͺ¨λΈμ€ ProxylessNAS: Direct Neural Architecture Search on Target Task and Hardware λ…Όλ¬Έμ—μ„œ μ œμ•ˆλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

일반적으둜, μ‚¬λžŒλ“€μ€ λͺ¨λ“  ν•˜λ“œμ›¨μ–΄ ν”Œλž«νΌμ— λŒ€ν•΄ ν•˜λ‚˜μ˜ 효율적인 λͺ¨λΈμ„ μ„€κ³„ν•˜λŠ” κ²½ν–₯이 μžˆμŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ ν•˜λ“œμ›¨μ–΄λ§ˆλ‹€ νŠΉμ„±μ΄ λ‹€λ¦…λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ CPUλŠ” 더 높은 주파수λ₯Ό κ°€μ§€μ§€λ§Œ GPUλŠ” 병렬화에 더 λ›°μ–΄λ‚©λ‹ˆλ‹€. λ”°λΌμ„œ λͺ¨λΈμ„ μΌλ°˜ν™”ν•˜κΈ°λ³΄λ‹€λŠ” ν•˜λ“œμ›¨μ–΄ ν”Œλž«νΌμ— 맞게 CNN μ•„ν‚€ν…μ²˜λ₯Ό μ „λ¬Έν™”ν•΄μ•Ό ν•©λ‹ˆλ‹€. μ•„λž˜μ—μ„œ λ³Ό 수 μžˆλ“―μ΄, μ „λ¬Έν™”λŠ” μ„Έ 가지 ν”Œλž«νΌ λͺ¨λ‘μ—μ„œ μƒλ‹Ήν•œ μ„±λŠ₯ ν–₯상을 μ œκ³΅ν•©λ‹ˆλ‹€.

Model structure GPU Latency CPU Latency Mobile Latency
proxylessnas_gpu 5.1ms 204.9ms 124ms
proxylessnas_cpu 7.4ms 138.7ms 116ms
proxylessnas_mobile 7.2ms 164.1ms 78ms

사전 ν›ˆλ ¨λœ λͺ¨λΈμ— ν•΄λ‹Ήν•˜λŠ” Top-1 μ •ν™•λ„λŠ” μ•„λž˜μ— λ‚˜μ—΄λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

Model structure Top-1 error
proxylessnas_cpu 24.7
proxylessnas_gpu 24.9
proxylessnas_mobile 25.4
proxylessnas_mobile_14 23.3

μ°Έκ³ λ¬Έν—Œ