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 |
SqueezeNet |
Alexnet-level accuracy with 50x fewer parameters. |
researchers |
squeezenet.png |
Pytorch Team |
|
pytorch/vision |
squeezenet.png |
no-image |
cuda-optional |
10 |
import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'squeezenet1_0', pretrained=True)
# ๋๋
# model = torch.hub.load('pytorch/vision:v0.10.0', 'squeezenet1_1', pretrained=True)
model.eval()
์ฌ์ ์ ํ๋ จ๋ ๋ชจ๋ธ์ ๋ชจ๋ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ ๊ทํ(normalize)ํ ์ด๋ฏธ์ง๋ฅผ ์ ๋ ฅ์ผ๋ก ๋ฐ์ต๋๋ค.
์๋ฅผ ๋ค์ด, (3 x H x W)
ํฌ๋งท์ 3์ฑ๋ rgb ์ด๋ฏธ์ง๋ค์ ๋ฏธ๋ ๋ฐฐ์น์ ๊ฒฝ์ฐ H ์ W ์ ํฌ๊ธฐ๋ 224 ์ด์์ด์ด์ผ ํฉ๋๋ค.
์ด ๋ ๋ชจ๋ ํฝ์
๋ค์ 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)
# ์์ (ํ ์น๋น์ ํ์)
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) # ๋ชจ๋ธ์์ ์๊ตฌํ๋ ํ์์ธ mini batch ํํ๋ก ๋ณํ
# ๋น ๋ฅด๊ฒ ์คํํ๊ธฐ ์ํด ๊ฐ๋ฅํ ๊ฒฝ์ฐ model ๊ณผ input image ๋ฅผ 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๊ฐ ๋ฒ์ฃผ์ ๋ํ ์ ๋ขฐ ์ ์๋ฅผ ๋ํ๋ด๋ ํ
์ ๋ฐํ
print(output[0])
# ํด๋น ์ ๋ขฐ ์ ์๋ softmax๋ฅผ ์ทจํด ํ๋ฅ ๊ฐ์ผ๋ก ๋ณํ๊ฐ๋ฅํฉ๋๋ค.
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())
squeezenet1_0
๋ชจ๋ธ์ SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size ๋
ผ๋ฌธ์ ๊ตฌํํ ๊ฒ์
๋๋ค.
squeezenet1_1
๋ชจ๋ธ์ official squeezenet repo ์์ ์์ต๋๋ค.
squeezenet1_0
์์ค์ ์ ํ๋๋ฅผ ์ ์งํ๋ฉฐ 2.4๋ฐฐ ๊ณ์ฐ์ด ๋ ํ์ํ๊ณ , squeezenet1_0
๋ณด๋ค ๋งค๊ฐ๋ณ์์ ์๊ฐ ์ ์ต๋๋ค.
ImageNet ๋ฐ์ดํฐ์ ๊ธฐ์ค์ผ๋ก ํ๋ จ๋ ๋ชจ๋ธ๋ค์ 1-crop ์๋ฌ์จ์ ์๋์ ๊ฐ์ต๋๋ค.
๋ชจ๋ธ | Top-1 ์๋ฌ | Top-5 ์๋ฌ |
---|---|---|
squeezenet1_0 | 41.90 | 19.58 |
squeezenet1_1 | 41.81 | 19.38 |