This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

AM5708: TIDL questions

Part Number: AM5708

Hello:

I am a new to use TIDL library, I meet something which confuses me.  

First, when I convert a .ONNX model to the .bin files which tidl can use, how can I create  sampleInData which ends with '.y' format use my own dataset? Is this file used for calibration?

Second, I want to use python to develop my project, but how can send picture data to eo after I create eo.get_input_buffer()?

  • Part Number: AM5708

    Tool/software: Linux

    Hello

       I use tidl tools for view a net, but I meet some errors

      ERROR: Wrong network binary size: 483364

      ERROR: Invaild betwork binary: ./test/testvecs/congif/tidl_models/tidl_net_jedtNet_ssd.bin

     

  • Hi,

    The sampleInData ".y" is the preprocessed raw image, structured as B frame, followed by G frame, followed by R frame.

    eo.get_input_buffer returns a Python buffer that represents the input buffer to the eo. Simply put your data into this returned Python buffer. Please see "mnist.py" as an example.

    The x86 version of tidl_viewer has a bug at the moment. Please use the arm version on the EVM filesystem.

    Thanks!

    - Yuan
  • Thanks, the "mnist.py" example put data into the python buffer by using a file object with 'readinto', but how can I put a picture data captured by opencv into it?

    Another qusetion: tidl_model_import.out tool config file has a line : sampleInData = "./test/testvecs/input/preproc_0_224x224.y" this file aim at calibration?? This '.y' file just only one picture data ?
  • Hi,

        You can convert the python buffer as a numpy array and manipulate the contents.

      arg_info = eo.get_input_buffer()
      np_arg_info = np.asarray(arg_info)
      # modify np_arg_info as regular numpy array
    

        preproc_0_224x224.y contains a single pre-processed image.  It is used to derive the quantization parameters for the network.  You can have multiple pre-processed images in the same file, you just need to set "numSampleInData = " in the config file if you intend to use multiple images.

    - Yuan

  • still struggling with the same question.can i add ur wechat account?my account number is echo117chunxiao.

  • when use np.asarray(arg_info) to convert arg_info object to a ndarray object and modify the ndarray object , the arg_info object is not modified.

    I send different picture data but I always get same result from outputbuffer. so I suspect the input data do not work??

  • Hi,

        We use python buffer protocol for accessing EO/EOP's input/output buffers.  Using np.asarray() is one way of accessing them.  Enclosed is a complete imagenet example that uses OpenCV to read/transform image file, place data into EOP's input data, run network, and process output data.  Hope this helps.

    - Yuan

    tidl/examples/pybind# cat imagenet.py 
    #!/usr/bin/python3
    
    # Copyright (c) 2019 Texas Instruments Incorporated - http://www.ti.com/
    # All rights reserved.
    #
    # Redistribution and use in source and binary forms, with or without
    # modification, are permitted provided that the following conditions are met:
    # * Redistributions of source code must retain the above copyright
    # notice, this list of conditions and the following disclaimer.
    # * Redistributions in binary form must reproduce the above copyright
    # notice, this list of conditions and the following disclaimer in the
    # documentation and/or other materials provided with the distribution.
    # * Neither the name of Texas Instruments Incorporated nor the
    # names of its contributors may be used to endorse or promote products
    # derived from this software without specific prior written permission.
    #
    # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    # THE POSSIBILITY OF SUCH DAMAGE.
    
    """ Process each frame using a single ExecutionObject.
        Increase throughput by using multiple ExecutionObjects.
    """
    
    import os
    import argparse
    import numpy as np
    import json
    import cv2
    import heapq
    
    from tidl import DeviceId, DeviceType, Configuration, TidlError
    from tidl import Executor, ExecutionObjectPipeline
    from tidl import allocate_memory, free_memory
    
    
    def main():
        """Read the configuration and run the network"""
    
        args = parse_args()
    
        config_file = '../test/testvecs/config/infer/tidl_config_j11_v2.txt'
        labels_file = '../imagenet/imagenet_objects.json'
    
        configuration = Configuration()
        configuration.read_from_file(config_file)
    
        if os.path.isfile(args.input_file):
            configuration.in_data = args.input_file
        else:
            print('Input image {} does not exist'.format(args.input_file))
            return
        print('Input: {}'.format(args.input_file))
    
        num_eve = Executor.get_num_devices(DeviceType.EVE)
        num_dsp = Executor.get_num_devices(DeviceType.DSP)
    
        if num_eve == 0 and num_dsp == 0:
            print('No TIDL API capable devices available')
            return
    
        if num_eve > 0:
            num_eve = 1
            num_dsp = 0
        else:
            num_dsp = 1
    
        run(num_eve, num_dsp, configuration, labels_file)
    
        return
    
    
    DESCRIPTION = 'Run the imagenet network on input image.'
    DEFAULT_INFILE = '../test/testvecs/input/objects/cat-pet-animal-domestic-104827.jpeg'
    
    def parse_args():
        """Parse input arguments"""
    
        parser = argparse.ArgumentParser(description=DESCRIPTION)
        parser.add_argument('-i', '--input_file',
                            default=DEFAULT_INFILE,
                            help='input image file (that OpenCV can read)')
        args = parser.parse_args()
    
        return args
    
    PIPELINE_DEPTH = 2
    
    def run(num_eve, num_dsp, configuration, labels_file):
        """ Run the network on the specified device type and number of devices"""
    
        #print('Running network across {} EVEs, {} DSPs'.format(num_eve, num_dsp))
    
        dsp_device_ids = set([DeviceId.ID0, DeviceId.ID1,
                              DeviceId.ID2, DeviceId.ID3][0:num_dsp])
        eve_device_ids = set([DeviceId.ID0, DeviceId.ID1,
                              DeviceId.ID2, DeviceId.ID3][0:num_eve])
    
        # Heap sizes for this network determined using Configuration.showHeapStats
        configuration.param_heap_size = (3 << 20)
        configuration.network_heap_size = (20 << 20)
    
    
        try:
            #print('TIDL API: performing one time initialization ...')
    
            # Collect all EOs from EVE and DSP executors
            eos = []
    
            if eve_device_ids:
                eve = Executor(DeviceType.EVE, eve_device_ids, configuration, 1)
                for i in range(eve.get_num_execution_objects()):
                    eos.append(eve.at(i))
    
            if dsp_device_ids:
                dsp = Executor(DeviceType.DSP, dsp_device_ids, configuration, 1)
                for i in range(dsp.get_num_execution_objects()):
                    eos.append(dsp.at(i))
    
    
            eops = []
            num_eos = len(eos)
            for j in range(num_eos):
                for i in range(PIPELINE_DEPTH):
                    eops.append(ExecutionObjectPipeline([eos[i%num_eos]]))
    
            allocate_memory(eops)
    
            # open labels file
            with open(labels_file) as json_file:
                labels_data = json.load(json_file)
    
            configuration.num_frames = 1;
            #print('TIDL API: processing {} input frames ...'.format(configuration.num_frames))
    
            num_eops = len(eops)
            num_errors = 0
            for frame_index in range(configuration.num_frames+num_eops):
                eop = eops[frame_index % num_eops]
    
                if eop.process_frame_wait():
                    num_errors += process_output(eop, labels_data)
    
                if read_frame(eop, frame_index, configuration):
                    eop.process_frame_start_async()
    
            free_memory(eops)
    
        except TidlError as err:
            print(err)
    
    def read_frame(eo, frame_index, configuration):
        """Read a frame into the ExecutionObject input buffer"""
    
        if frame_index >= configuration.num_frames:
            return False
    
        # Read into the EO's input buffer
        arg_info = eo.get_input_buffer()
        np_arg = np.asarray(arg_info)
    
        img = cv2.imread(configuration.in_data)
        resized = cv2.resize(img, (224, 224), interpolation = cv2.INTER_AREA)
        b,g,r = cv2.split(resized)
        np_arg[0:224*224] = np.reshape(b, 224*224)
        np_arg[224*224:2*224*224] = np.reshape(g, 224*224)
        np_arg[2*224*224:3*224*224] = np.reshape(r, 224*224)
    
        eo.set_frame_index(frame_index)
    
        return True
    
    def process_output(eo, labels_data):
        """Display the inference result using labels."""
    
        # keep top k predictions in heap
        k = 5
        # output predictions with probability of 10/255 or higher
        threshold = 10
    
        out_buffer = eo.get_output_buffer()
        output_array = np.asarray(out_buffer)
    
        h = []
        for i in range(k):
            heapq.heappush(h, (output_array[i], i))
    
        for i in range(k, out_buffer.size()):
            if output_array[i] > h[0][0]:
                heapq.heappushpop(h, (output_array[i], i))
    
        sorted = []
        for i in range(k):
            sorted.insert(0, heapq.heappop(h))
    
        for i in range(k):
            if sorted[i][0] > threshold:
                print('{}: {},   prob = {:5.2f}%'.format(i+1, \
                                   labels_data['objects'][sorted[i][1]]['label'], \
                                   sorted[i][0]/255.0*100))
    
        return 0
    
    if __name__ == '__main__':
        main()
    

  • Some example output:

    root@am57xx-evm:~/tidl/examples/pybind# ./imagenet.py 
    Input: ../test/testvecs/input/objects/cat-pet-animal-domestic-104827.jpeg
    1: Egyptian_cat,   prob = 34.12%
    2: tabby,   prob = 34.12%
    3: Angora,   prob =  9.41%
    4: tiger_cat,   prob =  7.84%
    root@am57xx-evm:~/tidl/examples/pybind# ./imagenet.py -i ../test/testvecs/input/objects/pexels-photo-209424.jpeg 
    Input: ../test/testvecs/input/objects/pexels-photo-209424.jpeg
    1: ice_cream,   prob = 60.78%
    2: trifle,   prob = 14.12%
    3: chocolate_sauce,   prob =  6.67%
    

  • Thankss, it sloved