1 #!/usr/bin/python3
3 # Copyright (c) 2018 Texas Instruments Incorporated - http://www.ti.com/
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of Texas Instruments Incorporated nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 # THE POSSIBILITY OF SUCH DAMAGE.
29 """ Layer output
31 Illustrates writing the outputs of intermediate layers in the network to file.
32 """
34 import argparse
36 from tidl import DeviceId, DeviceType, Configuration, Executor, TidlError
37 from tidl import allocate_memory, free_memory
39 from tidl_app_utils import read_frame
42 def main():
43 """ Parse arguments, read configuration and run network"""
45 parser = argparse.ArgumentParser(description=
46 'Dump output of each network layer to file.')
47 parser.add_argument(
48 '-c', '--config_file',
49 default='../test/testvecs/config/infer/tidl_config_j11_v2.txt',
50 help='Path to TIDL config file')
52 args = parser.parse_args()
54 # Run network for 1 frame since we interested in intermediate layer outputs
55 num_frames = 1
57 # Read configuration from file
58 configuration = Configuration()
59 configuration.read_from_file(args.config_file)
60 configuration.enable_layer_dump = True
61 configuration.num_frames = num_frames
63 num_dsp = Executor.get_num_devices(DeviceType.DSP)
64 num_eve = Executor.get_num_devices(DeviceType.EVE)
66 if num_dsp == 0 and num_eve == 0:
67 print('No TIDL API capable devices available')
68 return
70 if num_eve > 0:
71 device_type = DeviceType.EVE
72 else:
73 device_type = DeviceType.DSP
75 # Since we are dumping layer outputs, just run on one device
76 run(device_type, 1, configuration)
79 def run(device_type, num_devices, configuration):
80 """ Run the network on a single device and dump output of each layer"""
82 print('Running network on {} {}'.format(num_devices, device_type))
84 device_ids = set([DeviceId.ID0])
86 try:
87 print('TIDL API: performing one time initialization ...')
89 executor = Executor(device_type, device_ids, configuration, 1)
91 # Collect all EOs from EVE and DSP executors
92 eos = []
93 for i in range(executor.get_num_execution_objects()):
94 eos.append(executor.at(i))
96 allocate_memory(eos)
98 # Open input, output files
99 f_in = open(configuration.in_data, 'rb')
102 print('TIDL API: processing input frames ...')
104 num_eos = len(eos)
105 for frame_index in range(configuration.num_frames+num_eos):
106 execution_object = eos[frame_index % num_eos]
108 if execution_object.process_frame_wait():
109 execution_object.write_layer_outputs_to_file()
111 if read_frame(execution_object, frame_index, configuration, f_in):
112 execution_object.process_frame_start_async()
114 f_in.close()
116 free_memory(eos)
117 except TidlError as err:
118 print(err)
121 if __name__ == '__main__':
122 main()