예제 #1
0
    def predict(self,
                x,
                batch_per_thread=1,
                distributed=True,
                mini_batch=False):
        """
        Use a model to do prediction.
        """
        if isinstance(x, ImageSet):
            results = callZooFunc(self.bigdl_type, "zooPredict", self.value, x,
                                  batch_per_thread)
            return ImageSet(results)

        if isinstance(x, TFImageDataset):
            results = callZooFunc(self.bigdl_type, "zooPredict", self.value,
                                  x.get_prediction_data(), x.batch_per_thread)
            return ImageSet(results)

        if isinstance(x, MapDataset):
            raise ValueError("MapDataset is not supported in TFNet")

        if isinstance(x, TFDataset):
            results = callZooFunc(self.bigdl_type, "zooPredict", self.value,
                                  x.get_prediction_data())
            return results.map(lambda result: Layer.convert_output(result))

        if mini_batch:
            results = callZooFunc(self.bigdl_type, "zooPredict", self.value, x)
            return results.map(lambda result: Layer.convert_output(result))

        if distributed:
            if isinstance(x, np.ndarray):
                data_rdd = to_sample_rdd(x, np.zeros([x.shape[0]]),
                                         getOrCreateSparkContext())
            elif isinstance(x, RDD):
                data_rdd = x
            else:
                raise TypeError("Unsupported prediction data type: %s" %
                                type(x))
            results = callZooFunc(self.bigdl_type, "zooPredict", self.value,
                                  data_rdd, batch_per_thread)
            return results.map(lambda result: Layer.convert_output(result))
        else:
            start_idx = 0
            results = []
            while start_idx < len(x):
                end_idx = min(start_idx + batch_per_thread, len(x))
                results.append(self.forward(x[start_idx:end_idx]))
                start_idx += batch_per_thread

            return np.concatenate(results, axis=0)
예제 #2
0
 def predict(self, x, batch_per_thread=1, distributed=True):
     """
     Use a model to do prediction.
     """
     if isinstance(x, ImageSet):
         results = callBigDlFunc(self.bigdl_type, "zooPredict",
                                 self.value,
                                 x,
                                 batch_per_thread)
         return ImageSet(results)
     if distributed:
         if isinstance(x, np.ndarray):
             data_rdd = to_sample_rdd(x, np.zeros([x.shape[0]]), getOrCreateSparkContext())
         elif isinstance(x, RDD):
             data_rdd = x
         else:
             raise TypeError("Unsupported prediction data type: %s" % type(x))
         results = callBigDlFunc(self.bigdl_type, "zooPredict",
                                 self.value,
                                 data_rdd,
                                 batch_per_thread)
         return results.map(lambda result: Layer.convert_output(result))
     else:
         if isinstance(x, np.ndarray) or isinstance(x, list):
             results = callBigDlFunc(self.bigdl_type, "zooPredict",
                                     self.value,
                                     self._to_jtensors(x),
                                     batch_per_thread)
             return [Layer.convert_output(result) for result in results]
         else:
             raise TypeError("Unsupported prediction data type: %s" % type(x))
예제 #3
0
 def __call__(self, input, bigdl_type="float"):
     """
     transform ImageSet
     """
     # move the import here to break circular import
     if "zoo.feature.image.imageset.ImageSet" not in sys.modules:
         from zoo.feature.image import ImageSet
     if type(input) is ImageSet:
         jset = callBigDlFunc(bigdl_type, "transformImageSet", self.value,
                              input)
         return ImageSet(jvalue=jset)
예제 #4
0
 def get_raw_image_set(self, with_label):
     resource_path = os.path.join(
         os.path.split(__file__)[0], "../../resources")
     if with_label:
         image_folder = os.path.join(resource_path, "cat_dog")
     else:
         image_folder = os.path.join(resource_path, "cat_dog/*")
     image_set = ImageSet.read(image_folder,
                               with_label=with_label,
                               sc=get_spark_context(),
                               one_based_label=False)
     return image_set
예제 #5
0
 def __call__(self, input):
     """
     Transform ImageSet or TextSet.
     """
     # move the import here to break circular import
     if "zoo.feature.image.imageset.ImageSet" not in sys.modules:
         from zoo.feature.image import ImageSet
     if "zoo.feature.text.text_set.TextSet" not in sys.modules:
         from zoo.feature.text import TextSet
     # if type(input) is ImageSet:
     if isinstance(input, ImageSet):
         jset = callBigDlFunc(self.bigdl_type, "transformImageSet", self.value, input)
         return ImageSet(jvalue=jset)
     elif isinstance(input, TextSet):
         jset = callBigDlFunc(self.bigdl_type, "transformTextSet", self.value, input)
         return TextSet(jvalue=jset)
