forked from facebookresearch/mobile-vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_fbnet_v2.py
54 lines (40 loc) · 1.36 KB
/
run_fbnet_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""
Example code to run fbnet model on a given image
Usage:
python3 -m examples.run_fbnet_v2
"""
import urllib
import torch
from mobile_cv.model_zoo.models.fbnet_v2 import fbnet
from mobile_cv.model_zoo.models.preprocess import get_preprocess
from PIL import Image
def _get_input():
# Download an example image from the pytorch website
url, filename = (
"https://github.com/pytorch/hub/raw/master/dog.jpg",
"dog.jpg",
)
local_filename, headers = urllib.request.urlretrieve(url, filename)
input_image = Image.open(local_filename)
return input_image
def run_fbnet_v2():
# fbnet models, supported models could be found in
# mobile_cv/model_zoo/models/model_info/fbnet_v2/*.json
model_name = "dmasking_l3"
# load model
model = fbnet(model_name, pretrained=True)
model.eval()
preprocess = get_preprocess(model.arch_def.get("input_size", 224))
# load and process input
input_image = _get_input()
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0)
# run model
with torch.no_grad():
output = model(input_batch)
output_softmax = torch.nn.functional.softmax(output[0], dim=0)
print(output_softmax.max(0))
if __name__ == "__main__":
run_fbnet_v2()