コード例 #1
0
 def get_probability(self, img_list):
     batch_size = 32
     self.set_models(inference=True)
     if isinstance(img_list, (list, str)):
         if isinstance(img_list, (tuple, list)):
             if len(img_list) >= 32:
                 test_dist = ImageDistributor(img_list)
                 results = []
                 bar = tqdm(range(int(np.ceil(len(test_dist) /
                                              batch_size))))
                 for i, (x_img_list, _) in enumerate(
                         test_dist.batch(batch_size, shuffle=False)):
                     img_array = np.vstack([
                         load_img(path, self.imsize)[None]
                         for path in x_img_list
                     ])
                     img_array = self.preprocess(img_array)
                     results.extend(
                         np.argmax(rm.softmax(self(img_array)).as_ndarray(),
                                   axis=1))
                     bar.update(1)
                 return results
             img_array = np.vstack(
                 [load_img(path, self.imsize)[None] for path in img_list])
             img_array = self.preprocess(img_array)
         else:
             img_array = load_img(img_list, self.imsize)[None]
             img_array = self.preprocess(img_array)
             return np.argmax(rm.softmax(self(img_array)).as_ndarray(),
                              axis=1)[0]
     else:
         img_array = img_list
     return rm.softmax(self(img_array)).as_ndarray()
コード例 #2
0
ファイル: inception.py プロジェクト: DaikiOnodera/ReNomIMG
 def predict(self, img_list):
     self.set_models(inference=True)
     if isinstance(img_list, (list, str)):
         if isinstance(img_list, (tuple, list)):
             img_array = np.vstack([load_img(path, self.imsize)[None] for path in img_list])
             img_array = self.preprocess(img_array)
         else:
             img_array = load_img(img_list, self.imsize)[None]
             img_array = self.preprocess(img_array)
             return np.argmax(rm.softmax(self(img_array)[1]).as_ndarray(), axis=1)[0]
     else:
         img_array = img_list
     return np.argmax(rm.softmax(self(img_array)[1]).as_ndarray(), axis=1)
コード例 #3
0
    def predict(self, img_list):
        """Perform prediction.
        Argument can be an image array, image path list or a image path.

        Args:
            img_list(ndarray, list, string): Image array, image path list or image path.

        Return:
            (list): List of class of each image.

        """
        batch_size = 32
        self.set_models(inference=True)
        if isinstance(img_list, (list, str)):
            if isinstance(img_list, (tuple, list)):
                if len(img_list) >= 32:
                    test_dist = ImageDistributor(img_list)
                    results = []
                    bar = tqdm(range(int(np.ceil(len(test_dist) /
                                                 batch_size))))
                    for i, (x_img_list, _) in enumerate(
                            test_dist.batch(batch_size, shuffle=False)):
                        img_array = np.vstack([
                            load_img(path, self.imsize)[None]
                            for path in x_img_list
                        ])
                        img_array = self.preprocess(img_array)
                        results.extend(
                            np.argmax(rm.softmax(self(img_array)).as_ndarray(),
                                      axis=1))
                        bar.update(1)
                    return results
                img_array = np.vstack(
                    [load_img(path, self.imsize)[None] for path in img_list])
                img_array = self.preprocess(img_array)
            else:
                img_array = load_img(img_list, self.imsize)[None]
                img_array = self.preprocess(img_array)
                return np.argmax(rm.softmax(self(img_array)).as_ndarray(),
                                 axis=1)[0]
        else:
            img_array = img_list
        return np.argmax(rm.softmax(self(img_array)).as_ndarray(), axis=1)
