| layout | hub_detail | |
|---|---|---|
| background-class | hub-background | |
| body-class | hub | |
| title | HarDNet | |
| summary | Harmonic DenseNet pre-trained on ImageNet | |
| category | researchers | |
| image | hardnet.png | |
| author | PingoLH | |
| tags |
|
|
| github-link | https://github.com/PingoLH/Pytorch-HarDNet | |
| github-id | PingoLH/Pytorch-HarDNet | |
| featured_image_1 | hardnet.png | |
| featured_image_2 | hardnet_blk.png | |
| accelerator | cuda-optional | |
| order | 10 | |
| demo-model-link | https://huggingface.co/spaces/pytorch/HardNet |
import torch
model = torch.hub.load('PingoLH/Pytorch-HarDNet', 'hardnet68', pretrained=True)
# or any of these variants
# model = torch.hub.load('PingoLH/Pytorch-HarDNet', 'hardnet85', pretrained=True)
# model = torch.hub.load('PingoLH/Pytorch-HarDNet', 'hardnet68ds', pretrained=True)
# model = torch.hub.load('PingoLH/Pytorch-HarDNet', 'hardnet39ds', 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())
HarDNet(Harmonic DenseNet)์ ๋ฎ์ ๋ฉ๋ชจ๋ฆฌ ํธ๋ํฝ์ ๊ฐ์ง๋ CNN ๋ชจ๋ธ๋ก ๋น ๋ฅด๊ณ ํจ์จ์ ์ ๋๋ค. ๊ธฐ๋ณธ ๊ฐ๋ ์ ๊ณ์ฐ ๋น์ฉ๊ณผ ๋ฉ๋ชจ๋ฆฌ ์ ๊ทผ ๋น์ฉ์ ๋์์ ์ต์ํํ๋ ๊ฒ์ ๋๋ค. ๋ฐ๋ผ์ HarDNet ๋ชจ๋ธ์ ๋์ผํ ์ ํ๋๋ฅผ ๊ฐ์ง ResNet ๋ชจ๋ธ์ ๋นํด GPU์์ ์คํ๋๋ ์๋๊ฐ 35% ๋ ๋น ๋ฆ ๋๋ค. (MobileNet๊ณผ ๋น๊ตํ๊ธฐ ์ํด ์ค๊ณ๋ ๋ DS ๋ชจ๋ธ์ ์ ์ธ)
์๋์๋ ๊ฐ๊ฐ ๊น์ด๋ณ ๋ถ๋ฆฌ ๊ฐ๋ฅํ Conv ๋ ์ด์ด๊ฐ ์๊ฑฐ๋ ์๋ 39, 68, 85๊ฐ์ ๋ ์ด์ด๋ฅผ ํฌํจํ 4๊ฐ์ง ๋ฒ์ ์ HardNet ๋ชจ๋ธ์ด ์์ต๋๋ค. ์ฌ์ ํ๋ จ๋ ๋ชจ๋ธ์ ๋ํด ImageNet ๋ฐ์ดํฐ์ ์ 1-crop ์ค๋ฅ์จ์ ์๋์ ๋์ด๋์ด ์์ต๋๋ค.
| Model structure | Top-1 error | Top-5 error |
|---|---|---|
| hardnet39ds | 27.92 | 9.57 |
| hardnet68ds | 25.71 | 8.13 |
| hardnet68 | 23.52 | 6.99 |
| hardnet85 | 21.96 | 6.11 |