Skip to content

Latest commit

Β 

History

History
107 lines (89 loc) Β· 4.12 KB

pytorch_vision_ghostnet.md

File metadata and controls

107 lines (89 loc) Β· 4.12 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
GhostNet
Efficient networks by generating more features from cheap operations
researchers
ghostnet.png
Huawei Noah's Ark Lab
vision
scriptable
huawei-noah/ghostnet
ghostnet.png
no-image
cuda-optional
10
import torch
model = torch.hub.load('huawei-noah/ghostnet', 'ghostnet_1x', pretrained=True)
model.eval()

λͺ¨λ“  사전 ν•™μŠ΅λœ λͺ¨λΈλ“€μ€ μž…λ ₯ 이미지가 λ™μΌν•œ λ°©μ‹μœΌλ‘œ μ •κ·œν™” λ˜λŠ” 것을 μš”κ΅¬ν•©λ‹ˆλ‹€. λ‹€μ‹œ 말해 H와 Wκ°€ 적어도 224이고, (3 x H x W)의 shapeλ₯Ό κ°€μ§€λŠ” 3채널 RGB μ΄λ―Έμ§€λ“€μ˜ λ―Έλ‹ˆλ°°μΉ˜λ₯Ό λ§ν•©λ‹ˆλ‹€. 이 이미지듀은 [0, 1]의 λ²”μœ„λ‘œ λ‘œλ“œλ˜μ–΄μ•Ό ν•˜κ³ , mean = [0.485, 0.456, 0.406] κ³Ό std = [0.229, 0.224, 0.225]λ₯Ό μ‚¬μš©ν•˜μ—¬ μ •κ·œν™”λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.

μ—¬κΈ°μ„œλΆ€ν„°λŠ” μ˜ˆμ‹œ μ½”λ“œ μž…λ‹ˆλ‹€.

# pytorch μ›Ήμ‚¬μ΄νŠΈμ—μ„œ μ˜ˆμ‹œ 이미지λ₯Ό λ‹€μš΄λ‘œλ“œ ν•©λ‹ˆλ‹€.
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) # λͺ¨λΈμ— λ§žμΆ”μ–΄ λ―Έλ‹ˆλ°°μΉ˜λ₯Ό 생성 ν•©λ‹ˆλ‹€.

# 연산속도λ₯Ό μœ„ν•΄ inputκ³Ό λͺ¨λΈμ„ 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,) 의 ν…μ„œλ₯Ό return ν•©λ‹ˆλ‹€.
print(output[0])
# output은 μ •κ·œν™”λ˜μ§€ μ•Šμ€ μ‹ λ’° 점수둜 μ–»μ–΄μ§‘λ‹ˆλ‹€. ν™•λ₯ μ„ μ–»κΈ° μœ„ν•΄ μ†Œν”„νŠΈλ§₯슀λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
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_prob, top5_catid = torch.topk(probabilities, 5)
for i in range(top5_prob.size(0)):
    print(categories[top5_catid[i]], top5_prob[i].item())

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

κ³ μŠ€νŠΈλ„· μ•„ν‚€ν…μ²˜λŠ” λ‹€μ–‘ν•œ νŠΉμ§• 맡을 효율적인 μ—°μ‚°μœΌλ‘œ μƒμ„±ν•˜λŠ” 고슀트 λͺ¨λ“ˆ ꡬ쑰둜 μ΄λ£¨μ–΄μ§‘λ‹ˆλ‹€. ν•©μ„±κ³± μ‹ κ²½λ§μ—μ„œμ˜ ν•™μŠ΅ κ³Όμ •μ—μ„œ 좔둠에 μ€‘μš”ν•œ μ€‘λ³΅λ˜λŠ” 고유 νŠΉμ§•λ§΅(고슀트 맡)듀이 λ‹€μˆ˜ μƒμ„±λ˜λŠ” ν˜„μƒμ— κΈ°λ°˜ν•˜μ—¬ 섀계 λ˜μ—ˆμŠ΅λ‹ˆλ‹€. κ³ μŠ€νŠΈλ„·μ—μ„œλŠ” 더 효율적인 μ—°μ‚°μœΌλ‘œ 고슀트 맡듀을 μƒμ„±ν•©λ‹ˆλ‹€. λ²€μΉ˜λ§ˆν¬μ—μ„œ μˆ˜ν–‰λœ μ‹€ν—˜μ„ 톡해 속도와 μ •ν™•λ„μ˜ 상좩 관계에 κ΄€ν•œ κ³ μŠ€νŠΈλ„·μ˜ μš°μˆ˜μ„±μ„ λ³΄μ—¬μ€λ‹ˆλ‹€.

사전 ν•™μŠ΅λœ λͺ¨λΈμ„ μ‚¬μš©ν•œ ImageNet 데이터셋에 λ”°λ₯Έ μ •ν™•λ„λŠ” μ•„λž˜μ— λ‚˜μ—΄λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

Model structure FLOPs Top-1 acc Top-5 acc
GhostNet 1.0x 142M 73.98 91.46

μ°Έκ³ 

λ‹€μŒ λ§ν¬μ—μ„œ λ…Όλ¬Έμ˜ 전체적인 λ‚΄μš©μ— λŒ€ν•˜μ—¬ 읽을 수 μžˆμŠ΅λ‹ˆλ‹€.

@inproceedings{han2019ghostnet, title={GhostNet: More Features from Cheap Operations}, author={Kai Han and Yunhe Wang and Qi Tian and Jianyuan Guo and Chunjing Xu and Chang Xu}, booktitle={CVPR}, year={2020}, }