예제 #6
0
    def predict(self, x, batch_per_thread=4, distributed=True):
        """
        Use a model to do prediction.

        # Arguments
        x: Prediction data. A Numpy array or RDD of Sample or ImageSet.
        batch_per_thread:
          The default value is 4.
          When distributed is True,the total batch size is batch_per_thread * rdd.getNumPartitions.
          When distributed is False the total batch size is batch_per_thread * numOfCores.
        distributed: Boolean. Whether to do prediction in distributed mode or local mode.
                     Default is True. In local mode, x must be a Numpy array.
        """
        if isinstance(x, ImageSet) or isinstance(x, TextSet):
            results = callBigDlFunc(self.bigdl_type, "zooPredict",
                                    self.value,
                                    x,
                                    batch_per_thread)
            return ImageSet(results) if isinstance(x, ImageSet) else TextSet(results)
        if distributed:
            if isinstance(x, np.ndarray):
                data_rdd = to_sample_rdd(x, np.zeros([x.shape[0]]))
            elif isinstance(x, RDD):
                data_rdd = x
            else:
                raise TypeError("Unsupported prediction data type: %s" % type(x))
            results = callBigDlFunc(self.bigdl_type, "zooPredict",
                                    self.value,
                                    data_rdd,
                                    batch_per_thread)
            return results.map(lambda result: Layer.convert_output(result))
        else:
            if isinstance(x, np.ndarray) or isinstance(x, list):
                results = callBigDlFunc(self.bigdl_type, "zooPredict",
                                        self.value,
                                        self._to_jtensors(x),
                                        batch_per_thread)
                return [Layer.convert_output(result) for result in results]
            else:
                raise TypeError("Unsupported prediction data type: %s" % type(x))
예제 #7
0
    def predict(self, x, batch_size=-1, distributed=True):
        """
        Use a model to do prediction.

        # Arguments
        x: Prediction data. A Numpy array or RDD of Sample or ImageSet.
        batch_size: When distributed is True, the default value is 4 * rdd.getNumPartitions.
                    When distributed is False the default value is 4 * numOfCores.
                    If you want to tune the batch_size,
                    you should make sure its value can be divisible by
                    rdd.getNumPartitions(distributed=True) or numOfCores(distributed=False)
        distributed: Boolean. Whether to do prediction in distributed mode or local mode.
                     Default is True. In local mode, x must be a Numpy array.
        """
        if isinstance(x, ImageSet):
            results = callBigDlFunc(self.bigdl_type, "zooPredict", self.value,
                                    x, batch_size)
            return ImageSet(results)
        if distributed:
            if isinstance(x, np.ndarray):
                data_rdd = to_sample_rdd(x, np.zeros([x.shape[0]]))
            elif isinstance(x, RDD):
                data_rdd = x
            else:
                raise TypeError("Unsupported prediction data type: %s" %
                                type(x))
            results = callBigDlFunc(self.bigdl_type, "zooPredict", self.value,
                                    data_rdd, batch_size)
            return results.map(lambda result: Layer.convert_output(result))
        else:
            if isinstance(x, np.ndarray) or isinstance(x, list):
                results = callBigDlFunc(self.bigdl_type,
                                        "zooPredict", self.value,
                                        self._to_jtensors(x), batch_size)
                return [Layer.convert_output(result) for result in results]
            else:
                raise TypeError("Unsupported prediction data type: %s" %
                                type(x))
예제 #8
0
def predict(model_path, image_path, top_n):
    sc = init_nncontext(
        "Image classification inference example using int8 quantized model")
    images = ImageSet.read(image_path, sc, image_codec=1)
    model = ImageClassifier.load_model(model_path)
    output = model.predict_image_set(images)
    label_map = model.get_config().label_map()

    # list of images composing uri and results in tuple format
    predicts = output.get_predict().collect()

    sequential = Sequential()
    sequential.add(Activation("softmax", input_shape=predicts[0][1][0].shape))
    for pre in predicts:
        (uri, probs) = pre
        out = sequential.forward(probs[0])
        sortedProbs = [(prob, index) for index, prob in enumerate(out)]
        sortedProbs.sort()
        print("Image : %s, top %d prediction result" % (uri, top_n))
        for i in range(top_n):
            print(
                "\t%s, %f" %
                (label_map[sortedProbs[999 - i][1]], sortedProbs[999 - i][0]))
예제 #9
0
if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("--image",
                      type=str,
                      dest="img_path",
                      help="The path where the images are stored, "
                      "can be either a folder or an image path")
    parser.add_option("--model",
                      type=str,
                      dest="model_path",
                      help="Path to the TensorFlow model file")

    (options, args) = parser.parse_args(sys.argv)

    sc = init_nncontext("OpenVINO Object Detection Inference Example")
    images = ImageSet.read(options.img_path,
                           sc,
                           resize_height=600,
                           resize_width=600).get_image().collect()
    input_data = np.concatenate(
        [image.reshape((1, 1) + image.shape) for image in images], axis=0)
    model_path = options.model_path
    model = InferenceModel()
    model.load_openvino(model_path,
                        weight_path=model_path[:model_path.rindex(".")] +
                        ".bin")
    predictions = model.predict(input_data)
    # Print the detection result of the first image.
    print(predictions[0])