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