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 |
|
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 |