mobilenet_v2.py 2.5 KB
Newer Older
F
fytao 已提交
1 2 3 4
import numpy as np
import cv2 as cv

class MobileNetV2:
5
    def __init__(self, modelPath, labelPath=None, topK=1, backendId=0, targetId=0):
F
fytao 已提交
6 7
        self.model_path = modelPath
        self.label_path = labelPath
8 9
        assert topK >= 1
        self.top_k = topK
F
fytao 已提交
10 11 12 13 14 15 16 17 18 19
        self.backend_id = backendId
        self.target_id = targetId

        self.model = cv.dnn.readNet(self.model_path)
        self.model.setPreferableBackend(self.backend_id)
        self.model.setPreferableTarget(self.target_id)

        self.input_names = ''
        self.output_names = ''
        self.input_size = [224, 224]
Y
Yuantao Feng 已提交
20 21
        self.mean=[0.485, 0.456, 0.406]
        self.std=[0.229, 0.224, 0.225]
F
fytao 已提交
22 23

        # load labels
W
Wanli 已提交
24
        self._labels = self._load_labels()
F
fytao 已提交
25 26 27

    def _load_labels(self):
        labels = []
28 29 30 31
        if self.label_path is not None:
            with open(self.label_path, 'r') as f:
                for line in f:
                    labels.append(line.strip())
F
fytao 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        return labels

    @property
    def name(self):
        return self.__class__.__name__

    def setBackend(self, backendId):
        self.backend_id = backendId
        self.model.setPreferableBackend(self.backend_id)

    def setTarget(self, targetId):
        self.target_id = targetId
        self.model.setPreferableTarget(self.target_id)

    def _preprocess(self, image):
Y
Yuantao Feng 已提交
47 48 49 50 51
        input_blob = (image / 255.0 - self.mean) / self.std
        input_blob = input_blob.transpose(2, 0, 1)
        input_blob = input_blob[np.newaxis, :, :, :]
        input_blob = input_blob.astype(np.float32)
        return input_blob
F
fytao 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    def infer(self, image):
        # Preprocess
        input_blob = self._preprocess(image)

        # Forward
        self.model.setInput(input_blob, self.input_names)
        output_blob = self.model.forward(self.output_names)

        # Postprocess
        results = self._postprocess(output_blob)

        return results

    def _postprocess(self, output_blob):
67
        batched_class_id_list = []
F
fytao 已提交
68
        for o in output_blob:
69 70
            class_id_list = o.argsort()[::-1][:self.top_k]
            batched_class_id_list.append(class_id_list)
W
Wanli 已提交
71
        if len(self._labels) > 0:
72 73 74 75 76 77 78 79 80
            batched_predicted_labels = []
            for class_id_list in batched_class_id_list:
                predicted_labels = []
                for class_id in class_id_list:
                    predicted_labels.append(self._labels[class_id])
                batched_predicted_labels.append(predicted_labels)
            return batched_predicted_labels
        else:
            return batched_class_id_list
F
fytao 已提交
81