Exemple #1
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--input',
                        '-i',
                        required=True,
                        help='Input image file.')
    parser.add_argument('--threshold',
                        '-t',
                        type=float,
                        default=0.1,
                        help='Classification probability threshold.')
    parser.add_argument('--top_k',
                        '-n',
                        type=int,
                        default=5,
                        help='Max number of returned classes.')
    parser.add_argument('--sparse',
                        '-s',
                        action='store_true',
                        default=False,
                        help='Use sparse tensors.')
    parser.add_argument('--model',
                        '-m',
                        choices=('squeezenet', 'mobilenet'),
                        default='mobilenet',
                        help='Model to run.')
    args = parser.parse_args()

    # There are two models available for image classification task:
    # 1) MobileNet based (image_classification.MOBILENET), which has 59.9% top-1
    # accuracy on ImageNet;
    # 2) SqueezeNet based (image_classification.SQUEEZENET), which has 45.3% top-1
    # accuracy on ImageNet;
    model_type = {
        'squeezenet': image_classification.SQUEEZENET,
        'mobilenet': image_classification.MOBILENET
    }[args.model]

    with ImageInference(image_classification.model(model_type)) as inference:
        image = Image.open(args.input)

        if args.sparse:
            configs = image_classification.sparse_configs(
                top_k=args.top_k,
                threshold=args.threshold,
                model_type=model_type)
            result = inference.run(image, sparse_configs=configs)
            classes = image_classification.get_classes_sparse(result)
        else:
            result = inference.run(image)
            classes = image_classification.get_classes(
                result, top_k=args.top_k, threshold=args.threshold)

        for i, (label, score) in enumerate(classes):
            print('Result %d: %s (prob=%f)' % (i, label, score))
Exemple #2
0
 def test_image_classification_mobilenet_sparse(self):
     sparse_configs = image_classification.sparse_configs(
         threshold=CLASSIFICATION_THRESHOLD,
         model_type=image_classification.MOBILENET)
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.MOBILENET),
         image_classification.get_classes_sparse,
         sparse_configs=sparse_configs)
     self.assertLatency(avg_bonnet, 42.0)
     self.assertLatency(avg_end_to_end, 56.0)
Exemple #3
0
 def test_image_classification_squeezenet_sparse(self):
     sparse_configs = image_classification.sparse_configs(
         threshold=CLASSIFICATION_THRESHOLD,
         model_type=image_classification.SQUEEZENET)
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.SQUEEZENET),
         partial(image_classification.get_classes_sparse),
         sparse_configs=sparse_configs)
     self.assertLatency(avg_bonnet, 183.0)
     self.assertLatency(avg_end_to_end, 202.0)
 def test_image_classification_mobilenet_sparse(self):
     sparse_configs = image_classification.sparse_configs(
         threshold=CLASSIFICATION_THRESHOLD,
         model_type=image_classification.MOBILENET)
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.MOBILENET),
         image_classification.get_classes_sparse,
         sparse_configs=sparse_configs)
     self.assertLatency(avg_bonnet, 42.0)
     self.assertLatency(avg_end_to_end, 56.0)
 def test_image_classification_squeezenet_sparse(self):
     sparse_configs = image_classification.sparse_configs(
         threshold=CLASSIFICATION_THRESHOLD,
         model_type=image_classification.SQUEEZENET)
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.SQUEEZENET),
         partial(image_classification.get_classes_sparse),
         sparse_configs=sparse_configs)
     self.assertLatency(avg_bonnet, 183.0)
     self.assertLatency(avg_end_to_end, 202.0)
    def test_classification(self):
        with TestImage(self.image_file) as image:
            with ImageInference(ic.model(self.model_type)) as inference:
                if self.sparse:
                    sparse_configs = ic.sparse_configs(top_k=self.TOP_K,
                                                       threshold=self.THRESHOLD,
                                                       model_type=self.model_type)
                    result = inference.run(image, sparse_configs=sparse_configs)
                    classes = ic.get_classes_sparse(result)
                else:
                    result = inference.run(image)
                    classes = ic.get_classes(result, top_k=self.TOP_K, threshold=self.THRESHOLD)

                self.check(classes)
def main():
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--input', '-i', required=True,
                        help='Input image file.')
    parser.add_argument('--threshold', '-t', type=float, default=0.1,
                        help='Classification probability threshold.')
    parser.add_argument('--top_k', '-n', type=int, default=5,
                        help='Max number of returned classes.')
    parser.add_argument('--sparse', '-s', action='store_true', default=False,
                        help='Use sparse tensors.')
    parser.add_argument('--model', '-m', choices=('squeezenet', 'mobilenet'), default='mobilenet',
                        help='Model to run.')
    args = parser.parse_args()

    # There are two models available for image classification task:
    # 1) MobileNet based (image_classification.MOBILENET), which has 59.9% top-1
    # accuracy on ImageNet;
    # 2) SqueezeNet based (image_classification.SQUEEZENET), which has 45.3% top-1
    # accuracy on ImageNet;
    model_type = {'squeezenet': image_classification.SQUEEZENET,
                  'mobilenet': image_classification.MOBILENET}[args.model]

    with ImageInference(image_classification.model(model_type)) as inference:
        image = Image.open(args.input)

        if args.sparse:
            configs = image_classification.sparse_configs(top_k=args.top_k,
                                                          threshold=args.threshold,
                                                          model_type=model_type)
            result = inference.run(image, sparse_configs=configs)
            classes = image_classification.get_classes_sparse(result)
        else:
            result = inference.run(image)
            classes = image_classification.get_classes(result,
                                                       top_k=args.top_k,
                                                       threshold=args.threshold)

        for i, (label, score) in enumerate(classes):
            print('Result %d: %s (prob=%f)' % (i, label, score))