コード例 #4
0
ファイル: __init__.py プロジェクト: DaikiOnodera/ReNomIMG
    def predict(self, img_list):
        """
        Returns:
            (Numpy.array or list): If only an image or a path is given, an array whose shape is **(width, height)** is returned.
            If multiple images or paths are given, then a list in which there are arrays whose shape is **(width, height)** is returned.
        """

        batch_size = 32
        self.set_models(inference=True)
        if isinstance(img_list, (list, str)):
            if isinstance(img_list, (tuple, list)):
                if len(img_list) >= 32:
                    test_dist = ImageDistributor(img_list)
                    results = []
                    bar = tqdm()
                    bar.total = int(np.ceil(len(test_dist) / batch_size))
                    for i, (x_img_list, _) in enumerate(
                            test_dist.batch(batch_size, shuffle=False)):
                        img_array = np.vstack([
                            load_img(path, self.imsize)[None]
                            for path in x_img_list
                        ])
                        img_array = self.preprocess(img_array)
                        results.extend(
                            np.argmax(rm.softmax(self(img_array)).as_ndarray(),
                                      axis=1))
                        bar.update(1)
                    return results
                img_array = np.vstack(
                    [load_img(path, self.imsize)[None] for path in img_list])
                img_array = self.preprocess(img_array)
            else:
                img_array = load_img(img_list, self.imsize)[None]
                img_array = self.preprocess(img_array)
                return np.argmax(rm.softmax(self(img_array)).as_ndarray(),
                                 axis=1)[0]
        else:
            img_array = img_list
        return np.argmax(rm.softmax(self(img_array)).as_ndarray(), axis=1)
コード例 #5
0
ファイル: yolo_v1.py プロジェクト: DaikiOnodera/ReNomIMG
    def predict(self, img_list, score_threshold=0.3, nms_threshold=0.4):
        """
        This method accepts either ndarray and list of image path.

        Args:
            img_list (string, list, ndarray): Path to an image, list of path or ndarray.
            score_threshold (float): The threshold for confidence score.
                                     Predicted boxes which have lower confidence score than the threshold are discarderd.
                                     Defaults to 0.3
            nms_threshold (float): The threshold for non maximum supression. Defaults to 0.4

        Return:
            (list): List of predicted bbox, score and class of each image.
            The format of return value is bellow. Box coordinates and size will be returned as
            ratio to the original image size. Therefore the range of 'box' is [0 ~ 1].

        .. code-block :: python

            # An example of return value.
            [
                [ # Prediction of first image.
                    {'box': [x, y, w, h], 'score':(float), 'class':(int), 'name':(str)},
                    {'box': [x, y, w, h], 'score':(float), 'class':(int), 'name':(str)},
                    ...
                ],
                [ # Prediction of second image.
                    {'box': [x, y, w, h], 'score':(float), 'class':(int), 'name':(str)},
                    {'box': [x, y, w, h], 'score':(float), 'class':(int), 'name':(str)},
                    ...
                ],
                ...
            ]

        Example:
            >>>
            >>> model.predict(['img01.jpg', 'img02.jpg']])
            [[{'box': [0.21, 0.44, 0.11, 0.32], 'score':0.823, 'class':1, 'name':'dog'}],
             [{'box': [0.87, 0.38, 0.84, 0.22], 'score':0.423, 'class':0, 'name':'cat'}]]

        Note:
            Box coordinate and size will be returned as ratio to the original image size.
            Therefore the range of 'box' is [0 ~ 1].

        """
        batch_size = 32
        self.set_models(inference=True)
        if isinstance(img_list, (list, str)):
            if isinstance(img_list, (tuple, list)):
                if len(img_list) >= 32:
                    test_dist = ImageDistributor(img_list)
                    results = []
                    bar = tqdm()
                    bar.total = int(np.ceil(len(test_dist) / batch_size))
                    for i, (x_img_list, _) in enumerate(
                            test_dist.batch(batch_size, shuffle=False)):
                        img_array = np.vstack([
                            load_img(path, self.imsize)[None]
                            for path in x_img_list
                        ])
                        img_array = self.preprocess(img_array)
                        results.extend(
                            self.get_bbox(
                                self(img_array).as_ndarray(), score_threshold,
                                nms_threshold))
                        bar.update(1)
                    return results
                img_array = np.vstack(
                    [load_img(path, self.imsize)[None] for path in img_list])
                img_array = self.preprocess(img_array)
            else:
                img_array = load_img(img_list, self.imsize)[None]
                img_array = self.preprocess(img_array)
                return self.get_bbox(
                    self(img_array).as_ndarray(), score_threshold,
                    nms_threshold)[0]
        else:
            img_array = img_list
        return self.get_bbox(
            self(img_array).as_ndarray(), score_threshold, nms_threshold)