Skip to content

Commit

Permalink
pytorch_vision_ghostnet λ²ˆμ—­(PyTorchKorea#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyubumShin committed Aug 28, 2022
1 parent 7fdca78 commit 93bdb72
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions pytorch_vision_ghostnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ model = torch.hub.load('huawei-noah/ghostnet', 'ghostnet_1x', pretrained=True)
model.eval()
```

All pre-trained models expect input images normalized in the same way,
i.e. mini-batches of 3-channel RGB images of shape `(3 x H x W)`, where `H` and `W` are expected to be at least `224`.
The images have to be loaded in to a range of `[0, 1]` and then normalized using `mean = [0.485, 0.456, 0.406]`
and `std = [0.229, 0.224, 0.225]`.
λͺ¨λ“  사전 ν•™μŠ΅λœ λͺ¨λΈλ“€μ€ μž…λ ₯ 이미지가 λ™μΌν•œ λ°©μ‹μœΌλ‘œ μ •κ·œν™” λ˜λŠ” 것을 μš”κ΅¬ν•©λ‹ˆλ‹€.
λ‹€μ‹œ 말해 `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]`λ₯Ό μ‚¬μš©ν•˜μ—¬ μ •κ·œν™”λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.

Here's a sample execution.
μ—¬κΈ°μ„œλΆ€ν„°λŠ” μ˜ˆμ‹œ μ½”λ“œ μž…λ‹ˆλ‹€.

```python
# Download an example image from the pytorch website
# 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)
```

```python
# sample execution (requires torchvision)
# μ‹€ν–‰ μ˜ˆμ‹œ μ½”λ“œ (torchvision ν•„μš”)
from PIL import Image
from torchvision import transforms
input_image = Image.open(filename)
Expand All @@ -50,51 +50,53 @@ preprocess = transforms.Compose([
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) # create a mini-batch as expected by the model
input_batch = input_tensor.unsqueeze(0) # λͺ¨λΈμ— λ§žμΆ”μ–΄ λ―Έλ‹ˆλ°°μΉ˜λ₯Ό 생성 ν•©λ‹ˆλ‹€

# move the input and model to GPU for speed if available
# 연산속도λ₯Ό μœ„ν•΄ inputκ³Ό λͺ¨λΈμ„ GPU에 λ‘œλ“œ ν•©λ‹ˆλ‹€
if torch.cuda.is_available():
input_batch = input_batch.to('cuda')
model.to('cuda')

with torch.no_grad():
output = model(input_batch)
# Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes
# ImageNet 1000개의 클래슀의 μ‹ λ’°μ μˆ˜λ₯Ό ν¬ν•¨ν•˜λŠ” (1000,) 의 ν…μ„œλ₯Ό return ν•©λ‹ˆλ‹€
print(output[0])
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
# output은 μ •κ·œν™”λ˜μ§€ μ•Šμ€ μ‹ λ’° 점수둜 μ–»μ–΄μ§‘λ‹ˆλ‹€. ν™•λ₯ μ„ μ–»κΈ° μœ„ν•΄ μ†Œν”„νŠΈλ§₯슀λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€
probabilities = torch.nn.functional.softmax(output[0], dim=0)
print(probabilities)
```

```
# Download ImageNet labels
# ImageNet의 라벨을 λ‹€μš΄λ‘œλ“œ ν•©λ‹ˆλ‹€
!wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt
```

```
# Read the categories
# μΉ΄ν…Œκ³ λ¦¬λ₯Ό μ½μ–΄μ˜΅λ‹ˆλ‹€
with open("imagenet_classes.txt", "r") as f:
categories = [s.strip() for s in f.readlines()]
# Show top categories per image
# 이미지 λ§ˆλ‹€ ν™•λ₯ κ°’이 κ°€μž₯ 높은 λ²”μ£Ό 좜λ ₯ ν•©λ‹ˆλ‹€
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())
```

### Model Description
### λͺ¨λΈ μ„€λͺ…

The GhostNet architecture is based on an Ghost module structure which generate more features from cheap operations. Based on a set of intrinsic feature maps, a series of cheap operations are applied to generate many ghost feature maps that could fully reveal information underlying intrinsic features. Experiments conducted on benchmarks demonstrate that the superiority of GhostNet in terms of speed and accuracy tradeoff.
κ³ μŠ€νŠΈλ„· μ•„ν‚€ν…μ²˜λŠ” λ‹€μ–‘ν•œ νŠΉμ§• 맡을 효율적인 μ—°μ‚°μœΌλ‘œ μƒμ„±ν•˜λŠ” 고슀트 λͺ¨λ“ˆ ꡬ쑰둜 μ΄λ£¨μ–΄μ§‘λ‹ˆλ‹€.
ν•©μ„±κ³± μ‹ κ²½λ§μ—μ„œμ˜ ν•™μŠ΅ κ³Όμ •μ—μ„œ 좔둠에 μ€‘μš”ν•œ μ€‘λ³΅λ˜λŠ” 고유 νŠΉμ§•λ§΅(고슀트 맡)듀이 λ‹€μˆ˜ μƒμ„±λ˜λŠ” ν˜„μƒμ— κΈ°λ°˜ν•˜μ—¬ 섀계 λ˜μ—ˆμŠ΅λ‹ˆλ‹€. κ³ μŠ€νŠΈλ„·μ—μ„œλŠ” 더 효율적인 μ—°μ‚°μœΌλ‘œ 고슀트 맡듀을 μƒμ„±ν•©λ‹ˆλ‹€.
λ²€μΉ˜λ§ˆν¬μ—μ„œ μˆ˜ν–‰λœ μ‹€ν—˜μ„ 톡해 속도와 μ •ν™•λ„μ˜ 상좩 관계에 κ΄€ν•œ κ³ μŠ€νŠΈλ„·μ˜ μš°μˆ˜μ„±μ„ λ³΄μ—¬μ€λ‹ˆλ‹€.

The corresponding accuracy on ImageNet dataset with pretrained model is listed below.
사전 ν•™μŠ΅λœ λͺ¨λΈμ„ μ‚¬μš©ν•œ ImageNet 데이터셋에 λ”°λ₯Έ μ •ν™•λ„λŠ” μ•„λž˜μ— λ‚˜μ—΄λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

| Model structure | FLOPs | Top-1 acc | Top-5 acc |
| --------------- | ----------- | ----------- | ----------- |
| GhostNet 1.0x | 142M | 73.98 | 91.46 |


### References
### μ°Έκ³ 

You can read the full paper at this [link](https://arxiv.org/abs/1911.11907).
λ‹€μŒ [링크](https://arxiv.org/abs/1911.11907)μ—μ„œ λ…Όλ¬Έμ˜ 전체적인 λ‚΄μš©μ— λŒ€ν•˜μ—¬ 읽을 수 μžˆμŠ΅λ‹ˆλ‹€.

>@inproceedings{han2019ghostnet,
> title={GhostNet: More Features from Cheap Operations},
Expand Down

0 comments on commit 93bdb72

Please sign in to comment.