Beispiel #1
0
    def __reformat_and_normalize(img: np.array,
                                 mean=None,
                                 std=None) -> np.array:
        """

        Reformats the image to match the input dimensions (224, 224, 3) of the standard networks
        downloaded from gluon model zoo, see ImageFeaturizer

        :param img: image as numpy array
        :param mean: mean of the image for each color channel
        :param std: standard deviation of each color channel
        :return: reformatted and normalized image


        """

        if not mean:
            mean = [0.485, 0.456, 0.406]

        if not std:
            std = [0.229, 0.224, 0.225]

        if img.shape != (224, 224, 3):
            img = pad_to_square(img)
            img = mx.image.imresize(img, 224, 224)
        img = img.astype(float) / 255
        img = mx.image.color_normalize(img,
                                       mean=mx.nd.array(mean).astype(float),
                                       std=mx.nd.array(std).astype(float))
        img = img.transpose((2, 0, 1))
        img = img.expand_dims(axis=0)

        return img.asnumpy()