Ejemplo n.º 1
0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""API for Dish Detection."""

from collections import namedtuple

from aiy.vision.inference import ModelDescriptor
from aiy.vision.models import utils


_COMPUTE_GRAPH_NAME = 'dish_detection.binaryproto'
_CLASSES = utils.load_labels('mobilenet_v1_192res_1.0_seefood_labels.txt')

# sorted_scores: sorted list of (label, score) tuples.
# bounding_box: (x, y, width, height) tuple.
Dish = namedtuple('Dish', ('sorted_scores', 'bounding_box'))


def model():
    return ModelDescriptor(
        name='DishDetection',
        input_shape=(1, 0, 0, 3),
        input_normalizer=(0, 0),
        compute_graph=utils.load_compute_graph(_COMPUTE_GRAPH_NAME))


def _get_sorted_scores(scores, top_k, threshold):
Ejemplo n.º 2
0
 def labels(self):
     if not hasattr(self, '_labels'):
         setattr(self, '_labels', utils.load_labels(self.labels_file))
     return self._labels
import argparse
from PIL import Image

from aiy.vision.inference import ModelDescriptor
from aiy.vision.inference import ImageInference
from aiy.vision.models import utils

# Costants
_COMPUTE_GRAPH_NAME = 'frozengraph_datagen_v1.binaryproto'
_CLASSES = utils.load_labels('cifar10_labels.txt')


# Define structure of the model
def model():
    return ModelDescriptor(
        name='cifar10_classification',
        input_shape=(1, 32, 32, 3),
        input_normalizer=(127.5, 127.5),
        compute_graph=utils.load_compute_graph(_COMPUTE_GRAPH_NAME))


# Return the predictions
def _get_probs(result):
    assert len(result.tensors) == 1
    tensor = result.tensors['result/Softmax']
    assert utils.shape_tuple(tensor.shape) == (1, 1, 1, len(_CLASSES))
    return tuple(tensor.data)


# Return the most probable classes
def get_classes(result, top_k=None, threshold=0.0):
Ejemplo n.º 4
0
PLANTS = 'inaturalist_plants'
INSECTS = 'inaturalist_insects'
BIRDS = 'inaturalist_birds'


class Model(
        namedtuple('Model', ('labels', 'compute_graph_file', 'input_shape',
                             'input_normalizer', 'output_name'))):
    def compute_graph(self):
        return utils.load_compute_graph(self.compute_graph_file)


_MODELS = {
    PLANTS:
    Model(labels=utils.load_labels(
        'mobilenet_v2_192res_1.0_inat_plant_labels.txt'),
          compute_graph_file='mobilenet_v2_192res_1.0_inat_plant.binaryproto',
          input_shape=(1, 192, 192, 3),
          input_normalizer=(128.0, 128.0),
          output_name='prediction'),
    INSECTS:
    Model(labels=utils.load_labels(
        'mobilenet_v2_192res_1.0_inat_insect_labels.txt'),
          compute_graph_file='mobilenet_v2_192res_1.0_inat_insect.binaryproto',
          input_shape=(1, 192, 192, 3),
          input_normalizer=(128.0, 128.0),
          output_name='prediction'),
    BIRDS:
    Model(labels=utils.load_labels(
        'mobilenet_v2_192res_1.0_inat_bird_labels.txt'),
          compute_graph_file='mobilenet_v2_192res_1.0_inat_bird.binaryproto',
Ejemplo n.º 5
0
image = Image.open(args.input)
width, height = image.size

model = ModelDescriptor(name=args.model_name,
                        input_shape=(1, args.input_size, args.input_size, 3),
                        input_normalizer=(128, 128),
                        compute_graph=utils.load_compute_graph(
                            args.model_path))

inference = ImageInference(model)
if inference:
    starttime = datetime.now()
    result = inference.run(image)
    deltatime = datetime.now() - starttime
    print(
        str(deltatime.seconds) + "s " + str(deltatime.microseconds / 1000) +
        "ms")

    assert len(result.tensors) == 1
    tensor = result.tensors[args.output_key]

    probs = tuple(tensor.data)
    pairs = [pair for pair in enumerate(probs) if pair[1] > 0.1]
    pairs = sorted(pairs, key=lambda pair: pair[1], reverse=True)
    pairs = pairs[0:5]
    _CLASSES = utils.load_labels(args.label_path)
    classes = [('/'.join(_CLASSES[index]), prob) for index, prob in pairs]

    for i, (label, score) in enumerate(classes):
        print('Result %d: %s (prob=%f)' % (i, label, score))
Ejemplo n.º 6
0
# MobileNet based model has 59.9% top-1 accuracy on ImageNet.
# SqueezeNet based model has 45.3% top-1 accuracy on ImageNet.
MOBILENET = 'image_classification_mobilenet'
SQUEEZENET = 'image_classification_squeezenet'

_COMPUTE_GRAPH_NAME_MAP = {
    MOBILENET: 'mobilenet_v1_160res_0.5_imagenet.binaryproto',
    SQUEEZENET: 'squeezenet_160res_5x5_0.75.binaryproto',
}

_OUTPUT_TENSOR_NAME_MAP = {
    MOBILENET: 'MobilenetV1/Predictions/Softmax',
    SQUEEZENET: 'Prediction',
}

_CLASSES = utils.load_labels('mobilenet_v1_160res_0.5_imagenet_labels.txt')


def sparse_configs(top_k=len(_CLASSES), threshold=0.0, model_type=MOBILENET):
    name = _OUTPUT_TENSOR_NAME_MAP[model_type]
    return {
        name:
        ThresholdingConfig(logical_shape=[len(_CLASSES)],
                           threshold=threshold,
                           top_k=top_k,
                           to_ignore=[])
    }


def model(model_type=MOBILENET):
    return ModelDescriptor(name=model_type,
 def labels(self):
     if not hasattr(self, '_labels'):
       setattr(self, '_labels', utils.load_labels(self.labels_file))
     return self._labels