Skip to content

Latest commit

ย 

History

History
113 lines (96 loc) ยท 4.39 KB

pytorch_vision_ibnnet.md

File metadata and controls

113 lines (96 loc) ยท 4.39 KB

layout: hub_detail background-class: hub-background body-class: hub title: IBN-Net summary: Networks with domain/appearance invariance category: researchers image: ibnnet.png author: Xingang Pan tags: [vision] github-link: https://github.com/XingangPan/IBN-Net github-id: XingangPan/IBN-Net featured_image_1: ibnnet.png featured_image_2: no-image accelerator: cuda-optional order: 10 demo-model-link: https://huggingface.co/spaces/pytorch/IBN-Net

import torch
model = torch.hub.load('XingangPan/IBN-Net', 'resnet50_ibn_a', 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())

๋ชจ๋ธ ์„ค๋ช…

IBN-Net์€ ๋„๋ฉ”์ธ/์™ธ๊ด€ ๋ถˆ๋ณ€์„ฑ์„ ๊ฐ–๋Š” CNN ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. Style transfer์— ์˜๊ฐ์„ ์–ป์–ด IBN-Net์€ ๋‹จ์ผ ์‹ฌ์ธต ๋„คํŠธ์›Œํฌ์—์„œ ์ธ์Šคํ„ด์Šค ์ •๊ทœํ™”์™€ ์ผ๊ด„ ์ •๊ทœํ™”๋ฅผ ์‹ ์ค‘ํ•˜๊ฒŒ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ๋ณต์žก์„ฑ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋ธ๋ง ๋ฐ ๋ฒ”์šฉ์„ฑ์„ ๋ชจ๋‘ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. IBN-Net์€ ํŠนํžˆ ๊ต์ฐจ ๋„๋ฉ”์ธ ๋˜๋Š” ์‚ฌ๋žŒ/์ฐจ๋Ÿ‰ ์žฌ์‹๋ณ„ ์ž‘์—…์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.

ImageNet ๋ฐ์ดํ„ฐ์…‹์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋“ค์˜ ์ •ํ™•๋„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Model name Top-1 acc Top-5 acc
resnet50_ibn_a 77.46 93.68
resnet101_ibn_a 78.61 94.41
resnext101_ibn_a 79.12 94.58
se_resnet101_ibn_a 78.75 94.49

๋‘ ๊ฐ€์ง€ Re-ID ๋ฒค์น˜๋งˆํฌ Market1501 ๋ฐ DukeMTMC-reID์— ๋Œ€ํ•œ rank1/mAP๋Š” ์•„๋ž˜์— ๋‚˜์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.(michuanhaohao/reid-strong-baseline์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.)

Backbone Market1501 DukeMTMC-reID
ResNet50 94.5 (85.9) 86.4 (76.4)
ResNet101 94.5 (87.1) 87.6 (77.6)
SeResNet50 94.4 (86.3) 86.4 (76.5)
SeResNet101 94.6 (87.3) 87.5 (78.0)
SeResNeXt50 94.9 (87.6) 88.0 (78.3)
SeResNeXt101 95.0 (88.0) 88.4 (79.0)
ResNet50-IBN-a 95.0 (88.2) 90.1 (79.1)

์ฐธ๊ณ ๋ฌธํ—Œ