forked from mindspore-lab/mindcv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.py
61 lines (49 loc) · 1.63 KB
/
infer.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
55
56
57
58
59
60
61
"""MindSpore Inference Script
Example:
$ python inference.py --image-path="/path/to/image.png" --model="densenet121"
"""
import ast
import argparse
import numpy as np
from PIL import Image
import mindspore as ms
from mindspore import nn
from mindcv.models import create_model
from mindcv.data import create_transforms
parser = argparse.ArgumentParser(description='MindSpore Inference Demo')
parser.add_argument('--image_path', type=str, help='path to image')
parser.add_argument('--model', type=str, help='name of model')
parser.add_argument('--ckpt_path', type=str, help='checkpoint path')
def main():
args = parser.parse_args()
ms.set_seed(1)
ms.set_context(mode=ms.PYNATIVE_MODE)
img = Image.open(args.image_path).convert("RGB")
# create transform
transform_list = create_transforms(
dataset_name="imagenet",
is_training=False
)
transform_list.pop(0)
for transform in transform_list:
img = transform(img)
img = np.expand_dims(img, axis=0)
# create model
network = create_model(
model_name=args.model,
pretrained=True
)
network.set_train(False)
logits = nn.Softmax()(network(ms.Tensor(img)))[0].asnumpy()
preds = np.argsort(logits)[::-1][:5]
probs = logits[preds]
with open("./tutorials/imagenet1000_clsidx_to_labels.txt", encoding='utf-8') as f:
idx2label = ast.literal_eval(f.read())
#print(f"Predict result of {args.image_path}:")
cls_prob = {}
for pred, prob in zip(preds, probs):
cls_name = idx2label[pred]
cls_prob[cls_name] = prob
print(cls_prob)
if __name__ == '__main__':
main()