Skip to content

Latest commit

ย 

History

History
102 lines (85 loc) ยท 3.79 KB

pytorch_vision_squeezenet.md

File metadata and controls

102 lines (85 loc) ยท 3.79 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
SqueezeNet
Alexnet-level accuracy with 50x fewer parameters.
researchers
squeezenet.png
Pytorch Team
vision
scriptable
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

์ฐธ์กฐ