JeVoisBase
1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Tweet
Loading...
Searching...
No Matches
mobilenet_v1.py
Go to the documentation of this file.
1
import
numpy
as
np
2
import
cv2
as
cv
3
4
class
MobileNetV1
:
5
def
__init__
(self, modelPath, labelPath, backendId=0, targetId=0):
6
self.
model_path
= modelPath
7
self.
label_path
= labelPath
8
self.
backend_id
= backendId
9
self.
target_id
= targetId
10
11
self.
model
= cv.dnn.readNet(self.
model_path
)
12
self.
model
.setPreferableBackend(self.
backend_id
)
13
self.
model
.setPreferableTarget(self.
target_id
)
14
15
self.
input_names
=
''
16
self.
output_names
=
''
17
self.
input_size
= [224, 224]
18
self.
mean
=[0.485, 0.456, 0.406]
19
self.
std
=[0.229, 0.224, 0.225]
20
21
# load labels
22
self.
labels
= self.
_load_labels
()
23
24
def
_load_labels
(self):
25
labels = []
26
with
open(self.
label_path
,
'r'
)
as
f:
27
for
line
in
f:
28
labels.append(line.strip())
29
return
labels
30
31
@property
32
def
name
(self):
33
return
self.__class__.__name__
34
35
def
setBackend
(self, backendId):
36
self.
backend_id
= backendId
37
self.
model
.setPreferableBackend(self.
backend_id
)
38
39
def
setTarget
(self, targetId):
40
self.
target_id
= targetId
41
self.
model
.setPreferableTarget(self.
target_id
)
42
43
def
_preprocess
(self, image):
44
input_blob = (image / 255.0 - self.
mean
) / self.
std
45
input_blob = input_blob.transpose(2, 0, 1)
46
input_blob = input_blob[np.newaxis, :, :, :]
47
input_blob = input_blob.astype(np.float32)
48
return
input_blob
49
50
def
infer
(self, image):
51
# Preprocess
52
input_blob = self.
_preprocess
(image)
53
54
# Forward
55
self.
model
.setInput(input_blob, self.
input_names
)
56
output_blob = self.
model
.forward(self.
output_names
)
57
58
# Postprocess
59
results = self.
_postprocess
(output_blob)
60
61
return
results
62
63
def
_postprocess
(self, output_blob):
64
predicted_labels = []
65
for
o
in
output_blob:
66
class_id = np.argmax(o)
67
predicted_labels.append(self.
labels
[class_id])
68
return
predicted_labels
69
mobilenet_v1.MobileNetV1
Definition
mobilenet_v1.py:4
mobilenet_v1.MobileNetV1._preprocess
_preprocess(self, image)
Definition
mobilenet_v1.py:43
mobilenet_v1.MobileNetV1.setBackend
setBackend(self, backendId)
Definition
mobilenet_v1.py:35
mobilenet_v1.MobileNetV1.mean
mean
Definition
mobilenet_v1.py:18
mobilenet_v1.MobileNetV1.name
name(self)
Definition
mobilenet_v1.py:32
mobilenet_v1.MobileNetV1._postprocess
_postprocess(self, output_blob)
Definition
mobilenet_v1.py:63
mobilenet_v1.MobileNetV1.input_names
input_names
Definition
mobilenet_v1.py:15
mobilenet_v1.MobileNetV1.label_path
label_path
Definition
mobilenet_v1.py:7
mobilenet_v1.MobileNetV1.input_size
input_size
Definition
mobilenet_v1.py:17
mobilenet_v1.MobileNetV1.__init__
__init__(self, modelPath, labelPath, backendId=0, targetId=0)
Definition
mobilenet_v1.py:5
mobilenet_v1.MobileNetV1.model
model
Definition
mobilenet_v1.py:11
mobilenet_v1.MobileNetV1.infer
infer(self, image)
Definition
mobilenet_v1.py:50
mobilenet_v1.MobileNetV1.model_path
model_path
Definition
mobilenet_v1.py:6
mobilenet_v1.MobileNetV1.labels
labels
Definition
mobilenet_v1.py:22
mobilenet_v1.MobileNetV1.setTarget
setTarget(self, targetId)
Definition
mobilenet_v1.py:39
mobilenet_v1.MobileNetV1._load_labels
_load_labels(self)
Definition
mobilenet_v1.py:24
mobilenet_v1.MobileNetV1.target_id
target_id
Definition
mobilenet_v1.py:9
mobilenet_v1.MobileNetV1.backend_id
backend_id
Definition
mobilenet_v1.py:8
mobilenet_v1.MobileNetV1.output_names
output_names
Definition
mobilenet_v1.py:16
mobilenet_v1.MobileNetV1.std
std
Definition
mobilenet_v1.py:19
opencv_zoo
models
image_classification_mobilenet
mobilenet_v1.py
Please help us improve this page: Edit it on
GitHub
or
email us your edits and suggestions.
Generated by
1.9.8