Lines Matching refs:self
67 def __init__(self, tflite_model_json_path='/tmp'): argument
70 self.operators = model_json['subgraphs'][0]['operators']
71 self.operator_codes = [item['builtin_code']\
73 self.output_meta_data = []
74 self.load_output_meta_data()
76 def load_output_meta_data(self): argument
77 for operator in self.operators:
85 data['operator_code'] = self.operator_codes[operator['opcode_index']]
86 self.output_meta_data.append(data)
88 def __init__(self, android_build_top, dump_dir, tflite_model_json_dir='/tmp'): argument
90 self.models = dict()
91 self.ANDROID_BUILD_TOP = android_build_top + "/"
92 self.TFLITE_MODEL_JSON_DIR = tflite_model_json_dir + "/"
93 self.DUMP_DIR = dump_dir + "/"
94 self.nnapi_to_tflite_name = dict()
95 self.tflite_to_nnapi_name = dict()
96 self.__load_mobilenet_topk_aosp()
97 self.model_names = sorted(os.listdir(dump_dir))
99 def __load_mobilenet_topk_aosp(self): argument
102 self.ANDROID_BUILD_TOP,
107 self.nnapi_to_tflite_name[model['name']] = model['modelFile']
108 self.tflite_to_nnapi_name[model['modelFile']] = model['name']
110 def __get_model_json_path(self, tflite_model_name): argument
112 json_path = '{}/{}.json'.format(self.TFLITE_MODEL_JSON_DIR,
116 def __load_model(self, tflite_model_name): argument
118 model = self.ModelMetaData(self.__get_model_json_path(tflite_model_name))
119 nnapi_model_name = self.model_name_tflite_to_nnapi(tflite_model_name)
120 self.models[nnapi_model_name] = model
122 def model_name_nnapi_to_tflite(self, nnapi_model_name): argument
123 return self.nnapi_to_tflite_name.get(nnapi_model_name, nnapi_model_name)
125 def model_name_tflite_to_nnapi(self, tflite_model_name): argument
126 return self.tflite_to_nnapi_name.get(tflite_model_name, tflite_model_name)
128 def get_model_meta_data(self, nnapi_model_name): argument
130 tflite_model_name = self.model_name_nnapi_to_tflite(nnapi_model_name)
131 if nnapi_model_name not in self.models:
132 self.__load_model(tflite_model_name)
133 return self.models[nnapi_model_name]
135 def generate_animation_html(self, output_file_path, model_names=None, heatmap=True): argument
137 model_names = self.model_names if model_names is None else model_names
142 model_data = ModelData(nnapi_model_name=model_name, manager=self)
151 def generate_hist_animation_html(self, model_name): argument
154 model_data = ModelData(nnapi_model_name=model_name, manager=self)
158 self.return_dict[model_name + "-hist"] = html_data
160 def generate_heatmap_animation_html(self, model_name): argument
162 model_data = ModelData(nnapi_model_name=model_name, manager=self)
166 self.return_dict[model_name + "-heatmap"] = html_data
168 def multiprocessing_generate_animation_html(self, output_file_path, argument
174 model_names = self.model_names if model_names is None else model_names
176 self.return_dict = manager.dict()
179 for target_func in [self.generate_hist_animation_html, self.generate_heatmap_animation_html]:
189 f.write(self.return_dict[model_name + "-hist"])
190 f.write(self.return_dict[model_name + "-heatmap"])
200 def __init__(self, model_dir): argument
204 self[useNNAPIDir] = self.read_tensors_from_dir(dir_path)
205 self.tensor_sanity_check()
206 self.max_absolute_diff, self.min_absolute_diff = 0.0, 0.0
207 self.max_relative_diff, self.min_relative_diff = 0.0, 0.0
208 self.layers = sorted(self['cpu'].keys())
209 self.calc_range()
211 def bytes_to_numpy_tensor(self, file_path): argument
224 def read_tensors_from_dir(self, dir_path): argument
227 tensor = self.bytes_to_numpy_tensor(dir_path + tensor_file)
231 def tensor_sanity_check(self): argument
233 assert(set(self['cpu'].keys()) == set(self['nnapi'].keys()))
236 def calc_range(self): argument
237 for layer in self.layers:
238 diff = self.calc_diff(layer, relative_error=False)
240 self.max_absolute_diff = max(self.max_absolute_diff, np.max(diff))
241 self.min_absolute_diff = min(self.min_absolute_diff, np.min(diff))
242 self.absolute_range = max(abs(self.min_absolute_diff),
243 abs(self.max_absolute_diff))
245 def calc_diff(self, layer, relative_error=True): argument
246 cpu_tensor = self['cpu'][layer]
247 nnapi_tensor = self['nnapi'][layer]
260 def gen_tensor_diff_stats(self, relative_error=True, return_df=True, plot_diff=False): argument
262 for layer in self.layers:
263 diff = self.calc_diff(layer, relative_error)
265 self.plot_tensor_diff(diff)
291 def __init__(self, nnapi_model_name, manager, seq_limit=10): argument
292 self.nnapi_model_name = nnapi_model_name
293 self.manager = manager
294 self.model_dir = self.get_target_model_dir(manager.DUMP_DIR,
296 self.tensor_dict = TensorDict(self.model_dir)
297 self.mmd = manager.get_model_meta_data(nnapi_model_name)
298 self.stats = self.tensor_dict.gen_tensor_diff_stats(relative_error=True,
300 self.layers = sorted(self.tensor_dict['cpu'].keys())
301 self.cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
302 self.seq_limit = seq_limit
304 def get_target_model_dir(self, dump_dir, target_model_name): argument
309 def __sns_distplot(self, layer, bins, ax, range, relative_error): argument
310 sns.distplot(self.tensor_dict.calc_diff(layer, relative_error=relative_error), bins=bins,
313 def __plt_hist(self, layer, bins, ax, range, relative_error): argument
314 ax.hist(self.tensor_dict.calc_diff(layer, relative_error=relative_error), bins=bins,
317 def __get_layer_num(self): argument
318 if self.seq_limit:
319 return min(len(self.layers), len(self.mmd.output_meta_data) * self.seq_limit)
320 return len(self.layers)
322 def update_hist_data(self, i, fig, ax1, ax2, bins=50, plot_library='sns'): argument
324 operation = self.mmd.output_meta_data[i % len(self.mmd.output_meta_data)]['operator_code']
325 layer = self.layers[i]
327 .format(self.nnapi_model_name, layer, operation),
333 absolute_range = self.tensor_dict.absolute_range
336 hist_func = self.__plt_hist if plot_library == 'matplotlib' else self.__sns_distplot
342 def gen_error_hist_animation(self, save_video_path=None, video_fps=10): argument
344 ani = animation.FuncAnimation(fig, self.update_hist_data, self.__get_layer_num(),
353 def __sns_heatmap(self, data, ax, cbar_ax, **kwargs): argument
354 return sns.heatmap(data, cmap=self.cmap, cbar=True, ax=ax, cbar_ax=cbar_ax,
357 def update_heatmap_data(self, i, fig, axs): argument
359 operation = self.mmd.output_meta_data[i % len(self.mmd.output_meta_data)]['operator_code']
360 layer = self.layers[i]
362 .format(self.nnapi_model_name, layer, operation),
373 reshaped_diff = reshape_to_matrix(self.tensor_dict.calc_diff(layer, relative_error=False))
374 reshaped_cpu = reshape_to_matrix(self.tensor_dict['cpu'][layer])
375 reshaped_nnapi = reshape_to_matrix(self.tensor_dict['nnapi'][layer])
376 absolute_range = self.tensor_dict.absolute_range
377 g1 = self.__sns_heatmap(data=reshaped_diff, ax=axs[0][0], cbar_ax=axs[1][0],
379 g2 = self.__sns_heatmap(data=reshaped_cpu, ax=axs[0][1], cbar_ax=axs[1][1])
380 g3 = self.__sns_heatmap(data=reshaped_nnapi, ax=axs[0][2], cbar_ax=axs[1][2])
382 def gen_heatmap_animation(self, save_video_path=None, video_fps=10, figsize=(13,6)): argument
394 ani = animation.FuncAnimation(fig, self.update_heatmap_data, self.__get_layer_num(),
403 def plot_error_heatmap(self, target_layer, vmin=None, vmax=None): argument
405 target_diff = self.tensor_dict['cpu'][target_layer] - \
406 self.tensor_dict['nnapi'][target_layer]
411 cmap=self.cmap,
425 def __init__(self, dump_dir_list, android_build_top, tflite_model_json_dir, model_name): argument
426 self.dump_dir_list = dump_dir_list
427 self.android_build_top = android_build_top
428 self.tflite_model_json_dir = tflite_model_json_dir
429 self.set_model_name(model_name)
431 def set_model_name(self, model_name): argument
433 self.model_name = model_name
434 self.__load_data()
436 def __load_data(self): argument
438 self.manager_list = []
439 self.model_data_list = []
440 for i, dump_dir in enumerate(self.dump_dir_list):
441 manager = ModelMetaDataManager(self.android_build_top,
443 tflite_model_json_dir=self.tflite_model_json_dir)
444 model_data = ModelData(nnapi_model_name=self.model_name, manager=manager)
445 self.manager_list.append(manager)
446 self.model_data_list.append(model_data)
447 self.sanity_check()
449 def sanity_check(self): argument
453 assert(len(self.model_data_list) >= 1)
454 sample_model_data = self.model_data_list[0]
455 for i in range(1, len(self.model_data_list)):
457 set(self.model_data_list[i].tensor_dict['nnapi'].keys()))
459 self.layers = sample_model_data.layers
460 self.mmd = sample_model_data.mmd
462 def update_hist_comparison_data(self, i, fig, axs, bins=50): argument
464 sample_model_data = self.model_data_list[0]
465 operation = self.mmd.output_meta_data[i % len(self.mmd.output_meta_data)]['operator_code']
466 layer = self.layers[i]
476 labels = [dump_dir.split('/')[-2] for dump_dir in self.dump_dir_list]
479 model_data = self.model_data_list[i]
482 self.model_data_list[i].tensor_dict.calc_diff(layer, relative_error=False))
489 def gen_error_hist_comparison_animation(self, save_video_path=None, video_fps=10): argument
490 layers = self.layers
491 N = len(self.model_data_list)
503 ani = animation.FuncAnimation(fig, self.update_hist_comparison_data, len(layers),
521 def default(self, obj): argument
524 return json.JSONEncoder.default(self, obj)