예제 #1
0
def do_cleanup(device: mvnc2.Device, graph: mvnc2.Graph,
               input_fifo: mvnc2.Fifo, output_fifo: mvnc2.Fifo) -> None:
    """Cleans up the NCAPI resources.

    Parameters
    ----------
    device : mvncapi.Device
            Device instance that was initialized in the do_initialize method.
    graph : mvncapi.Graph
            Graph instance that was initialized in the do_initialize method.
    input_fifo : mvnc2.Fifo
            The input FIFO queue for network.
    output_fifo : mvnc2.Fifo
            The output FIFO queue for network.

    Returns
    -------
    None

    """
    input_fifo.destroy()
    output_fifo.destroy()
    graph.destroy()
    device.close()
    device.destroy()
예제 #2
0
def do_inference(graph: mvnc.Graph,
                 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
    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.LoadTensor(image_for_inference.astype(numpy.float16), None)

    # Get the result from the device/graph.  userobj should be the
    # same value that was passed in LoadTensor above.
    output, userobj = graph.GetResult()

    # 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
예제 #3
0
def do_cleanup(device: mvnc.Device, graph: mvnc.Graph) -> None:
    """Cleans up the NCAPI resources.
    Parameters
    ----------
    device : mvncapi.Device
             Device instance that was initialized in the do_initialize method
    graph : mvncapi.Graph
            Graph instance that was initialized in the do_initialize method
    Returns
    -------
    None
    """
    graph.DeallocateGraph()
    device.CloseDevice()
예제 #4
0
    def do_inference(
            self,
            graph: mvnc.Graph,
            img,
            number_results: int = 5) -> (List[str], List[numpy.float16]):
        labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        image_for_inference = img
        image_for_inference = cv2.cvtColor(image_for_inference,
                                           cv2.COLOR_BGR2GRAY)
        image_for_inference = cv2.resize(image_for_inference,
                                         NETWORK_IMAGE_DIMENSIONS)
        ret, image_for_inference = cv2.threshold(image_for_inference, 120, 255,
                                                 cv2.THRESH_BINARY_INV)
        cv2.imshow("image_for_inference:", image_for_inference)
        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.LoadTensor(image_for_inference.astype(numpy.float16), None)

        # Get the result from the device/graph.  userobj should be the
        # same value that was passed in LoadTensor above.
        output, userobj = graph.GetResult()

        # 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
예제 #6
0
    def do_cleanup(self, device: mvnc.Device, graph: mvnc.Graph) -> None:

        graph.DeallocateGraph()
        device.CloseDevice()