-
Notifications
You must be signed in to change notification settings - Fork 965
/
imagenet_test.py
77 lines (52 loc) · 2.66 KB
/
imagenet_test.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#----------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#----------------------------------------------------------------------------------------------
import argparse
import numpy as np
import sys
import os
from mmdnn.conversion.examples.imagenet_test import TestKit
import coremltools
class TestCoreML(TestKit):
def __init__(self):
from six import text_type as _text_type
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--preprocess', type=_text_type, help='Model Preprocess Type')
parser.add_argument('--model', '-n', '-w', type=_text_type,
required=True, help='CoreML Model path.')
parser.add_argument('-s', type=_text_type, help='Source Framework Type',
choices=self.truth.keys())
parser.add_argument('--image', '-i',
type=_text_type, help='Test image path.',
default="mmdnn/conversion/examples/data/seagull.jpg")
parser.add_argument('-input', type=_text_type,
required=True, help='CoreML Input Node')
parser.add_argument('-output', type=_text_type,
required=True, help='CoreML Output Node')
parser.add_argument('-size', type=int,
default=224, help='CoreML Input Image Size')
self.args = parser.parse_args()
print("Loading model [{}].".format(self.args.model))
self.model = coremltools.models.MLModel(self.args.model.encode())
print("Model loading success.")
def preprocess(self, image_path):
from PIL import Image as pil_image
img = pil_image.open(image_path)
img = img.resize((self.args.size, self.args.size))
self.data = img
def print_result(self):
coreml_inputs = {self.args.input: self.data}
self.coreml_output = self.model.predict(coreml_inputs, useCPUOnly=False)
predict = self.coreml_output[self.args.output]
super(TestCoreML, self).print_result(predict)
def print_intermediate_result(self, layer_name, if_transpose = False):
super(TestCoreML, self).print_intermediate_result(self.coreml_output[layer_name], if_transpose)
def inference(self, image_path):
self.preprocess(image_path)
self.print_result()
# self.print_intermediate_result('conv1_7x7_s2_1', True)
# self.test_truth()
if __name__=='__main__':
tester = TestCoreML()
tester.inference(tester.args.image)