Exemple #1
0
def do_inference(graph: mvnc2.Graph,
                 input_fifo: mvnc2.Fifo,
                 output_fifo: mvnc2.Fifo,
                 image_filename: str,
                 number_results: int = 5) -> (List[str], List[numpy.float16]):
    """ executes one inference which will determine the top classifications for an image file.

    Parameters
    ----------
    graph : Graph
        The graph to use for the inference.  This should be initialize prior to calling.
    input_fifo : mvnc2.Fifo
        The input FIFO queue for network.
    output_fifo : mvnc2.Fifo
        The output FIFO queue for network.
    image_filename : string
        The filename (full or relative path) to use as the input for the inference.
    number_results : int
        The number of results to return, defaults to 5

    Returns
    -------
    labels : List[str]
        The top labels for the inference.  labels[i] corresponds to probabilities[i]
    probabilities: List[numpy.float16]
        The top probabilities for the inference. probabilities[i] corresponds to labels[i]
    """

    # Text labels for each of the possible classfications
    labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

    # Load image from disk and preprocess it to prepare it for the network assuming we are reading a .jpg or .png
    # color image so need to convert it single channel gray scale image for mnist network.
    # Then resize the image to the size of image the network was trained with.
    # Next convert image to floating point format and normalize so each pixel is a value between 0.0 and 1.0
    image_for_inference = cv2.imread(image_filename)
    image_for_inference = cv2.cvtColor(image_for_inference, cv2.COLOR_BGR2GRAY)
    image_for_inference = cv2.resize(image_for_inference,
                                     NETWORK_IMAGE_DIMENSIONS)
    image_for_inference = image_for_inference.astype(numpy.float32)
    image_for_inference[:] = ((image_for_inference[:]) * (1.0 / 255.0))

    # Start the inference by sending to the device/graph
    graph.queue_inference_with_fifo_elem(
        input_fifo, output_fifo, image_for_inference.astype(numpy.float16),
        None)

    # Get the result from the device/graph.
    output, userobj = output_fifo.read_elem()

    # Sort indices in order of highest probabilities
    five_highest_indices = (-output).argsort()[:number_results]

    # Get the labels and probabilities for the top results from the inference
    inference_labels = []
    inference_probabilities = []

    for index in range(0, number_results):
        inference_probabilities.append(str(
            output[five_highest_indices[index]]))
        inference_labels.append(labels[five_highest_indices[index]])

    return inference_labels, inference_probabilities
def do_inference(
    graph: mvnc2.Graph,
    input_fifo: mvnc2.Fifo,
    output_fifo: mvnc2.Fifo,
    image_filename: str,
    number_results: int = 5
) -> (List[numpy.int], List[str], List[numpy.float16]):
    """ executes one inference which will determine the top classifications for an image file.

    Parameters
    ----------
    graph : Graph
        The graph to use for the inference.  This should be initialize prior to calling.
    input_fifo : mvnc2.Fifo
        The input FIFO queue for network.
    output_fifo : mvnc2.Fifo
        The output FIFO queue for network.
    image_filename : string
        The filename (full or relative path) to use as the input for the inference.
    number_results : int
        The number of results to return, defaults to 5

    Returns
    -------
    top_inds: List[numpy.int]
        The top category numbers for the inference.
    categories: List[str]
        The top categories for the inference. categories[i] corresponds to top_inds[i]
    probabilities: List[numpy.float16]
        The top probabilities for the inference. probabilities[i] corresponds to categories[i] 
    """

    #Load preprocessing data
    mean = 128
    std = 1.0 / 128.0

    #Load categories
    categories = []
    with open(PATH_TO_NETWORKS + PATH_TO_CATEGORIES, 'r') as f:
        for line in f:
            cat = line.split('\n')[0]
            if cat != 'classes':
                categories.append(cat)
        f.close()
        print('Number of categories:', len(categories))

    img = cv2.imread(image_filename).astype(numpy.float32)

    dx, dy, dz = img.shape
    delta = float(abs(dy - dx))
    if dx > dy:  #crop the x dimension
        img = img[int(0.5 * delta):dx - int(0.5 * delta), 0:dy]
    else:
        img = img[0:dx, int(0.5 * delta):dy - int(0.5 * delta)]

    img = cv2.resize(img, IMAGE_DIMENSIONS)

    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    for i in range(3):
        img[:, :, i] = (img[:, :, i] - mean) * std

    print('Start download to NCS...')

    # Start the inference by sending to the device/graph
    graph.queue_inference_with_fifo_elem(input_fifo, output_fifo,
                                         img.astype(numpy.float16), None)

    # Get the result from the device/graph.
    probabilities, userobj = output_fifo.read_elem()

    # Sort indices in order of highest probabilities
    top_inds = (-probabilities).argsort()[:number_results]

    return top_inds, categories, probabilities